简体   繁体   English

keras 模型上的迁移学习总是给出相同的预测

[英]Transfer learning on keras model always gives same predictions

I'm trying to train an image classifier using keras applications module.我正在尝试使用 keras 应用程序模块训练图像分类器。 When I run predictions on validation set, all images are predicted as the same class.当我在验证集上运行预测时,所有图像都被预测为同一类。 It is not always the same class, it varies during training.它并不总是同一个班级,它在训练期间会有所不同。 I'm using MobileNetV2 with weights from ImageNet but I also tried other models with same result.我正在使用带有 ImageNet 权重的 MobileNetV2,但我也尝试了其他具有相同结果的模型。

I've tried using model from TensorFlow hub like described in this tutorial:https://www.tensorflow.org/beta/tutorials/images/hub_with_keras and it worked fine, so it is not a data set issue.我已经尝试使用 TensorFlow hub 中的模型,如本教程中所述:https ://www.tensorflow.org/beta/tutorials/images/hub_with_keras并且它工作正常,所以它不是数据集问题。

My code snippet:我的代码片段:

image_size = 224
batch_size = 32
train_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input)
validation_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input)
train_generator = train_datagen.flow_from_directory(training_data_dir,
                                                    target_size=(image_size, image_size),
                                                    batch_size=batch_size)
validation_generator = train_datagen.flow_from_directory(validation_data_dir,
                                                         target_size=(image_size, image_size),
                                                         batch_size=batch_size)

IMG_SHAPE = (image_size, image_size, 3)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights="imagenet")
base_model.trainable = False

model = tf.keras.Sequential([
    base_model,
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dense(train_generator.num_classes, activation='softmax')
])
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=0.001),
              loss="categorical_crossentropy",
              metrics=["accuracy"])
model.summary()

batch_stats = CollectBatchStats()
epoch_stats = CollectEpochStats(model, validation_generator)
checkpoint = tf.keras.callbacks.ModelCheckpoint(...)

epochs = 10
steps_per_epoch = train_generator.n // train_generator.batch_size
validation_steps = validation_generator.n // validation_generator.batch_size

history = model.fit_generator(train_generator,
                              epochs=epochs,
                              steps_per_epoch=steps_per_epoch,
                              callbacks=[batch_stats, epoch_stats, checkpoint],
                              workers=4,
                              validation_data=validation_generator,
                              validation_steps=validation_steps)

Issue resolved: in my code I had following lines after model compilation:问题已解决:在我的代码中,模型编译后有以下几行:

sess = keras_backend.get_session()
init = tf.compat.v1.global_variables_initializer()
sess.run(init)

After removing them everything works fine.删除它们后一切正常。

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

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