我目前正在尝试使用 MobileNetV2 的前 50 层。因此,我想提取这些层并创建一个新模型。
我以为我可以调用每一层,但是“block_2_add”层会导致错误,我不明白为什么。
import tensorflow as tf
from keras.models import Model
mobile_net=tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224,224,3), alpha=0.5, include_top=False, weights='imagenet')
inputs = Input(shape=(224, 224, 3))
x=mobile_net.layers[1](inputs)
for layer in mobile_net.layers[2:50]:
x=layer(x)
{'name': 'block_2_add', 'trainable': True, 'dtype': 'float32'}
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-77-5873b9344fa3> in <module>()
3 for layer in mobile_net.layers[2:50]:
4 print(layer.get_config())
----> 5 x=layer(x)
6
7 for layer in mobile_net.layers[:50]:
1 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in call(self, inputs)
119 def call(self, inputs):
120 if not isinstance(inputs, list):
--> 121 raise ValueError('A merge layer should be called on a list of inputs.')
122 if self._reshape_required:
123 reshaped_inputs = []
ValueError: A merge layer should be called on a list of inputs.
我的猜测是 MobileNetV2 不是顺序模型,即层图不是线性的。如果您只想要模型的输出而不是任何中间层输出,我认为以下代码应该可以完成这项工作(即使您似乎想在输出之前计算最后一层,结果仍然应该是您想要的):
import tensorflow as tf
from keras.models import Model
mobile_net=tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224,224,3), alpha=0.5, include_top=False, weights='imagenet')
inputs = Input(shape=(224, 224, 3))
output = mobile_net(inputs)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)