Reputation: 912
In my Keras CNN, I add the Input layer like this:
model.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, nb_channel)))
with nb_channel = 3 for RGB input and = 1 for grayscale input and the flow_from_directory
and ImageDataGenerator
However, I want to specify a set of color to channel to input to my CNN, for example, only green and red channels are permitted, how can I do so?
I'm using Keras with tensorflow Backend
Beside from the neat solution of @Minh-Tuan Nguyen, we can also do the slicing as follow
#custom filter
def filter_layer(x):
red_x = x[:,:,:,0]
blue_x = x[:,:,:,2]
green_x = x[:,:,:,1]
red_x = tf.expand_dims(red_x, axis=3)
blue_x = tf.expand_dims(blue_x, axis=3)
green_x = tf.expand_dims(green_x, axis=3)
output = tf.concat([red_x, blue_x], axis=3)
return output
#model
input = Input(shape=(img_height, img_width, img_channels))
at the concat step we can choose the slice we want.
Upvotes: 1
Views: 3384
Reputation: 358
I think it would be easier to process the slicing a bit more "naively" here since as my knowledge, Keras hasn't support slicing the tensor using a list of indices like python and numpy. Below is the example of my code for this problem. Try to see if it fit your requirement.
indices = [0,2]
def filter_layer(input, indices=indices):
for i in range(len(indices)):
index = indices[i]
x_temp = Lambda(lambda x: x[:,:,:,index][...,None])(input)
if i==0:
x = x_temp
else:
x = Concatenate(-1)([x, x_temp])
return x
input = Input(shape=(img_height, img_width, img_channels))
x = Lambda(filter_layer)(input)
Upvotes: 2
Reputation: 11225
You can slice the input tensor inside a custom Lambda layer. Suppose you want only red and green:
model.add(Lambda(lambda x: x[:,:,:,:2], input_shape=(w, h, channels)))
TensorFlow allows for similar slicing to NumPy, for Keras you need wrap it around a Lambda layer to incorporate into your model.
Upvotes: 4