图像分类数据集划分(创建ImageNet格式的数据集)

bash 复制代码
原始数据文件夹如下:
├──data
    ├── 0  类别1
    ├── 1  类别2

制作数据集格式如下所示:
├──datasets
    ├── meta
        │   ├── test.txt     # 测试数据集的标注文件
        │   ├── train.txt    # 训练数据集的标注文件
        │   └── val.txt      # 验证数据集的标注文件
        ├── train
        │   ├── 0
        │   ├── 1
        │   
        ├── test
        │   ├── 0
        │   ├── 1
        │
        └── val
        │   ├── 0
        │   ├── 1

划分数据集比例,训练集:验证集:测试集=0.6:0.2:0.2

划分数据集代码如下:

bash 复制代码
import os
import shutil
import random
from tqdm import tqdm

# 定义原始数据文件夹和目标数据集文件夹
data_dir = 'data'
target_dir = 'datasets'

# 定义数据集划分比例
train_split_ratio = 0.6
val_split_ratio = 0.2
test_split_ratio = 0.2

# 创建目标数据集文件夹及其子目录结构
os.makedirs(target_dir, exist_ok=True)
os.makedirs(os.path.join(target_dir, 'meta'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'train'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'test'), exist_ok=True)
os.makedirs(os.path.join(target_dir, 'val'), exist_ok=True)

# 获取原始数据文件夹下的子目录列表
categories = os.listdir(data_dir)

# 遍历每个子目录
for category in categories:
    # 获取该类别下的所有文件
    files = os.listdir(os.path.join(data_dir, category))

    # 随机打乱文件顺序
    random.shuffle(files)

    # 计算划分数据集的索引
    total_files = len(files)
    train_split = int(train_split_ratio * total_files)
    val_split = int(val_split_ratio * total_files)

    # 划分数据集并复制到目标文件夹,使用tqdm添加进度条
    for file in tqdm(files[:train_split], desc=f'Copying train data for {category}'):
        src = os.path.join(data_dir, category, file)
        dst = os.path.join(target_dir, 'train', category)
        os.makedirs(dst, exist_ok=True)
        shutil.copy(src, os.path.join(dst, file))

    for file in tqdm(files[train_split:train_split + val_split], desc=f'Copying validation data for {category}'):
        src = os.path.join(data_dir, category, file)
        dst = os.path.join(target_dir, 'val', category)
        os.makedirs(dst, exist_ok=True)
        shutil.copy(src, os.path.join(dst, file))

    for file in tqdm(files[train_split + val_split:], desc=f'Copying test data for {category}'):
        src = os.path.join(data_dir, category, file)
        dst = os.path.join(target_dir, 'test', category)
        os.makedirs(dst, exist_ok=True)
        shutil.copy(src, os.path.join(dst, file))

# 创建标注文件(train.txt、val.txt、test.txt)
with open(os.path.join(target_dir, 'meta', 'train.txt'), 'w') as train_txt:
    for category in categories:
        train_files = os.listdir(os.path.join(target_dir, 'train', category))
        for file in train_files:
            train_txt.write(f'{os.path.join("train", category, file)} {category}\n')

with open(os.path.join(target_dir, 'meta', 'val.txt'), 'w') as val_txt:
    for category in categories:
        val_files = os.listdir(os.path.join(target_dir, 'val', category))
        for file in val_files:
            val_txt.write(f'{os.path.join("val", category, file)} {category}\n')

with open(os.path.join(target_dir, 'meta', 'test.txt'), 'w') as test_txt:
    for category in categories:
        test_files = os.listdir(os.path.join(target_dir, 'test', category))
        for file in test_files:
            test_txt.write(f'{os.path.join("test", category, file)} {category}\n')

print("数据集划分完成!")
相关推荐
boooo_hhh25 分钟前
深度学习笔记16-VGG-16算法-Pytorch实现人脸识别
pytorch·深度学习·机器学习
m0_748235952 小时前
Python大数据可视化:基于Python的王者荣耀战队的数据分析系统设计与实现_flask+hadoop+spider
hadoop·python·flask
Dyan_csdn2 小时前
【Python项目】基于Python的Web漏洞挖掘系统
网络·python·安全·web安全
Minner-Scrapy2 小时前
DApp 开发入门指南
开发语言·python·web app
&小刘要学习&3 小时前
anaconda不显示jupyter了?
python·jupyter
jerry-893 小时前
jupyterhub_config配置文件内容
python
奔跑吧邓邓子3 小时前
【Python爬虫(36)】深挖多进程爬虫性能优化:从通信到负载均衡
开发语言·爬虫·python·性能优化·负载均衡·多进程
美狐美颜sdk3 小时前
直播美颜工具架构设计与性能优化实战:美颜SDK集成与实时处理
深度学习·美颜sdk·第三方美颜sdk·视频美颜sdk·美颜api
学长学姐我该怎么办3 小时前
年前集训总结python
python
量化投资技术4 小时前
【量化科普】Sharpe Ratio,夏普比率
python·量化交易·量化·量化投资·qmt·miniqmt