File size: 1,507 Bytes
d0ce7c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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