I'm building a neural network and I'm trying to load colored images into the network but I keep getting a reshaping error. I resized all of the images to the smallest dimensions (in this case 110 x 110) but when I try to convert the X (an unflattened 3d list of the pixels of each image) to a numpy array to be called xTrain with this line of code:
xTrain = np.array(X[:trainNum])
i get this error: "ValueError: could not broadcast input array from shape (110,110,3) into shape (110,110)"
does anyone know why it keeps doing that? i assume it's because of my data because my partner copied the same exact code with his own images and the conversion to a numpy array was successful but mine isn't. for reference the list titled X is in this format:
[array([[[137, 151, 199],
[ 93, 114, 166],
[116, 121, 164],
...,
[124, 124, 175],
[160, 162, 193],
[154, 157, 177]],
[[ 81, 94, 153],
[106, 123, 184],
[119, 124, 180],...
how do I fix this?
Most likely, your X list contains a mixture of grayscale and RGB images.
img_rgb = np.zeros((110, 110, 3))
img_gry = np.zeros((110, 110))
X_good = [img_rgb, img_rgb, img_rgb]
np.array(X_good[:])
# OK
X_bad = [img_rgb, img_gry, img_rgb]
np.array(X_bad[:])
# ValueError: could not broadcast input array from shape (110,110,3) into shape (110,110)
You can convert the grayscale image(s) in X to RGB:
def make_rgb(img):
if len(img.shape) == 3:
return img
img3 = np.empty(img.shape + (3,))
img3[:, :, :] = img[:, :, np.newaxis]
return img3
X_repaired = [make_rgb(im) for im in X_bad]
np.array(X_repaired[:])
# No problem
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With