python -keras:将两个模型合并为一个顺序

发布于 2025-01-23 08:49:57 字数 800 浏览 0 评论 0原文

如何使用两个模型创建一个模型顺序? 我有两个模型,一个是一个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.sstatic.net/xsktg.png

但是我想拥有我的总结VGG16型号的所有层,我该怎么做?

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.sstatic.net/xSKTg.png

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静待花开 2025-01-30 08:49:57

尝试这样的事情:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文