Thanksgiving Sale: 15% Off on All Courses | Use Coupon GO15 During Checkout | Offer Expires In

  Enroll Now

Loading Pre-trained VGG19

Let us load VGG19 previously trained to classify Imaagenet data. Let us test run it on our image to ensure it's used correctly.

Note:

  • tf.keras.applications are canned architectures with pre-trained weights.

  • tf.keras.applications.VGG19 is VGG19 model for Keras.

  • tf.keras.applications.vgg19.preprocess_input returns the images converted from RGB to BGR, then each color channel is zero-centered with respect to the ImageNet dataset, without scaling.

  • tf.image.resize resizes image to size using the specified method.

  • tf.keras.applications.vgg19.decode_predictions decodes the prediction of an ImageNet model.

  • vgg.layers returns the list of all the layers in the vgg model.

INSTRUCTIONS
  • Get the preprocessed form of the content_image using tf.keras.applications.vgg19.preprocess_input.

    x = << your code comes here >>(content_image*255)
    
  • We will have to set include_top=True since we want to cross-check if the network is able to correctly predict our content_image. When setting include_top=True and loading imagenet weights, input_shape should be (224, 224, 3). So, let us resize it using tf.image.resize.

    x = tf.image.resize(x, (224, 224))
    
  • Now, let us instantiate the VGG19 model as follows, using tf.keras.applications.VGG19:

    vgg = << your code comes here >>(include_top=True, weights='imagenet')
    
  • Pass x, that is the preprocessed and resized content image, to the vgg and get the prediction_probabilities. We expect the shape of this prediction_probabilities to be of 1000 dimensions, as VGG19 on the Imagenet database is trained to classify 1000 classes.

    prediction_probabilities = vgg(<< your code comes here >>)
    print(prediction_probabilities.shape)
    
  • Let us print the top 5 predicted classes of our content image using tf.keras.applications.vgg19.decode_predictions.

    predicted_top_5 = tf.keras.applications.vgg19.decode_predictions( prediction_probabilities.numpy() )[0]
    
    print([(class_name, prob) for (number, class_name, prob) in predicted_top_5])
    

Observe that the classes predicted are all different breeds of dogs (you could google these classes to cross-check though :-D ).

This assures that the network is able to recognize that it is an image of a dog! Voila, the network is already powerful enough to recognize the main features of the given image! So we can assuredly go forward to use this to extract the features (content from content image and style from the style image) of the input image!

  • Now, let us load the VGG19 network without the classification head (by setting include_top to False) , just to see the list of all the layer names.

    vgg = tf.keras.applications.VGG19(include_top=<< your code comes here >>, weights='imagenet')
    
    for layer in vgg.layers:
        print(layer.name)
    
  • Now, let us choose intermediate layers from the network to represent the style and content of the image:

    content_layers = ['block5_conv2'] 
    
    style_layers = ['block1_conv1',
            'block2_conv1',
            'block3_conv1', 
            'block4_conv1', 
            'block5_conv1']
    

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...