简体   繁体   中英

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM