DisentangledColorization icon indicating copy to clipboard operation
DisentangledColorization copied to clipboard

Training issue: nan occurs after totalLoss_idx.backward()

Open yyang181 opened this issue 3 years ago • 7 comments

Hi, I am trying to finetune DISCO model on other color image datasets. The training settings are kept as default as on ./scripts/anchorcolorprob_hint2class-enhanced-h8.sh. After several loops, "nan" value occurs and the training losses become nan too. I checked the intermediate variables. The "nan" starts after the execution of "totalLoss_idx.backward()". Would you please give me some suggestion? This error usually caused by gradient explosion or gradient vanishing. I tried gradient clipping using torch.nn.utils.clip_grad_norm(model.parameters(), args.clip), and didn't work. Looking forward to your reply.

Best.

yyang181 avatar Feb 22 '23 12:02 yyang181

It is possible to cause NAN loss because the forward process involves doing k-means clustering over the GT images. So, you need to ensure there is no broken or constant-color images in your dataset. In addition, cosine learning rate scheduler sometimes may help you avoid NAN if training with resumed checkpoints.

MenghanXia avatar Feb 23 '23 01:02 MenghanXia

@MenghanXia Thanks for your reply.

Following your first suggestion, I checked the entire training dataset. I don't think there is broken or constant-color images. And the NAN happens after several epochs instead of within the first epoch, which indicates that the dataset should be ok.

In addition, I also tried the cosine learning rate scheduler you mentioned above. This way doesn't work either.

Besides, I debugged the training scripts line by line, NAN occurs as the followings:

  1. before totalLoss_idx.backward(): model.parameters : clear model.parameters.grad : clear
  2. after totalLoss_idx.backward(): model.parameters : clear model.parameters.grad : NAN occurs
  3. after totalLoss_idx.backward(): pred_feats = self.repnet(input_grays) on L105 in models/model.py input_grays: clear pred_feats: NAN occurs

My training logs looks like this: 2023-02-23 16:48:50,620 - INFO: @2023-02-23 16:48:50 (effective batch size = 16) 2023-02-23 16:48:50,620 - INFO: ***** AnchorColorProb_hint2class-enhanced-h8_yyx_20230221_test [GPU_Num:1 | DDP:True | Resume:False | Pretrain:True] ***** 2023-02-23 16:48:50,621 - INFO: >> loaded 172 images from imagenet [train]. 2023-02-23 16:48:50,622 - INFO: >> loaded 172 images from imagenet [val]. 2023-02-23 16:48:50,622 - INFO: >> dataset (10 iters) was created 2023-02-23 16:49:05,196 - INFO: >> [1/600] iter:1 totalLoss:21.6313 palLoss:5.8840 refLoss:5.8835 recLoss:9.8638 [io/proc:82.305%] 2023-02-23 16:49:06,084 - INFO: >> [1/600] iter:2 totalLoss:17.2338 palLoss:3.4953 refLoss:5.0917 recLoss:8.6467 [io/proc:0.026%] 2023-02-23 16:49:06,943 - INFO: >> [1/600] iter:3 totalLoss:14.8359 palLoss:2.1108 refLoss:4.6885 recLoss:8.0366 [io/proc:0.022%] 2023-02-23 16:49:07,762 - INFO: >> [1/600] iter:4 totalLoss:9.9142 palLoss:1.5792 refLoss:2.6450 recLoss:5.6900 [io/proc:0.019%] 2023-02-23 16:49:08,601 - INFO: >> [1/600] iter:5 totalLoss:8.3655 palLoss:1.4391 refLoss:1.7655 recLoss:5.1609 [io/proc:0.034%] 2023-02-23 16:49:09,409 - INFO: >> [1/600] iter:6 totalLoss:8.3754 palLoss:1.3937 refLoss:1.4657 recLoss:5.5160 [io/proc:0.034%] 2023-02-23 16:49:10,233 - INFO: >> [1/600] iter:7 totalLoss:8.2447 palLoss:1.4692 refLoss:1.4688 recLoss:5.3068 [io/proc:0.022%] 2023-02-23 16:49:11,047 - INFO: >> [1/600] iter:8 totalLoss:8.1828 palLoss:1.5010 refLoss:1.4460 recLoss:5.2358 [io/proc:0.015%] 2023-02-23 16:49:11,883 - INFO: >> [1/600] iter:9 totalLoss:8.3967 palLoss:1.6025 refLoss:1.5470 recLoss:5.2472 [io/proc:0.014%] 2023-02-23 16:49:12,722 - INFO: >> [1/600] iter:10 totalLoss:8.1378 palLoss:1.6194 refLoss:1.5852 recLoss:4.9332 [io/proc:0.032%] 2023-02-23 16:49:13,659 - INFO: --- epoch: 1/600 | train: 11.3318 | duration: 15.83 --- 2023-02-23 16:49:31,724 - INFO: --- epoch: 1/600 | val : 8.2834 | duration: 18.06 --- 2023-02-23 16:49:31,725 - INFO: >> saving checkpoint ... 2023-02-23 16:49:38,094 - INFO: >> [2/600] iter:1 totalLoss:8.1635 palLoss:1.5041 refLoss:1.5224 recLoss:5.1370 [io/proc:372.321%] 2023-02-23 16:49:38,917 - INFO: >> [2/600] iter:2 totalLoss:8.1152 palLoss:1.4448 refLoss:1.4854 recLoss:5.1850 [io/proc:0.028%] 2023-02-23 16:49:39,709 - INFO: >> [2/600] iter:3 totalLoss:7.9107 palLoss:1.3967 refLoss:1.4502 recLoss:5.0638 [io/proc:0.019%] 2023-02-23 16:49:40,494 - INFO: >> [2/600] iter:4 totalLoss:7.9243 palLoss:1.3570 refLoss:1.3991 recLoss:5.1682 [io/proc:0.027%] 2023-02-23 16:49:41,258 - INFO: >> [2/600] iter:5 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.019%] 2023-02-23 16:49:42,019 - INFO: >> [2/600] iter:6 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.015%] 2023-02-23 16:49:42,810 - INFO: >> [2/600] iter:7 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.020%] 2023-02-23 16:49:43,620 - INFO: >> [2/600] iter:8 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.021%] 2023-02-23 16:49:44,409 - INFO: >> [2/600] iter:9 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.024%] 2023-02-23 16:49:45,221 - INFO: >> [2/600] iter:10 totalLoss: nan palLoss: nan refLoss: nan recLoss: nan [io/proc:0.020%] 2023-02-23 16:49:46,219 - WARNING: NaN or Inf found in input tensor. 2023-02-23 16:49:46,219 - INFO: --- epoch: 2/600 | train: nan | duration: 12.42 --- 2023-02-23 16:49:46,219 - WARNING: NaN or Inf found in input tensor. 2023-02-23 16:49:46,224 - WARNING: NaN or Inf found in input tensor. 2023-02-23 16:49:46,224 - WARNING: NaN or Inf found in input tensor. 2023-02-23 16:50:01,625 - INFO: --- epoch: 2/600 | val : nan | duration: 15.40 --- 2023-02-23 16:50:01,626 - WARNING: NaN or Inf found in input tensor.

It is possible to cause NAN loss because the forward process involves doing k-means clustering over the GT images. So, you need to ensure there is no broken or constant-color images in your dataset. In addition, cosine learning rate scheduler sometimes may help you avoid NAN if training with resumed checkpoints.

yyang181 avatar Feb 23 '23 10:02 yyang181

(1) Let's do an ablation study. To verify whether it is because of your dataset, you can try to resume the training over a normal color dataset, such as VOC2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/) or a subset of it. If no NAN occurs, it is probably means you have to think over your dataset. (2) There are three terms in the loss function. You are suggested to check which one causes NAN at the very beginning, and then track back to the tensors that are used to compute it. Once you find the tensor with NAN value, you will have an idea about why it happen.

MenghanXia avatar Feb 23 '23 13:02 MenghanXia

Will do, thanks a lot.

yyang181 avatar Feb 23 '23 13:02 yyang181

(1) Let's do an ablation study. To verify whether it is because of your dataset, you can try to resume the training over a normal color dataset, such as VOC2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/) or a subset of it. If no NAN occurs, it is probably means you have to think over your dataset. (2) There are three terms in the loss function. You are suggested to check which one causes NAN at the very beginning, and then track back to the tensors that are used to compute it. Once you find the tensor with NAN value, you will have an idea about why it happen.

The Lab to RGB convertion might be the one to blame. I use with torch.autograd.detect_anomaly() to check where the NAN value first occurs and find that rgb = (1.055*(rgb**(1./2.4)) - 0.055)mask + 12.92rgb*(1-mask) within method(xyz2rgb) in ./model/basic.py raised PowBackwardError.

As mentioned in https://github.com/MenghanXia/DisentangledColorization/issues/4#issuecomment-1436602931 , ab value would fall in -86.187<a<98.234 and -107.859<b<94.469. So the predicted rgb values might be out of range.

I am still not sure how to solve this issue. I will try on it.

yyang181 avatar Feb 23 '23 17:02 yyang181

FYI: I have fine-tuned the model on 100 color images (randomly picked from VOC dataset) for another 20 epochs, and found no NAN problem. So, you may pay more attention to your data (e.g. whether it has a domain gap with natural images), and tune the learning rates if resumed from our chechpoint. Good luck!

MenghanXia avatar Feb 24 '23 05:02 MenghanXia

(1) Let's do an ablation study. To verify whether it is because of your dataset, you can try to resume the training over a normal color dataset, such as VOC2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/) or a subset of it. If no NAN occurs, it is probably means you have to think over your dataset. (2) There are three terms in the loss function. You are suggested to check which one causes NAN at the very beginning, and then track back to the tensors that are used to compute it. Once you find the tensor with NAN value, you will have an idea about why it happen.

The Lab to RGB convertion might be the one to blame. I use with torch.autograd.detect_anomaly() to check where the NAN value first occurs and find that rgb = (1.055*(rgb**(1./2.4)) - 0.055)_mask + 12.92_rgb*(1-mask) within method(xyz2rgb) in ./model/basic.py raised PowBackwardError.

As mentioned in #4 (comment) , ab value would fall in -86.187<a<98.234 and -107.859<b<94.469. So the predicted rgb values might be out of range.

I am still not sure how to solve this issue. I will try on it.

Solved, it turns out to be the lab2rgb convertion that raise this issue. It's known that the Lab color space covers a larger range than the RGB color space. So, when converting a predicted Lab image to a RGB image, the values in RGB image might be out of range, such as be negetive values. According to L.419-422 in ./models/basic.py, when the tensor rgb on L.422 contains negetive values, the pow operation of rgb**(1./2.4) would raise NAN values. Similar issue has been discussed in https://discuss.pytorch.org/t/function-powbackward0-returned-nan-values-in-its-0th-output/60882 .

To solve this problem, modify L.422 by (rgb.abs() + eps)**(1./2.4) to handle rgb values lower than zero would work.

By the way, I have fine-tuned the model on the validation set of COCO2017, and the NAN problem occurs if do not modify the code mentioned above. My testing machines include both NVIDIA-GTX3090 and NVIDIA-A6000.

Thanks a lot for your kindly help!

Best.

yyang181 avatar Feb 24 '23 08:02 yyang181