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

# Speed Up FiftyOneTorchDataset with Vectorize Mode

This recipe shows how to eliminate database query overhead during training by enabling vectorize mode in [FiftyOneTorchDataset](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.FiftyOneTorchDataset). With `vectorize=True`, all required fields are preloaded into memory before training begins, removing per-sample database lookups from the hot path. Specifically, it covers:

- Enabling vectorize mode with `vectorize=True` and a [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem) subclass
- Understanding how vectorize mode changes the input to your `__call__` method
- Writing a [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem) that works with preloaded field dicts for efficient training

**API references:** [FiftyOneTorchDataset](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.FiftyOneTorchDataset) · [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem)

## Setup

If you haven’t already, install FiftyOne:

In this tutorial, we’ll use [PyTorch](https://pytorch.org/) for working with tensors and inspecting sample data. To follow along, you’ll need to install `torch` and `torchvision`, if necessary:

## Import Libraries

This recipe requires a helper file, `utils.py`, which contains reusable functions for building `get_item` methods, creating dataloaders, and setting up models. The following cell downloads it into your working directory so it can be imported directly.

## Vectorize Mode

By default, [FiftyOneTorchDataset](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.FiftyOneTorchDataset) queries the backing MongoDB database on every `__getitem__` call. For large training runs this per-sample overhead adds up quickly.

Passing `vectorize=True` to `.to_torch()` switches to vectorize mode: all fields declared in your [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem) are serialized into memory up front, so sample retrieval during training is a simple dict lookup with no database I/O. This delivers a significant speedup when data loading is the bottleneck.

```python
# Wrap your function with SimpleGetItem to declare which fields to preload
get_item_wrapper = SimpleGetItem(my_get_item_fn, ["id", "filepath", "my_field"])

# vectorize=True preloads all declared fields into memory before training begins
torch_dataset = view.to_torch(get_item_wrapper, vectorize=True)
```

> **Note:** With `vectorize=True`, all fields you declare in your [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem) are preloaded into memory before training begins — no additional configuration is needed beyond the field list you already provide.

## Load Dataset

## Writing a GetItem for Vectorize Mode

In vectorize mode, your `GetItem.__call__` receives a `dict` of preloaded field values rather than a live `fiftyone.core.sample.Sample`. The keys match the field names you declared in your [GetItem](https://docs.voxel51.com/api/fiftyone.utils.torch.html#fiftyone.utils.torch.GetItem). Here’s an example that loads detections from the quickstart dataset:

## Visualizing the result

Run the cell below a few times to inspect different samples from the dataset:

The dataset works with a standard `DataLoader` for use in a training loop:
