pytorch / pytorch/tutorials

💡 [REQUEST] - Neural Style Transfer: use AvgPool2d

Open
#3,731 0 comments 0 reactions 1 assignee View on GitHub

@sekyondaMeta is already working on this.

Since Apr 27, 2026.

core
Dominant language
Python
Stars
9.3k
Forks
4.4k
Avg merge
1d 21h
Merged PRs (30d)
4

Description

🚀 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

cc @albanD @jbschlosser

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.