total variation loss it was observed that optimization to reduce only the style and content losses led to highly pixelated and noisy outputs to cover the sametotal variation loss was introduced the total variation loss is analogous to regularization loss this is introduced for ensuring spatial continuity and smoothness in the generated image to avoid noisy and overly pixelated results the same is defined in the function as follows def total_variation_loss( ) squarex[::img_height :img_width : [: ::img_width :] squarex[::img_height :img_width : [::img_height ::]return sum( pow( )overall loss function having defined the components of the overall loss function for neural style transferthe next step is to piece together these building blocks since content and style information is captured by the cnns at different depths in the networkwe need to apply and calculate loss at appropriate layers for each type of loss utilizing insights and research by gatys et al and johnson et al in their respective paperswe define the following utility to identify the content and style layers from the vgg- model even though johnson et al leverages the vgg- model for faster and better performancewe constrain ourselves to the vgg- model for ease of understanding and consistency across runs define function to set layers based on source paper followed def set_cnn_layers(source='gatys')if source ='gatys'config from gatys et al content_layer 'block _conv style_layers ['block _conv ''block _conv ''block _conv ''block _conv ''block _conv 'elif source ='johnson'config from johnson et al content_layer 'block _conv style_layers ['block _conv ''block _conv ''block _conv ''block _conv ''block _conv 'elseuse gatys config as the default anyway content_layer 'block _conv style_layers ['block _conv ''block _conv ''block _conv ''block _conv ''block _conv 'return content_layerstyle_layers the following snippet then applies the overall loss function based on the layers selected from the set_cnn_layers(function for content and style in [ ]weights for the weighted average loss function content_weight style_weight total_variation_weight -