Giter Club home page Giter Club logo

probdet's Introduction

Probabilistic Detectron2

This repository contains the official implementation of Estimating and Evaluating Regression Predictive Uncertainty in Deep Object Detectors.

This code extends the detectron2 framework to estimate bounding box covariance matrices, and is meant to be a starter kit for entering the domain of probabilistic object detection.

Disclaimer

This research code was produced by one person with a single set of eyes, it may contain bugs and errors that I did not notice by the time of release.

Updates

Date Change
30-September-2021 Added pip frozen requirements (requirements_pip_freeze.txt).
10-October-2021 Added ability to perform inference on images without passing through specific dataset handlers.

Requirements

Software Support:

Name Supported Versions
Ubuntu 20.04
Python 3.8
CUDA 11.0+
Cudnn 8.0.1+
PyTorch 1.8+

To install requirements choose between a python virtualenv or build a docker image using the provided Dockerfile.

# Clone repo
git clone https://github.com/asharakeh/probdet.git
cd probdet
git submodule update --init --recursive
  1. Virtual Environment Creation:
# Create python virtual env
mkvirtualenv probdet

# Add library path to virtual env
add2virtualenv src

# Install requirements
cat requirements.txt | xargs -n 1 -L 1 pip install
  1. Docker Image
# Clone repo
git clone https://github.com/asharakeh/probdet.git
cd probdet/Docker

# Build docker image
sh build.sh 

Datasets

COCO Dataset

Download the COCO Object Detection Dataset here. The COCO dataset folder should have the following structure:

 └── COCO_DATASET_ROOT
     |
     ├── annotations
     ├── train2017
     └── val2017

To create the corrupted datasets using Imagenet-C corruptions, run the following code:

python src/core/datasets/generate_coco_corrupted_dataset.py --dataset-dir=COCO_DATASET_ROOT

OpenImages Datasets

Download our OpenImages validation splits here. We created a tarball that contains both shifted and out-of-distribution data splits used in our paper to make our repo easier to use. Do not modify or rename the internal folders as those paths are hard coded in the dataset reader. We will refer to the root folder extracted from the tarball as OPENIM_DATASET_ROOT.

Training

To train the models in the paper, use this command:

python src/train_net.py
--num-gpus xx
--dataset-dir COCO_DATASET_ROOT
--config-file COCO-Detection/architecture_name/config_name.yaml
--random-seed xx
--resume

For an explanation of all command line arguments, use python src/train_net.py -h

Evaluation

To run model inference after training, use this command:

python src/apply_net.py 
--dataset-dir TEST_DATASET_ROOT 
--test-dataset test_dataset_name 
--config-file path/to/config.yaml 
--inference-config /path/to/inference/config.yaml 
--random-seed xx
--image-corruption-level xx

For an explanation of all command line arguments, use python src/apply_net.py -h

--image-corruption-level can vary between 0-5, with 0 being the original COCO dataset with no corruption. In addition, --image-corruption-level has no effect when used with OpenImages dataset splits.

--test-dataset can be one of coco_2017_custom_val, openimages_val, or openimages_ood_val. --dataset-dir corresponds to the root directory of the dataset used. Evaluation code will run inference on the test dataset and then will generate mAP, Negative Log Likelihood, Brier Score, Energy Score, and Calibration Error results. If only evaluation of metrics is required, add --eval-only to the above code snippet.

Inference on new images

We provide a script to perform inference on new images without passing through dataset handlers.

python single_image_inference.py 
--image-dir /path/to/image/dir
--output-dir /path/to/output/dir
--config-file /path/to/config/file 
--inference-config /path/to/inference/config 
--model-ckpt /path/to/model.pth

image-dir is a folder containing all images to be used for inference. output-dir is a folder to write the output json file containing probabilistic detections. model-ckpt is the path to the model checkpoint to be used for inference. Look below to download model checkpoints.

Configurations in the paper

We provide a list of config combinations that generate the architectures used in our paper:

Method Name Config File Inference Config File Model
Deterministic RetinaNet retinanet_R_50_FPN_3x.yaml standard_nms.yaml retinanet_R_50_FPN_3x.pth
RetinaNet NLL retinanet_R_50_FPN_3x_reg_var_nll.yaml standard_nms.yaml retinanet_R_50_FPN_3x_reg_var_nll.pth
RetinaNet DMM retinanet_R_50_FPN_3x_reg_var_dmm.yaml standard_nms.yaml retinanet_R_50_FPN_3x_reg_var_dmm.pth
RetinaNet ES retinanet_R_50_FPN_3x_reg_var_es.yaml standard_nms.yaml retinanet_R_50_FPN_3x_reg_var_es.pth
--- --- --- ---
Deterministic FasterRCNN faster_rcnn_R_50_FPN_3x.yaml standard_nms.yaml faster_rcnn_R_50_FPN_3x.pth
FasterRCNN NLL faster_rcnn_R_50_FPN_3x_reg_covar_nll.yaml standard_nms.yaml faster_rcnn_R_50_FPN_3x_reg_covar_nll.pth
FasterRCNN DMM faster_rcnn_R_50_FPN_3x_reg_var_dmm.yaml standard_nms.yaml faster_rcnn_R_50_FPN_3x_reg_var_dmm.pth
FasterRCNN ES faster_rcnn_R_50_FPN_3x_reg_var_es.yaml standard_nms.yaml faster_rcnn_R_50_FPN_3x_reg_var_es.pth
--- --- --- ---
Deterministic DETR detr_R_50.yaml standard_nms.yaml detr_R_50.pth
DETR NLL detr_R_50_reg_var_nll.yaml standard_nms.yaml detr_R_50_reg_var_nll.pth
DETR DMM detr_R_50_reg_var_dmm.yaml standard_nms.yaml detr_R_50_reg_var_dmm.pth
DETR ES detr_R_50_reg_var_es.yaml standard_nms.yaml detr_R_50_reg_var_es.pth

Experiments in the paper were performed on 5 models trained and evaluated using random seeds [0, 1000, 2000, 3000, 4000]. The variance in performance between different seeds was seen to be negligible, and the results of the top performing seed were reported.

Additional Configurations

The repo supports many more variants including dropout and ensemble methods for estimating epistemic uncertainty. We provide a list of config combinations that generate the architectures used in our paper:

Method Name Config File Inference Config File
RetinaNet Classification Loss Attenuation retinanet_R_50_FPN_3x_cls_la.yaml standard_nms.yaml
RetinaNet Dropout Post-NMS Uncertainty Computation retinanet_R_50_FPN_3x_dropout.yaml mc_dropout_ensembles_post_nms_mixture_of_gaussians.yaml
RetinaNet Dropout Pre-NMS Uncertainty Computation retinanet_R_50_FPN_3x_dropout.yaml mc_dropout_ensembles_pre_nms.yaml
RetinaNet BayesOD with NLL loss retinanet_R_50_FPN_3x_reg_var_nll.yaml bayes_od.yaml
RetinaNet BayesOD with ES loss retinanet_R_50_FPN_3x_reg_var_es.yaml bayes_od.yaml
RetinaNet BayesOD with ES loss and Dropout retinanet_R_50_FPN_3x_reg_var_es_dropout.yaml bayes_od_mc_dropout.yaml
RetinaNet Ensembles Post-NMS Uncertainty Estimation with NLL loss retinanet_R_50_FPN_3x_reg_var_nll.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml
RetinaNet Ensembles Pre-NMS Uncertainty Estimation with NLL loss retinanet_R_50_FPN_3x_reg_var_nll.yaml (Need to train 5 Models with different random seeds) ensembles_pre_nms.yaml
RetinaNet Ensembles Post-NMS Uncertainty Estimation with ES loss retinanet_R_50_FPN_3x_reg_var_es.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml
RetinaNet Ensembles Pre-NMS Uncertainty Estimation with ES loss retinanet_R_50_FPN_3x_reg_var_es.yaml (Need to train 5 Models with different random seeds) ensembles_pre_nms.yaml
--- --- ---
FasterRCNN Classification Loss Attenuation faster_rcnn_R_50_FPN_3x_cls_la.yaml standard_nms.yaml
FasterRCNN Dropout Post-NMS Uncertainty Computation faster_rcnn_R_50_FPN_3x_dropout.yaml mc_dropout_ensembles_post_nms_mixture_of_gaussians.yaml
FasterRCNN Dropout Pre-NMS Uncertainty Computation faster_rcnn_R_50_FPN_3x_dropout.yaml mc_dropout_ensembles_pre_nms.yaml
FasterRCNN BayesOD with NLL loss faster_rcnn_R_50_FPN_3x_reg_var_nll.yaml bayes_od.yaml
FasterRCNN BayesOD with ES loss faster_rcnn_R_50_FPN_3x_reg_var_es.yaml bayes_od.yaml
FasterRCNN BayesOD with ES loss and Dropout retinanet_R_50_FPN_3x_reg_var_es_dropout.yaml bayes_od_mc_dropout.yaml
FasterRCNN Ensembles Post-NMS Uncertainty Estimation with NLL loss faster_rcnn_R_50_FPN_3x_reg_var_nll.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml
FasterRCNN Ensembles Pre-NMS Uncertainty Estimation with NLL loss faster_rcnn_R_50_FPN_3x_reg_var_nll.yaml (Need to train 5 Models with different random seeds) ensembles_pre_nms.yaml
FasterRCNN Ensembles Post-NMS Uncertainty Estimation with ES loss faster_rcnn_R_50_FPN_3x_reg_var_es.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml
FasterRCNN Ensembles Pre-NMS Uncertainty Estimation with ES loss faster_rcnn_R_50_FPN_3x_reg_var_es.yaml (Need to train 5 Models with different random seeds) ensembles_pre_nms.yaml
--- --- ---
DETR Classification Loss Attenuation detr_R_50_cls_la.yaml standard_nms.yaml
DETR Dropout detr_R_50.yaml (dropout is included in original implementation of DETR) mc_dropout_ensembles_post_nms_mixture_of_gaussians.yaml
DETR Ensembles with NLL loss detr_R_50_reg_var_nll.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml
DETR Ensembles with ES loss detr_R_50_reg_var_es.yaml (Need to train 5 Models with different random seeds) ensembles_post_nms_mixture_of_gaussians.yaml

DETR has no NMS post-processing, and as such does not support BayesOD NMS replacement. The repo also supports many additional lower performing configurations. I will continue developing it and add additional configurations when time allows.

Citation

If you use this code, please cite our paper:

@inproceedings{
harakeh2021estimating,
title={Estimating and Evaluating Regression Predictive Uncertainty in Deep Object Detectors},
author={Ali Harakeh and Steven L. Waslander},
booktitle={International Conference on Learning Representations},
year={2021},
url={https://openreview.net/forum?id=YLewtnvKgR7}
}

License

This code is released under the Apache 2.0 License.

probdet's People

Contributors

asharakeh avatar jskhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

probdet's Issues

Inconsistent results using your checkpoint

Hi, @asharakeh , thanks for your implementation, actually I tried to reproduce the results of the faster-rcnn with energy score for inference, and I get inconsistent results with table.1 about the number of TP, local errors, and duplicates. The calibration error is also different, please see the attached figure for my results. I tested the faster_rcnn_R_50_FPN_3x_reg_var_es.pth on COCO validation set.
attach

And I use this command line to run the code.
python src/apply_net.py --dataset-dir /nobackup-slow/dataset/my_xfdu/coco2017 --test-dataset coco_2017_custom_val --config-file COCO-Detection/faster-rcnn/faster_rcnn_R_50_FPN_3x_reg_var_es.yaml --inference-config Inference/standard_nms.yaml --random-seed 0 --image-corruption-level 0

Are there any mistakes I make?

Thanks,

Implementation of DETR-NLL

Hallo @asharakeh, first of all, thank you for your wonderful work. I am very interested in your work and also have some questions about the implementation of DETR.

  1. Can you explain why the predicted covariance of the bounding box for DETR-NLL detection is too large?
  2. What is the meaning of the digit in transform_mat in probabilistic_detr_predictor.py.

Thank you!

AttributeError: 'ProbabilisticRetinaNet' object has no attribute 'in_features'

I used the evaluation command to run model inference after training, but there was an error happened.

the command is:
python src/apply_net.py --dataset-dir COCO_DATASET_ROOT --test-dataset coco_2017_custom_val --config-file COCO-Detection/retinanet/retinanet_R_50_FPN_3x_reg_var_es_dropout.yaml --inference-config Inference/bayes_od_mc_dropout.yaml --random-seed 0 --image-corruption-level 0

and the error is:
Traceback (most recent call last):
File "src/apply_net.py", line 112, in
launch(
File "/root/miniconda3/envs/probdet/lib/python3.8/site-packages/detectron2/engine/launch.py", line 84, in launch
main_func(*args)
File "src/apply_net.py", line 81, in main
outputs = predictor(input_im)
File "/root/autodl-tmp/mzy/Git/probdet/src/probabilistic_inference/inference_core.py", line 78, in call
results = self.post_processing_bayes_od(input_im)
File "/root/autodl-tmp/mzy/Git/probdet/src/probabilistic_inference/probabilistic_retinanet_predictor.py", line 400, in post_processing_bayes_od
outputs = self.retinanet_probabilistic_inference(input_im)
File "/root/autodl-tmp/mzy/Git/probdet/src/probabilistic_inference/probabilistic_retinanet_predictor.py", line 53, in retinanet_probabilistic_inference
n_fms = len(self.model.in_features)
File "/root/miniconda3/envs/probdet/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1695, in getattr
raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")
AttributeError: 'ProbabilisticRetinaNet' object has no attribute 'in_features'

A doubt regarding estimating the confidence over class prediction

Hi,

Thanks for a very insightful paper titled "Estimating and Evaluating Regression Predictive Uncertainty in Deep Object Detectors".

I am planning to use it for assess the reliability of the prediction. I have a doubt regarding the inference stage. Suppose on a test image I run the predictive inference. Assume that I obtain a box which is classified as 'car' with a categorical probability .9. In your paper I could not see any direct way of finding how sure the model on this classification. Variance is estimated only for ground box co-ordinates. Do you compute the confidence on this prediction based on the determinant of covariance matrix of predicted bounding box?

Thanks for your time.

Implementation of Energy score

Hi,

This is a great work. I am working on a very similar problem and would like to feature this as a baseline. Would it be possible to get a pointer for implementation of energy score that was presented in section 3.4 in the paper.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.