How do I use only parts of a pretrained network in tensorflow with custom input layer?

Issue

How do I use a certain layer output of a pretrained network, as part of my own custom model with custom input layer?

Heres the code I use:

inputs = layers.Input(shape=(299,299,3)) # my custom input layer, same shape as inceptionv3
inputs = ... # do some stuff here but keep the shapes

pre_trained_model = InceptionV3(input_shape=(299,299,3),
                                include_top=False,
                                weights='imagenet')

pre_trained_model.inputs = inputs

# Freeze the weights of the layers.
for layer in pre_trained_model.layers:
    layer.trainable=False

last_layer = pre_trained_model.get_layer('mixed4')
last_output = last_layer.output
x = layers.Flatten()(last_output)
x = layers.BatchNormalization()(x)
x = layers.Dense(500, activation='relu', kernel_regularizer='l2')(x)
x = layers.Dropout(0.2)(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(256, activation='relu', kernel_regularizer='l2')(x)
x = layers.BatchNormalization()(x)
x = layers.Dense(4, activation='softmax')(x)

model = Model(pre_trained_model.inputs, x)

Solution

You can include an input_tensor in your creation of the InceptionV3 layer.

pre_trained_model = InceptionV3(input_tensors=inputs, 
   input_shape=(299,299,3),
   include_top=False,
   weights='imagenet')

https://www.tensorflow.org/api_docs/python/tf/keras/applications/inception_v3/InceptionV3

Answered By – peytoncas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published