0
votes

I have the following network that will be used for binary classification on medical image data. However, I would like to use only the 80 first layers of this model as I currently don't have a lot of data and my model is overfitting. I would like to delete all layers from block 4 or 5, and only keep blocks 1, 2 and 3. I have tried using layer.pop() but it does not work.

from keras.applications.resnet50 import ResNet50

resnet = ResNet50(include_top=False, weights='imagenet', input_shape=(im_size,im_size,3))

headModel = AveragePooling2D(pool_size=(7, 7))(resnet.output)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(256, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(1, activation="sigmoid")(headModel)
1
Why not using a second model inspired by ResNet50? You can then save the two versions, and compare them. Btw, 80 layers is already a quite deep model. - David Thery

1 Answers

0
votes

Solution 1 : You could use model.summary() to quickly check the names of the layers (80th).

layer_name = 'name_of_the_80th_layer'
intermediate_model = Model(inputs=resnet.input,
                           outputs=resnet.get_layer(layer_name).output)
final_model = AveragePooling2D(pool_size=(7, 7))(intermediate_model.output)
final_model = Flatten(name="flatten")(final_model)
final_model = Dense(256, activation="relu")(final_model)
final_model = Dropout(0.5)(final_model)
final_model = Dense(1, activation="sigmoid")(final_model)

Solution 2:

You could directly get it like:

intermediate_model = Model(inputs=resnet.input,
                           outputs=resnet.layers[80].output)
...