models icon indicating copy to clipboard operation
models copied to clipboard

Object detection memory leak

Open YijinLiu opened this issue 8 years ago • 25 comments

System information

  • What is the top-level directory of the model you are using: research/object_detection
  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • TensorFlow installed from (source or binary): Source
  • TensorFlow version (use command below): 1.10.0
  • Bazel version (if compiling from source): 0.15.2
  • CUDA/cuDNN version: 9.2/7.1.4.18
  • GPU model and memory: GTX 960/4G
  • Exact command to reproduce: python research/object_detection/model_main.py --alsologtostderr
    --pipeline_config_path=research/object_detection/samples/configs/ssdlite_mobilenet_v2_coco.config
    --model_dir=XXX --num_eval_steps=6000

Describe the problem

There is a slow memory leak, about 600M-1G per hour. I tried to change some parameters to save memory according to earlier issues. They don't work though. train_config: { batch_size: 24 batch_queue_capacity: 10 num_batch_queue_threads: 8 ... } train_input_reader: { .... num_readers: 10 } eval_input_reader: { num_readers: 1 }

YijinLiu avatar Aug 20 '18 04:08 YijinLiu

I've got this problem too when I use estimator-based training. Haven't meet this while using slim-based training. I wonder if evaluating while training cause this.

bleqdyce avatar Aug 20 '18 10:08 bleqdyce

the same problem as me. For my situation, I added a line plt.close(fig) in object detection source code, then it at least can be trained about 200k steps and not be killed by OOM.you can refer from this issue for details.

wxzs5 avatar Aug 20 '18 12:08 wxzs5

@wxzs5 thank you, I'll try this. btw, training process never got killed in my case. However it would occupy all available memory which make pc extremely slow.

bleqdyce avatar Aug 20 '18 16:08 bleqdyce

Any update on this? Even I am facing the exact same issue when I am training a mask rcnn model.

KapoorHitesh avatar Aug 24 '18 06:08 KapoorHitesh

Still got this issue. I found that lower frequency of saving checkpoint and evaluate do make leak much slower, and that's all. At least, training won't stop during I'm at work.

bleqdyce avatar Aug 24 '18 06:08 bleqdyce

Issue 3603 is just part of the problem. I added plt.close(fig) in two places, which only slowed the leak a bit. I wonder how does Google not bothered by this? (given it has been there for almost half a year!)

YijinLiu avatar Aug 25 '18 04:08 YijinLiu

I experience the same problem too.

ldalzovo avatar Aug 31 '18 08:08 ldalzovo

Same here. This is probably a bug. Can't locate the leak source though.

liuchang8am avatar Sep 05 '18 12:09 liuchang8am

UPDATE:

  1. Add plt.close(fig) in visualization_utils.py DO NOT work.
  2. Tried eval_interval_secs flag in config, in order to slow down the eval process. Yet it is not implemented. Following https://github.com/tensorflow/models/pull/5144 to implement eval_interval_secs, still, DO NOT work.

Still memory leaking with tf version 1.10.

Workaround: When training is killed because of out of memory, I can pick up the training process by configure the fine_tune_checkpoint entry in config file, and adjust the decayed learning rate by calculation. However, it consistantly shows worse results than training directly, if you look at the tensorboard graphs.

liuchang8am avatar Sep 06 '18 03:09 liuchang8am

Tried eval_interval_secs flag in config, in order to slow down the eval process. Yet it is not implemented. Following #5144 to implement eval_interval_secs, still, DO NOT work.

hi, @liuchang8am . I'm the guy who commit #5144. It worked in tensorflow r1.9, however they change the way estimator trigger evaluation in 1.10. See https://github.com/tensorflow/tensorflow/commit/3edb609926f2521c726737fc1efeae1572dc6581#diff-bc4a1638bbcd88997adf5e723b8609c7 for detail.

Simply speaking:

  1. evaluation is triggered after you save a checkpoint.
  2. RunConfig.save_checkpoints_secs and RunConfig.save_checkpoints_steps is now the legitimate way to define the frequency of saving checkpoint.
  3. EvalSpec.throttle_secs does not define the frequency of saving checkpoint anymore. However it define the minimum time interval of evaluation.
  4. EvalSpec.throttle_secs will override RunConfig, which means that evaluation will be skipped if it is still in the minimum time interval of evaluation.

Here is my workaround

  1. To adjust the frequency of saving checkpoint, you can simply add save_checkpoints_secs or save_checkpoints_steps in this line. https://github.com/tensorflow/models/blob/7b3046768d2d6ea3f306b6ee7b62c02c9ca128a1/research/object_detection/model_main.py#L55
  2. To prevent checkpoint saving to trigger evaluation, follow #5144 and set eval_interval_secs in config. or simply pass throttle_secs to EvalSpec here. https://github.com/tensorflow/models/blob/7b3046768d2d6ea3f306b6ee7b62c02c9ca128a1/research/object_detection/model_lib.py#L609

For example:

I set RunConfig.save_checkpoints_steps to 5000 and EvalSpec.throttle_secs to 864000 (10 days). to skipped most evaluation other than first and last time. and I run evaluation on every saved checkpoint after training ends or while training is stopped.

As for training process is killed thing, I just restart the training with exactly same command. I do zero change to my config. It just automatically detect the last checkpoint and re-start training from it.

bleqdyce avatar Sep 06 '18 04:09 bleqdyce

@bleqdyce Hi, I'd like to know your way to set RunConfig.save_checkpoints_steps=k means to get model-ckpt in every k step ? If not ,how can we achieve that? Thanks

swg209 avatar Sep 06 '18 08:09 swg209

Hi, @swg209 It does mean to save model-ckpt in model_dir every k step in Tensorflow r1.10. At least it works like that on my PC =P, check official doc for more detail.

bleqdyce avatar Sep 06 '18 08:09 bleqdyce

@bleqdyce Thanks for your help. When I directly run model_main.py ,your way works. But when I run legacy/train.py , it doesn't save checkpoint . Did you meet the same issue?

swg209 avatar Sep 06 '18 13:09 swg209

legacy/train.py use slim as its tensorflow wrapper.

If you want to use legacy version, you may want to check out tf slim. I'm not familiar with slim, but I find out that according to this, you can pass save_interval_secs to slim.learning.train in legacy/trainer.py

https://github.com/tensorflow/models/blob/7b3046768d2d6ea3f306b6ee7b62c02c9ca128a1/research/object_detection/legacy/trainer.py#L402

and save_interval_secs is to define How often, in seconds, to save the model to logdir. . which is writing in tensorflow/contrib/slim/python/slim/learning.py#L607

hope this help but cant guarantee though =P

bleqdyce avatar Sep 06 '18 14:09 bleqdyce

Sorry for the late response! We removed the usage of "add_cdf_image_summary" in PR, you're encouraged to try again once it gets in.

pkulzc avatar Sep 14 '18 23:09 pkulzc

following

zishanahmed08 avatar Sep 18 '18 08:09 zishanahmed08

Has any body checked now that PR is merged, if the memory leak still exists?

zishanahmed08 avatar Sep 24 '18 10:09 zishanahmed08

@zishanahmed08 With tensorflow 1.11.0-rc2 and the latest from models, the memory leak is gone for me.

YijinLiu avatar Sep 25 '18 03:09 YijinLiu

@YijinLiu @zishanahmed08 I tried agian with the latest tensorflow/model repo in docker using image tensorflow/tensorflow:1.11.0-rc2-devel-gpu, still leak (~ 500 MB) every time when evaluation is done.

bleqdyce avatar Sep 25 '18 07:09 bleqdyce

Please re-open the issue : it's still there with tf 1.11.0 (from pip install) I am using ssd_resnet50_v1_fpn : same parameter as for pets example exept :

  • 1 class
  • 10 files of 1.8GB of tfrecord

The process go from 13G at start training to 31G after 2h (35100 global_step) on CPU RAM. The GPU RAM do no change from 5.4GB (I have the gpu option allow_growth activated) I throttle the eval to every 1 day (although I still have one eval run after the first checkpoint is saved). So basically there is no eval step during those 2h of training.

mhtrinh avatar Oct 05 '18 00:10 mhtrinh