I want to use ResNet model architecture and want to change last few layers; how can I only use model architecture from model zoo in Tensorflow?
-
How do you want to change the last few layers? I recommend using `tf.keras.applications.ResNet50` and setting the `weights` parameters to be `None` as a first step. – Richard X Aug 12 '20 at 02:26
-
Okay, also how can I only get the convolution base and change classifier completely? – nerdalert Aug 12 '20 at 03:01
-
I will set top include as false but I want to see the architecture of the base layer by layer in the summary – nerdalert Aug 12 '20 at 03:03
2 Answers
To use a ResNet model, you can choose a select few from tensorflow.keras.applications
including ResNet50
, ResNet101
, and ResNet152
. Then, you will need to change a few of the default arguments if you want to do transfer learning. For your question, you will need to set the weights
parameter equal to None
. Otherwise, 'imagenet'
weights are provided. Also, you need to set include_top
to be False
since the number of classes for your problem will likely be different from ImageNet. Finally, you will need to provide the shape of your data in input_shape
. This would look something like this.
base = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=shape)
To get a summary of the model, you can do
base.summary()
To add your own head, you can use the Functional API. You will need to add an Input
layer and your own Dense
layer which will correspond to your task. This could be
input = tf.keras.layers.Input(shape=shape)
base = base(input)
out = tf.keras.layers.Dense(num_classes, activation='softmax')(base)
Finally, to construct a model, you can do
model = tf.keras.models.Model(input, out)
The Model
constructor takes 2 arguments. The first being the inputs to your model, and the second being the outputs. Note that calling model.summary()
will show the ResNet base as a separate layer. To view all layers of the ResNet base, you can do model.layers[1].summary()
, or you can modify the code on how you built your model. The second way would be
out = tf.keras.layers.Dense(num_classes, activation='softmax')(base.output)
model = tf.keras.models.Model(base.input, out)
Now you can view all layers with just model.summary()
.

- 1,119
- 6
- 18
from tensorflow.keras.applications.resnet50 import ResNet50
inp_sh = (256,256,3) # for examples
base_model = ResNet50(weights='imagenet', include_top=False,
input_shape=inp_sh)
CLASSES = 2
x = base_model.output
x = GlobalMaxPooling2D(name='max_pool')(x) # or avarage pooling
out = Dense(CLASSES , activation='softmax')(x)
mdl = Model(inputs=base_model.input, outputs=out)
mdl.summary()

- 2,095
- 2
- 25
- 36