# Loss "stuck" while learning affine transform

I am trying to learn a piecewise affine transform model where the input images are converted via the transform into output data. One example transformation would be (first to second) –  The input images, are however, neither simple down samples with borders, nor distortion free rectangles, which is why I need piecewise affine transform to work.
I use the following code to “learn” the transform but obviously i am going wrong somewhere as the loss is stuck from the very first epoch( i use MSE loss)

``````class Net(nn.Module):

def __init__(self):
super(Net, self).__init__()
# Spatial transformer localization-network
self.localization = nn.Sequential(
nn.Conv2d(3, 8, kernel_size=7),
nn.MaxPool2d(2, stride=2),
nn.ReLU(True),
nn.Conv2d(8, 10, kernel_size=5),
nn.MaxPool2d(2, stride=2),
nn.MaxPool2d(2, stride=2),
nn.ReLU(True)
)

# Regressor for the 3 * 2 affine matrix
self.fc_loc = nn.Sequential(
nn.Linear(10 * 30 * 30, 360),
nn.ReLU(True),
nn.Linear(360, 3 * 2)
)

# Initialize the weights/bias with identity transformation
self.fc_loc.weight.data.zero_()

# Spatial transformer network forward function
def stn(self, x):

xs = self.localization(x)
xs = xs.view(-1, 30* 10 * 30)
theta = self.fc_loc(xs)
theta = theta.view(-1, 2, 3)

grid = F.affine_grid(theta, x.size())
print(x.shape)
x = F.grid_sample(x, grid)
return x

def forward(self, x):
# transform the input
x = self.stn(x)

``````

Code is heavily borrowed for the pytorch STN example as I first wanted to try out things before stepping it up.
Any help would be highly appreciated Edit:
This is the output from the model – The loss is stuck as the output is same, no matter what the input Are there any constraints for defining the grid ? Something that maybe I can teach the model–which it would take a long time to learn by itself.

The code seems to look alright, but I’m wondering if the zero initialization is a good idea:

``````self.fc_loc.weight.data.zero_()
``````

Could you remove it and check, if your model trains better without it?

Yeah i have tried that but it doesn’t seem to work either I have shuffled with the grid_sample function and only very specific matrices seem to work on it–which led me to believe there might be any constraints to define the matrix