How to use MaxUnpool2D after AdaptiveAvgPool2D?

Is there a way to get indices from AdaptiveAvgPool2D output so I can use it for the MaxUnpool2D? I looked at the docs and I don’t think it says how to do this.

Manually calculating finding them right after the AdaptiveAvgPool2d seems inefficient.

I’m unsure what indices AdaptiveAvgPool2d should return, so could you explain your use case a bit?
Maxpooling payers are selecting the max. value in the kernel/window using an index and can thus return these indices so that they can be reused in e.g. MaxUnpool2d layers.
However, Avgpool layers are calculating the average in each window, so there is no “selection” involving indices.

Maybe set return_indices=True

Vgg16 has AdaptiveAvgPool2d as its final layer. I’am using vgg’s initial layers to extract features, After which, I want to MaxUnpool2d the output.

nn.MaxUnpool2d expects to get indices, which are created by e.g. nn.MaxPool2d.
As described before, neither nn.AdaptiveAvgPool2d nor nn.AvgPool2d return indices and setting return_indices=True in these layers will return a TypeError:

nn.AdaptiveAvgPool2d(2, return_indices=True)
> TypeError: __init__() got an unexpected keyword argument 'return_indices'
nn.AvgPool2d(2, return_indices=True)
> TypeError: __init__() got an unexpected keyword argument 'return_indices'

shall I construct my own indices tensor then? Or Is there a way to just call some pytorch function to make that?

You would have to define what these indices should represent.
In max pooling layers the indices are representing the indices of the max. values, which were “selected”.
In avg pooling layers no indexing/selection is done, since the values covered by the filter kernel are averaged, so all values are used.

Oops, meant to reply to this one.

Anyways, I want the indices to represent the max values in a 2x2 window with stride = 2. Any way to make the indices quickly?

Yes, you can use nn.MaxPool2d with a kernel_size of 2 and using return_indices=True.

Yeah, but I don’t actually want to maxpool then unpool the feature map, I just want to unpool it