当前位置: 首页 > news >正文

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

原始数据文件夹如下:
├──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

划分数据集代码如下:

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("数据集划分完成!")
http://www.lryc.cn/news/187043.html

相关文章:

  • ArcGIS Engine:报错无法嵌入互操作类型“ESRI.ArcGIS.Geometry.EnvelopeClass”。请改用适用的接口。
  • 核货宝:服装店收银系统如何选择?收银系统选购指南!
  • GB/T 7134-2008 浇筑型工业有机玻璃板材检测
  • 数据采集平台(二)
  • Nginx + PHP 异常排查,open_basedir 异常处理
  • Linux免密登录
  • 迷宫 蓝桥杯
  • 25 mysql like 是否使用索引
  • Android---Class 对象在执行引擎中的初始化过程
  • Altium Designer实用系列(二)----PCB绘图小技巧
  • threejs-开发入门与调试设置
  • win11安装双系统Ubuntu的坎坷记录
  • 关于docker的xuexi
  • Python接口自动化测试实战详解,你想要的全都有
  • SparkSQL 外部数据源
  • leetcode做题笔记167. 两数之和 II - 输入有序数组
  • [ZJCTF 2019]NiZhuanSiWei - 伪协议+文件包含+反序列化
  • 如何提升和扩展 PostgreSQL — 从共享缓冲区到内存数据网格
  • Elasticsearch:使用 huggingface 模型的 NLP 文本搜索
  • 论文解析——异构多芯粒神经网络加速器
  • MyBatisPlus(十六)逻辑删除
  • 基于黏菌优化的BP神经网络(分类应用) - 附代码
  • C语言基础语法复习08-位域bit-fields
  • 3.2.OpenCV技能树--二值图像处理--图像腐蚀与膨胀
  • 基于FPGA的数字时钟系统设计
  • linux centos Python + Selenium+Chrome自动化测试环境搭建?
  • mysql面试题20:有哪些合适的分布式主键方案
  • git的基础操作
  • lua 中文字符的判断简介
  • SSM-XML整合