简体   繁体   中英

TypeError: Output tensors to a Model must be Keras tensors

I want to take an input image img (which also has negative values) and feed it into two activation layers. However, I want to make a simple transformation eg multiply the whole image with -1.0 :

left = Activation('relu')(img)
right = Activation('relu')(tf.mul(img, -1.0))

If I do it this way I am getting:

TypeError: Output tensors to a Model must be Keras tensors. Found: Tensor("add_1:0", shape=(?, 5, 1, 3), dtype=float32)

and I am not sure how I can fix that. Is there a Keras side mul() method that I can use for such a thing? Or can I wrap the result of tf.mul(img, -1.0) somehow such that I can pass it on to Activation ?

Please note: The negative values may be important. Thus transforming the image st the minimum is simply 0.0 is not a solution here.


I am getting the same error for

left = Activation('relu')(conv)
right = Activation('relu')(-conv)

The same error for:

import tensorflow as tf

minus_one = tf.constant([-1.])

# ...

    right = merge([conv, minus_one], mode='mul')

Does creating a Lambda Layer to wrap your function work?

See doc here

from keras.layers import Lambda
import tensorflow as tf

def mul_minus_one(x):
    return tf.mul(x,-1.0)
def mul_minus_one_output_shape(input_shape):
    return input_shape

myCustomLayer = Lambda(mul_minus_one, output_shape=mul_minus_one_output_shape)
right = myCustomLayer(img)
right = Activation('relu')(right)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM