Skip to main content

This utility splits an image dataset into train, validation, and test subsets while preserving the class folder structure.

Project description

DatasetSpliter

A simple Python utility for splitting an image dataset into train, validation, and test subsets while preserving class folder structure.

Overview

DatasetSpliter provides a function that takes a list of class folders (each containing image files) and splits the dataset into training, validation, and testing subsets. The images are copied into a new output directory while keeping the original class names.

Resulting directory structure

output_folder/
├── train/
│   └── class_name/
├── val/
│   └── class_name/
└── test/
    └── class_name/

The split process is random but reproducible using a fixed random seed.

Features

  • Supports common image formats: .jpg, .jpeg, .png, .bmp, .gif, .tiff, .webp
  • Preserves class directory names
  • Prints a summary for each class
  • Uses only the Python standard library

Installation

pip install ImageDataSpliter

Usage

from imagedataspliter import split_dataset

source_folders = [
    "data/cats",
    "data/dogs",
    "data/birds"
]

output_folder = "dataset_split"

split_dataset(
    source_folders=source_folders,
    output_folder=output_folder,
    train_ratio=0.7,
    val_ratio=0.15,
    test_ratio=0.15,
    seed=42
)

Function Signature

split_dataset(
    source_folders: list[str],
    output_folder: str,
    train_ratio: float = 0.7,
    val_ratio: float = 0.15,
    test_ratio: float = 0.15,
    seed: int = 42
)

Parameters

  • source_folders List of paths to class directories containing images.

  • output_folder Path to the directory where the split dataset will be saved.

  • train_ratio Fraction of images used for training.

  • val_ratio Fraction of images used for validation.

  • test_ratio Fraction of images used for testing.

  • seed Random seed for reproducible splits.

The sum of train_ratio, val_ratio, and test_ratio must equal 1.0.

Output

  • Image files are copied, not moved

  • Original data remains unchanged

  • Class folder structure is preserved

  • Progress information is printed to the console

Example Output

Processing class: cats
  Total images: 100
  Train: 70, Val: 15, Test: 15

Processing class: dogs
  Total images: 120
  Train: 84, Val: 18, Test: 18

Dataset saved to: dataset_split

Requirements

Python 3.8 or newer

No external dependencies

Contributing

Pull requests and issues are welcome. Feel free to suggest improvements or report bugs.

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

imagedataspliter-0.1.4.tar.gz (3.1 kB view details)

Uploaded Source

File details

Details for the file imagedataspliter-0.1.4.tar.gz.

File metadata

  • Download URL: imagedataspliter-0.1.4.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for imagedataspliter-0.1.4.tar.gz
Algorithm Hash digest
SHA256 93ba8261935e713acb9256a1586f3bad1a45e903473af5bd1ce180272d447239
MD5 4c955249efb5be89a46fdae464d3e543
BLAKE2b-256 7daf19bd62af2aef2d098edd91b3323e9c2c9ff499d6501e98a0b0486163ad76

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