0

I couldn't find the solution.My image shape is 128*128*3,it has three channel,but it also cause the error

File "E:/ML/keras_test/vgg.py", line 30, in load_data data[i,:,:,:] = arr

ValueError: could not broadcast input array from shape (128,128) into shape (128,128,3)

My code as below:

def load_data(path):
data = np.empty((12755,128,128,3),dtype="uint8")
label = np.empty((12755,),dtype="uint8")


imgs = []
imgs_name = []
for each_person in os.listdir(path):
    temp = os.path.join(path,each_person)
    for each_image in os.listdir(temp):
        imgs.append(temp + "\\" + each_image)
        imgs_name.append(each_image)  

num = len(imgs)
for i in range(num):
    img = Image.open(imgs[i])
    arr = np.asarray(img,dtype="uint8")
    print arr.shape
    data[i,:,:,:] = arr
    label[i] = int(imgs_name[i].split('.')[0])

print 'load_data is ok!' + str(data.shape[0])
return data,label
sky
  • 103
  • 1
  • 7

1 Answers1

0

You are trying to put the original data into a small package, which is impossible. I think you are trying to transfer a image which has RGB channel into a gray scale image which has one channel. Try

datum = (imgs.sum(axis=2) / 3).reshape((12755, -1))

The resulting datum is a 12755 x 16384 array.

David S.
  • 10,578
  • 12
  • 62
  • 104
  • but my image really has RGB channel.And if I have use arr.shape to check it.I am sure each image has three channel – sky Mar 24 '16 at 10:18
  • My image has three channel,then it should be 16384*3 array.So I creat an array which has the shpe(12755,128,128,3). But I don't know why it'll have this error? – sky Mar 24 '16 at 10:26
  • So you want to keep the 3 channels...I misunderstood you. Try explain the target array shape. – David S. Mar 25 '16 at 08:31