'Python - Keras : Merge two models into one sequential

How do create one model sequential with two models? I have two models, one a Keras application (vgg16 model) and a custom model and I would like to merge them into one sequential model.

I try to do it in this way :

VGG16_model = tf.keras.applications.VGG16(
    include_top=False,
    weights='imagenet',
    pooling='avg'
)

teacher = tf.keras.Sequential(
    [
        VGG16_model,
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation=('relu')),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(256, activation=('relu')),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, activation=('softmax'))
    ],
    name = 'teacher',
)


But when I print the summary of the model I have this thing : https://i.stack.imgur.com/xSKTg.png

But I would like to have in my summary all the layers of the VGG16 model, how can I do it?



Solution 1:[1]

Try something like this:

import tensorflow as tf

VGG16_model = tf.keras.applications.VGG16(
    include_top=False,
    weights='imagenet',
    pooling='avg'
)

teacher = tf.keras.Sequential(name = 'teacher')
for l in VGG16_model.layers:
  teacher.add(l)

teacher.add(tf.keras.layers.Flatten())
teacher.add(tf.keras.layers.Dense(512, activation=('relu')))
teacher.add(tf.keras.layers.Dropout(0.2))
teacher.add(tf.keras.layers.Dense(256, activation=('relu')))
teacher.add(tf.keras.layers.Dropout(0.2))
teacher.add(tf.keras.layers.Dense(10, activation=('softmax')))

print(teacher.summary())
Model: "teacher"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 block1_conv1 (Conv2D)       (None, None, None, 64)    1792      
                                                                 
 block1_conv2 (Conv2D)       (None, None, None, 64)    36928     
                                                                 
 block1_pool (MaxPooling2D)  (None, None, None, 64)    0         
                                                                 
 block2_conv1 (Conv2D)       (None, None, None, 128)   73856     
                                                                 
 block2_conv2 (Conv2D)       (None, None, None, 128)   147584    
                                                                 
 block2_pool (MaxPooling2D)  (None, None, None, 128)   0         
                                                                 
 block3_conv1 (Conv2D)       (None, None, None, 256)   295168    
                                                                 
 block3_conv2 (Conv2D)       (None, None, None, 256)   590080    
                                                                 
 block3_conv3 (Conv2D)       (None, None, None, 256)   590080    
                                                                 
 block3_pool (MaxPooling2D)  (None, None, None, 256)   0         
                                                                 
 block4_conv1 (Conv2D)       (None, None, None, 512)   1180160   
                                                                 
 block4_conv2 (Conv2D)       (None, None, None, 512)   2359808   
                                                                 
 block4_conv3 (Conv2D)       (None, None, None, 512)   2359808   
                                                                 
 block4_pool (MaxPooling2D)  (None, None, None, 512)   0         
                                                                 
 block5_conv1 (Conv2D)       (None, None, None, 512)   2359808   
                                                                 
 block5_conv2 (Conv2D)       (None, None, None, 512)   2359808   
                                                                 
 block5_conv3 (Conv2D)       (None, None, None, 512)   2359808   
                                                                 
 block5_pool (MaxPooling2D)  (None, None, None, 512)   0         
                                                                 
 global_average_pooling2d_2   (None, 512)              0         
 (GlobalAveragePooling2D)                                        
                                                                 
 flatten_1 (Flatten)         (None, 512)               0         
                                                                 
 dense_3 (Dense)             (None, 512)               262656    
                                                                 
 dropout_2 (Dropout)         (None, 512)               0         
                                                                 
 dense_4 (Dense)             (None, 256)               131328    
                                                                 
 dropout_3 (Dropout)         (None, 256)               0         
                                                                 
 dense_5 (Dense)             (None, 10)                2570      
                                                                 
=================================================================
Total params: 15,111,242
Trainable params: 15,111,242
Non-trainable params: 0
_________________________________________________________________
None

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AloneTogether