|
|
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"): |
|
|
|
|
|
MODEL_INPUT_SHAPE = (224,224,3) |
|
|
|
|
|
|
|
|
pretrained_vgg_model = VGG16(include_top = False, input_shape = MODEL_INPUT_SHAPE) |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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" |
|
|
) |
|
|
|
|
|
|
|
|
output = classifier(pretrained_vgg_model.layers[-1].output) |
|
|
model = Model(inputs = pretrained_vgg_model.layers[0].input,outputs = output) |
|
|
|
|
|
|
|
|
model.load_weights(weights_path) |
|
|
|
|
|
return model |
|
|
|