Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:NVIDIA DALI YOLOv4 COCO

From Leeroopedia


Knowledge Sources
Domains Object_Detection, TensorFlow
Last Updated 2026-02-08 16:00 GMT

Overview

Provides a COCO dataset API implementation for loading, parsing, querying, and visualizing COCO-format annotations, used within the YOLOv4 training pipeline.

Description

This module implements the `COCO` class, a Python API for working with the Microsoft COCO dataset format. It is a customized version of the official pycocotools COCO API (v2.0, originally by Piotr Dollar and Tsung-Yi Lin) adapted for use within the YOLOv4 detection example. The class handles loading COCO JSON annotation files, building efficient lookup indices, and providing query interfaces for annotations, categories, and images.

Upon initialization with an annotation file path, the class parses the JSON structure and builds several index dictionaries: `anns` (annotation ID to annotation), `imgs` (image ID to image info), `cats` (category ID to category info), `imgToAnns` (image ID to list of annotations), and `catToImgs` (category ID to list of image IDs). These indices enable efficient filtering and retrieval operations.

The class provides comprehensive query methods: `getAnnIds()` filters annotations by image IDs, category IDs, area range, and crowd status; `getCatIds()` filters categories by name, supercategory, or ID; `getImgIds()` retrieves image IDs optionally filtered by category; `loadAnns()`, `loadCats()`, `loadImgs()` load specific entries by ID. Visualization is supported through `showAnns()` which renders annotations as colored polygons on matplotlib axes. The `loadRes()` method loads algorithm result files and creates a new COCO instance for evaluation comparison, supporting both bounding box and segmentation results. The `annToMask()` and related methods convert segmentation annotations to binary masks.

Usage

Use this class within the YOLOv4 pipeline for loading COCO dataset annotations, querying image and annotation metadata, and performing evaluation. It is used during dataset preparation and mAP computation.

Code Reference

Source Location

Signature

class COCO:
    def __init__(self, annotation_file=None): ...
    def createIndex(self): ...
    def info(self): ...
    def getAnnIds(self, imgIds=[], catIds=[], areaRng=[], iscrowd=None) -> list: ...
    def getCatIds(self, catNms=[], supNms=[], catIds=[]) -> list: ...
    def getImgIds(self, imgIds=[], catIds=[]) -> list: ...
    def loadAnns(self, ids=[]) -> list: ...
    def loadCats(self, ids=[]) -> list: ...
    def loadImgs(self, ids=[]) -> list: ...
    def showAnns(self, anns): ...
    def loadRes(self, resFile) -> 'COCO': ...
    def download(self, tarDir=None, imgIds=[]): ...
    def loadNumpyAnnotations(self, data) -> list: ...
    def annToRLE(self, ann) -> dict: ...
    def annToMask(self, ann) -> np.ndarray: ...

Import

from src.np.coco import COCO

coco = COCO(annotation_file="/data/coco/annotations/instances_val2017.json")

I/O Contract

Inputs

Name Type Required Description
annotation_file str No Path to COCO-format JSON annotation file
imgIds list[int] No Image IDs to filter by (for query methods)
catIds list[int] No Category IDs to filter by (for query methods)
areaRng list[float] No Area range [min, max] to filter annotations
iscrowd bool No Filter by crowd annotation flag
catNms list[str] No Category names to filter by
supNms list[str] No Supercategory names to filter by
resFile str No Path to algorithm results file for loadRes()

Outputs

Name Type Description
ann_ids list[int] Filtered annotation IDs
cat_ids list[int] Filtered category IDs
img_ids list[int] Filtered image IDs
annotations list[dict] Loaded annotation dictionaries
categories list[dict] Loaded category dictionaries
images list[dict] Loaded image info dictionaries
mask np.ndarray Binary mask from segmentation annotation

Usage Examples

Query COCO Annotations

from src.np.coco import COCO

# Load annotations
coco = COCO("/data/coco/annotations/instances_val2017.json")

# Get all image IDs containing 'person' category
person_cat_ids = coco.getCatIds(catNms=['person'])
person_img_ids = coco.getImgIds(catIds=person_cat_ids)

# Load annotations for a specific image
ann_ids = coco.getAnnIds(imgIds=[person_img_ids[0]])
annotations = coco.loadAnns(ann_ids)

# Get all categories
categories = coco.loadCats(coco.getCatIds())
print([cat['name'] for cat in categories])

# Load algorithm results for evaluation
coco_results = coco.loadRes("detection_results.json")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment