Python 3.x Keras Model.fit X在列表中与ndarray错误
我正在为我的自定义图像分类(使用预训练的Imagenet权重)培训VGG16,并转移学习最后一个密集层(因为输出是二进制分类,而Imagenet为1000类)。希望使用训练集进行训练,我总是会出错 这是我的VGG16代码,带有迁移学习Python 3.x Keras Model.fit X在列表中与ndarray错误,python-3.x,keras,deep-learning,vgg-net,Python 3.x,Keras,Deep Learning,Vgg Net,我正在为我的自定义图像分类(使用预训练的Imagenet权重)培训VGG16,并转移学习最后一个密集层(因为输出是二进制分类,而Imagenet为1000类)。希望使用训练集进行训练,我总是会出错 这是我的VGG16代码,带有迁移学习 来自keras.models导入模型 #请注意,我们排除了最后的密集层,并在下面添加一层,我们将自己重新训练它 base=VGG16(weights='imagenet',include_top=False,input_shape=(3224224)) #冻结卷积
来自keras.models导入模型
#请注意,我们排除了最后的密集层,并在下面添加一层,我们将自己重新训练它
base=VGG16(weights='imagenet',include_top=False,input_shape=(3224224))
#冻结卷积层
对于base.layers中的图层:
layer.trainable=错误
x=基本输出
x=展平()(x)#从卷积张量输出展平
预测=密集(2,激活='softmax')(x)#应与预测的类匹配#
#这是我们将要训练的模型
模型=模型(输入=基本输入,输出=预测)
编译(优化器=SGD(lr=0.0001,动量=0.9),
损失=‘分类的’
这是我的model.fit代码
model.fit(X_-train.as_-matrix(),y_-train.as_-matrix())
使用sklearn的训练测试分割训练集。由于X_-train和y_-train是熊猫系列,我把它们变成了Ndarray。当我执行此操作时,会出现以下错误:
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)
---------------------------------------------------------------------------
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...
但现在我得到了这个错误:
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)
---------------------------------------------------------------------------
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...
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1个模型装配(列表(X\U系列、y\U系列)
~/anaconda3/envs/tensorflow\u p36/lib/python3.6/site-packages/keras/engine/training.py适合(self、x、y、批量大小、历元、冗余、回调、验证分割、验证数据、无序排列、类权重、样本权重、初始历元、每历元的步骤、验证步骤、**kwargs)
950样品重量=样品重量,
951类重量=类重量,
-->952批次大小=批次大小)
953#准备验证数据。
954 do_验证=错误
~/anaconda3/envs/tensorflow\u p36/lib/python3.6/site-packages/keras/engine/training.py in\u-standard\u-user\u数据(自身、x、y、样本重量、类别重量、检查数组长度、批次大小)
749馈送输入形状,
750检查批处理轴=False,#不强制执行批处理大小。
-->751异常(前缀为“输入”)
752
753如果y不是无:
标准化输入数据中的~/anaconda3/envs/tensorflow\u p36/lib/python3.6/site-packages/keras/engine/training\u utils.py(数据、名称、形状、检查批处理轴、异常前缀)
100'预计将看到'+str(len(name))+'数组,'
101'但却得到了以下列表'+
-->102 str(len(data))+'数组:'+str(data)[:200]+'…')
103 elif len(名称)>1:
104升值误差(
ValueError:检查模型输入时出错:您传递给模型的Numpy数组列表的大小不是模型预期的大小。预期会看到1个数组,但得到以下4200个数组的列表:[array([[[149.061,151.061,150.061,…,151.061,
150.061 , 151.061 ],
[ 150.061 , 123.061 , 90.061 , ..., 77.061 ,
100.061...
如果有帮助的话,我的y_列车的标签是0或1