简体   繁体   中英

Adding layers to RESNET50 in order to build a JOIN CNN Model

This is my code in order to join resnet50 model with this model (that I want to train on my dataset). I want to freeze layers of the resnet50 model ( see Trainable=false) in the code . Here I'm importing resnet 50 model

`` 
import tensorflow.keras
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
resnet50_imagnet_model = tensorflow.keras.applications.resnet.ResNet50(weights = "imagenet", 
                           include_top=False, 
                           input_shape = (150, 150, 3),
                           pooling='max')
  ``

Here I create my model

 ```
# freeze feature layers and rebuild model
for l in resnet50_imagnet_model.layers:
    l.trainable = False

#construction du model
model5 = [
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(12, activation='softmax')
]

#Jointure des deux modeles
model_using_pre_trained_resnet50 = tf.keras.Sequential(resnet50_imagnet_model.layers + model5 )
 ```

Last line doesn't work and I have this error : Input 0 of layer conv2_block1_3_conv is incompatible with the layer: expected axis -1 of input shape to have value 64 but received input with shape [None, 38, 38, 256

Thanks for help .

You can also use keras' functional API , like below

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

    resnet50_imagenet_model = ResNet50(include_top=False, weights='imagenet', input_shape=(150, 150, 3))

    #Flatten output layer of Resnet
    flattened = tf.keras.layers.Flatten()(resnet50_imagenet_model.output)

    #Fully connected layer 1
    fc1 = tf.keras.layers.Dense(128, activation='relu', name="AddedDense1")(flattened)

    #Fully connected layer, output layer
    fc2 = tf.keras.layers.Dense(12, activation='softmax', name="AddedDense2")(fc1)

    model = tf.keras.models.Model(inputs=resnet50_imagenet_model.input, outputs=fc2)

Also refer this question .

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