0
$\begingroup$

I'm new to CNN and I was trying to reshape a few images for image recognition but I keep getting an error message which I'm not sure how to fix.

library(keras)

reticulate::install_miniconda()

for (i in 1:10) {mypic[[i]] <- array_reshape(mypic[[i]], c(28,28,3))}
str(mypic)

I get the error message as below.

Error in py_call_impl(callable, dots$args, dots$keywords) : 

ValueError: cannot reshape array of size 784 into shape (28,28,3)

Is there something I'm missing?

$\endgroup$

1 Answer 1

0
$\begingroup$

The error gives you a hint on what the issue is, which is that you cannot reshape an array of 784 items into an array/matrix of shape (28, 28, 3) since they don't have the same number of elements. Based on the number of items I am guessing you are using the MNIST dataset, which consists of black and white images instead of color images. The number of channels in the image should therefore be one instead of three, so changing the shape from c(28, 28, 3) to c(28, 28, 1) should solve the error.

$\endgroup$
3
  • $\begingroup$ I used 10 colour images (5 of plastic bottles and 5 of plastic bags) from Google Images. Is there a different number I can use instead of three? $\endgroup$ Commented Jun 20, 2022 at 21:01
  • $\begingroup$ Then you simply need to make sure that you reshape the array to the correct size of the images you are using. If they indeed are color images then the number channels should be three, you just then need to make sure you are using the correct height and width. $\endgroup$ Commented Jun 21, 2022 at 9:55
  • $\begingroup$ I'm not sure how to reshape the array to the size of images. Is there another line of code or a function I have to use? $\endgroup$ Commented Jun 21, 2022 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.