Mossand
Mossand

Reputation: 3

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (80, 120, 3)

I am a learner who is just beginning to learn deep learning. I just started using Keras. I want to implement SRCNN. This problem occurs when I try to import a picture to test the model first.

Problem:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (80, 120, 3)

My code is as follows:

from PIL import Image
import numpy as np
from keras import Sequential
from keras.layers import Conv2D, Activation

input_image = Image.open('../../res/image/120x80/120x80 (1).png')
input_image_array = np.array(input_image)

model = Sequential()

model.add(Conv2D(64, (9, 9), data_format='channels_last', activation='relu', input_shape=(80, 120, 3)))
model.add(Conv2D(35, (1, 1), data_format='channels_last', activation='relu', input_shape=(80, 120, 3)))
model.add(Conv2D(1, (5, 5), data_format='channels_last', input_shape=(120, 80, 3)))
model.compile(loss='mean_squared_error', optimizer='sgd')
model.fit(input_image_array, input_image_array)
print(model.summary())

Upvotes: 0

Views: 907

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56347

To give a single input image, you need to include the samples dimension (the first one), so you need to add dimension with a value of one:

input_image_array = np.array(input_image)
input_image_array = input_image_array[np.newaxis, :, :, :]

This will change the shape to (1, 80, 120, 3) which corresponds to one image sample.

Upvotes: 1

Related Questions