0
votes

I am reading 230 images as arrays from 2 different folders and resizing it such that it would keep the aspect ratio intact of each image(Resized image size width=600 * height=800). After that I am trying to separate the labels and image arrays into 2 different list. Now before giving the image array list to CNN model I am reshaping it to reshape([-1, 3, 600, 800]) format, but I am getting error as:

ValueError: cannot reshape array of size 230 into shape (3,600,800)

How can I reshape it in above format?

Code written is:

def create_data():
    for category in LABELS:  
        path = os.path.join(DATADIR,category)  
        class_num = LABELS.index(category)  # get the classification  (0 or a 1).
        for img in tqdm(os.listdir(path)):
            img_array = cv2.imread(os.path.join(path,img))  # convert to array
            fac = np.array(img_array).shape[0]/np.array(img_array).shape[1]
            new_array = cv2.resize(img_array, (600, int(np.ceil((fac*600)))))# resize to normalize data size
            data.append([new_array, class_num])  # add to data


create_data()


Xtest = []
ytest = []


for features,label in data:
    Xtest.append(features)
    ytest.append(label)

X = np.array(Xtest).reshape([-1, 3, 600, 800]) 
2
Question has nothing to do with deep-learningorconv-neural-network - kindly do not spam irrelevant tags (removed & replaced with opencv & cv2)desertnaut

2 Answers

2
votes

After cv2.resize, your images all have a height of 600, but different widths. This means they all have a different number of pixels, maybe too many or too few to form the output shape you expect. You will also not be able to concatenate these images into a single large array.

You will need to crop/pad your images to all have the same size.

-1
votes

Don't resize the whole array, resize each image in array individually.

X = np.array(Xtest).reshape([-1, 3, 600, 800]) 

This creates a 1-D array of 230 items. If you call reshape on it, numpy will try to reshape this array as a whole, not individual images in it!