How to take maxpool over a specific image area


(Malcolm McLean) #1

Hello. I am doing some ML experiments and would like to understand how to implement a particular architecture.

The existing network outputs a full image segmentation map in three categories. I want to append a layer that takes a single spatial maxpool over just the central area of the images. The output would therefore be three numbers, the maximum activation for each category that appears anywhere in the specified area of the image.

What is the best way, or any way, to implement? I do hope that this question makes sense.

Thanks for your help!


(Irhum Shafkat) #2

You could pass the image through the network, index out the appropriate region, and then run a maxpool on the indexed tensor. In pure PyTorch, it’d look roughly like:

# assuming an output size of 224x224, and a center crop of size 100   

pool = nn.MaxPool2D(100)

x = segmentation_model(x)
x = x[:, :, 62:162, 62:162]
x = pool(x).view(-1, num_channels)

It is a rather curious way of implementing what appears to be a classification model, might I ask, why this approach?

Also, the fastai users category is intended for users who need help with the fastai library, more general purpose deep learning questions should be asked in the deep learning category.