LossTensor is inf or nan while training ssd_inception_v2 model in my own dataset.
Use google object_detection api.
TensorFlow version 1.2.0
Describe the problem
LossTensor is inf or nan while training ssd_inception_v2 model in my own dataset. training config: train_config: { batch_size: 24 optimizer { rms_prop_optimizer: { learning_rate: { exponential_decay_learning_rate { initial_learning_rate: 0.004 decay_steps: 800720 decay_factor: 0.95 } } momentum_optimizer_value: 0.9 decay: 0.9 epsilon: 1.0 } } #fine_tune_checkpoint: "/opt/user/awens/tf/models-master/object_detection/models/model/pre_trained/ssd/model.ckpt" from_detection_checkpoint: true data_augmentation_options { random_horizontal_flip { } } data_augmentation_options { ssd_random_crop { } } }
Source code / logs
INFO:tensorflow:global step 3161: loss = 7.0152 (16.166 sec/step) INFO:tensorflow:global step 3162: loss = 6.2710 (18.039 sec/step) INFO:tensorflow:global step 3163: loss = 6.5963 (16.896 sec/step) INFO:tensorflow:global step 3164: loss = 6.6896 (15.880 sec/step) INFO:tensorflow:global step 3165: loss = 7.1895 (15.575 sec/step) INFO:tensorflow:Recording summary at step 3165. INFO:tensorflow:global step 3166: loss = 6.5400 (20.047 sec/step) INFO:tensorflow:global step 3167: loss = 7.0436 (15.845 sec/step) INFO:tensorflow:global step 3168: loss = 6.8610 (16.426 sec/step) INFO:tensorflow:global step 3169: loss = 7.8241 (15.983 sec/step) INFO:tensorflow:global step 3170: loss = 7.3034 (15.400 sec/step) INFO:tensorflow:global step 3171: loss = 6.5742 (16.132 sec/step) INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.InvalidArgumentError'>, LossTensor is inf or nan. : Tensor had NaN values [[Node: CheckNumerics = CheckNumericsT=DT_FLOAT, message="LossTensor is inf or nan.", _device="/job:localhost/replica:0/task:0/cpu:0"]]
Caused by op u'CheckNumerics', defined at:
File "./object_detection/train.py", line 198, in
InvalidArgumentError (see above for traceback): LossTensor is inf or nan. : Tensor had NaN values [[Node: CheckNumerics = CheckNumericsT=DT_FLOAT, message="LossTensor is inf or nan.", _device="/job:localhost/replica:0/task:0/cpu:0"]]
Hi @Wenstery - It's a bit hard to say without knowing more, but typical ways to deal with this are to reduce the learning rate or to increase the batch size. I also recommend using a GPU if possible as it will significantly decrease your turnaround time on these experiments.
Hi @jch1 -It's also happend when training on a GPU,I 'll try to reduce the learning rate. Thanks a lot!
To provide another data point, I had the same bug on both ssd models (while the other 3 models were fine). I will try reducing the learning rate and if the problem persists, I will comment back here.
Thanks @timisplump - also maybe kick up the batch size to 32 if you can hold it in memory.
@jch1 I'm unfortunately training on images that are too large (504x960) to hold a batch size of 32. I'm currently using a batch size of 12. When I moved to 16, the training worked fine until it tried to store a checkpoint, at which point I got an OOM error.
I could downsample the images and might try that later, but for now I'd like to see how the models do with this image size
@timisplump does reducing the learning rate help?
@cy89 well I only started the new task with a lower learning rate about an hour ago but it hasn't crashed. Last time, it crashed after running for about 10 hours, so i can't give you a great response yet. Will let you know when I check back on Monday!
I had the same problem. The real reason is your annotation have problem. If any boundingbox value is Nan in your annotation will cause this problem. You should go back and check your annotation. That's how I solve this problem when training on my own dataset.
@cy89 Sorry for the slow reply. Been busy with other stuff. The learning rate did not solve the problem. I'm going to see if I can run multi-gpu with a larger batch size and check to see if that will help. I'm pretty convinced it's not an error with my dataset because the atrous faster rcnn was working fine after a few days of training. Moreover, the other models that crashed (all 4 others), crashed at different points in the dataset/different epochs. It's a pretty strange error. Hope we can find it
Same problem with rfcn model. The training process crashed after 573k iterations and reported the same error. @lzkmylz What exactly do you mean for NAN in annotations? NAN is generated by the computation that in annotations there cannot be this kind of number.
Had the same problem. I double checked my annotations and found some bounding boxes outside the image boundaries. Fixing the bounding boxes solved the problem for me.
I'm suffering from the same problem. I cleaned all my annotations, deleted small objects and limit the annotation within the range of 0.005 to 0.995. But still I will got "Tensor had NaN values" error. And I found that the error always happen around 3000 steps. I wonder whether it is related to test or val progress of the object detection API? And I also tried to reduce the learning rate and change another optimizer, error still occur. I got a model that can repeat the error, usually, the error happen within 30 steps, sometimes the second step will raise the error by finetune that model.
Anybody got any more experience on this, any other way that I can try?
I noticed that the optimizer for SSD incetion v2 is RMSPropOptimizer.
In the config file line around 136, training config part:
train_config: {
batch_size: 24
optimizer {
rms_prop_optimizer: {
learning_rate: {
exponential_decay_learning_rate {
initial_learning_rate: 0.004
decay_steps: 800720
decay_factor: 0.95
}
}
momentum_optimizer_value: 0.9
decay: 0.9
epsilon: 1.0
}
}
epsilon was set to 1.0, but in the official tensorflow document, epsilon should be a very small value to avoid 0 denominator.
I wonder whether this parameter was intentionally set to 1.0?
Could it be the reason for NaN error?
@jch1
Thanks!
I kept having this issue with SSD MobileNet_v1 and after fixing bounding boxes out of range and removing objects that were too small (less than 15% of the image) I have had no issues training so far. Others had similar issues here #1907
Is is this a problem allocated with the data set ?
Yeah. This loss error occurs when there's some kind of wrong annotation . I tried with different data set it works really fine. It's always good keep a track of following things ,
- Image size
- Object size
- Boundaries of the bounding boxes
Do you have any advices on range of values that bboxes should be with? How small or big can they big ? I have rather small 40x40 object in dataset and getting nan/inf at start.
Curiously when I apply batch normalization during training, I'm getting no nan/inf but constant loss :(
batch_norm {
train: true
}
I am also having this problem. My images do not have any sort of orientation, so I am have flipped,flopped,transposed,transversed, and rotated my images by 90,-90, and 180 degrees. It works great on the original dataset, but not on artificially augmented training set.
@jerowe ,You can try to fix the bounding boxes like @ph463," I double checked my annotations and found some bounding boxes outside the image boundaries."
@Wenstery , I took another look back over my dataset. I checked the annotations for boxes outside of the boundaries, and I didn't find any. I am using the faster_rcnn_inception_resnet_v2_atrous_coco model. I do have very small boxes.
Oddly enough I found that when I tried to use the flipped (horizontal), flopped (vertical), transverse (y=x), or rotate 90/180/-90 I got the NaN error, but when I used the transposed dataset it was fine.
I noticed in the newest tensorflow models there are pre processing steps for vertical and rotation 90. There is already a flip horizontal preprocess step in there. Just waiting for the tensorflow 1.4 release on conda to try it out!