Tensorflow InvalidArgumentError: Incompatible shapes after slicing input by Lambda Layer

Multi tool use
Tensorflow InvalidArgumentError: Incompatible shapes after slicing input by Lambda Layer
In my Convolution Network, I recently add a Lambda Layer as the input layer for select specific channels of the input images following the answer from this question
model.add(Lambda(lambda x: x[:,:,:2], input_shape=(w, h, 3)))
When I tried to add the MaxPooling2D Layer, I got the error ValueError: Negative dimension size caused by subtracting 3 from 2 for 'max_pooling2d_14/MaxPool' (op: 'MaxPool') with input shapes: [?,250,2,64]
ValueError: Negative dimension size caused by subtracting 3 from 2 for 'max_pooling2d_14/MaxPool' (op: 'MaxPool') with input shapes: [?,250,2,64]
I thought I make some mistakes between Theano and Tensorflow dim order so I edited the Lambda Layer:
model.add(Lambda(lambda x: x[:2,:,:], input_shape=(w, h, 3)))
This time I got no problem when adding more layer, but when I tried to use fit_generator
, it gets the error: InvalidArgumentError: Incompatible shapes: [64] vs. [2]
fit_generator
InvalidArgumentError: Incompatible shapes: [64] vs. [2]
The full trace back is very long, I upload them to here.
I'm using on Linux with 4 GPU for calculation, thanks for your help.
1 Answer
1
The problem lies the way I slice the input using the Lambda Layer.
The input shape has 4 properties in this order: batch_size, width, height, channels.
For selecting multiple array of the input data, because Tensorflow is not supporting the advance indexing method from numpy, we should slice the input tensor first, use dim expanding to add the color depth, and then concatenate them later.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.