Python - Keras:将两个模型合并为一个序列



如何用两个模型创建一个连续的模型?我有两个模型,一个是Keras应用程序(vgg16模型),一个是自定义模型,我想将它们合并到一个顺序模型中。

我试着这样做:

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',
)

但是当我打印模型的摘要时,我有这样的东西:https://i.stack.imgur.com/xSKTg.png

但是我想在我的总结中有VGG16模型的所有层,我怎么做呢?

试试这样:

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

相关内容

  • 没有找到相关文章

最新更新