例外:模型的输出张量必须是Keras张量

时间:2016-11-18 20:44:25

标签: python tensorflow deep-learning keras

我在运行以下代码时遇到此异常:

base_model = VGG16(weights='imagenet')

conv4_3, conv3_3, conv2_2, conv1_2 = base_model.get_layer('block4_conv3').output, base_model.get_layer('block3_conv3').output, base_model.get_layer('block2_conv2').output, base_model.get_layer('block1_conv2').output

conv1 = Convolution2D(256, 1, 1, border_mode='same')(BatchNormalization()(conv4_3))
conv1_scaled = tf.image.resize_bilinear(conv1, (56, 56))
x = merge([BatchNormalization()(conv3_3), conv1_scaled], mode='sum')
conv2 = Convolution2D(128, 3, 3, border_mode='same', input_shape=(256, 56, 56))(x)

op = Convolution2D(2, 3, 3, border_mode='same')(conv2)

model = Model(input=base_model.input, output=op)

追溯:

Traceback (most recent call last):
  File "keras_btp.py", line 147, in <module>
    model = Model(input=base_model.input, output=op)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1661, in __init__
    'Keras tensors. Found: ' + str(x))
Exception: Output tensors to a Model must be Keras tensors. Found: 

Tensor("add_19:0", shape=(?, 56, 56, 2), dtype=float32)

我意识到错误在merge行。我该如何解决?

1 个答案:

答案 0 :(得分:1)

您会看到此错误,因为合并的其中一个输入不是来自Keras层。相反,请尝试defining your own custom layer for resizing