Я тренирую VGG16 для своей пользовательской классификации изображений (с предварительно подготовленным весом Imagenet) и переношу обучение на последний плотный слой (поскольку на выходе получается двоичная классификация по сравнению с 1000-классом для Imagenet).Глядя на тренировку, используя тренировочный набор, я продолжаю получать ошибки.
Вот мой код 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's train_test_split.Поскольку X_train и y_train - серии панд, я превращаю их в 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)
Согласно Модель документа Кераса , X должен быть список ndarrays (размер каждого изображения - 1x3x244x244), а Y должен быть массивом целевых меток .Итак, я попробовал это:
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, если это помогает.