如何在功能性张量RESNET50模型中添加一层?
我正在构建图像字幕模型,为此,我将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个功能。我没有那么多内存,所以我想更改(无,7、7、2048)
(无,7、7、1024)
我该怎么做?
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:
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()
):
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. I don't have that much memory, so I wanted to change that (None, 7, 7, 2048)
to (None, 7, 7, 1024)
How do I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是找到具有输出形状
的最后一层(无,14、14、1024)
并提取模型的图层直到那一点。conv4_block6_out
图层恰好是最后一个块开始之前的最后一层。这样,最后一个块被完全跳过,从而节省了更多的内存。然后,应用一个或多个conv2d
或maxpooling
层以获取形状(无,7、7、1024)
: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. Theconv4_block6_out
layer happens to be the last layer before the last block begins. This way, the last block is skipped altogether, which saves more memory. Then, apply one or moreConv2D
orMaxPooling
layers to get the shape(None, 7, 7, 1024)
: