Skip to main content

Convert COCO dataset to YOLO format in Kaggle environment

Project description

COCO2YOLO Kaggle

A tool for converting COCO dataset to YOLO format in Kaggle environment. This tool is based on ultralytics/JSON2YOLO and optimized for Kaggle environment.

Features

  • Automatically convert COCO JSON annotations to YOLO format
  • Intelligently handle Kaggle storage space limitations
  • Support parallel file operations for faster processing
  • Support segment annotations
  • Flexible commands for different operations
  • Customizable directory structure

Installation

pip install coco2yolo-kaggle

Usage

Python API

Label Conversion Only

from coco2yolo_kaggle import convert_coco_labels

# Convert labels only
labels_dir = convert_coco_labels(
    json_dir="/kaggle/input/coco-2017-dataset/coco2017/annotations",
    output_dir="/kaggle/working/coco_yolo",
    use_segments=True,
    cls91to80=True  # Map 91 COCO classes to 80 classes (default: True)
)

File Copy Only (with Custom Directory Names)

from coco2yolo_kaggle import copy_dataset

# Copy dataset files to final destination
dataset_path = copy_dataset(
    json_dir="/kaggle/input/coco-2017-dataset/coco2017/annotations",
    output_dir="/kaggle/working/coco_yolo",
    final_dest="/kaggle/tmp/COCO2017",
    max_workers=8,
    train_dir_name="train",  # Custom train directory name (default: "train2017")
    val_dir_name="val"       # Custom validation directory name (default: "val2017")
)

Complete Conversion Process

from coco2yolo_kaggle import convert_coco_dataset

# Complete process with custom directory names
dataset_path = convert_coco_dataset(
    json_dir="/kaggle/input/coco-2017-dataset/coco2017/annotations",
    output_dir="/kaggle/working/coco_yolo",
    final_dest="/kaggle/tmp/COCO2017",
    use_segments=True,
    cls91to80=True,  # Map 91 COCO classes to 80 classes (default: True)
    max_workers=8,
    copy_files=True,  # Set to False to skip copying files
    train_dir_name="train",  # Custom destination directory (default: "train2017")
    val_dir_name="val",      # Custom destination directory (default: "val2017")
    src_train_dir_name="train2017",  # Source train directory name
    src_val_dir_name="val2017"       # Source validation directory name
)

Command Line Usage

Convert Labels Only (Default Mode)

coco2yolo-kaggle --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo

By default, this will map the 91 COCO classes to 80 classes. If you want to keep the original 91 classes, use:

coco2yolo-kaggle --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo --no-cls91to80

Copy Files Only (with Custom Directory Names)

coco2yolo-kaggle --mode=copy --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo --final-dest=/kaggle/tmp/COCO2017 --train-dir-name=train --val-dir-name=val

Complete Process with Custom Directory Names

coco2yolo-kaggle --mode=all --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo --final-dest=/kaggle/tmp/COCO2017 --train-dir-name=train --val-dir-name=val

Class Mapping and Customizing Directory Structure

Class Mapping

  • By default, the tool maps the original 91 COCO classes to the standard 80 classes (cls91to80=True)
  • To preserve the original 91 classes, use the --no-cls91to80 flag

Directory Name Customization

You can customize the directory names using these parameters:

  • --train-dir-name: Set destination train directory name (default: "train2017")
  • --val-dir-name: Set destination validation directory name (default: "val2017")
  • --src-train-dir: Source train directory name (default: "train2017")
  • --src-val-dir: Source validation directory name (default: "val2017")

Kaggle Example Code

# 1. Install the package
!pip install coco2yolo-kaggle

# 2. Convert labels only (with class mapping)
!coco2yolo-kaggle --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo

# 3. Copy dataset files with custom directory names
!coco2yolo-kaggle --mode=copy --json-dir=/kaggle/input/coco-2017-dataset/coco2017/annotations --output-dir=/kaggle/working/coco_yolo --final-dest=/kaggle/tmp/COCO2017 --train-dir-name=train --val-dir-name=val

# 4. Train a YOLOv8 model
!pip install ultralytics
from ultralytics import YOLO

# Create dataset configuration file with custom directory structure
%%writefile coco.yaml
path: /kaggle/tmp/COCO2017
train: images/train  # Using custom directory name
val: images/val      # Using custom directory name
nc: 80
names: ['person', 'bicycle', 'car', ... ] # Complete 80 class names

# Train the model
model = YOLO('yolov8n.pt')  # Use nano model
results = model.train(data='coco.yaml', epochs=3, imgsz=640)

Acknowledgements

This tool is based on ultralytics/JSON2YOLO, thanks to the original authors' contributions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coco2yolo_kaggle-0.2.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coco2yolo_kaggle-0.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file coco2yolo_kaggle-0.2.1.tar.gz.

File metadata

  • Download URL: coco2yolo_kaggle-0.2.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.8

File hashes

Hashes for coco2yolo_kaggle-0.2.1.tar.gz
Algorithm Hash digest
SHA256 d9176ffe2a9bb9236b6a9682b3d9c6ee617768add38762af9a96ea4fe491573e
MD5 751021623f2d77f2640af9c9c8bd877d
BLAKE2b-256 70ee00338875fd989e0107675c871ed647cd3e8a4fb37e945ee49e1bccea6e7e

See more details on using hashes here.

File details

Details for the file coco2yolo_kaggle-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for coco2yolo_kaggle-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1c87210026c3f0eeec682c6a87cb162a2fa30fe11bdd58274a5417d1fb10abb4
MD5 6b882639fc4e42433a84dc89df0d1af0
BLAKE2b-256 a9eebe8b01d09c9a8ee6c9496995673f7a97ff3535ce1f210245ab7332de6431

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page