I have trained a semantic segmentation classifier (UNet-variant) and now need to apply the classifier to a to a large stack of climate data. Given the dimensions of the climate data over my study area are much larger than than the classifier input size.
I know I need to break the larger image into smaller chunks and feed them through the classifier. I also know that including some overlap during inference is generally good practice to reduce edge effects in the predictions.
Has someone produced a script that illustrates an efficient way of doing this?
I would try to use nn.Unfold to create the smaller patches and nn.Fold to recreate the bigger output.
Note that nn.Fold will accumulate the values in the overlapping regions, so you might want to use a manual approach with permutations and some view operations.
Thanks for the suggestion! Could you please help me fill out an example of how to use unfold and fold in conjunction.
For simplicity sake, let’s say we have an 3x2048x2048 image (my actual images are a lot larger) and a semantic segmentation model that has an input resolution of 256x256.
The following code for example doesn’t produce a tensor of 256x256 windows like I’d expect…which I assume means I am using the unfold function incorrectly:
import torch
import torch.nn.functional as f
# Load in a big image
big_image = torch.rand(1,3,2048,2048)
# Unfold the image into 3 x 256 x 256
windows = f.unfold(big_image, kernel_size=256)
print(windows.shape)