3

I'm trying to use my own 28x28 pixel image in my CNN but I keep getting this error when I'm trying to reshape it:

x = x.reshape(1,28,28,1)

ValueError: cannot reshape array of size 2352 into shape (1,28,28,1)

My image is 28x28, but I can't change it into a single greyscale channel. I've been using spyder via anaconda and I keep getting an "UnsatisfiableError" whenever I am trying to install opencv onto my root environment. Pip install doesn't work either. I was wondering if anyone had anyway to reshape my images without using an external library and just using tensorflow.

I have searched SO for the answer and tried solutions suggested but unfortunately none of them have worked.

Thanks in advance.


In reply to AKX:

def Predict(imgPath):
    x = plt.imread(imgPath)
    x = x.reshape(1,28,28,1)


    with graph.as_default():
        out = model.predict(x)
        return out

In reply to Skander HR:

(28,28,3)

In reply to Matias Valdenegro:

My problem is that openCV isn't working. It's installed but I've tried to check if there were any proxy servers, installed python3.dll and tried installing the microsoft mediafeaturepack to get rid of the error but none of those worked. When I write a program like this:

import cv2 as cv
print("done")

I get the error message:

    import cv2 as cv
ImportError: DLL load failed: The specified module could not be found.
Alice Lin
  • 33
  • 7

3 Answers3

0

you are mostly using an RGB image of size 28*28 so your size is 28*28*3=2352.you need to convert it into grayscale image to match your tensor shape.

you can convert using opencv using below command:-

gray_image = cv2.imread(path_to_image, cv2.IMREAD_GRAYSCALE) 
Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
Nikas
  • 247
  • 1
  • 6
  • Thanks but the problem is that I can't use opencv therefore cant use cv2. – Alice Lin Feb 06 '20 at 09:38
  • check this link if you are using numpy https://stackoverflow.com/questions/41971663/use-numpy-to-convert-rgb-pixel-array-into-grayscale – Nikas Feb 06 '20 at 09:46
0

Just use Pillow to resize your image:

from PIL import Image
import numpy as np 
...
img = Image.fromarray(x)
img = img.resize((28,28))
x = np.asarray(img)
x = x.reshape(1,28,28,1)
...

install it using pip pip install pillow or python3 -m pip install pillow so that you're sure you're using the correct python version.

Frederik Bode
  • 2,632
  • 1
  • 10
  • 17
  • Hi Frederik, when I add it to my code I get this error: raise TypeError("Cannot handle this data type") TypeError: Cannot handle this data type – Alice Lin Feb 06 '20 at 09:58
0

There are two concepts to take into consideration when predicting on your own image, and using grayscale images.

  1. By default, the grayscale mode in OpenCV or PIL, considers a gray image without the last axis(color axis). That means, you have to use np.expand_dims(image,axis=2) or image = image[..., np.newaxis]. After this step, your image will be of size (width,height,1).
  2. You use again, np.expand_dims(image, axis=0), to add the batch_index size. This is necessary, since by default in Keras/TensorFlow you only predict on batches. In your case, a photo practically means a batch_size of 1.

If you bear these two in mind you will successfully and easily use model.predict() to get your prediction.

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59