models icon indicating copy to clipboard operation
models copied to clipboard

Background only images support

Open MyVanitar opened this issue 8 years ago • 31 comments

Hi,

Does the object detection model support "background only" images in training?

MyVanitar avatar Feb 12 '18 07:02 MyVanitar

My experience is that, although you can put the background images in the tf-records (if you follow what is done for the open images dataset, they allow you to do this in their create_oid_tfrecord - something like this), but beware the difference between RCNN and SSD models. Unfortunately, I couldn't get a good loss convergence with this approach. I tried balancing the dataset (positive and negative images, as I only had one class), and even collating the images together, because the model already extracts negative examples from the positive image (regions with no bounding box overlap).

In summary, the best loss I got was always with using only the "positive" images, i.e., at least one bounding box in them. Although a correct handling of background images would be of great help, especially in cases where you know some objects which might be detected as false positives...

AVCarreiro avatar Feb 19 '18 16:02 AVCarreiro

+1

saiaman avatar Feb 19 '18 19:02 saiaman

@tombstone can you please take a look?

bignamehyp avatar Feb 23 '18 07:02 bignamehyp

Background only images definitely increase the performance, specially by adding false positive containing images

MyVanitar avatar Feb 23 '18 07:02 MyVanitar

+1 Any updates on this?

azmathmoosa avatar Nov 04 '18 15:11 azmathmoosa

+1, also looking to increase performance with the use of background images, but currently unsure how to add them in. Any help appreciated

Trotts avatar Nov 09 '18 15:11 Trotts

I have a sparse dataset in which some images don't have any object instances of interest - so I can call them background only.

For those images without any detections, the dataset has still annotation files per image, with all entries filled with zeros: instance_id= 0, class_id = 0.0, x = 0.0, y = 0.0, w= 0.0, h=0.0 etc.

Now I don't understand how I will create the tfrecords. When I try to use all annotation files, I have a key error for class_id=0, because it's not the in label map.

I don't think that it's possible not to use any annotation files for a sample while creating the tfrecords, so I need to use this abstract annotation with zeros. Any ideas about how to solve this problem?

mirayyuce avatar Dec 05 '18 10:12 mirayyuce

I created my tfrecord data using something like this. For background images, all bounding box related features('image/object/bbox/xmin', 'image/object/class/label', ...) in tf.train.Features are empty lists. After doing this, I saw no bounding box on preprocessed background images, and the number of false positives did decrease a lot.

xrz000 avatar Dec 06 '18 05:12 xrz000

@xrz000 I am facing issue in generating tf records with background images. I was successfully able to generate the record files by removing all ground truth values. But facing issue while training the model. Getting error message: InvalidArgumentError (see above for traceback): indices[0] = 4 is not in [0, 1)

BalajiB3663 avatar Dec 26 '18 12:12 BalajiB3663

I created my tfrecord data using something like this. For background images, all bounding box related features('image/object/bbox/xmin', 'image/object/class/label', ...) in tf.train.Features are empty lists. After doing this, I saw no bounding box on preprocessed background images, and the number of false positives did decrease a lot.

@xrz000 did you use RCNN or SSD architecture? I have just tried with SSD (Retinanet), and I did exactly what you said but the models is not learning at all...

ldalzovo avatar Mar 01 '19 21:03 ldalzovo

I used SSD. Have you tried to plot preprocessed images? Maybe bbox labels were not correctly written for positive examples.

On Sat, Mar 2, 2019 at 05:10 Leonardo Dal Zovo [email protected] wrote:

I created my tfrecord data using something like [image: this] https://github.com/tensorflow/models/blob/2c181308e897175f0463eaca4db15e92d5d5c29f/research/object_detection/dataset_tools/create_pascal_tf_record.py#L124. For background images, all bounding box related features('image/object/bbox/xmin', 'image/object/class/label', ...) in tf.train.Features are empty lists. After doing this, I saw no bounding box on preprocessed background images, and the number of false positives did decrease a lot.

@xrz000 https://github.com/xrz000 did you use RCNN or SSD architecture? I have just tried with SSD (Retinanet), and I did exactly what you said but the models is not learning at all...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/models/issues/3365#issuecomment-468812352, or mute the thread https://github.com/notifications/unsubscribe-auth/AJ4ioV1nLP1ZunlZ7vV676ZEzYc6hwU5ks5vSZc7gaJpZM4SBy7S .

xrz000 avatar Mar 03 '19 16:03 xrz000

I have, all seems fine regarding the data... But I have found also this comment by @pkulzc :"For your negative images, is it true that you have zero groundtruth boxes? We currently don't support explicit negative image in training so adding those images don't help at all" (source). Is it still true now?

ldalzovo avatar Mar 03 '19 17:03 ldalzovo

@ldalzovo No it's not true. Sorry for the confusion but I was thinking of something else when answering that SO question. Adding background images would help decrease false positives.

pkulzc avatar Mar 05 '19 09:03 pkulzc

+1

Dinaya avatar Jul 26 '19 09:07 Dinaya

Hi There, We are checking to see if you still need help on this, as this seems to be considerably old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.

tensorflowbutler avatar Jan 29 '20 23:01 tensorflowbutler

@ldalzovo No it's not true. Sorry for the confusion but I was thinking of something else when answering that SO question. Adding background images would help decrease false positives.

But how to feed background images? with just .xml that includes class_name('background') info or includes class_name + bbox info as dummy(i.e. [0,0,0,0])? If we give an empty list for bbox coordinates, then how error to be calculated?

YetkinMesutKaraman avatar Feb 06 '20 18:02 YetkinMesutKaraman

I am facing issue with false positvies, while training a single class classifier with ssd_mobilenet v2. Could you please explain the way tfrecord should be generated for background images?

sampathkumaran90 avatar Apr 06 '20 11:04 sampathkumaran90

+1

Nimrods avatar Apr 30 '20 11:04 Nimrods

OK for future references, this is how i add background images to the dataset allowing the model to train on it. Functions used from: datitran/raccoon_dataset

  1. Generate CSV file -> xml_to_csv.py
  2. Generate TFRecord from CSV file -> generate_tfrecord.py

First Step - Creating XML file for it

Example of background image XML file

<annotation>
    <folder>test/</folder>
    <filename>XXXXXX.png</filename>
    <path>your_path/test/XXXXXX.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>640</width>
        <height>640</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
</annotation>

Basically you remove the entire <object> (i.e no annotation )

Second Step - Generate CSV file

Using the xml_to_csv.py I just add a little change, to consider the XML file that do not have any annotation (the background images) as so: From the original: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/xml_to_csv.py#L12-L22

I add:

value = None
for member in root.findall('object'):
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     member[0].text,
                     int(member[4][0].text),
                     int(member[4][1].text),
                     int(member[4][2].text),
                     int(member[4][3].text)
                     )
            xml_list.append(value)
        if value is None:
            value = (root.find('filename').text,
                     int(root.find('size')[0].text),
                     int(root.find('size')[1].text),
                     '-1',
                     '-1',
                     '-1',
                     '-1',
                     '-1'
                     )
            xml_list.append(value)

I'm just adding negative values to the coordinates of the bounding box if there is no in the XML file, which is the case for the background images, and it will be usefull when generating the TFRecords.

Third and Final Step - Generating the TFRecords

Now, when creating the TFRecords, if the the corresponding row/image has negative coordinates, i just add zero values to the record (before, this would not even be possible).

So from the original: https://github.com/datitran/raccoon_dataset/blob/93938849301895fb73909842ba04af9b602f677a/generate_tfrecord.py#L60-L66

I add:

for index, row in group.object.iterrows():
        if int(row['xmin']) > -1:
            xmins.append(row['xmin'] / width)
            xmaxs.append(row['xmax'] / width)
            ymins.append(row['ymin'] / height)
            ymaxs.append(row['ymax'] / height)
            classes_text.append(row['class'].encode('utf8'))
            classes.append(class_text_to_int(row['class']))
        else:
            xmins.append(0)
            xmaxs.append(0)
            ymins.append(0)
            ymaxs.append(0)
            classes_text.append('something'.encode('utf8'))  # this doe not matter for the background
            classes.append(5000)

To note that in the class_text (of the else statement), since for the background images there are no bounding boxes, you can replace the string with whatever you would like, for the background cases, this will not appear anywhere.

And lastly for the classes (of the else statement) you just need to add a number label that does not belong to neither of your own classes.

For those who are wondering, I've used this procedure many times, and currently works for my use cases.

Hope it helped in some way.

dinis-rodrigues avatar Jul 20 '20 20:07 dinis-rodrigues

@dinis-rodrigues
In that case, do we also need to change the "mscoco_label_map.pbtxt" to include the class value 5000 and also increase the number of classes by 1.

varunvv avatar Aug 08 '20 03:08 varunvv