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
)

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,
    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

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

Customizing Directory Structure

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
!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.0.tar.gz (10.5 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.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coco2yolo_kaggle-0.2.0.tar.gz
  • Upload date:
  • Size: 10.5 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.0.tar.gz
Algorithm Hash digest
SHA256 af392262ea98fb68ae579b84ff8289b67377adc2b19a489bbbd1412ba1fe6828
MD5 85e2e78bde553bf13b1423e73660a9c1
BLAKE2b-256 2fb8a3947c526ac5686cf945ea666ff2eb8cf860b17691f836be647a536b8bf6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for coco2yolo_kaggle-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67032f21adc4029723fe16466bc1da3b28766fef2bb359cfa1e961b055b3524d
MD5 1d888ed7e9b2a857488ff768832f30a1
BLAKE2b-256 51e05812d6b611b8ffa8b9672342b780d2bb2e3f3aa8b6a7fb2d5a72f8b983ce

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