Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 790
bottledatthesource Avatar asked Oct 04 '18 18:10

bottledatthesource


2 Answers

You need to use

new_image = tf.expand_dims(image,0)

because the model expects a dataset instead of single images.

like image 174
Bill Chen Avatar answered Oct 21 '22 17:10

Bill Chen


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)
like image 3
Baher El Naggar Avatar answered Oct 21 '22 17:10

Baher El Naggar