hh tt
hh tt

Reputation: 405

Transfer learning by fine tuning with weight of pretrained model in keras

I'm trying to fine tune with weights of my pretrained model. I already fine tune with available VGG network that trained on VGGface dataset, but I want to fine tune with my pretrained model on my specific face dataset. My code that fine tuning with VGG net is as the following:

img_width, img_height = 256, 256
train_data_dir = "data/train"
validation_data_dir = "data/val"
nb_train_samples = 4125
nb_validation_samples = 466
batch_size = 16
epochs = 10

model = applications.VGG19(weights = "imagenet", include_top=False, input_shape = (img_width, img_height, 3))
# Freeze the layers which you don't want to train. Here I am freezing the first 5 layers.
for layer in model.layers[:5]:
    layer.trainable = False

#Adding custom Layers
x = model.output
x = Flatten()(x)
x = Dense(1024, activation="relu")(x)
x = Dropout(0.5)(x)
x = Dense(1024, activation="relu")(x)
predictions = Dense(5, activation="softmax")(x)



# creating the final model
model_final = Model(input = model.input, output = predictions)

# compile the model
model_final.compile(loss = "categorical_crossentropy", optimizer = optimizers.SGD(lr=0.0001, momentum=0.9), metrics=["accuracy"])



# Initiate the train and test generators with data Augumentation
train_datagen = ImageDataGenerator(
rescale = 1./255,
horizontal_flip = True,
fill_mode = "nearest",
zoom_range = 0.3,
width_shift_range = 0.3,
height_shift_range=0.3,
rotation_range=30)



test_datagen = ImageDataGenerator(rescale = 1./255, horizontal_flip = True, fill_mode = "nearest",
                zoom_range = 0.3, width_shift_range = 0.3, height_shift_range=0.3, rotation_range=30)

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

validation_generator = test_datagen.flow_from_directory(validation_data_dir,
target_size = (img_height, img_width), class_mode = "categorical")

# Save the model according to the conditions
checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto')

# Train the model
H1 = model_final.fit_generator(
train_generator,
samples_per_epoch = nb_train_samples,
epochs = epochs,
validation_data = validation_generator,
nb_val_samples = nb_validation_samples,
callbacks = [checkpoint, early])

How can I fine tuning with my specific model weight? can anyone please help me. Thanks..

Upvotes: 0

Views: 1005

Answers (1)

Vigneswaran C
Vigneswaran C

Reputation: 501

Save the specific model you pre-trained and create the fine-tuning model with your desired hyper-parameters. Then load the saved weights using model.load_weights("<saved_model_file>.h5")

Upvotes: 0

Related Questions