# Uploading COCO

## Coco format

[Coco](http://cocodataset.org/#home) (Common Objects in Context) is a popular public dataset with its own label format. This label format combines all labels for the dataset into one common json file for each label task. As of 2019 COCO has focused on the tasks of Image Captioning, Object Detection (bbox and polygon), Keypoint Detection, Stuff (semantic segmentation of common background objects), Panoptic (advanced semantic segmentation).

## How to use our coco importer

#### Prepare instances json file

The instances json file contains 5 main entries, 3 out of which contain the relevant information for a label upload (images, annotations, categories).  The images list contains all images of the dataset with their metadata. The categories entry contains all categories with their relevant subcategories. The annotations' entry is the most important part as it lists all geometry labels (bbox and polygon) for the images. Use our coco class with its **add\_object\_detection\_data** method to prepare the instances json for upload. When calling this function you can choose to either prepare the bbox annotation or the contained polygon for upload.&#x20;

```python
from datagym.importers.coco import Coco
coco = Coco()

with open(<PATH_TO_instances_YEAR.json>) as json_file:
    instances_data = json.load(json_file)

# Here we chose to prepare the polygons.
coco.add_object_detection_data(instances_data, polygon=True)
```

#### Prepare captions json file

The captions json file has a similar layout to the instances json file. Image captions give a quick description of what can be seen on the image. In order to prepare your captions json file for upload you need to use the add\_captions\_data method of the coco class.

```python
from datagym.importers.coco import Coco
coco = Coco()

with open(<PATH_TO_captions_YEAR.json>) as json_file:
    captions_data = json.load(json_file)

# Here we chose to prepare the polygons.
coco.add_captions_data(captions_data)
```

#### Prepare your datagym project

Before you use the datagym coco importer make sure to upload the images to a datagym dataset with the same image name as in the coco json files. Within your python code you then generate an image\_ids\_dict so that the internal image id is connected to the external image name.&#x20;

```python
image_ids_dict = dict()

for image in dataset.images:
    image_ids_dict[image.image_name] = image.id
    
print(f'One example from the image_ids_dict:\n {{"{image.image_name}" : "{image_ids_dict[image.image_name]}"}}')
```

Additionally the label configuration needs to be manually configured to contain all the relevant entries. In this example we wanted to upload the instances\_val2017.json and captions\_val2017.json to datagym and set up the following label configuration. Inside the label configuration it's important that the sub categories for each bounding box/polygon are named in the following way:  "\<category\_name>\_type"

![Label configuration for instances\_val2017.json and captions\_val2017.json ](/files/-M5Wl_uKtcI-aTGSxxkP)

#### Upload to datagym

Once the relevant coco labels are prepared for upload and the datagym project is prepared as described above, the labels are ready to be uploaded:

```python
upload_dict = coco.get_datagym_label_dict(image_ids_dict)

project = client.get_project_by_name(project_name=PROJECT_NAME)

errors = client.import_label_data(project_id=project.id, label_data=upload_dict)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datagym.ai/documentation/python-api/uploading-coco.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
