【问题标题】:not able to configure image for prediction in ResNetV2 model trained using transfer learning无法在使用迁移学习训练的 ResNetV2 模型中配置预测图像
【发布时间】:2020-12-29 06:31:06
【问题描述】:

我训练了一个用于人脸识别的 ResNetV2 模型,得到了大约 90% 的验证准确率。我正在使用来自 40 个班级的 320 张灰度图像进行训练。我无法预处理图像以使用 model.predict() 函数。我没有使用 preprocess_input 函数,因为在使用它来训练模型时精度很低。请帮忙

base_model = ResNet50V2(weights='imagenet',include_top=False,input_shape=(224, 224,3))
# don't train existing weights
for layer in base_model.layers:
  layer.trainable = False

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #dense layer 1
x=Dropout(0.2)(x)
preds=Dense(40,activation='softmax')(x) #final layer with softmax activation
model=Model(inputs=base_model.input,outputs=preds)
model.summary()

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory(train_path,
                                                 target_size = (224,224),
                                                 batch_size = 8,
                                                 class_mode = 'categorical')

test_set = test_datagen.flow_from_directory(valid_path,
                                            target_size = (224,224),
                                            batch_size = 8,
                                            class_mode = 'categorical')

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

training = model.fit(
  training_set,
  validation_data=test_set,
  epochs=40,
  steps_per_epoch=8
)

img = image.load_img('/content/9.pgm.jpg.jpg', target_size=(224,224,3))
pixels = image.img_to_array(img)
pixels = pixels.astype('float32')
pixels /= 255.0
model.predict(pixels)

编辑:我最终丢弃的用于预处理的代码是 -

from keras.preprocessing.image import ImageDataGenerator
from keras.applications.resnet50 import preprocess_input
datagen = ImageDataGenerator(preprocessing_function=preprocess_input)

training_set = datagen.flow_from_directory(train_path,
                                                 target_size = (224,224),
                                                 batch_size = 8,
                                                 class_mode = 'categorical')

test_set = datagen.flow_from_directory(valid_path,
                                            target_size = (224,224),
                                            batch_size = 8,
                                            class_mode = 'categorical')

【问题讨论】:

  • 您说对图像进行预处理导致准确性低。您是否使用 tf.keras.applications.resnet.preprocess_input 作为预处理函数?这应该使您的模型工作得更好而不是更糟。请显示您尝试使用预处理功能时使用的代码
  • @GerryP 我已经更新了代码,看看。
  • 您用于预处理的代码看起来不错。不明白为什么会导致精度低。一个问题是你说你有 320 个灰度图像和 40 个类。所以你总共只有 320 张图片?每堂课只有 8 张图片。图像增强会有所帮助,但这对于您的网络来说训练的样本很少。你能展示每个时期的训练数据吗(有然后没有预处理)?另外你的预测有什么问题?您收到错误代码吗?
  • 另外,测试集最好在 flow_from_directory 中设置 shuffle=False。此外,如果您有 batch_size=8 并且有 320 张图像,那么在 model.fit 中设置 steps_per_epoch=40
  • 如果你的图片是灰度的,那么 flow_from_directory 你应该设置 color_mode='grayscale'

标签: python tensorflow keras deep-learning conv-neural-network


【解决方案1】:

用于预测的正确代码如下:

from PIL import Image
from tensorflow.keras.utils import load_img

class_names=['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

img = tf.keras.utils.load_img('./dataset/rose_test.jpeg', target_size=(224,224))
pixels =tf.keras.utils.img_to_array(img)
pixels=tf.expand_dims(pixels, 0)
#pixels = pixels.astype('float32')
pixels /= 255.0
pred = model.predict(pixels)  # as I have used 5 classes dataset
pred

输出:

array([[1.0110709e-07, 6.0101044e-08, 9.4008398e-01, 4.7367696e-08,
        5.9915919e-02]], dtype=float32)

获取加载图像的类名:

print(class_names[np.argmax(p)])

输出:

roses

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2021-04-17
    • 2017-05-05
    • 1970-01-01
    • 2022-11-10
    • 2022-01-14
    相关资源
    最近更新 更多