<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/merge_datasets.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/merge_datasets.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/merge_datasets.ipynb" download>
            <img src="https://cdn.voxel51.com/cloud-icon-256px.png"> &nbsp; Download notebook
        </a>
    </td>
</table>

# Merging Datasets

This recipe demonstrates a simple pattern for merging FiftyOne Datasets via [Dataset.merge_samples()](https://voxel51.com/docs/fiftyone/api/fiftyone.core.dataset.html?highlight=merge_samples#fiftyone.core.dataset.Dataset.merge_samples).

Merging datasets is an easy way to:

- Combine multiple datasets with information about the same underlying raw media (images and videos)
- Add model predictions to a FiftyOne dataset, to compare with ground truth annotations and/or other models

## Setup

If you haven’t already, install FiftyOne:

In this recipe, we’ll work with a dataset downloaded from the [FiftyOne Dataset Zoo](https://docs.voxel51.com/dataset_zoo/index.html).

To access the dataset, install `torch` and `torchvision`, if necessary:

Then download the test split of [CIFAR-10](https://www.cs.toronto.edu/~kriz/cifar.html):

## Merging model predictions

Load the test split of CIFAR-10 into FiftyOne:

The dataset contains ground truth labels in its `ground_truth` field:

Suppose you would like to add model predictions to some samples from the dataset.

The usual way to do this is to just iterate over the dataset and add your predictions directly to the samples:

However, suppose you store the predictions in a separate dataset:

You can easily merge the `predictions` dataset into the main dataset via [Dataset.merge_samples()](https://voxel51.com/docs/fiftyone/api/fiftyone.core.dataset.html?highlight=merge_samples#fiftyone.core.dataset.Dataset.merge_samples).

Let’s start by creating a fresh copy of CIFAR-10 that doesn’t have predictions:

Now let’s merge the predictions into the fresh dataset:

Let’s print a sample with predictions to verify that the merge happened as expected:

## Customizing the merge key

By default, samples with the same absolute `filepath` are merged. However, you can customize this as desired via various keyword arguments of [Dataset.merge_samples()](https://voxel51.com/docs/fiftyone/api/fiftyone.core.dataset.html?highlight=merge_samples#fiftyone.core.dataset.Dataset.merge_samples).

For example, the command below will merge samples with the same base filename, ignoring the directory:

Let’s print a sample with predictions to verify that the merge happened as expected:
