'ValueError: Error when checking input: expected vgg16_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)
I'm trying to create a model for facial expression recognition. For this, I'm using transfer learning.
Here is the train_generator:
IMG_HEIGHT=224
IMG_WIDTH = 224
batch_size=32
train_generator = train_datagen.flow_from_directory(
train_data_dir,
color_mode='grayscale',
target_size=(IMG_HEIGHT, IMG_WIDTH),
batch_size=batch_size,
class_mode='categorical',
shuffle=True)
But in this step, I'm getting the error ValueError: Error when checking input: expected vgg16_input to have shape (224, 224, 3) but got array with shape (224, 224, 1). I set the input shapes as (224,224,3), but I'm still getting the same error. I also checked the previous questions and solutions, but they didn't work. I tried train_generator=np.array(train_generator), and there is no change. What should I do to solve this error?
epochs=50
#train_generator=np.array(train_generator)
history=model.fit(train_generator,
steps_per_epoch=num_train_imgs//batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=num_test_imgs//batch_size)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-25-abee8cf0777d> in <module>
1 epochs=50
2 #train_generator=np.array(train_generator)
----> 3 history=model.fit(train_generator,
4 steps_per_epoch=num_train_imgs//batch_size,
5 epochs=epochs,
~\anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1131 training_utils.check_generator_arguments(
1132 y, sample_weight, validation_split=validation_split)
-> 1133 return self.fit_generator(
1134 x,
1135 steps_per_epoch=steps_per_epoch,
~\anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\anaconda3\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1716 ```
1717 """
-> 1718 return training_generator.fit_generator(
1719 self, generator,
1720 steps_per_epoch=steps_per_epoch,
~\anaconda3\lib\site-packages\keras\engine\training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
215 callbacks.on_batch_begin(batch_index, batch_logs)
216
--> 217 outs = model.train_on_batch(x, y,
218 sample_weight=sample_weight,
219 class_weight=class_weight,
~\anaconda3\lib\site-packages\keras\engine\training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1503 the display labels for the scalar outputs.
1504 """
-> 1505 x, y, sample_weights = self._standardize_user_data(
1506 x, y,
1507 sample_weight=sample_weight,
~\anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
572
573 # Standardize the inputs.
--> 574 x = training_utils.standardize_input_data(
575 x,
576 feed_input_names,
~\anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
139 for dim, ref_dim in zip(data_shape, shape):
140 if ref_dim != dim and ref_dim:
--> 141 raise ValueError(
142 'Error when checking ' + exception_prefix +
143 ': expected ' + names[i] + ' to have shape ' +
ValueError: Error when checking input: expected vgg16_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)
Solution 1:[1]
You have color_mode='grayscale'. That is why you have 1 channel image. Set "rgb" instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | teplandr |
