[英]How to add a layer in a functional tensorflow ResNet50 model?
我正在構建一個圖像標題 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)
以下是 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
但是,問題是它生成了 2048 個特征。 我沒有那么多 memory,所以我想將其(None, 7, 7, 2048)
更改為(None, 7, 7, 1024)
我該怎么做?
一種方法是找到具有 output 形狀(None, 14, 14, 1024)
的最后一層,然后提取模型的層直到該點。 conv4_block6_out
層恰好是最后一個塊開始之前的最后一層。 這樣,最后一個塊被完全跳過,節省了更多 memory。然后,應用一個或多個Conv2D
或MaxPooling
層來獲得形狀(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.