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

51sep picture 51sep · Feb 11, 2020 · Viewed 11.3k times · Source

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

Answer

Dev Bhuyan picture Dev Bhuyan · Nov 2, 2020

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.