question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AdaptiveAvgPool2D layer

See original GitHub issue

Short Description At Hugging Face we’ve seen a few PyTorch vision transformer models using AdaptiveAvgPool2D. In a lot of cases these are just resizing to (1,) or (1, 1), in which case they’re just a strange way to compute torch.mean(), but in some cases they’re actually using this layer’s full functionality.

The problem with AdaptiveAvgPool2D is that it computes the pooling windows in a unique way, and the size of the windows can be variable. This makes it impossible to implement with a standard pooling layer, and very annoying to port to TF, especially if you want to load weights from a model that was trained with the Torch layer. There is an implementation in tensorflow-addons but it uses fixed size windows, and so does not match the output of the Torch layer unless the input size is an integer multiple of the output size.

We made a reasonably performant TF version that does correctly match the Torch layer in all cases - do you need this in keras-cv? We’d be happy to add it as a PR if it’s useful.

Existing Implementations Torch layer

Other information See this StackOverflow post for a good description of how the Torch layer works internally.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Rocketknight1commented, Jul 21, 2022

Hi @innat I could, yes! If you look at the gist I linked above, the pseudo_1d_pool function is basically just a 1D AdaptivePool, so that would be very easy to implement as a separate layer. To do a 3D pool I would just do a 1D pool on each of the 3 dimensions.

0reactions
innatcommented, Jul 21, 2022

@Rocketknight1 Do you also implement 1D and 3D version of this layer?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AdaptiveAvgPool2d — PyTorch 1.13 documentation
Applies a 2D adaptive average pooling over an input signal composed of several input planes. The output is of size H x W,...
Read more >
How does adaptive pooling in pytorch work?
I would imagine using different paddings for left/right. This is not supported in pooling layers for the moment. From a practical perspective it ......
Read more >
Global Average Pooling in PyTorch using AdaptiveAvgPool
PyTorch provides a slightly more versatile module called nn.AdaptiveAvgPool2d(), which averages a grid of activations into whatever sized ...
Read more >
AdaptiveAvgPool2D-API Document-PaddlePaddle Deep ...
This operation applies 2D adaptive avg pooling on input tensor. The h and w dimensions of the output.
Read more >
tfa.layers.AdaptiveAveragePooling1D | TensorFlow Addons
tfa.layers.AdaptiveAveragePooling1D ; input, Retrieves the input tensor(s) of a layer. Only applicable if the layer has exactly one input, i.e. ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found