Element-wise multiplication with Keras
You need a Reshape
so both tensors have the same number of dimensions, and a Multiply
layer
mask = Reshape((256,256,1))(mask)
out = Multiply()([image,mask])
If you have variable shapes, you can use a single Lambda
layer like this:
import keras.backend as K
def multiply(x):
image,mask = x
mask = K.expand_dims(mask, axis=-1) #could be K.stack([mask]*3, axis=-1) too
return mask*image
out = Lambda(multiply)([image,mask])
As an alternative you can do this using a Lambda
layer (as in @DanielMöller's answer you need to add a third axis to the mask):
from keras import backend as K
out = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([image, mask])