This only works if your flow is uniform. If you use - flow with the forward flow applied on image1 in the grid sampler you will move the wrong pixels. It’s easier to see it in 1D. If you have [0, 0, 1, 0, 0] and a flow of [0, 0, +1, 0, 0] then the warp image is [0, 0, ?? , 1, 0] where ?? being occlusion.
But what the sampler does is to sample from a source given the coordinates in a grid. Your grid with -flow would be:
[0, 1, 2-1 = 1, 3, 4]
After sampling you get:
[0, 0, 0, 0, 0]
because it moved pixel at index 1 instead of pixel at index 2.
That is why the code above is suggesting to use the backward flow instead. In our case that flow would be [0, 0, 0, -1, 0] and then with grid [0, 1, 2, 3-1 = 2, 0]. When using grid sampler from im1 with that grid it will yield:
[0, 0, 1, 1, 0, ]
Which is what we should have without an occlusion mask which should be at index 2.
It sometimes works when you have some general movement in the frame but I prefer to use backward flow instead.