I have to say I am not that experienced with image processing using neural networks. You might be better off starting a new thread to ask why the network might output NaN.
An alternative would be to stick a load of print statements in the forward method in order to try and figure out at what point in the calculation the NaNs start appearing.