Fix bugs in image pre-processing and foreground training