0

I am trying to convert 100 images into a numpy array, which in turn will be fed into my neural network. My NN is training data was a 4D numpy array (No of Images, 32, 32, 3). When using below code to read images and feed into model.predict() i am getting following error.

"Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (100, )"

This is the code i have written:

'''new_data = []
files = glob.glob (r"load images")
for myFile in files:
    #print(myFile)
    image = cv2.imread(myFile)
    new_data.append(np.asarray(image))
    
#new_data = np.array(new_data)
print('new_data shape:', np.array(new_data).shape)'''

Output is "new_data shape: (100,)"

I am expecting new_data dimention to be (100, 32, 32, 3). Please help on how to achieve this.

Thanks, Mrinal

Mrinal
  • 105
  • 7

2 Answers2

1

Thanks for all the response.The issue was that images were not of same size. After i resized them all to 32*32 and did a np.reshape(). Below is the revised code

files = glob.glob (r"files\*.png*")
for myFile in files:
    image = cv2.imread(myFile)
    img = cv2.resize(image , (32 , 32)) # Reshaping the testing images to 32*32
    new_data.append(img)



new_data = np.reshape(new_data, (len(new_data),32,32,3))   
Mrinal
  • 105
  • 7
0

you can directly use PILLOW library for this

from PIL import Image
from numpy import asarray

image = Image.open('kolala.jpeg')
# convert image to numpy array
data = asarray(image)
print(type(data))

print(data.shape)


image2 = Image.fromarray(data)
print(type(image2))