If you want to use a constant size, you could create the nn.Upsample module in the __init__ method of your model and directly use it for all images:
up = nn.Upsample(size=(24, 24))

x = torch.randn(1, 3, 10, 10)
print(up(x).shape)
> torch.Size([1, 3, 24, 24])

x = torch.randn(1, 3, 40, 40)
print(up(x).shape)
> torch.Size([1, 3, 24, 24])

On the other hand, if you want to use different upsampling layers depending on the current input shape, you could use a condition and either create the nn.Upsample layer in the forward (as it doesn’t have any parameters, this would be fine), or use the functional API via e.g. F.interpolate.
You could also create a list or dict of various nn.Upsample modules in the __init__ and select the appropriate one in the forward method.