python-3.x - 列表中的 Keras Model.fit X 与 ndarray 错误
问题描述
我正在为我的自定义图像分类(使用预训练的 Imagenet 权重)训练 VGG16,并迁移学习最后一个密集层(因为输出是二进制分类与 Imagenet 的 1000 类)。想要使用训练集进行训练,我不断收到错误。
这是我的 VGG16 代码,带有迁移学习。
from keras.models import Model
# note we exclude the final dense layers and add one back below, we would retrain it ourselves
base = VGG16(weights='imagenet', include_top=False, input_shape=(3,224,224))
# Freeze convolutional layers
for layer in base.layers:
layer.trainable = False
x = base.output
x = Flatten()(x) # flatten from convolution tensor output
predictions = Dense(2, activation='softmax')(x) # should match # of classes predicted
# this is the model we will train
model = Model(inputs=base.input, outputs=predictions)
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy', metrics=['accuracy'])
这是我的 model.fit 代码。
model.fit(X_train.as_matrix(),y_train.as_matrix())
使用 sklearn 的 train_test_split 拆分训练集。由于 X_train 和 y_train 是 pandas 系列,我把它们变成了 ndarrays。当我这样做时,我得到这个错误:
ValueError Traceback (most recent call last)
<ipython-input-74-6a40f048f921> in <module>()
----> 1 model.fit(X_train.as_matrix(),y_train.as_matrix())
~/anaconda3/envs/tensorflow_p36/lib/python3.6/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, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
126 ': expected ' + names[i] + ' to have ' +
127 str(len(shape)) + ' dimensions, but got array '
--> 128 'with shape ' + str(data_shape))
129 if not check_batch_axis:
130 data_shape = data_shape[1:]
ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (4200, 1)
根据Keras 的模型文档,X 必须是 ndarrays 的列表(每个图像的维度是 1x3x244x244),并且 Y 必须是目标标签的 numpy 数组。所以我尝试了这个:
model.fit(X_train.tolist,y_train.as_matrix())
但现在我收到了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-113-783987041ff8> in <module>()
----> 1 model.fit(list(X_train),y_train)
~/anaconda3/envs/tensorflow_p36/lib/python3.6/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, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
~/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
100 'Expected to see ' + str(len(names)) + ' array(s), '
101 'but instead got the following list of ' +
--> 102 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
103 elif len(names) > 1:
104 raise ValueError(
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 4200 arrays: [array([[[[ 149.061 , 151.061 , 150.061 , ..., 151.061 ,
150.061 , 151.061 ],
[ 150.061 , 123.061 , 90.061 , ..., 77.061 ,
100.061...
我的 y_train 有标签 0 或 1,如果有帮助的话。
解决方案
推荐阅读
- c# - 匹配给定文本字符串中“a”链接的 atlassian wiki 标记
- java - 从表中读取大量数据(java.lang.OutOfMemoryError :)
- laravel-8 - Laravel 嵌套护照范围路由,没有达到嵌套路由方法
- android - 如何格式化来自服务器的传入时间戳字符串?
- python - 用于计算机视觉的 ORB 数据:一行中的项目是什么?
- c++ - 围绕局部轴的 OpenGL 旋转未按设计工作
- discord.py - 如何使用单个命令一次禁用所有命令
- python - 如何从 SQLite 数据库中获取 2 个条目的值,如果它们正确,则插入它们输入的时间/日期?
- python - Python - 从嵌套字典中提取特定项目
- arrays - Swift - 我如何遍历这个数组并打印每个值而没有下标错误