3

hi I am building an image classifier and this an small part of my code in this part m trying to 'img_data_list' is an array and I am converting this array into np.array but (img_ data = np.array(img_data_list)) this line of code giving me an error (could not broadcast input array from shape (128,128,3) into shape (128,128)) i dont know why this is happening

for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)

    print ('Loaded the images of dataset-'+'{}\n'.format(dataset))

    for img in img_list:
        image_path = os.path.join(data_path, dataset, img)

        input_img=cv2.imread(image_path)

        if input_img is not None:
            input_img_resize=cv2.resize(input_img,(128,128))
            img_data_list.append(input_img_resize)

        else:
            print(img+' image didnt read')

img_data = np.array(img_data_list)
img_data = img_data.astype('float32')
img_data /= 255
Dexter
  • 630
  • 1
  • 11
  • 24
  • sorry but I am new in this I didnt understand i mean i dont know what to do I mean how to do it – Dexter Nov 08 '17 at 17:49
  • Sorry, I didn't read your question properly. I know a bit of Numpy, but I don't know cv2. I don't understand how `img_data = np.array(img_data_list)` could give you that error message. Which array has the (128,128,3) shape? – PM 2Ring Nov 08 '17 at 17:57
  • cv2 is an library which is used to read , resize,etc an image and img_data_list is (128,128,3) – Dexter Nov 08 '17 at 18:09
  • 1
    Can you check the shape of `input_img=cv2.imread(image_path,0)` ? – arshovon Nov 08 '17 at 18:11
  • [[[123 175 188] [124 176 189] [124 176 189] ..., [255 255 255] [255 255 255] [255 255 255]] – Dexter Nov 08 '17 at 18:30

3 Answers3

4

I found the solution some images are corrupted in dataset after removing them classifier is working perfectly

Dexter
  • 630
  • 1
  • 11
  • 24
  • May I ask, how did you find these corrupted images? I think I may be having the same problem. The CNN was working nicely until I tried to add more data to it – The-IT Jun 14 '18 at 03:29
  • when I was loading the images and some images were not loading and that thing I can see on my terminal so I got the name of images from the terminal. thanks. – Dexter Oct 17 '18 at 06:24
0

I have not used cv2, but seen this problem in some other places. If any one of the image in the list does not have the expected size(224,224,3) it can give the above problem.

Another similar SO post: ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

Can you regenerate the data and try?

Rahul Pant
  • 707
  • 5
  • 7
  • I didnt see any satisfying answer – Dexter Nov 08 '17 at 18:40
  • Yes I agree this is not an answer but wanted to add as a comment. But my profile did not allow. – Rahul Pant Nov 09 '17 at 01:22
  • https://stackoverflow.com/questions/47295025/valueerror-at-image-tensor-tensoractivation-5-softmax0-shape-4-dtyp?noredirect=1#comment81541739_47295025 any suggestions – Dexter Nov 15 '17 at 05:10
0

That happens when you have an image with the expected shape like @Rahul Pant said, maybe other than (224,224,3), since youve only checked the 0th element shape. I had this error with an Image array, and I was able to fix it by this code.

print(len(img_data_list)) #to check the lenght of the list with elements having different shape

for item in img_data_list:
  if item.shape!=(224,224,3):
    img_data_list.remove(item)

print(len(img_data_list)) #you'll know how many corrupt sized images you had