Extract denoising logic, for training and test respectively.