# Load a COCO format database from disk for DETR

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%3E%5C
**Category:** 🤗Datasets\
**Created:** [May 6, 2025, 12:13pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752 "2025-05-06T12:13:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Godouche](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/godouche/32/46990_2.png) [@Godouche](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGodouche)%5C
**Post date:** [May 6, 2025, 12:13pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%2F1 "2025-05-06T12:13:56Z")

</div>

I have a COCO database in my disk (with a JSON in the annotations folder that contains image directions) and I would like to load it in HF dataset in orther to use CV models.

Is there a function that allows that?

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [May 7, 2025, 1:56am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%2F2 "2025-05-07T01:56:39Z")

</div>

Hmm… This?

> <https://github.com/huggingface/datasets/issues/2526>
>
> \## Adding a Dataset
> \- \*\*Name:\*\* COCO
> \- \*\*Description:\*\* COCO is a large-scale …object detection, segmentation, and captioning dataset.
> \- \*\*Paper + website:\*\* https://cocodataset.org/#home
> \- \*\*Data:\*\* https://cocodataset.org/#download
> \- \*\*Motivation:\*\* It would be great to have COCO available in HuggingFace datasets, as we are moving beyond just text. COCO includes multi-modalities (images + text), as well as a huge amount of images annotated with objects, segmentation masks, keypoints etc., on which models like DETR (which I recently added to HuggingFace Transformers) are trained. Currently, one needs to download everything from the website and place it in a local folder, but it would be much easier if we can directly access it through the datasets API.
> 
> Instructions to add a new dataset can be found \[here\](https://github.com/huggingface/datasets/blob/master/ADD\_NEW\_DATASET.md).

---

<div class="post-metadata">

**Author:** ![lhoestq](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lhoestq/32/64813_2.png) [@lhoestq](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flhoestq)%5C
**Post date:** [May 7, 2025, 12:45pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%2F3 "2025-05-07T12:45:42Z")

</div>

> [@Godouche](#):
>
> I have a COCO database in my disk (with a JSON in the annotations folder that contains image directions) and I would like to load it in HF dataset in orther to use CV models.
> 
> Is there a function that allows that?

There is no COCO loader in the `datasets` library, but it would be a welcomed contribution in my opinion.

All the existing data modules are listed [here](https://github.com/huggingface/datasets/tree/main/src/datasets/packaged_modules)

---

<div class="post-metadata">

**Author:** ![Godouche](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/godouche/32/46990_2.png) [@Godouche](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FGodouche)%5C
**Post date:** [May 14, 2025, 12:48pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%2F4 "2025-05-14T12:48:46Z")

</div>

I wrote this code for loading COCO datasets in hugging face datasets that works with DETR,

Adaptations:

- features of your COCO JSON file
- path to COCO folder in local

```auto
import json
import os
import subprocess
from datasets import DatasetDict, Dataset, Features, Value, Sequence, ClassLabel, Image

# Ensure the datasets module is installed
subprocess.check_call(["pip", "install", "datasets"])

class CocoDatasetLoader:
    def __init__ (self, coco_folder):
        self.coco_folder = coco_folder

    def group_by_key_id(self, data, key_id, category_id_to_index):
        """
        Groups data by a specified key and maps category IDs to indices.
        
        Args:
            data (list): List of dictionaries containing the data.
            key_id (str): The key to group by.
            category_id_to_index (dict): Mapping from category IDs to indices.
            
        Returns:
            dict: Grouped data.
        """
        grouped_data = {}
        for item in data:
            key_value = item[key_id]
            if key_value not in grouped_data:
                grouped_data[key_value] = {k: [] for k in item.keys() if k != key_id}
            for k, v in item.items():
                if k != key_id:
                    grouped_data[key_value][k].append(v)
            grouped_data[key_value]['category'] = [category_id_to_index[x] for x in grouped_data[key_value]['category_id']]
        return grouped_data
    
    def load_coco_hf_dataset(self, split):
        """
        Loads COCO dataset and processes it into a format suitable for Hugging Face datasets.
        
        Args:
            split (str): Dataset split (e.g., 'Train', 'Test', 'Validation').
            
        Returns:
            Dataset: HuggingFace Dataset of the split of COCO dataset.
        """
        # Load the JSON file
        json_file_path = os.path.join(self.coco_folder, f'annotations/instances_{split}.json')
        try:
            with open(json_file_path, 'r') as f:
                coco_data = json.load(f)
        except FileNotFoundError:
            print(f"File not found: {json_file_path}")
            return []

        # Extract category names and create a mapping from category IDs to indices
        category_names = [cat['name'] for cat in coco_data['categories']]
        category_id_to_index = {cat['id']: idx for idx, cat in enumerate(coco_data['categories'])}

        # Group annotations by 'image_id'
        grouped_annotations = self.group_by_key_id(coco_data['annotations'], 'image_id', category_id_to_index)

        # Create a dictionary of images
        grouped_images = {item['id']: item for item in coco_data['images']}

        # Initialize 'objects' field in grouped_images
        annotations_keys = list(grouped_annotations.values())[0].keys()
        for k, v in grouped_images.items():
            grouped_images[k]['objects'] = {key: [] for key in annotations_keys}

        # Populate 'objects' field with annotations
        for k, v in grouped_annotations.items():
            grouped_images[k]['objects'] = v

        # Add image paths and IDs
        for k, v in grouped_images.items():
            v['image'] = os.path.join(self.coco_folder, 'images', split, v['file_name'])
            v['image_id'] = v['id']

        # Create a Hugging Face dataset from the custom data using from_list for efficiency
        hf_dataset = Dataset.from_list(list(grouped_images.values()))

        # Define the features for the main dataset
        features = Features({
            'id': Value('int64'),
            'image_id': Value('int64'),
            'image': Image(),
            'file_name': Value('string'),
            'license': Value('string'),
            'flickr_url': Value('string'),
            'coco_url': Value('string'),
            'date_captured': Value('string'),
            'width': Value('int64'),
            'height': Value('int64'),
            'objects': Sequence({
                'id': Value('int64'),
                'area': Value('float32'),
                'bbox': Sequence(Value('float32')),
                'category': ClassLabel(names=category_names),
                'attributes': {'occluded': Value('bool')},
                'category_id': Value('int64'),
                'iscrowd': Value('int64'),
                'segmentation': {
                    'counts': Sequence(Value('int64')),
                    'size': Sequence(Value('int64'))
                }
            })
        })

        # Cast the features for the Hugging Face dataset
        hf_dataset = hf_dataset.cast(features)

        return hf_dataset

# Initialize the CocoDatasetLoader class
coco_loader = CocoDatasetLoader('/path/to/coco/folder/')

hf_dataset_dict = DatasetDict()
for split in ['Train', 'Test', 'Validation']:
    # Load the COCO dataset for each split
    hf_dataset = coco_loader.load_coco_hf_dataset(split)
    
    # Print the dataset
    print(f"Dataset for {split} split:")
    print(hf_dataset)
    
    # Create a DatasetDict with the split
    hf_dataset_dict[split.lower()] = hf_dataset

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [May 15, 2025, 12:48am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fload-a-coco-format-database-from-disk-for-detr%2F153752%2F5 "2025-05-15T00:48:58Z")

</div>

Closed on May 15, 2025, 12:48am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
