<table class="fo-notebook-links" align="left">
    <td>
        <a target="_blank" href="https://colab.research.google.com/github/voxel51/fiftyone/blob/main/docs/source/recipes/adding_detections.ipynb">
            <img src="https://cdn.voxel51.com/colab-logo-256px.png"> &nbsp; Run in Google Colab
        </a>
    </td>
    <td>
        <a target="_blank" href="https://github.com/voxel51/fiftyone/blob/main/docs/source/recipes/adding_detections.ipynb">
            <img src="https://cdn.voxel51.com/github-logo-256px.png"> &nbsp; View source on GitHub
        </a>
    </td>
    <td>
        <a target="_blank" href="https://raw.githubusercontent.com/voxel51/fiftyone/main/docs/source/recipes/adding_detections.ipynb" download>
            <img src="https://cdn.voxel51.com/cloud-icon-256px.png"> &nbsp; Download notebook
        </a>
    </td>
</table>

# Adding Object Detections to a Dataset

This recipe provides a glimpse into the possibilities for integrating FiftyOne into your ML workflows. Specifically, it covers:

- Loading an object detection dataset from the [Dataset Zoo](https://voxel51.com/docs/fiftyone/user_guide/dataset_zoo/index.html)
- [Adding predictions](https://voxel51.com/docs/fiftyone/user_guide/using_datasets.html#object-detection) from an object detector to the dataset
- Launching the [FiftyOne App](https://voxel51.com/docs/fiftyone/user_guide/app.html) and visualizing/exploring your data
- Integrating the App into your data analysis workflow

## Setup

If you haven’t already, install FiftyOne:

In this tutorial, we’ll use an off-the-shelf [Faster R-CNN detection model](https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn) provided by PyTorch. To use it, you’ll need to install `torch` and `torchvision`, if necessary.

## Loading a detection dataset

In this recipe, we’ll work with the validation split of the [COCO dataset](https://cocodataset.org/#home), which is conveniently available for download via the [FiftyOne Dataset Zoo](https://voxel51.com/docs/fiftyone/user_guide/dataset_zoo/datasets.html#coco-2017).

The snippet below will download the validation split and load it into FiftyOne.

Let’s inspect the dataset to see what we downloaded:

Note that the ground truth detections are stored in the `ground_truth` field of the samples.

Before we go further, let’s launch the [FiftyOne App](https://voxel51.com/docs/fiftyone/user_guide/app.html) and use the GUI to explore the dataset visually:

## Adding model predictions

Now let’s add some predictions from an object detector to the dataset.

We’ll use an off-the-shelf [Faster R-CNN detection model](https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn) provided by PyTorch. The following cell downloads the model and loads it:

The code below performs inference with the model on a randomly chosen subset of 100 samples from the dataset and [stores the predictions](https://voxel51.com/docs/fiftyone/user_guide/using_datasets.html#object-detection) in a `predictions` field of the samples.

Let’s load `predictions_view` in the App to visualize the predictions that we added:

## Using the FiftyOne App

Now let’s use the App to analyze the predictions we’ve added to our dataset in more detail.

### Visualizing bounding boxes

Each field of the samples are shown as togglable checkboxes on the left sidebar which can be used to control whether ground truth or predicted boxes are rendered on the images.

You can also double-click on an image to view individual samples in more detail:

### Visualizing object patches

It can be beneficial to view every object as an individual sample, especially when there are multiple overlapping detections like in the image above.

In FiftyOne this is called a [patches view](https://voxel51.com/docs/fiftyone/user_guide/app.html#viewing-object-patches) and can be created through Python or directly in the App.

Let’s use the App to create the same view as above. To do so, we just need to click the [patches button](https://voxel51.com/docs/fiftyone/user_guide/app.html#viewing-object-patches) in the App and select `ground_truth`.

### Confidence thresholding in the App

From the App instance above, it looks like our detector is generating some spurious low-quality detections. Let’s use the App to interactively filter the predictions by `confidence` to identify a reasonable confidence threshold for our model:

### Confidence thresholding in Python

FiftyOne also provides the ability to [write expressions](https://voxel51.com/docs/fiftyone/user_guide/using_views.html#filtering) that match, filter, and sort detections based on their attributes. See [using DatasetViews](https://voxel51.com/docs/fiftyone/user_guide/using_views.html) for full details.

For example, we can programmatically generate a view that contains only detections whose `confidence` is at least `0.75` as follows:

Now let’s load our view in the App to view the predictions that we programmatically selected:

### Selecting samples of interest

You can select images in the App by clicking on them. Then, you can create a view that contains only those samples by opening the selected samples dropdown in the top left corner of the image grid and clicking `Only show selected`.
