models icon indicating copy to clipboard operation
models copied to clipboard

API ObjectDetection size of input images issues

Open chenyuZha opened this issue 9 years ago • 43 comments

GPU : GeForce GTX 1080 Ti/PCIe/SSE2 (11 GB) Tensorflow version: 1.1.0 Python version: 2.7.12 Model checkpoint : ssd_mobilenet_v1_coco_11_06_2017

Context :After read the tutorial of ObjectDetection, I converted my own image data sets to tfRecord files by create the *.xml file to each image. As the sizes of images of my own data sets are very large : about width=4000pixels and height=2000pixels for each , the train.tfRecord is about 55G.

Issues: When I began to train with train.py by using the lightest model ssd_mobilenet_v1_coco_11_06_2017, after 4-5 steps, it crashed by error OOM. The error message is below: 125 Il seems like that the OOM error happened when allocating tensor with shape[1,2969,3546,3], As the capacity of my GPU is 11GB, I didn't understand why it causes this problem..

chenyuZha avatar Jul 06 '17 14:07 chenyuZha

Hi @chenyuZha - These resolutions are problematic because we keep an entire queue of images in memory, not just the batch that you are currently training on. See e.g. the queue_capacity and min_after_dequeue parameters in https://github.com/tensorflow/models/blob/master/object_detection/protos/input_reader.proto

Though note that we typically resize in SSD immediately to 300x300. So given this, it makes sense to just resize your input images to be smaller.

jch1 avatar Jul 06 '17 17:07 jch1

@jch1 Thanks for your response !

chenyuZha avatar Jul 06 '17 20:07 chenyuZha

Hi @jch1 . I'm training SSD on my own dataset, the result I think is good but not as good as I expected, I'm trying to finetune the model to increase the performace. I noticed that SSD resize the image to 300 by 300 using API tf.image.resize_images() which, according to official document,

Resized images will be distorted if their original aspect ratio is not the same as size.

And I did some experiments, it's true.

So, I changed resize API to resize_image_with_crop_or_pad(). Result is that the LOSS gets very high and very hard to converge.

My question is did you take resize distortion into consideration during the development of the object detection API? If you did, whether the mismatch of the aspect ratio of original image will affect the final result?

With thanks and regards!

yeephycho avatar Aug 01 '17 04:08 yeephycho

@yeephycho you should first resize image and keep original aspect ratio to e.g. 300px by larger side and then you shoudl pad image to square by resizing it with resize_image_with_crop_or_pad(). Code of aspect ratio preserving resize with TF's ops you can find in magenta repository, where neural style transfer is done

Luonic avatar Oct 30 '17 10:10 Luonic

Hi @yeephycho, I am also trying to train an object detector using my own images with various sizes and aspect ratio. I tried the following two image_resizer configurations in the pipeline.config and a pre-trained model from the model zoo, and it seems that model is not converging with the TotalLoss fluctuating around 4.0. Which image_resizer configuration did you use to make the model converge faster? Or do you pre-scale the dataset and bounding boxed as suggested by @Luonic to train your object detector.

Thanks!

    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    image_resizer {
      keep_aspect_ratio_resizer {
        min_dimension: 300
        max_dimension: 300
      }
    }

scotthong avatar Jan 04 '18 19:01 scotthong

Hi @scotthong, I really cannot remember how I solved this problem. But I can provide some experience which may not be correct but works for me.

  1. I used fixed_shape_resizer
  2. Total loss around 4.0 is OK, try to evaluate your model and see how it works with your validation set.
  3. If image is too small after resize, it is hard to detect small objects.

With regards! Yeephycho

yeephycho avatar Jan 05 '18 01:01 yeephycho

Hi all,

Maybe someone can help me with this issue:

https://github.com/tensorflow/models/issues/3196

Regards, Hao

Tsuihao avatar Jan 20 '18 07:01 Tsuihao

Hi @yeephycho @scotthong , I am encountering the same problem that the TotalLoss fluctuating around a high value(4.0). And is there anything I can do to avoid small objects miss with high resolution training images?

Regards, Lee

syndec avatar Feb 01 '18 08:02 syndec

Hi @syndec Try to study the network and especially the file "core/preprocessor.py". You should be able to find clues and determine if it is possible to resolve the problem you are facing.

scotthong avatar Feb 01 '18 15:02 scotthong

Hello @yeephycho and @syndec , Is there a definition of "small objects"? For example, if the ratio of the object's size to the whole image is 0.2, we consider it is a "small object". I am facing the same issue, the total loss fluctuating around 4.0. Also, the localization_loss/classification_loss is affected by the number of ground-truth in the image. For example, the loss increases when the number of ground-truth in an image increases. Because those ground-truth boxes are small, I wonder if I should exclude them from my training set. I use the pre-trained SSD-InceptionV2-Coco model to train with my dataset for vehicle detection. The performance of the model trained with my dataset is worse than the pre-trained SSD-InceptionV2-Coco.

Thank you for precious time on my question.

WillSapgreen avatar Apr 27 '18 06:04 WillSapgreen

I am facing similar issues but only when I use the ssd_random_crop data augmentation option in the pipeline config. My total loss converges much lower when I don't use the crop preprocessing. Also, I'm finding that the position of the object in the image has a huge impact on score - the detector behaves like it has blind spots. My training images are 1920x1200 and I'm using the fixed_shape_resizer to 300x300. My training runs have about 100 images and it converges to a total loss of about 0.26 after 10K steps. When I add the ssd_random_crop the total loss is around 2.5 even after 20K steps. I've tried variations with random_crop_image as well with no luck. edit: the same training set with ssd_random_crop produced a lower mAP, even with twice as many steps - this doesn't seem right -is there a bug in the cropping algorithm?

funkysandman avatar May 03 '18 17:05 funkysandman

Hello @funkysandman , Thank you for sharing "ssd_random_crop" idea, unfortunately, it does not resolve my issue. My total loss still remains around 4.

Would you mind to share what "good data" for SSD-InceptionV2 training in your opinion?

  1. Does "resolution" matter? ( my image from Udacity is kind of blurred )
  2. Will the number of objects per image affect the Box predictor?

Because SSD-InceptionV2 has limitation to detect the small objects, I think to provide a 300x300 image within the target object should be "big" enough to allow the model learn the features. So I prepare the image data from Udacity:

  1. 300x300( crop the car object from Udacity image dataset )
  2. the object to be classified at least occupy 10% in the image. ( make sure the car is big enough )
  3. Only one object per image.

The total loss is close to 1.0 after 200k iteration.

But after training with this kind of data, the trained model cannot detect any object in my evaluation set.

I am working on the root cause. So I wonder what the good data should be for SSD-InceptionV2's training.

P.S. My Tensflow is 1.8.0 and the Tensorflow Object Detection repo is pulled from 2018.05.03

Thank you.

WillSapgreen avatar May 05 '18 20:05 WillSapgreen

Make sure your pipeline.config is matched perfectly to the model you are using. I've accidentally run training with the wrong config file. Maybe share your pipeline.config file? Also, the detection program must be tailored to the specific model as well and the images normalized properly for the detector or you may get weird results

funkysandman avatar May 07 '18 15:05 funkysandman

Hello @funkysandman , I made a mistake that in the label map the item's "name' is capitalized but in my transfer-tfrecord script, I use the non-capitalized character. Now I am working on the bad performance on SSD-InceptionV2 now.

WillSapgreen avatar May 08 '18 16:05 WillSapgreen

Question. Say all the train and test images from the raw data is of size (512, 7000). Can anyone who works on the object detection api tell me if it is ok to leave the image_resizer set to the 300x300 setting? Or should i change it to the following to match the dimension of my images. I'm not sure what the purpose of that piece of code is. Currently training a model with the setting as: image_resizer { fixed_shape_resizer { height: 512 width: 7000 } }

cmbowyer13 avatar May 10 '18 13:05 cmbowyer13

Hello @cmbowyer13 ,

If you use SSD from the model zoo,

you need either resize your image to 300x300 before feeding to the model or let the model does it for you.

Good luck!

WillSapgreen avatar May 10 '18 22:05 WillSapgreen

I don't know what youre saying at willSapgreen. Why can't i do as i am doing or why cannot the model accept arbitrary sized images?

cmbowyer13 avatar May 11 '18 02:05 cmbowyer13

You can provide any size image for detection. I would not resize it so small that you cannot see the features you're after (this depends on what your objects are). During training the images are resized to 300x300. If the images you are using to train your model lose their features at that resolution, you can increase it to something like 600x600 in the pipeline.config file. This has an effect on training time as more memory is needed. Resizing images at detection time can help speed things up.

funkysandman avatar May 11 '18 14:05 funkysandman

But what does resizing to 300x300 in the config file do to all my images during training which are of size (512x7000)? You're saying it's fine to leave it as 300x300, and then be able to detect any size image I want as long as it contains the same class of objects? And I will need to be detection matrices of raw data in real time. Are there any guidelines for doing realtime object-detection with our trained object detection model? Any good links or references are appreciated. Also, do you know of the best way to convert the model to a C++ format once it has created and performs as I like?

cmbowyer13 avatar May 11 '18 15:05 cmbowyer13

@cmbowyer13 I would guess you can leave it at 512x7000 for training, as long as you have the memory + gpu to handle it. As long as the model loss converges it should be ok. When it comes to using the trained model, you can give it pictures in their original size I believe. You can resize them if it is too slow.

funkysandman avatar May 11 '18 15:05 funkysandman