Faster-rcnn.pytorch: RPN regression loss suddenly becomes NaN

Created on 11 Jun 2018  ·  18Comments  ·  Source: jwyang/faster-rcnn.pytorch

When I use this code to train on customer dataset(Pascal VOC format), RPN loss always turns to NaN after several dozen iterations.
I have excluded the possibility of Coordinates out of the image resolution,xmin=xmax and ymin=ymax.
[session 1][epoch 1][iter 12/4500] loss: 1.1964, lr: 1.00e-03
fg/bg=(16/496), time cost: 0.503772
rpn_cls: 0.1663, rpn_box: 0.0488, rcnn_cls: 0.9381, rcnn_box 0.0433
[session 1][epoch 1][iter 13/4500] loss: 0.8909, lr: 1.00e-03
fg/bg=(12/500), time cost: 0.516370
rpn_cls: 0.1984, rpn_box: 0.0421, rcnn_cls: 0.6251, rcnn_box 0.0254
[session 1][epoch 1][iter 14/4500] loss: 1.1052, lr: 1.00e-03
fg/bg=(20/492), time cost: 0.490039
rpn_cls: 0.1901, rpn_box: 0.0351, rcnn_cls: 0.8329, rcnn_box 0.0469
[session 1][epoch 1][iter 15/4500] loss: nan, lr: 1.00e-03
fg/bg=(6/506), time cost: 0.530968
rpn_cls: 0.1404, rpn_box: nan, rcnn_cls: 0.2575, rcnn_box 0.0102

Most helpful comment

its just for dataset annotations.
you should modify the pascal_voc.py code as follows :+1:

x1 = max(float(bbox.find('xmin').text), 0)
y1 = max(float(bbox.find('ymin').text) , 0)
x2 = max(float(bbox.find('xmax').text) , 0)
y2 = max(float(bbox.find('ymax').text) , 0)
or
x1 = max(float(bbox.find('xmin').text) - 1, 0)
y1 = max(float(bbox.find('ymin').text) - 1, 0)
x2 = max(float(bbox.find('xmax').text) - 1, 0)
y2 = max(float(bbox.find('ymax').text) - 1, 0)

All 18 comments

I met a similar problem here, trained on KITTI pedestrian detection, converted to voc format
loss would go to NaN in like 20 iters
wondering how to solve the problem.

@ZHOUXINWEN @rxqy Have you solved this problem? I am stuck in the same problem. Thanks!

@xiaomengyc

This should help you. it's to do with your annotations and possible the "-1" when feeding in the annotations in the pascal voc dataset script.

https://github.com/jwyang/faster-rcnn.pytorch/issues/136#issuecomment-390544655

Thank you @Worulz. I have found this solution and fixed my problem. I train this faster-rcnn on a pedestrian dataset. When I adopted the restriction for the box sizes, the NaN problem disappeared.

@xiaomengyc
How to restrict the box sizes,can you show the modification in code?I have been stuck in the same problem,I need your help.Thanks a lot.

You just need to change this line to

https://github.com/jwyang/faster-rcnn.pytorch/blob/28db6d0b313220d200b739f4e22410fbe35529f4/lib/roi_data_layer/roibatchLoader.py#L190

not_keep = (gt_boxes[:,2] - gt_boxes[:,0]) < 10 and (gt_boxes[:,3] - gt_boxes[:,1]) < 10
where 10 is the minimum width and height.

@xiaomengyc from your description seems that's because RPN couldn't propose very small anchors? How about trying to set config.py:__C.ANCHOR_SCALES to smaller values, e.g. [1,2,3] (corresponding to 16,32,48 pixels)?

@askerlee As I understand, ANCHOR_SCALES should be set with respect to the scale of ground-bboxes.
Filtering out some very small boxes can avoid the model producing proposals with the area of 0, which may cause NaN problem.

According to my experiments, loading pre-trained weights, e.g. Faster-RCNN trained on COCO, can also avoid the NaN problem, without filtering out small bboxes.
So, if there are no pre-trained weights, I assume that we can train the model with the filter for several epochs, and then remove the filter to continue training.

@xiaomengyc I've also met the same problem. But the culprits are ground bboxes of sizes around 50x10. So I applied your trick and filtered these bboxes. I guess nan appears because the proposals are too much bigger than ground bboxes and hence big rpn_box losses are incurred.

@askerlee I might be.
In my case, I traced the code, and found the area of some proposals become zero, which cause the denominator of somewhere to be zero.

@askerlee Hi, how do you solve your problem, i met the same problem that the sizes of ground bboxes in my datast are small and around to 20*20. And i get no nan loss in first epoch and it gets nan loss from second epoch. Can you tell me your solution, thanks.

@Tianlock do you have large and very small bounding boxes?. You could always crop the image to find feature areas. Then run the algorithm on top.

@Tianlock I fixed it by filtering bboxes smaller than 20x20. You could set the filtering threshold to say 15x15, if 20x20 filters many useful bboxes. You could also try to reduce the learning rate at the same time.

@Tianlock
Great
Can you tell how to filtering the bboxes ??
Thanks

its just for dataset annotations.
you should modify the pascal_voc.py code as follows :+1:

x1 = max(float(bbox.find('xmin').text), 0)
y1 = max(float(bbox.find('ymin').text) , 0)
x2 = max(float(bbox.find('xmax').text) , 0)
y2 = max(float(bbox.find('ymax').text) , 0)
or
x1 = max(float(bbox.find('xmin').text) - 1, 0)
y1 = max(float(bbox.find('ymin').text) - 1, 0)
x2 = max(float(bbox.find('xmax').text) - 1, 0)
y2 = max(float(bbox.find('ymax').text) - 1, 0)

@xiaomengyc

not_keep = (gt_boxes[:,2] - gt_boxes[:,0]) < 10 and (gt_boxes[:,3] - gt_boxes[:,1]) < 10

Hi, I get RuntimeError: bool value of Tensor with more than one value is ambiguous error when I tried to replace the line with your code. Any idea why this might have happened? Thanks in advance!

@xiaomengyc

not_keep = (gt_boxes[:,2] - gt_boxes[:,0]) < 10 and (gt_boxes[:,3] - gt_boxes[:,1]) < 10

Hi, I get RuntimeError: bool value of Tensor with more than one value is ambiguous error when I tried to replace the line with your code. Any idea why this might have happened? Thanks in advance!

Try to replace the and operation by * , see if it works.

@xiaomengyc

not_keep = (gt_boxes[:,2] - gt_boxes[:,0]) < 10 and (gt_boxes[:,3] - gt_boxes[:,1]) < 10

Hi, I get RuntimeError: bool value of Tensor with more than one value is ambiguous error when I tried to replace the line with your code. Any idea why this might have happened? Thanks in advance!

Try to replace the and operation by * , see if it works.

Yes it works! Thanks. Although I am still getting nan loss but thanks anyways :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ZhangJiajun1995 picture ZhangJiajun1995  ·  5Comments

GPaolo picture GPaolo  ·  5Comments

gayathrimahalingam picture gayathrimahalingam  ·  3Comments

HaiminZhang picture HaiminZhang  ·  3Comments

herrickli picture herrickli  ·  3Comments