【问题标题】:How to do transfer learning on a pre-trained ResNet50 with different image size如何在具有不同图像大小的预训练 ResNet50 上进行迁移学习
【发布时间】:2019-03-31 16:30:01
【问题描述】:

我有一个在 64x64 图像上训练的预训练 ResNet 模型。我想使用包含 200x200 图像的新数据集进行迁移学习。

我正在加载模型:

model = ResNet50(include_top=False, weights=None, input_shape=(64,64,3))
model.load_weights("a trained model weights on 64x64")

model.layers.pop()
for layer in model.layers:
   layer.trainable = False

x = model.output
x = MaxPooling2D((2,2), strides=(2,2), padding='same')(x)
x = Flatten(name='flatten')(x)
x = Dropout(0.2)(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(101, activation='softmax', name='predictions')(x)

top_model = Model(inputs=model.input, outputs=predictions)

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

EPOCHS = 100
BATCH_SIZE = 32
STEPS_PER_EPOCH = 4424 // BATCH_SIZE
VALIDATION_STEPS = 466 // BATCH_SIZE

callbacks = [LearningRateScheduler(schedule=Schedule(EPOCHS, initial_lr=lr_rate)),
                ModelCheckpoint(str(output_dir) + "/weights.{epoch:03d}-{val_loss:.3f}-{val_age_mae:.3f}.hdf5",
                                 monitor="val_age_mae",
                                 verbose=1,
                                 save_best_only=False,
                                 mode="min")
                 ]

hist = top_model.fit_generator(generator=train_set,
                               epochs=EPOCHS,
                               steps_per_epoch = STEPS_PER_EPOCH,
                               validation_data=val_set,
                               validation_steps = VALIDATION_STEPS,
                               verbose=1,
                               callbacks=callbacks)


我想基于 200x200 像素的图像进行迁移学习。我对此很陌生,我该如何修改?

有没有办法修改模型输入形状?我需要做一些空间大小的事情吗?

推荐使用哪种优化器?亚当还是新元?


__________________________________________________________________________________________________
res5c_branch2a (Conv2D)         (None, 2, 2, 512)    1049088     activation_46[0][0]              
__________________________________________________________________________________________________
bn5c_branch2a (BatchNormalizati (None, 2, 2, 512)    2048        res5c_branch2a[0][0]             
__________________________________________________________________________________________________
activation_47 (Activation)      (None, 2, 2, 512)    0           bn5c_branch2a[0][0]              
__________________________________________________________________________________________________
res5c_branch2b (Conv2D)         (None, 2, 2, 512)    2359808     activation_47[0][0]              
__________________________________________________________________________________________________
bn5c_branch2b (BatchNormalizati (None, 2, 2, 512)    2048        res5c_branch2b[0][0]             
__________________________________________________________________________________________________
activation_48 (Activation)      (None, 2, 2, 512)    0           bn5c_branch2b[0][0]              
__________________________________________________________________________________________________
res5c_branch2c (Conv2D)         (None, 2, 2, 2048)   1050624     activation_48[0][0]              
__________________________________________________________________________________________________
bn5c_branch2c (BatchNormalizati (None, 2, 2, 2048)   8192        res5c_branch2c[0][0]             
__________________________________________________________________________________________________
add_16 (Add)                    (None, 2, 2, 2048)   0           bn5c_branch2c[0][0]              
                                                                 activation_46[0][0]              
__________________________________________________________________________________________________
activation_49 (Activation)      (None, 2, 2, 2048)   0           add_16[0][0]                     
__________________________________________________________________________________________________
pred_age (Dense)                (None, 2, 2, 101)    206848      activation_49[0][0]              
==================================================================================================
Total params: 23,794,560
Trainable params: 23,741,440
Non-trainable params: 53,120
__________________________________________________________________________________________________

得到以下错误

ValueError: Error when checking input: expected input_1 to have shape (64, 64, 3) but got array with shape (128, 128, 3)

【问题讨论】:

  • 你试过什么?你的方法有效吗?
  • 我的方法不起作用:(因为我是新手,是否可以在具有不同图像大小的预训练模型上对不同图像大小进行迁移学习?
  • 错误信息说明了什么?
  • ValueError: 检查输入时出错:预期 input_1 的形状为 (64, 64, 3) 但得到的数组的形状为 (128, 128, 3)
  • 请用您的完整代码更新您的问题。具体来说,添加top_model.summary() 的输出,如果您尝试使用`.fit(),请添加输入数据的形状。还要添加错误消息。这将使您的问题清晰而具体

标签: python keras conv-neural-network resnet


【解决方案1】:

考虑示例,我按原样使用您的模型,仅更改输入数据。

test = np.random.rand(10, 128, 128, 3)

如你所见,它是一个随机数组,10批次大小128, 128, 3

top_model.fit(test, epochs=1, batch_size=1, steps_per_epoch = 10)

然后我使用 fit 方法,只是为了演示。

ValueError:检查输入时出错:预期 input_1 具有形状 (64, 64, 3) 但得到了形状为 (128, 128, 3) 的数组

这是错误信息。很明显,您的输入数据形状错误。添加函数,产生generator=train_set。并且最好使用带有 fit 方法的 Dataset API。它更容易,更快捷。 https://www.tensorflow.org/guide/datasets

【讨论】:

    猜你喜欢
    • 2021-04-17
    • 2020-05-21
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多