Variable output shape for adaptivemaxpooling

Hi I would like to create a network, where the last layer will be an adaptive max-pooling layer, and the output shape will vary on the input size of the network. Now in my dataset, the input sizes are not of equal size. Here is my network code:

class Net(nn.Module):

def __init__(self, blocks, block, in_channel, out_channel):

    super(Net, self).__init__()

    layer = []

    layer.append(block(in_channel,out_channel))

    for i in range(0,blocks):

      layer.append(block(out_channel,out_channel))

    self.network = nn.Sequential(*layer)

    self.maxpoolbyrow = nn.AdaptiveMaxPool2d(output_size=(50,50))

def forward(self, x):

    out = self.network(x)

    out = self.maxpoolbyrow(out)

    return out.size()

Now here in the output_size argument inside nn.AdaptiveMaxPool2d, I want it to be (L x 50) instead of a fixed (50x50), where the L is the Height of the input image. Considering the image sizes are variable, how can I extract the height of the input in the forward function and add it as the output_size parameter in AdaptiveMaxPool2d?

You could get the shape of the input via: x.size() or x.shape and use the functional API via out = F.adaptive_max_pool2d(out, output_size=...).

1 Like