sovit-123 / fasterrcnn-pytorch-training-pipeline

PyTorch Faster R-CNN Object Detection on Custom Dataset
MIT License
223 stars 75 forks source link

How to train faster_rcnn with hard-background? #45

Closed plantroots closed 1 year ago

plantroots commented 1 year ago

Hello! Does anyone have any idea how I can force feed the model images with potentially tricky objects so that it learns to avoid these biases? I was thinking of adding some background images in the training process (hand picked) with NO LABELS.

My question basically is: will they be tagged as background, and if so, will they end up in the loss function?

sovit-123 commented 1 year ago

Hi @plantroots For background images, you can just create an empty XML file. It will not be discarded and will be used an a background image.

plantroots commented 1 year ago

So basically, if I add an image as a background image, but it doesn't have an empty XML, it will be discarded, and if I add an image as background and it has an empty XML it will be automatically added to the background class?

And my final question is: Do I need to have _ background _ as the index 0 class in CLASSES=[] (config.py)?

sovit-123 commented 1 year ago

@plantroots Yes, you will need an empty XML file, or else it will be discarded. I know it's a bit of an inconvenience. When I wrote the dataset pipeline, I did not account for this and it's a bit complicated to change that now.

And yes, you do need __background__ as index 0 in config.py.

plantroots commented 1 year ago

Thank you sir! I really appreciate the prompt reply. I will give it a shot now.

plantroots commented 1 year ago

Just to check, would this classify as good empty XML file? example:

image

sovit-123 commented 1 year ago

Yes, I think this also works. I think the dataset.py does not find the bounding box attribute, then it's a background image. So, the above example should also work.

plantroots commented 1 year ago

I also have a version of the repo from https://debuggercafe.com/ from late November/early December 2022. Tell me if I'm wrong, but this background part was not implemented back then, was it?

sovit-123 commented 1 year ago

@plantroots No it was not. You will get the best results by using the code from this repo. There is one more thing. By default, the code in this repo applies mosaic augmentation as per the Ultralytics YOLO standard. You can turn off mosaic augmentation by using --no-mosaic flag in the training command. I recommend that you try both with and without mosaic augmentation.

The reason is that originally these models were no trained on mosaic images. So, smaller models sometimes tend to give bad results when mosaic augmentation is applied (but not always). This depends on the complexity of the dataset.

plantroots commented 1 year ago

Is this graph normal for the tensorboard logging (the lines that come from the origin)? image

I am experimenting with --no-mosaic since my dataset is quite small. Thank you for the tip.

sovit-123 commented 1 year ago

@plantroots No, this is not normal. I have not checked the tensorboard logging for quite some time now. I think its a bit buggy after a recent update. Please refer to the graphs that are stored on the local disk. I will correct the tensorboard logging in the next few days.

plantroots commented 1 year ago

Going back to the empty xml files: if an image has an empty associated xml file, then that entire image is tagged as background? Lets say if I have a 640 x 640 image, then it will be entirely tagged as background ?

sovit-123 commented 1 year ago

Yes.

plantroots commented 1 year ago

Thank you for all the replies. I hope these clarifications help others as well in inserting backgrounds! It really helps reduce the false positives!

sovit-123 commented 1 year ago

Glad that it helped. @plantroots In case the issue is solved for now, you may close the issue from your side.

plantroots commented 1 year ago

Adding backgrounds/hard-backgrounds is as easy as adding them into your training set and generating and empty .xml for each background image (with matching names of course) and they will end up in your loss function.