I am training a VGG16 for my custom image classification (with pretrained Imagenet weight) and transfer learning the last dense layer (since the output is binary classification vs 1000-class for Imagenet). Looking to train using a training set, I keep getting errors.
Here is my VGG16 code, with transfer learning.
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'])
Here is my model.fit code.
model.fit(X_train.as_matrix(),y_train.as_matrix())
training set was split using sklearn's train_test_split. Since X_train and y_train are pandas series, I turn them into ndarrays. When I do this, I get this error:
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)
According to Keras' Model doc, X has to be a list of ndarrays (the dimension of each image is 1x3x244x244), and Y has to be a numpy array of target label. So I tried this:
model.fit(X_train.tolist,y_train.as_matrix())
But now I'm getting this error:
---------------------------------------------------------------------------
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...
my y_train has labels either 0 or 1, if that helps.