<table class="fo-notebook-links" align="left">
    <td>
        <a target="_blank" href="https://colab.research.google.com/github/voxel51/fiftyone/blob/main/docs/source/tutorials/uniqueness.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/tutorials/uniqueness.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/tutorials/uniqueness.ipynb" download>
            <img src="https://cdn.voxel51.com/cloud-icon-256px.png"> &nbsp; Download notebook
        </a>
    </td>
</table>

# Exploring Image Uniqueness with FiftyOne

During model training, the best results will be seen when training on *unique data samples*. For example, finding and removing similar samples in your dataset can avoid accidental concept imbalance that can bias the learning of your model. Or, if duplicate or near-duplicate data is present in both training and validation/test splits, evaluation results may not be reliable. Just to name a few.

In this tutorial, we explore how FiftyOne’s image uniqueness tool can be used to analyze and extract insights from raw (unlabeled) datasets.

We’ll cover the following concepts:

- Loading a dataset from the [FiftyOne Dataset Zoo](https://voxel51.com/docs/fiftyone/user_guide/dataset_zoo/index.html)
- Applying FiftyOne’s [uniqueness method](https://voxel51.com/docs/fiftyone/user_guide/brain.html#image-uniqueness) to your dataset
- Launching the [FiftyOne App](https://voxel51.com/docs/fiftyone/user_guide/app.html) and visualizing/exploring your data
- Identifying duplicate and near-duplicate images in your dataset
- Identifying the most unique/representative images in your dataset

**So, what’s the takeaway?**

This tutorial shows how FiftyOne can automatically find and remove near-duplicate images in your datasets and recommend the most unique samples in your data, enabling you to start your model training off right with a high-quality bootstrapped training set.

## Setup

If you haven’t already, install FiftyOne:

This tutorial requires either [Torchvision Datasets](https://pytorch.org/docs/stable/torchvision/datasets.html) or [TensorFlow Datasets](https://www.tensorflow.org/datasets) to download the CIFAR-10 dataset used below.

You can, for example, install PyTorch as follows:

## Part 1: Finding duplicate and near-duplicate images

A common problem in dataset creation is duplicated data. Although this could be found using file hashing—as in the [image_deduplication](https://colab.research.google.com/github/voxel51/fiftyone-examples/blob/master/examples/image_deduplication.ipynb) walkthrough—it is less possible when small manipulations have occurred in the data. Even more critical for workflows involving model training is the need to get as much power out of each data samples as possible; near-duplicates, which are
samples that are exceptionally similar to one another, are intrinsically less valuable for the training scenario. Let’s see if we can find such duplicates and near-duplicates in a common dataset: CIFAR-10.

### Load the dataset

Open a Python shell to begin. We will use the CIFAR-10 dataset, which is available in the [FiftyOne Dataset Zoo](https://voxel51.com/docs/fiftyone/user_guide/dataset_zoo/datasets.html#dataset-zoo-cifar10):

The dataset contains the ground truth labels in a `ground_truth` field:

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

### Compute uniqueness

Now we can process the entire dataset for uniqueness. This is a fairly expensive operation, but should finish in a few minutes at most. We are processing through all samples in the dataset, then building a representation that relates the samples to each other. Finally, we analyze this representation to output uniqueness scores for each sample.

The above method populates a `uniqueness` field on each sample that contains the sample’s uniqueness score. Let’s confirm this by printing some information about the dataset:

### Visualize to find duplicate and near-duplicate images

Now, let’s visually inspect the least unique images in the dataset to see if our dataset has any issues:

You will easily see some near-duplicates in the App. It surprised us that there are duplicates in CIFAR-10, too!

Of course, in this scenario, near duplicates are identified from visual inspection. So, how do we get the information out of FiftyOne and back into your working environment. Easy! The `session` variable provides a bidirectional bridge between the App and your Python environment. In this case, we will use the `session.selected` bridge. So, in the App, select some of the duplicates and near-duplicates. Then, execute the following code in the Python shell.

And the App will only show these samples now. We can, of course access the filepaths and other information about these samples programmatically so you can act on the findings. But, let’s do that at the end of Part 2 below!

## Part 2: Bootstrapping a dataset of unique samples

When building a dataset, it is important to create a diverse dataset with unique and representative samples. Here, we explore FiftyOne’s ability to help identify the most unique samples in a raw dataset.

### Download some images

This walkthrough will process a directory of images and compute their uniqueness. The first thing we need to do is get some images. Let’s get some images from Flickr, to keep this interesting!

You need a Flickr API key to do this. If you already have a Flickr API key, then skip the next steps.

1. Go to [https://www.flickr.com/services/apps/create/](https://www.flickr.com/services/apps/create/)
2. Click on Request API Key. ([https://www.flickr.com/services/apps/create/apply/](https://www.flickr.com/services/apps/create/apply/)) You will need to login (create account if needed, free).
3. Click on “Non-Commercial API Key” (this is just for a test usage) and fill in the information on the next page. You do not need to be very descriptive; your API will automatically appear on the following page.
4. Install the Flickr API:

You will also need to enable ETA’s storage support to run this script, if you haven’t yet:

Next, let’s download three sets of images to process together. I suggest using three distinct object-nouns like “badger”, “wolverine”, and “kitten”. For the actual downloading, we will use the provided [query_flickr.py](https://raw.githubusercontent.com/voxel51/fiftyone/main/docs/source/tutorials/query_flickr.py) script:

The rest of this walkthrough assumes you’ve downloaded some images to your local `.data/` directory.

### Load the data into FiftyOne

Let’s now work through getting this data into FiftyOne and working with it.

The above command uses a [factory method](https://voxel51.com/docs/fiftyone/api/fiftyone.core.dataset.html?highlight=from_images_dir#fiftyone.core.dataset.Dataset.from_images_dir) on the `Dataset` class to traverse a directory of images (including subdirectories) and generate a dataset instance in FiftyOne containing those images.

Note that the images are not loaded from disk, so this operation is fast. The first argument is the path to the directory of images on disk, and the third is a name for the dataset.

With the dataset loaded into FiftyOne, we can easily launch the App and visualize it:

Refer to the [User Guide](https://voxel51.com/docs/fiftyone/user_guide/index.html) for more useful things you can do with the dataset and App.

### Compute uniqueness and analyze

Now, let’s analyze the data. For example, we may want to understand what are the most unique images among the data as they may inform or harm model training; we may want to discover duplicates or redundant samples.

Continuing in the same Python shell, let’s compute and visualize uniqueness.

Now, just visualizing the samples is interesting, but we want more. We want to get the most unique samples from our dataset so that we can use them in our work. Let’s do just that. In the same Python session, execute the following code.

Alternatively, you can simply tag the most unique samples and persist the dataset so you can return to it later in FiftyOne.
