【问题标题】:如何使用 inception resnet v2 训练我的模型?
【发布时间】:2022-01-23 16:58:02
【问题描述】:

我正在研究使用 tensorflow 和 inception resnet v2 架构训练图像的模型,但无法训练这个模型,我试图训练它,但每次我得到

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)

这是我使用 inception resnet v2 架构制作 tensorflow 模型的代码,我不知道如何训练我的数据集。有什么帮助吗?

【问题讨论】:

  • 如果你安装了 TF2,你不需要使用 v1 兼容来训练 inception Resnet。 TF2 keras 应用程序已经有了模型架构和权重

标签: python tensorflow machine-learning deep-learning


【解决方案1】:

实际上,通过 Tensorflow 2,您可以直接从 tensorflow.keras.applications 使用 Inception Resnet V2。下面是演示。

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

在这之后,你可以准备你的数据(我猜你可能已经这样做了)

  • 文件夹名称 = train -> 1) Class 1 文件夹 2) Class 2 文件夹 ...
  • 文件夹名称 = test -> 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 + "/*")



现在,让我们获取预训练模型。假设你想做迁移学习。

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)


现在,让我们获取将从文件夹中获取数据的生成器。

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
)

现在,让我们编译我们的模型。

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


最后,让我们的模型适应数据

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]
)

让我们也得到一些情节

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

还有一些……

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

让我们保存模型以备将来使用..

model.save("model.h5")


如果需要,我们可以稍后生成预测。

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)))

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 2018-02-07
    • 2019-02-21
    • 2018-07-25
    • 2017-09-22
    • 2018-08-02
    • 2017-03-09
    • 2017-11-10
    • 2021-06-09
    相关资源
    最近更新 更多