使用tf.keras和Inception-v3进行转移学习:无需培训

时间:2019-06-11 15:11:44

标签: python tensorflow keras pre-trained-model

我正在尝试基于冻结的Inception_v3模型训练模型,该模型具有3个类作为输出。当我进行训练时,训练的准确性会提高,但验证的准确性却不会提高,验证的准确性或多或少准确地达到了33.33%,即显示出完全随机的预测。我不知道代码和/或方法中的错误在哪里

在Inception v3内核之后,我尝试了各种形式的输出,完全没有差异。

# Model definition
# InceptionV3 frozen, flatten, dense 1024, dropout 50%, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, flatten, dense 1024, dense 3, lr 0.005 --> does not train
# InceptionV3 frozen, GlobalAvgPooling, dense 1024, dense 1024, dense 512, dense 3, lr 0.001 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32 --> does not train
# InceptionV3 frozen, GlobalAvgPooling dropout 0.4 dense 3, lr 0.001, custom pre-process, batch=32, rebalance train/val sets --> does not train

IMAGE_SIZE = 150
BATCH_SIZE = 32

def build_model(image_size):
  input_tensor = tf.keras.layers.Input(shape=(image_size, image_size, 3))

  inception_base = InceptionV3(include_top=False, weights='imagenet', input_tensor=input_tensor)
  for layer in inception_base.layers:
    layer.trainable = False

  x = inception_base.output
  x = tf.keras.layers.GlobalAveragePooling2D()(x)
  x = tf.keras.layers.Dropout(0.2)(x)
  output_tensor = tf.keras.layers.Dense(3, activation="softmax")(x)

  model = tf.keras.Model(inputs=input_tensor, outputs=output_tensor)

  return model

model = build_model(IMAGE_SIZE)
model.compile(optimizer=RMSprop(lr=0.002), loss='categorical_crossentropy', metrics=['acc'])

# Data generators with Image augmentations
train_datagen = ImageDataGenerator(
      rescale=1./255,
      preprocessing_function=tf.keras.applications.inception_v3.preprocess_input,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

# Do not augment validation!
validation_datagen = ImageDataGenerator(
    rescale=1./255,
    preprocessing_function=tf.keras.applications.inception_v3.preprocess_input)

train_generator = train_datagen.flow_from_directory(
      train_dir,
      target_size=(IMAGE_SIZE, IMAGE_SIZE),
      batch_size=BATCH_SIZE,
      class_mode='categorical')

validation_generator = validation_datagen.flow_from_directory(
      valid_dir,
      target_size=(IMAGE_SIZE, IMAGE_SIZE),
      batch_size=BATCH_SIZE,
      class_mode='categorical')

此单元格的输出是:

找到1697张属于3类的图像。 找到712个属于3类的图像。

最后两个训练阶段的输出:

第19/20版
23/23 [==============================]-6s 257ms / step-损耗:1.1930-acc:0.3174
54/54 [==============================]-20s 363ms / step-损耗:0.7870-acc:0.6912-val_loss :1.1930-val_acc:0.3174
时代20/20
23/23 [==============================]-6s 255ms / step-损失:1.985-acc:0.3160
54/54 [==============================]-20s 362ms / step-损耗:0.7819-acc:0.7018-val_loss :1.1985-val_acc:0.3160

1 个答案:

答案 0 :(得分:0)

跳到我身上的唯一一件大事就是抛弃rescale=1./255 ImageDataGenerators,因为tf.keras.applications.inception_v3.preprocess_input也将其抛弃,它将范围从-1缩放到1;网络的预期输入。