A tiny yet robust dataset split tool for YOLO-style datasets
Project description
datasetool
一个轻巧而强大的 YOLO 风格数据集划分工具。
✨ 功能特性
- 多种划分比例: 支持按比例划分训练集、验证集和测试集。
- 高可复现性:可通过设置随机种子确保每次划分结果一致。
- 灵活的文件处理: 支持四种文件转移模式:
copy(复制)、move(移动)、symlink(符号链接)、hardlink(硬链接)。 - 严格模式: 可选的严格模式,确保图片和标签文件一一对应,否则中断操作。
- 保留目录结构: 可选择在输出目录中保留原始的子目录结构。
- 干预(Dry Run): 支持“只打印不执行”模式,方便预览划分结果。
- 自定义图片格式: 支持自定义需要处理的图片文件扩展名。
- 进度条显示: 在处理大量文件时显示进度条,方便跟踪进度(依赖
tqdm)。
📦 安装
-
通过
pip从 PyPI 安装 (推荐):pip install datasetool
-
或者从源码本地安装:
git clone https://github.com/mozihe/datasetool.git cd datasetool pip install .
🚀 使用方法
命令行接口 (CLI)
本工具的核心命令是 dtsplit。
基本用法
假设您的数据集结构如下:
my_dataset/
├── images/
│ ├── 1.jpg
│ ├── 2.png
│ └── ...
└── labels/
├── 1.txt
├── 2.txt
└── ...
您可以执行以下命令,将数据集划分为 80% 训练集、10% 验证集和 10% 测试集:
dtsplit --images ./my_dataset/images --labels ./my_dataset/labels --out ./my_dataset_split --val 0.1 --test 0.1
执行后,会在 my_dataset_split 目录下生成如下结构:
my_dataset_split/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
└── labels/
├── train/
├── val/
└── test/
参数详解
usage: dtsplit [-h] --images IMAGES --labels LABELS --out OUT [--val VAL] [--test TEST] [--seed SEED]
[--exts EXTS] [--strict] [--copy-mode {copy,move,symlink,hardlink}] [--dry-run]
[--keep-structure] [--no-progress] [-V]
Split YOLO-style dataset into train/val(/test).
options:
-h, --help show this help message and exit
--images IMAGES 图片根目录 (必需)
--labels LABELS 标签根目录 (.txt) (必需)
--out OUT 输出目录 (必需)
--val VAL 验证集比例,默认 0.2
--test TEST 测试集比例,默认 0
--seed SEED 随机种子(可复现)
--exts EXTS 图片扩展名,逗号分隔 (默认: .jpg,.jpeg,.png,.bmp,.tiff,.tif)
--strict 开启严格模式:一旦图片和标签不匹配直接报错
--copy-mode {copy,move,symlink,hardlink}
文件转移方式(默认 copy)
--dry-run 只打印文件转移计划,不实际执行
--keep-structure 在输出目录中保留原 images/labels 的子目录结构
--no-progress 不显示进度条
-V, --version 显示版本号
作为 Python 库使用 (API)
您也可以在自己的 Python 脚本中导入 datasetool 并使用其核心功能。
from pathlib import Path
from datasetool.split import SplitConfig, split_dataset
# 1. 配置划分参数
cfg = SplitConfig(
images_dir=Path("./my_dataset/images"),
labels_dir=Path("./my_dataset/labels"),
output_dir=Path("./my_dataset_split"),
val_ratio=0.2,
test_ratio=0.1,
seed=42, # 为了可复现
copy_mode="copy", # 使用复制模式
strict=True, # 开启严格模式
)
# 2. 执行划分
# 函数会返回一个元组,包含三个列表:train_ids, val_ids, test_ids
train_ids, val_ids, test_ids = split_dataset(cfg)
print(f"划分完成!")
print(f"训练集样本数: {len(train_ids)}")
print(f"验证集样本数: {len(val_ids)}")
print(f"测试集样本数: {len(test_ids)}")
📜 许可证
本项目基于 MIT License。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
datasetool-0.1.0.tar.gz
(7.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datasetool-0.1.0.tar.gz.
File metadata
- Download URL: datasetool-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23c808cd38af4c3926b3c95b61213cc1e48185f76d052aa37b1922c86b2d4be0
|
|
| MD5 |
f3197f871ea9f652bc281b5c15cde0e4
|
|
| BLAKE2b-256 |
e818b59a2e6560404db3e2f5132c42c32c9f3faee922d1f2b6051c352d56f6b7
|
File details
Details for the file datasetool-0.1.0-py3-none-any.whl.
File metadata
- Download URL: datasetool-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ad200dfd317cb322abde6c77f3c4e45e3e3bce30205efa2d5568c90af71e8c9
|
|
| MD5 |
b86a4d372000485538ae73dc5ce9be79
|
|
| BLAKE2b-256 |
faba5c6b608c3d945cfea2fae06365e283cde9477e8e0977682538dc2407ffce
|