models icon indicating copy to clipboard operation
models copied to clipboard

loss explodes after few iterations

Open dotannn opened this issue 8 years ago • 35 comments

System information What is the top-level directory of the model you are using: object_detection

Have I written custom code (as opposed to using a stock example script provided in TensorFlow): use train.py script on my own dataset

OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04

TensorFlow installed from (source or binary): installed from pip

TensorFlow version (use command below): 1.6

CUDA/cuDNN version: cuda 9.0

GPU model and memory: AWS - p3.2xlarge instance - using V100

I'm trying to train faster-rcnn resnet101 on my own dataset (something I did successfully a few months ago).
after a few steps, the loss gets insanely big, do you have any direction why it could happen?

RUNNING ON AWS - p3.2xlarge instance

INFO:tensorflow:global step 435: loss = 0.0630 (0.408 sec/step) INFO:tensorflow:global step 435: loss = 0.0630 (0.408 sec/step) INFO:tensorflow:global step 436: loss = 0.1628 (0.404 sec/step) INFO:tensorflow:global step 436: loss = 0.1628 (0.404 sec/step) INFO:tensorflow:global step 437: loss = 0.0577 (0.397 sec/step) INFO:tensorflow:global step 437: loss = 0.0577 (0.397 sec/step) INFO:tensorflow:global step 438: loss = 54059178029467303936.0000 (0.403 sec/step) INFO:tensorflow:global step 438: loss = 54059178029467303936.0000 (0.403 sec/step) INFO:tensorflow:global step 439: loss = 432537635714800549888.0000 (0.400 sec/step) INFO:tensorflow:global step 439: loss = 432537635714800549888.0000 (0.400 sec/step) INFO:tensorflow:global step 440: loss = 2106942657570782838784.0000 (0.405 sec/step) INFO:tensorflow:global step 440: loss = 2106942657570782838784.0000 (0.405 sec/step) INFO:tensorflow:global step 441: loss = 7774169971762292326400.0000 (0.401 sec/step) INFO:tensorflow:global step 441: loss = 7774169971762292326400.0000 (0.401 sec/step) INFO:tensorflow:global step 442: loss = 25262924095336287830016.0000 (0.404 sec/step) INFO:tensorflow:global step 442: loss = 25262924095336287830016.0000 (0.404 sec/step)

dotannn avatar Apr 04 '18 07:04 dotannn

Can you please:

  • Fill out the issues template.
  • Provide a minimal example demonstrating the problem-- this would involve replicating the problem with a subset of the dataset in question, so we have a better sense of what is going on.
  • Verify that you can run the model using the referenced datasets.

That will allow us to better understand the problem.

yhliang2018 avatar Apr 05 '18 00:04 yhliang2018

It is really hard to say why something is happening without some context. But my guess is that you are using a high learning rate that is making your model wiggle around the optimal.

I would suggest you to use learning rate scheduler that exponentially decreases with global step. This documentation might give an insight on how to use the learning rate scheduler. This helped when I faced similar issues, hope this helps. This learning rate decay is easy to code but should you run into trouble, here is a stackoverflow link that shows an example of decayed learning rate usage.

sk-g avatar Apr 06 '18 01:04 sk-g

I'm fine-tunning faster-rcnn-resnet101-coco model with my own dataset using 7 labels.

my initial learning rate is 0.0003. I don't see a standard 'jump' around optimum that usually seen when the LR is too high, my loss is decreasing as expected for hundreds or steps and then out of nowhere explodes to this very big numbers.

added general info

dotannn avatar Apr 06 '18 18:04 dotannn

Hi @dotannn , like you say the loss decreased for some steps and then it exploded it could be because of many reasons, my first bet was like I mentioned before. You could use a decayed learning rate so your model converges faster but if you do not want to do that, I think the next obvious reason could be vanishing gradients, assuming you are not familiar with that concept, maybe some gradients are so small that they are clipped to 0 this is a huge problem if many weights suddenly are clipped to 0, you WILL 'jump' across the optimal point and this will be chained to rest of the weights as well. If you are familiar with the problem, apologies.

But I would still emphasise on the 'jumping' part. For example, look at this from your output:

INFO:tensorflow:global step 435: loss = 0.0630 (0.408 sec/step)
INFO:tensorflow:global step 436: loss = 0.1628 (0.404 sec/step)
INFO:tensorflow:global step 436: loss = 0.1628 (0.404 sec/step)
INFO:tensorflow:global step 437: loss = 0.0577 (0.397 sec/step)

we can clearly see the phenomenon in this part.

sk-g avatar Apr 09 '18 02:04 sk-g

Thanks, @sk-g, Actually that was a memory issue...
closing it now

dotannn avatar Apr 10 '18 11:04 dotannn

Hello, I'm happy that I found this thread, since I'm facing the exact same issue. And this thread is the first on this problem. It would be great to hear what @dotannn your solution was.

I tried lower learning rates but it didn't have any impacts. Even with very low learning rates the problem ocurred. Always after about 500 steps. Then after about 30k to 50k steps the problem disappeared. Can anybody help me please? I would be so glad!

RoloffMatek avatar Apr 17 '18 14:04 RoloffMatek

I'm having the same problem, but the most common gradient is getting static and after some interactions, it suddenly explodes. With higher Learning Rates, the gradient grows exponentially.

I have tested numerous combinations of crop, learning rate, batch size, atrous rates, and nothing changes this pattern.

felipecicero avatar Jun 03 '18 17:06 felipecicero

Same Problem over here for faster rcnn using resnet101 and resnet50 For me the loss is decreasing as expected, but after 20/30/40k steps the loss explodes. After that it comes back to the original level (below 1 for rpn, below 5 for 2nd stage) but doesn't produce any meaningful boxes anymore. I have already varied:

  • the loss weights
  • resizing options

I have lowered the score level, so I can see all boxes the net predicts and it turns out, that it gets invariant towards the input, meaning it predicts always the same pattern of boxes.

I am really stuck. Did anybody resolve that issue? @sk-g @felipecicero @RoloffMatek @dotannn

ghost avatar Jun 12 '18 16:06 ghost

I have managed to improve the convergence a bit using last_layer_gradient_multiplier to 0.1 instead of 10 as I have seen in some cases here in the topics, I am also using slow_start_step for a few hundred iterations with 0.007 and then the momentum with 0.0001. I removed the random scale as well.

But sometime before reaching the global minimum the loss always ends up exploding and showed no signs of improvement in any tests I did.

I am having several other problems related to training with my own dataset in deeplab, I have not yet been able to report everything here due to my deadlines.

I am using 8 P100 GPUs.

python deeplab/train.py
--logtostderr
--training_number_of_steps=250000
--train_split="train"
--model_variant="xception_71"
--atrous_rates=6
--atrous_rates=12
--atrous_rates=18
--output_stride=16
--decoder_output_stride=4
--train_crop_size=513
--train_crop_size=513
--train_batch_size=24
--num_clones=4
--base_learning_rate=0.0001
--last_layer_gradient_multiplier=0.1
--slow_start_step=500
--slow_start_learning_rate=0.007
--fine_tune_batch_norm=True ...

felipecicero avatar Jun 12 '18 23:06 felipecicero

Thanks for your quick reply!

I was also able to shift the loss explosion through hyperparameter tuning, but actually I found an error which seems to cause the problem in my case.

I was using a corrupt label file, which caused some incompatibility while learning. So creating new tf records and a complete newly parametrized setup fixed it. I know it seems obvious but it took me quite a while to actually notice that there was something wrong with the labels Loss explosion due to corrupt labels was also reported in some threads on stackoverflow (sry I don't have the links anymore). Hope it helps!

ghost avatar Jun 16 '18 09:06 ghost

I've got this issue too, but I found the reason, the label_map.pbtxt contain 5 labels, but in my pipeline.config: model { faster_rcnn { num_classes: 4}}, which tells the machine only has 4 classes,if the network predict the 5th one, the previous 4 's probability will all be 0, which will generate a very large loss value(log(0) is big).

valleu326 avatar Aug 15 '18 03:08 valleu326

Issue may be caused by: damaged bounding boxes (out of image boundary or xMin>xMax) mismatched category names in tfrecord and map.pbtxt I have same issue with exploding loss after few iterations, fixed by checking names in map.pbtxt

lxq2t avatar Aug 20 '18 07:08 lxq2t

Where do I find this map.pbtxt file? In the tutorial for training is not explained about this file.

felipecicero avatar Aug 20 '18 18:08 felipecicero

@felipecicero map.pbtxt is just a text file which contains the labels of each object class (class with id xx has name yyy etc). Since you have trained your model you have use one in youir config file in your train_input_reader section label_map_path: "path to your map.pbtxt".

eypros avatar Aug 22 '18 12:08 eypros

Issue may be caused by: damaged bounding boxes (out of image boundary or xMin>xMax) mismatched category names in tfrecord and map.pbtxt I have same issue with exploding loss after few iterations, fixed by checking names in map.pbtxt

@highemhigh Thanks so much! After change category names in map.pbtxt to match with tfrecord, it works.

cannguyen275 avatar Oct 16 '18 09:10 cannguyen275

@highemhigh wow I never thought about the labelmap file. This solved my problem. Thank you so much!!

yanfengliu avatar May 24 '19 20:05 yanfengliu

Hallo my Name is Siggi,

and how can i Open the tfrecord files?

Siggi1988 avatar Jun 28 '19 23:06 Siggi1988

@Siggi1988

you need to decode the tfrecord files if you want to have a look inside. This should help you to do that.

53RT avatar Jul 03 '19 10:07 53RT

Thanks, @sk-g, Actually that was a memory issue... closing it now

@dotannn may I know how you resolve this memory issue? I think I have the same problem. Thanks!

imayachita avatar Aug 18 '19 13:08 imayachita

Issue may be caused by: damaged bounding boxes (out of image boundary or xMin>xMax) mismatched category names in tfrecord and map.pbtxt I have same issue with exploding loss after few iterations, fixed by checking names in map.pbtxt

Many thank for your reply. If dont have your reply, I will take long time to solve my problem @@.

loi-nguyen-khanh avatar Nov 28 '19 04:11 loi-nguyen-khanh