Arsey/keras-transfer-learning-for-oxford102

finetuning multiclass model

biswagsingh opened this issue · 2 comments

Hi Arsey,

I am fine tuning a multiclass problem (class 8)
My code is below:

top_model.load_weights(top_model_weights_path)

add the model on top of the convolutional base

model.add(top_model)

set the first 25 layers (up to the last conv block)

to non-trainable (weights will not be updated)

for layer in model.layers[:25]:
layer.trainable = False

compile the model with a SGD/momentum optimizer

and a very slow learning rate.

model.compile(loss='sparse_categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])

prepare data augmentation configuration

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=32,
class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=32,
class_mode='categorical')

fine-tune the model

model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)

I get the following error:
Traceback (most recent call last):
File "prog.py", line 131, in
nb_val_samples=nb_validation_samples)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\models.py", line 935, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1553, in fit_generator
class_weight=class_weight)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1310, in train_on_batch
check_batch_axis=True)
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 1034, in _standardize_user_data
exception_prefix='model target')
File "C:\Users\bgsingh\Anaconda2\lib\site-packages\keras\engine\training.py", line 124, in standardize_input_data
str(array.shape))
ValueError: Error when checking model target: expected sequential_2 to have shape (None, 1) but got array with shape (32L, 8L)

I guess the the generator takes care of the label?

Arsey commented

I think there's something wrong in your model's input