Added nan_to_num check in image masking