ValueError: Error when checking input: expected vgg16_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)
ValueError: Error when checking input: expected vgg16_input to have shape (224, 224, 3) but got array with shape (224, 224, 1)
我正在尝试创建面部表情识别模型。为此,我正在使用迁移学习。
这是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)
但在这一步中,我收到错误 ValueError:检查输入时出错:预期 vgg16_input 具有形状 (224, 224, 3) 但得到的数组具有形状 ( 224, 224, 1). 我将输入形状设置为 (224,224,3),但我仍然遇到相同的错误。之前的问题和解决方案我也都查了下,都没有用。我试了train_generator=np.array(train_generator)
,没有变化。我应该怎么做才能解决这个错误?
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)
你有 color_mode='grayscale'。这就是为什么你有 1 个通道图像。改为设置“rgb”。
我正在尝试创建面部表情识别模型。为此,我正在使用迁移学习。
这是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)
但在这一步中,我收到错误 ValueError:检查输入时出错:预期 vgg16_input 具有形状 (224, 224, 3) 但得到的数组具有形状 ( 224, 224, 1). 我将输入形状设置为 (224,224,3),但我仍然遇到相同的错误。之前的问题和解决方案我也都查了下,都没有用。我试了train_generator=np.array(train_generator)
,没有变化。我应该怎么做才能解决这个错误?
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)
你有 color_mode='grayscale'。这就是为什么你有 1 个通道图像。改为设置“rgb”。