Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidArgumentError : input depth must be evenly divisible by filter depth: 4 vs 3

I'm a beginner. I tried Image Classification by Tensorflow, and got the following error. I found the similar issue on web, but I couldn't understand. What does the error mean? How should I do for it? Please give me some advice. I use 100 files(png/15pix, 15pix) like a sample image. Tensorflow ver.2.0.0 / python ver.3.8.1 / Jupyter notebook.

sample image

    num_epochs = 30
    steps_per_epoch = round(num_train)//BATCH_SIZE
    val_steps = 20
    history = model.fit(train_data.repeat(),
                epochs=num_epochs,
                steps_per_epoch = steps_per_epoch,
                validation_data=val_data.repeat(), 
                validation_steps=val_steps)

InvalidArgumentError: input depth must be evenly divisible by filter depth: 4 vs 3 [[node sequential_2/mobilenetv2_1.00_96/Conv1/Conv2D (defined at C:\Users\XXXXX\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_distributed_function_42611] Function call stack: distributed_function

like image 915
51sep Avatar asked Feb 11 '20 18:02

51sep


4 Answers

I ran into this error because I was using images that had been converted to grayscale as my data. If anyone is doing this, you can either convert from grayscale to color-format, or re-prepare your data without converting to grayscale, which is what I did.

Per the solution I found: "Perhaps you are trying to feed a grayscale image into CNN which expects a color image. Find shape of input, e.g. print(model.input.shape) in Keras, you get e.g. (None, 224, 224, 3) and your input blob must have a corresponding shape, so having a grayscale image you have to convert it into a (formal) color image (all the three channels will be same). However, do not forget that you need know also further aspects of the input blob - mean, range, deviation, … having a good shape, it calculates something, but without concerning these aspects, the calculated result is not good"

like image 99
jpb647 Avatar answered Sep 19 '22 09:09

jpb647


If your model looks like this:

model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation = 'relu', input_shape = (150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3, 3), activation = 'relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.Dense(10, activation = 'softmax')
])

Change the value of input_shape (at the first convolutional layer) from (150, 150, 3) to (150, 150, 4).

Replace only the last term (which is 3 here) in the tuple with 4. That should make it work.

like image 10
Dev Bhuyan Avatar answered Oct 09 '22 03:10

Dev Bhuyan


I think you read a 4 channel format image. you should convert input image to 'RGB' before forwarding.

like image 5
hongxing zhang Avatar answered Oct 09 '22 04:10

hongxing zhang


I found the answer! In my case, the following program helped it.

XXX = tf.convert_to_tensor(XXX[:,:,:3])

I wish it would help you too. Thank you.

like image 4
51sep Avatar answered Oct 09 '22 05:10

51sep