1
$\begingroup$

I am running my LeNet code with LFW, but when I run it, I am getting the following error message:

Here is the code that it is getting the error

# Import the packages
from keras.preprocessing.image import ImageDataGenerator

# Image Data Augmentation
train_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
val_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)
test_generator = ImageDataGenerator(rotation_range=2, horizontal_flip=True, zoom_range=.1)

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain)
val_generator.fit(x_val)
test_generator.fit(xtest)

# Construct the image generator for data augmentation
aug = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1,
                         horizontal_flip=True, fill_mode="nearest")

I then added the following

# Fitting the augmentation defined above to the data
train_generator.fit(xtrain.reshape(96, 227, 227, 1))
val_generator.fit(x_val.reshape(96, 227, 227, 1))
test_generator.fit(xtest.reshape(96, 227, 227, 1))

but then got this:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 134, in <module>
    train_generator.fit(xtrain.reshape(96, 227, 227, 1))
ValueError: cannot reshape array of size 36276416 into shape (96,227,227,1)

I have added the full code for more help on how to combat this issue. What is the fix for something like this?

UPDATE: I then made the following changes to:

train_generator.fit(xtrain.reshape(-1, 227, 227))
val_generator.fit(x_val.reshape(-1, 227, 227))
test_generator.fit(xtest.reshape(-1, 227, 227))

to indicate that the value shall be computed automatically, but then when I run it. I get this error message:

Traceback (most recent call last):
  File "C:\Users\JoshG\PycharmProjects\LeNet\LeNet.py", line 135, in <module>
    train_generator.fit(xtrain.reshape(-1, 227, 227))
  File "C:\Users\JoshG\AppData\Local\Programs\Python\Python39\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 935, in fit
    raise ValueError('Input to `.fit()` should have rank 4. '
ValueError: Input to `.fit()` should have rank 4. Got array with shape: (704, 227, 227)
$\endgroup$

1 Answer 1

1
$\begingroup$

It is normal that it can't be reshape, because:

36276416 / (96227227*1) = 36276416 / 4946784 = 7.33333333

which is not an integer result.

Maybe there is a problem with some images' size or color formats.

Have you checked if all images are correctly sized and formatted?

$\endgroup$
3
  • $\begingroup$ So I checked the images and they are all gray-scaled instead of color. Would that make a difference? $\endgroup$
    – Josh
    Commented Jun 23, 2022 at 20:31
  • $\begingroup$ Could be: colors are usually a set of 3 features (red, green and blue), whereas gray scale has only one. You should check which is each dimension requested and which is each dimension in input. $\endgroup$ Commented Jun 23, 2022 at 21:03
  • $\begingroup$ So I updated the code, and got a different error message. The question was updated with new error $\endgroup$
    – Josh
    Commented Jun 23, 2022 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.