简体   繁体   English

如何在功能性 tensorflow ResNet50 model 中添加一层?

[英]How to add a layer in a functional tensorflow ResNet50 model?

I am building a image captioning model and for that I am using ResNet50 as a feature extraction model. I have written the code, and it is working properly:我正在构建一个图像标题 model,为此我使用ResNet50作为特征提取 model。我已经编写了代码,它工作正常:

rs50 = tf.keras.applications.ResNet50(include_top = False, weights = 'imagenet', input_shape = (224, 224, 3))
new_input = rs50.input
hidden_layer = rs50.layers[-1].output

feature_extract = tf.keras.Model(new_input, hidden_layer)

Below are the last few lines of model summary ( feature_extract.summary() ):以下是 model 摘要 ( feature_extract.summary() ) 的最后几行:

 conv5_block3_3_bn (BatchNormal  (None, 7, 7, 2048)  8192        ['conv5_block3_3_conv[0][0]']    
 ization)                                                                                         
                                                                                                  
 conv5_block3_add (Add)         (None, 7, 7, 2048)   0           ['conv5_block2_out[0][0]',       
                                                                  'conv5_block3_3_bn[0][0]']      
                                                                                                  
 conv5_block3_out (Activation)  (None, 7, 7, 2048)   0           ['conv5_block3_add[0][0]']       
                                                                                                  
==================================================================================================
Total params: 23,587,712
Trainable params: 23,534,592
Non-trainable params: 53,120

But, the problem is that it is generating 2048 features.但是,问题是它生成了 2048 个特征。 I don't have that much memory, so I wanted to change that (None, 7, 7, 2048) to (None, 7, 7, 1024)我没有那么多 memory,所以我想将其(None, 7, 7, 2048)更改为(None, 7, 7, 1024)

How do I do it?我该怎么做?

One way would be to find the last layer that has the output shape (None, 14, 14, 1024) and extract the model's layers until that point.一种方法是找到具有 output 形状(None, 14, 14, 1024)的最后一层,然后提取模型的层直到该点。 The conv4_block6_out layer happens to be the last layer before the last block begins. conv4_block6_out层恰好是最后一个块开始之前的最后一层。 This way, the last block is skipped altogether, which saves more memory. Then, apply one or more Conv2D or MaxPooling layers to get the shape (None, 7, 7, 1024) :这样,最后一个块被完全跳过,节省了更多 memory。然后,应用一个或多个Conv2DMaxPooling层来获得形状(None, 7, 7, 1024)

import tensorflow as tf
rs50 = tf.keras.applications.ResNet50(include_top = False, weights = 'imagenet', input_shape = (224, 224, 3))

index = 0
for i, l in enumerate(rs50.layers):
  if 'conv4_block6_out' in l.name:
    index = i

new_input = rs50.input
hidden_layer = rs50.layers[index].output
output = tf.keras.layers.Conv2D(1024, kernel_size=8)(hidden_layer)

feature_extract = tf.keras.Model(new_input, output)
print(feature_extract.output)
KerasTensor(type_spec=TensorSpec(shape=(None, 7, 7, 1024), dtype=tf.float32, name=None), name='conv2d_4/BiasAdd:0', description="created by layer 'conv2d_4'")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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