简体   繁体   English

如何使用 inception resnet v2 训练我的 model?

[英]How to Train my model using inception resnet v2?

I am working on model to train images using tensorflow and inception resnet v2 architecture and can't train this model, I have tried to train it but everytime I get我正在研究 model 来训练使用 tensorflow 和 inception resnet v2 架构的图像,并且无法训练这个 model,我试图训练它,但每次我得到

AttributeError: module 'tensorflow.compat.v1' has no attribute 'fit'
import tensorflow.compat.v1 as tf
import inception_resnet_v2 as incep_v2
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import selectivesearch
import matplotlib.patches as mpatches
import pandas as pd
import random
tf.disable_eager_execution()
# ------------------------------------------------------------------------------
# Configurations
# ------------------------------------------------------------------------------
IMG_SIZE = 150
TRAIN_DIR = "./dataset/train_images"
TEST_DIR = "./dataset/test_images"
data = pd.read_csv("./dataset/train.csv")
data = data.iloc[0:100, :]
# ------------------------------------------------------------------------------
# Read Train Image
# ------------------------------------------------------------------------------
def create_train_data():
    train_data = []
    for ind in data.index:
        path = os.path.join(TRAIN_DIR, data["image_name"][ind])
        img_data = cv2.imread(path)
        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
        train_data.append([np.array(img_data), data["label"][ind]])
        # fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
        # ax.imshow(img_data)
        # plt.show()
    random.shuffle(train_data)
    np.save('train_data.npy', train_data)
    return train_data


def create_test_data():
    test_data = []
    for img in os.listdir(TEST_DIR):
        path = os.path.join(TEST_DIR, img)
        img_data = cv2.imread(path)
        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
        test_data.append(np.array(img_data))
        break
    random.shuffle(test_data)
    return test_data


train_data = create_train_data()
test_data = create_test_data()

# ------------------------------------------------------------------------------
# Declarations
# ------------------------------------------------------------------------------
def define_model(model, is_training):
    model.Image = tf.placeholder(tf.float32, shape=[None, IMG_SIZE, IMG_SIZE, 3])
    with incep_v2.slim.arg_scope(incep_v2.inception_resnet_v2_arg_scope()):
        model.logits, model.end_points = incep_v2.inception_resnet_v2(model.Image, is_training=is_training)


class Model_Class:
    def __init__(self, is_training):
        define_model(self, is_training=is_training)


sess = tf.Session()
# ------------------------------------------------------------------------------
# Create Model
# ------------------------------------------------------------------------------
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
with tf.device('/cpu:0'):
    model = Model_Class(True)

This is the code I use to make a tensorflow model using inception resnet v2 architecture and I don't know how I can train my dataset.这是我使用 inception resnet v2 架构制作 tensorflow model 的代码,我不知道如何训练我的数据集。 Any help?有什么帮助吗?

Actually, with Tensorflow 2, you can use Inception Resnet V2 directly from tensorflow.keras.applications .实际上,使用 Tensorflow 2,您可以直接从tensorflow.keras.applications使用 Inception Resnet V2。 Below is the demo.下面是演示。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

import os
import sys
from glob import glob
import cv2 
import time
import datetime

from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input, Dense, Conv2D, Flatten
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 as  PretrainedModel,preprocess_input

from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator

Now after this, you can prepare your data as (you might have already done this, I guess)现在,在此之后,您可以准备数据(我猜您可能已经这样做了)

  • Folder Name = train -> 1) Class 1 folder 2) Class 2 folder...文件夹名称 = train -> 1) Class 1 文件夹 2) Class 2 文件夹...
  • Folder Name = test -> 1) Class 1 folder 2) Class 2 folder...文件夹名称 = 测试 -> 1) Class 1 文件夹 2) Class 2 文件夹...
train_path = r"./train"
test_path = r"./test"

IMAGE_SIZE = [150,150]

image_files = glob(train_path + '/*/*.png')
test_image_files = glob(test_path + '/*/*.png')

folders = glob(train_path + "/*")



Now, let's get the pretrained model.现在,让我们得到预训练的 model。 Assuming you want to do transfer learning.假设你想做迁移学习。

ptm = PretrainedModel(
        input_shape = IMAGE_SIZE + [3],
        weights = 'imagenet',
        include_top = False

)

ptm.trainable = False
K = len(folders)

x = Flatten()(ptm.output)
x = Dense(K, activation = 'softmax')(x)

model = Model(inputs = ptm.input , outputs = x)


Now, let's get the generators which will fetch the data from our folders.现在,让我们获取将从我们的文件夹中获取数据的生成器。

gen = ImageDataGenerator(
        rotation_range = 20,
        width_shift_range = 0.1,
        height_shift_range = 0.1,
        shear_range = 0.1,
        zoom_range = 0.2,
        horizontal_flip = True,
        preprocessing_function = preprocess_input
)

batch_size = 64

train_generator = gen.flow_from_directory(
            
    train_path,
    shuffle = True,
    target_size = IMAGE_SIZE,
    batch_size = batch_size
    )

test_generator = gen.flow_from_directory(
    
    test_path,
    target_size = IMAGE_SIZE,
    batch_size = batch_size
)

Now, let's compile our model.现在,让我们编译我们的 model。

model.compile(loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = ['accuracy'])


Finally, let's fit our model to the data最后,让我们将 model 拟合到数据中

r = model.fit(
        train_generator,
        validation_data = test_generator,
        epochs = 8,
        steps_per_epoch = int(np.ceil(len(image_files)/batch_size)),
        validation_steps = int(np.ceil(len(test_image_files)/batch_size)),
        callbacks=[myCall]
)

Let's get some plots as well让我们也得到一些情节

plt.plot(r.history['loss'] , color = 'red' , label = 'loss')
plt.plot(r.history['val_loss'] , color = 'blue' , label = 'val_loss')

Some more..多一点..

plt.plot(r.history['accuracy'] , color = 'red' , label = 'loss')
plt.plot(r.history['val_accuracy'] , color = 'blue' , label = 'val_loss')

Let's ave the model for future use..让我们保留 model 以备将来使用。

model.save("model.h5")


We can generate predictions later if we want.如果需要,我们可以稍后生成预测。

model = load_model("model.h5")
im = cv2.imread(r".\sample.png")
im  = cv2.resize(im , (150,150))
np.argmax(model.predict(im.reshape(1,150,150,3)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM