austinkjensen
austinkjensen

Reputation: 1056

Input to tf.keras Conv2D layer not of appropriate size

I am following the steps outlined in the tutorial here

I am attempting to run the following code from the tutorial in a cell inside of a Google Colaboratory notebook:

  import tensorflow as tf
  mnist = tf.keras.datasets.mnist
  (x_train, y_train), (x_test, y_test) = 
  tf.keras.datasets.fashion_mnist.load_data()
  x_train = x_train.astype('float32') / 255
  x_test = x_test.astype('float32') / 255
  model = tf.keras.Sequential()



 # Must define the input shape in the first layer of the neural network
  model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1))) 



 model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
  model.add(tf.keras.layers.Dropout(0.3))
  model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))
  model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
  model.add(tf.keras.layers.Dropout(0.3))
  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dense(256, activation='relu'))
  model.add(tf.keras.layers.Dropout(0.5))
  model.add(tf.keras.layers.Dense(10, activation='softmax'))
  # Take a look at the model summary
  model.compile(loss='categorical_crossentropy',
               optimizer='adam',
               metrics=['accuracy'])
  model.fit(x_train,
           y_train,
           batch_size=64,
           epochs=10)


  # Evaluate the model on test set
  score = model.evaluate(x_test, y_test, verbose=0)
  # Print test accuracy
  print('\n', 'Test accuracy:', score[1])

When I run the cell , I get the following error:

Error when checking input: expected conv2d_5_input to have 4 dimensions, but got array with shape (60000, 28, 28)

I feel like I am missing something that is fundamental to the usage of a convolutional layer, nonetheless it appears as though this should have worked. I found some similar questions on SO where people recommended manipulating the "input_shape" argument. I've tried changing it to (60000, 28, 28) and also added additional dimensions with values of 1, but nothing has worked so far. Can anyone point out where I might be missing something here?

Upvotes: 1

Views: 2304

Answers (1)

Denis Zubo
Denis Zubo

Reputation: 356

It looks like you skipped reshaping part from tutorial:

# Reshape input data from (28, 28) to (28, 28, 1)
w, h = 28, 28
x_train = x_train.reshape(x_train.shape[0], w, h, 1)
x_valid = x_valid.reshape(x_valid.shape[0], w, h, 1)
x_test = x_test.reshape(x_test.shape[0], w, h, 1)

The idea here is that your samples are 28x28x1 (one color, 28x28 pixels), and the first dimension - the number of the sample (60000 in your case).

Upvotes: 2

Related Questions