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)
CodePudding user response:
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