kapike
kapike

Reputation: 321

how can i use neutral network with gray-scale images in keras

I am trying to train on gray images. The batch_size = 32, image size = (48*48). I define my network input_shape = (48,48,1). I get an error like below when I train the network.

Error :

ValueError: Error when checking input: expected conv2d_17_input to have 4 dimensions, but got array with shape (32, 48, 48)

model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=(48,48,1)
                )
         )

Upvotes: 3

Views: 5293

Answers (3)

Sreeram TP
Sreeram TP

Reputation: 11907

Let's say you have 1000 training images where each image is 48x48 greyscale. After you have loaded the images into a numpy array, you will end up with the shape : (1000, 48, 48).

This essentially means you have 1000 elements in your array and each element is a 48x48 matrix.

Now in order to feed this data to train a CNN, you have to reshape this list to (1000, 48, 48, 1) where 1 stands for channel dimension. Since you are having greyscaled images you have to use 1. If it was RGB it will be 3.

Consider the toy example given below,

x_train = np.random.rand(1000, 48, 48) #images
y_train = np.array([np.random.randint(0, 2) for x in range(1000)]) # labels

# simple model

model = Sequential()

model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=(48,48,1)
                )
         )

model.add(Flatten())

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')

# fitting model
model.fit(x_train, y_train, epochs=10, batch_size=32)

This will throw an error,

Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (1000, 48, 48)

To fix it reshape the x_train like this,

x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)

Now fit the model,

model.fit(x_train, y_train, epochs=10, batch_size=32)

Epoch 1/10
1000/1000 [==============================] - 1s 1ms/step - loss: 0.7177
Epoch 2/10
1000/1000 [==============================] - 1s 882us/step - loss: 0.6762
Epoch 3/10
1000/1000 [==============================] - 1s 870us/step - loss: 0.5882
Epoch 4/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.4588
Epoch 5/10
1000/1000 [==============================] - 1s 906us/step - loss: 0.3272
Epoch 6/10
1000/1000 [==============================] - 1s 910us/step - loss: 0.2228
Epoch 7/10
1000/1000 [==============================] - 1s 895us/step - loss: 0.1607
Epoch 8/10
1000/1000 [==============================] - 1s 879us/step - loss: 0.1172
Epoch 9/10
1000/1000 [==============================] - 1s 886us/step - loss: 0.0935
Epoch 10/10
1000/1000 [==============================] - 1s 888us/step - loss: 0.0638

Upvotes: 12

Anakin
Anakin

Reputation: 2010

Your images should be reshaped to (sample_length, 48, 48, 1) and your input_shape = (48, 48, 1)

    x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
    x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
    input_shape = (48, 48, 1)

You can look at the MNIST example here, which is similar to your case.

Upvotes: 0

Krunal V
Krunal V

Reputation: 1253

Your input should have 4 dimenstions even if it is gray scale. So, you can use np.reshape(input,(32,48,48,1)) or np.expand_dims(input,axis=3).

Upvotes: 1

Related Questions