如何在功能性张量流 ResNet50 模型中添加一层?

How to add a layer in a functional tensorflow ResNet50 model?

我正在构建图像字幕模型,为此我使用 ResNet50 作为特征提取模型。代码我写好了,运行正常:

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)

以下是模型摘要的最后几行 (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

但是,问题在于它正在生成 2048 个特征。我没有那么多内存,所以我想将 (None, 7, 7, 2048) 更改为 (None, 7, 7, 1024)

我该怎么做?

一种方法是找到具有输出形状 (None, 14, 14, 1024) 的最后一层并提取模型的层直到该点。 conv4_block6_out 层恰好是最后一个块开始之前的最后一层。这样,最后一个块被完全跳过,从而节省了更多内存。然后,应用一个或多个 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'")