InvalidArgumentError: input must be 4-dimensional[8,6171,4]
Asked Answered
M

2

8

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.

Muniz answered 4/10, 2018 at 18:14 Comment(0)
T
9

You need to use

new_image = tf.expand_dims(image,0)

because the model expects a dataset instead of single images.

Thruway answered 27/4, 2019 at 19:34 Comment(0)
R
3

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)
Retrenchment answered 30/5, 2021 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.