from tensorflow.keras.models import Model,Sequential from tensorflow.keras.layers import Dense, Flatten, Dropout from tensorflow.keras.applications import VGG16 from tensorflow.keras.regularizers import L2 def get_pretrained_dog_emotion_classifier(weights_path = "dogs_emotion_model_weights.h5"): # images input shape MODEL_INPUT_SHAPE = (224,224,3) # instance the pretrained convolutional blocks pretrained_vgg_model = VGG16(include_top = False, input_shape = MODEL_INPUT_SHAPE) # freeze the first four pretrained layer target_freeze_blocks = ["block1","block2","block3","block4"] for layer in pretrained_vgg_model.layers: if layer.name.split("_")[0] in target_freeze_blocks: layer.trainable = False # create the model's classifier classifier = Sequential( [ Flatten(), Dense(32,activation = "relu",name = "classifier_dense1"), Dropout(0.2,name = "classifier_dropout1"), Dense(64,activation = "relu",kernel_regularizer = L2(),name = "classifier_dense2"), Dense(64,activation = "relu",name = "classifier_dense3"), Dropout(0.2,name = "classifier_dropout2"), Dense(1,activation = "sigmoid",name = "classifier_dense4") ],name = "classifier" ) # connect the two models output = classifier(pretrained_vgg_model.layers[-1].output) model = Model(inputs = pretrained_vgg_model.layers[0].input,outputs = output) # load the model_weights model.load_weights(weights_path) return model