'InvalidArgumentError: input must be 4-dimensional[8,6171,4]

I'm running a 2d convolution network. My input has 3 dimension however I am getting this 4-dimension error: dimension error

As you can see my input has the correct dimensions:

correct input dimension

here is my code:

from keras import models
from keras import layers
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(8,6171,4)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

Why then is it asking me for 4-D when I only built a 3-D input layer?

Please help. Thanks.



Solution 1:[1]

You need to use

new_image = tf.expand_dims(image,0)

because the model expects a dataset instead of single images.

Solution 2:[2]

Add an outer batch axis by passing axis=0, 0 is the location of the new dim (1, 416, 416, 3) if -1 will be at end

image_data = tf.expand_dims(image_data, axis=0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill Chen
Solution 2 General Grievance