tf.keras replace lower layer in pretrained resnet50

Viewed 1160

Is it possible to remove/replace the BOTTOM layers of a pretrained ResNet50 model in tf.keras.applications?

For instance, I've tried doing this:

import tensorflow as tf
pretrained_resnet = tf.keras.applications.ResNet50(include_top=False, weights='imagenet')
inputs = tf.keras.Input(shape=(256,256,1))
x = tf.keras.layers.ZeroPadding2D()(inputs)
x = tf.keras.layers.Conv2D(filters=64,
                           kernel_size=(7,7),
                           strides=(2,2),
                           padding='same')(x)
outputs = pretrained_resnet.layers[3](x)
test = tf.keras.Model(inputs, pretrained_resnet.output)

But it gives this error: ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_2:0", .......

I've also tried using the tf.keras Sequential API, but that doesn't work since ResNet isn't a sequential model. I'm basically just trying to replace the first Conv2D layer in ResNet50 with a new one. Is this possible? Or do I have to rewrite the entire ResNet model?

Any suggestions would be greatly appreciated!

1 Answers

ZeroPadding2D and Conv2D (7*7, 64, stride 2) are the 2nd and 3rd layers of Resnet50 network.

Hence, showing here to replace only first layer (i.e input layer) in Resnet50

from tensorflow.keras.applications import ResNet50
import tensorflow as tf

model = ResNet50(include_top = False, weights = 'imagenet')
model.save('model.h5')

res50_model = tf.keras.models.load_model('model.h5')
#res50_model.summary()

To remove first layer from network, you can run code as below

 res50_model._layers.pop(0)

Resnet50 expects the input must have 3 channels, hence adding input layer shape as (256,256,3) instead of (256,256,1).

To add new input layer, you can run code as below

newInput = tf.keras.Input(shape=(256,256,3))
newOutputs = res50_model(newInput)
newModel = tf.keras.Model(newInput, newOutputs)
newModel.summary()

Output:

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         [(None, 256, 256, 3)]     0         
_________________________________________________________________
resnet50 (Model)             multiple                  23587712  
=================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120
_________________________________________________________________
Related