Defining the model

Our application will need to classify 37 classes of dogs and cats. The VGG16 Model supports 1,000 different classes. In our application, we will reuse all layers up to the fc7 layer and train the last layer from scratch. In order to make the model output 37 classes, we need to modify the inference method in nets.py as follows:

    def inference(images, is_training=False): 
    # 
    # All the code before fc7 are not modified. 
    # 
    fc7 = _fully_connected(fc6, 4096, name="fc7") 
    if is_training: 
        fc7 = tf.nn.dropout(fc7, keep_prob=0.5) 
    fc8 = _fully_connected(fc7, 37, name='fc8-pets', relu=False) 
    return fc8
  • We add a new parameter, is_training, to the method. After the fc7 layer, we add a tf.nn.dropout layer if the inference is training. This dropout layer can help the model regularize better with unseen data and avoid overfitting.
  • The number of outputs in the fc8 layer is changed from 1,000 to 37. Besides, the name of the fc8 layer must be changed to another name; in this case, we choose fc8-pets. If we don't change the name of the fc8 layer, load_caffe_weights will still find the new layers and assign the original weights, which is not the same size as our new fc8 layer.
  • The softmax layer at the end of the inference method is also removed because the loss function that we will use later only needs unnormalized outputs.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10