I'm using a pre-trained ResNet model and I'm training few layers of the model with my dataset but I want to include the ResNet's preprocessing as a layer of the model.
How to do this ?
I'm using a pre-trained ResNet model and I'm training few layers of the model with my dataset but I want to include the ResNet's preprocessing as a layer of the model.
How to do this ?
Adding a preprocessing layer after the Input layer is the same as adding it before the ResNet50 model,
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
# Some preprocessing layer here ...
preprocessing_layer = tf.keras.layers.Normalization( mean=0 , variance=1 , input_shape=( 256 , 256 , 3 ) )
model = tf.keras.models.Sequential( [
preprocessing_layer ,
resnet,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(22, activation='softmax',name='output')
])
model.compile( loss='mse' , optimizer='adam' )
model.summary()
The output,
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
normalization (Normalizatio (None, 256, 256, 3) 0
n)
resnet50 (Functional) (None, 2048) 23587712
flatten (Flatten) (None, 2048) 0
output (Dense) (None, 22) 45078
=================================================================
Total params: 23,632,790
Trainable params: 45,078
Non-trainable params: 23,587,712
_________________________________________________________________
The inputs will now go through the preprocessing_layer first, and then to the resnet model.
If you want to use tf.keras.applications.resnet50.preprocess_input(), try:
import tensorflow as tf
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
model = tf.keras.Sequential()
model.add(tf.keras.layers.Lambda(tf.keras.applications.resnet50.preprocess_input, input_shape=(256, 256, 3)))
model.add(resnet)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(22, activation='softmax',name='output') )
model.compile( loss='mse' , optimizer='adam' )
print(model(tf.random.normal((1, 256, 256, 3))))
tf.Tensor(
[[0.12659772 0.02955576 0.13070999 0.0258545 0.0186768 0.01459627
0.07854564 0.010685 0.01598095 0.04758708 0.05001146 0.20679766
0.00975605 0.01047837 0.00401289 0.01095579 0.06127766 0.0313729
0.00884041 0.04098257 0.01187507 0.05484949]], shape=(1, 22), dtype=float32)