ValueError:应在输入列表上调用合并层.Tensorflow Keras [英] ValueError: A merge layer should be called on a list of inputs. Tensorflow Keras

查看:177
本文介绍了ValueError:应在输入列表上调用合并层.Tensorflow Keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用MobileNetV2的前50层.因此,我想提取这些图层并创建一个新模型.

I am currently trying to use the first 50 layers of the MobileNetV2. Therefore, I want to extract those layers and create a new model.

我以为我可以调用每个层,但是"block_2_add"层会导致错误,我不明白为什么.

I thought I could just call every layer, but the "block_2_add" layer causes an error and I don't understand why.

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不是顺序模型,即层图不是线性的.如果您只需要模型的输出而不是任何中间层的输出,我认为下面的代码就可以完成这项工作(即使您似乎想在输出之前计算最后一层,结果仍然应该是您想要的):

My guess is that the MobileNetV2 is not a sequential model, i.e. the layers graph is not linear. If you want just the output of the model and not any intermediate layer outputs, I think following code should do the job (even though it seems that you want to compute the last layer before output, the result still should be what you want):

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)

这篇关于ValueError:应在输入列表上调用合并层.Tensorflow Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆