简体   繁体   中英

How to do transfer learning on a pre-trained ResNet50 with different image size

I have a pretrained ResNet model which is trained on 64x64 images. I would like to do transfer learning with new dataset that contains 200x200 images.

I am loading the model like:

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)


I would like to do transfer learning based with images of 200x200 pixels. I am very new to this, how can I modify?

is there a way to modify the model input shape? and do I. need to do something with spatial size?

And which optimizer is recommended? Adam or SGD?


__________________________________________________________________________________________________
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
__________________________________________________________________________________________________

Getting the following error

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

Consider example, I used your model as is, changed only input data.

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

As you may see, it's a random array, 10 batches of size 128, 128, 3

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

Then I used fit method, just to demonstrate.

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

This is the error message. It's obvious that your input data is of wrong shape. Add function, that produces generator=train_set . And it's better to use Dataset API with fit method. It's easier and faster. https://www.tensorflow.org/guide/datasets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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