models icon indicating copy to clipboard operation
models copied to clipboard

LossTensor is inf or nan while training ssd_inception_v2 model in my own dataset.

Open Wenstery opened this issue 9 years ago • 34 comments

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 tf.app.run() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "./object_detection/train.py", line 194, in main worker_job_name, is_chief, FLAGS.train_dir) File "/opt/python-project/models-master/object_detection/trainer.py", line 221, in train total_loss = tf.check_numerics(total_loss, 'LossTensor is inf or nan.') File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 415, in check_numerics message=message, name=name) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op original_op=self._default_original_op, op_def=op_def) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in init self._traceback = _extract_stack()

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"]]

Wenstery avatar Jul 07 '17 02:07 Wenstery

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.

jch1 avatar Jul 07 '17 05:07 jch1

Hi @jch1 -It's also happend when training on a GPU,I 'll try to reduce the learning rate. Thanks a lot!

Wenstery avatar Jul 07 '17 06:07 Wenstery

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.

timisplump avatar Jul 07 '17 20:07 timisplump

Thanks @timisplump - also maybe kick up the batch size to 32 if you can hold it in memory.

jch1 avatar Jul 07 '17 20:07 jch1

@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 avatar Jul 07 '17 20:07 timisplump

@timisplump does reducing the learning rate help?

cy89 avatar Jul 08 '17 01:07 cy89

@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!

timisplump avatar Jul 08 '17 01:07 timisplump

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.

lzkmylz avatar Jul 10 '17 05:07 lzkmylz

@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

timisplump avatar Jul 13 '17 23:07 timisplump

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.

lionel92 avatar Jul 19 '17 02:07 lionel92

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.

ph463 avatar Jul 27 '17 14:07 ph463

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?

yeephycho avatar Aug 01 '17 09:08 yeephycho

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!

yeephycho avatar Aug 02 '17 09:08 yeephycho

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

andreabc avatar Aug 23 '17 16:08 andreabc

Is is this a problem allocated with the data set ?

shamanez avatar Sep 23 '17 23:09 shamanez

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 ,

  1. Image size
  2. Object size
  3. Boundaries of the bounding boxes

shamanez avatar Sep 24 '17 14:09 shamanez

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
}

Arsakes avatar Oct 08 '17 12:10 Arsakes

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 avatar Nov 18 '17 03:11 jerowe

@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 avatar Nov 23 '17 02:11 Wenstery

@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!

jerowe avatar Nov 27 '17 12:11 jerowe