1

I want to load the mnist dataset to the mobilenet V1 CNN then, I faced with this problem

ValueError: Error when checking input: expected input_1 to have shape (32, 32, 3) but got array with shape (28, 28, 1)

Below is my code

image_data, label_data = data['image'], data['label']


idx_list = {}
for i in range(10):
    idx_list[i] = np.where(label_data == i)  # return tuple dtype (rows indices, column indices)


selected_test_sample_indices = {}
for label in range(10):
    selected_test_sample_indices[label] = random.sample(set(idx_list[label][0]), int(len(idx_list[label][0]) * 0.2))


selected_train_sample_indicies = {}

for label in range(10):
    selected_train_sample_indicies[label] = list(set(idx_list[label][0])- set(selected_test_sample_indices[label]))


train_data_indicies, test_data_indicies = [],[]


for label, indicies in selected_train_sample_indicies.items():
    train_data_indicies = train_data_indicies + indicies # merge 2 list

for label, indicies in selected_test_sample_indices.items():
    test_data_indicies = test_data_indicies + indicies

random.shuffle(train_data_indicies)
random.shuffle(test_data_indicies)


y_train_data = np.array([label_data[idx] for idx in train_data_indicies])
X_train_data = np.array([image_data[idx] for idx in train_data_indicies])

y_test_data = np.array([label_data[idx] for idx in test_data_indicies])
X_test_data = np.array([image_data[idx] for idx in test_data_indicies])

number_of_classes = 10
y_train = y_train_data
y_test = y_test_data


X_train = X_train_data.reshape(X_train_data.shape[0], img_rows, img_cols, 1)
X_test = X_test_data.reshape(X_test_data.shape[0], img_rows, img_cols, 1)```

Whenn I tried to reshape I got the following error

ValueError: cannot reshape array of size 11146912 into shape (14218,32,32,1)

when I change it to (4500,32,32,3), the sum is lower than 11146912 It really confused me. Please help me to fix this bug.

coderina
  • 1,583
  • 13
  • 22
Jovan Mei
  • 15
  • 5

2 Answers2

0

The MNIST dataset contains images in grayscale with the size of 28x28 pixels. That is why the shape of each image is (28, 28, 1) with each value between 0-255. Here's another stackoverflow question with the same problem. The most valid answer is to convert the grayscale images into rgb images and then resizing the images.

reddragonnm
  • 16
  • 1
  • 2
  • Thanks for your explanation, I look through that website, which only changes the batch to 3, but for the value of col, row, I fail to change it to (32, 32), it also alters me that 'ValueError: cannot reshape array of size 11146912 into shape (14218,32,32,3)'", how can I solve this problem. – Jovan Mei Feb 11 '21 at 02:17
  • @JovanMei You can check out opencv to resize the images – reddragonnm Feb 12 '21 at 12:20
0

Well after converting the grayscale images to rgb images the shape of your images will change from

28 x 28 x 1 to 28 x 28 x 3

Then you need to resize it to 32. You can use openCV library for that.

resized_image = cv2.resize(image, (32, 32))

Then your resized_image shape would be 32 x 32 x 3

coderina
  • 1,583
  • 13
  • 22