Skip to content

💡 [REQUEST] - Neural Style Transfer: use AvgPool2d #3731

@tutubalin

Description

@tutubalin

🚀 Describe the improvement or the new tutorial

Neural Style Transfer utilizes VGG network, which uses maximum pooling.

Although Original article suggests using average pooling instead for better results:

For image synthesis we found that replacing the maximum
pooling operation by average pooling yields slightly more
appealing results, which is why the images shown were generated with average pooling.

This is unsurprising, considering that max pooling may hide gradients of lower layers, while average pooling preserves them, so gradient descent for reconstructing image works better.

I confirm that it actually improves results.

Suggested changes

Replace that:

elif isinstance(layer, nn.MaxPool2d):
    name = 'pool_{}'.format(i)

with that:

elif isinstance(layer, nn.MaxPool2d):
    name = 'pool_{}'.format(i)
    layer = nn.AvgPool2d(
        kernel_size=layer.kernel_size, 
        stride=layer.stride, 
        padding=layer.padding, 
        ceil_mode=layer.ceil_mode)

Existing tutorials on this topic

https://docs.pytorch.org/tutorials/advanced/neural_style_tutorial.html

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions