I get this exception running the following code:
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)
The error is:
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)
I realize that the error is in the merge line. How do I fix it?
i think the problem comes from the fact that you take the output of a keras.layer (a "keras tensor", which is basically a tensorflow-tensor with some additional attributes, such as _keras_history) and call the tensorflow function tf.image.resize_bilinear on it, which in turn produces a tf-tensor (lacking the additional attributes that the keras-tensor has). These missing attributes then propagate along , and in the end, Model(...) complains about not recieving a "keras-tensor".
Just encountered the same issue in a different context (basically also trying to sneak in a few tensorflow operations outside of the keras.layers) but havent found a solution to it (except creating a custom layer with the same functionality)
UPDATE: you're probably able to solve it by just putting your function call (tf.image.resize_bilinear) into a Lambda-layer, see e.g. #3130 or #2486
Most helpful comment
i think the problem comes from the fact that you take the output of a keras.layer (a "keras tensor", which is basically a tensorflow-tensor with some additional attributes, such as
_keras_history) and call the tensorflow functiontf.image.resize_bilinearon it, which in turn produces a tf-tensor (lacking the additional attributes that the keras-tensor has). These missing attributes then propagate along , and in the end,Model(...)complains about not recieving a "keras-tensor".Just encountered the same issue in a different context (basically also trying to sneak in a few tensorflow operations outside of the keras.layers) but havent found a solution to it (except creating a custom layer with the same functionality)
UPDATE: you're probably able to solve it by just putting your function call (
tf.image.resize_bilinear) into aLambda-layer, see e.g. #3130 or #2486