tensorflowkerasresnetimagenet

Obtaining model of ResNet50 which cannot be obtained with the include_top attribute


I am using ResNet50 model for feature extraction. I have two models initialized in the following way:

from tensorflow.keras.applications import ResNet50

model1=ResNet50(weights="imagenet", include_top=False) 
model2=ResNet50(weights="imagenet", include_top=True)`

Now when I plot the model architectures I get this: ( I have shown only the ending of architecture) screenshot

Neither of them ends at avg_pool: GlobalAveragePooling2D i.e I want the output of the model to be ( ?, 2048 ). Is it possible to get the architecture? Getting such an architecture with the imagenet weights would ease feature extraction.

Would be grateful for help.


Solution

  • You can generate a new keras model from the ResNet input and the output of the global average pooling (GAP) layer. In order to get the output of the GAP layer you need to extract this using the get_layer method. You can do this for any layer by finding the layer name with model.summary()

    model1=ResNet50(weights='imagenet', include_top=True)
    GAP_output = model1.get_layer('avg_pool').output
    new_model = tf.keras.Model(model1.input, GAP_output)
    new_model.summary()