如何在经过预训练的具有不同图像尺寸的ResNet50上进行转移学习

时间:2019-03-31 16:30:02

标签: python keras conv-neural-network resnet

我有一个预训练的ResNet模型,该模型在64x64图像上训练。我想使用包含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)

1 个答案:

答案 0 :(得分:0)

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

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

您可能会看到,它是一个随机数组,大小为128, 128, 3的10个批次

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。并且最好将Dataset API与fit方法一起使用。更容易,更快。 https://www.tensorflow.org/guide/datasets

相关问题