Have you seen this tutorial on PyTorch website for STN: https://pytorch.org/tutorials/intermediate/spatial_transformer_tutorial.html 29? There is an example showing how to implement STN for MNIST images.