Registrations Closing Soon for DevOps Certification Training by CloudxLab | Registrations Closing in

  Enroll Now

Getting model with the specified VGG19 Layers

We have defined from which layers we are going to extract the content of the image, and from which layers we are going to extract the style of the image. We shall define a function vgg_layers to do the same.

We will be getting an instance VGG19, and we will be getting the weights of these layers so that these weights act as the feature extractors and these features will be used by use as discussed previously.


  • Say vgg is an instance of tf.keras.applications.VGG19. Then, vgg.get_layers(layer_name).output returns the weights of the given layer. layer_name is the layer name in string format.

  • tf.keras.Model takes the input layer and list of other layers( regarded as output layers) as input arguments, and returns the model with these layers.

  • Define the vgg_layers function and pass the layers as input argument. In this function, we will:

    • get the instance vgg of tf.keras.applications.VGG19(include_top=False, weights='imagenet'). Remember, we have to set include_top=False, as this is not a classification problem, but we just want to use the network as a feature extractor to extract the content and the style.

    • set the trainable to False since we are not training the network but we will be using the same pre-trained.

    • use vgg.get_layer(name).output to get weights of that layer.

    • finally, we will return the model with the specified layer weights.

    All the above steps are Pythonically implemented in the below function. Use the below code to do the same.

    def vgg_layers(layer_names):
        vgg = tf.keras.applications.VGG19(include_top=False, weights='imagenet')
        vgg.trainable = False
        outputs = [vgg.get_layer(name).output for name in layer_names]
        model = tf.keras.Model([vgg.input], outputs)
        return model
  • Now, call the vgg_layer function and pass the style_layers as the input argument to the function to get the style extractor model.

    style_extractor = << your code comes here >>(style_layers)
  • Now, pass the style_image*255 to this style_extractor. This returns the layer-wise names and outputs.

    style_outputs = style_outputs = style_extractor(style_image*255)
  • Let us look at the statistics of each layer's output:

    for name, output in zip(style_layers, style_outputs):
        print("  shape: ", output.numpy().shape)
        print("  min: ", output.numpy().min())
        print("  max: ", output.numpy().max())
        print("  mean: ", output.numpy().mean())

No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...