当前位置:网站首页>三、如何搞自定义数据集?
三、如何搞自定义数据集?
2022-07-29 05:22:00 【MY头发乱了】
前言
MNIST数据这个最最基础的数据集已经被走在程序猿道路上的同学们玩坏了,所以今天教大家如何搞一个自定义数据集。
一、定义的数据集,未做预处理。
下面展示一些 内联代码片。
import os
from torch.utils.data import Dataset ,DataLoader
from PIL import Image
#1.创建数据集类,使用torch.utils.data中的Dataset方法。
class My_Dataset(Dataset):
#2.循环找到文件路径,并添加标签
def __init__(self,main_dir,data_type,transforms):
self.dataset=[]#空列表为装新增一个标签的数据库
self.transforms=transforms
if data_type==0:
data_filename='train'
elif data_type is 1:
data_filename='val'
else:
data_filename='test'
for i , cls_filename in enumerate(
os.listdir(os.path.join(main_dir,data_filename))):
for i ,img_data in enumerate(os.listdir(
os.path.join(main_dir,data_filename,cls_filename))):
self.dataset.append([os.path.join(main_dir,
data_filename,cls_filename,img_data),int(img_data[0]) ])
#3.计算图片长度,方便后面迭代
def __len__(self):
return len(self.dataset)#为了获取图片长度,方便迭代
#4、取出图片路径,并打开,便于做数据预处理
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=Image.open(img)
img_data=self.transforms(img_data)
return img_data,label示例:@TOC
二、定义数据集,并做数据预处理。
包括旋转、裁剪、转为张量、扩大、正则化等等。
1.预处理部分
#4、取出图片路径,并打开,便于做数据预处理
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=self.data_process(Image.open(img))
return img_data,label
#5.数据处理,数据增强、加噪声等等
def data_process(self,x):
return transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5,),std=(0.5,))])(x)2.定义数据过程
代码如下(示例):
import os
from torchvision import transforms
from torch.utils.data import Dataset ,DataLoader
from PIL import Image
#1.创建数据集类,使用torch.utils.data中的Dataset方法。
class My_Dataset(Dataset):
#2.循环找到文件路径,并添加标签
def __init__(self,main_dir,data_type):
self.dataset=[]#空列表为装新增一个标签的数据库
if data_type==0:
data_filename='train'
elif data_type is 1:
data_filename='val'
else:
data_filename='test'
for i , cls_filename in enumerate(
os.listdir(os.path.join(main_dir,data_filename))):
for i ,img_data in enumerate(os.listdir(
os.path.join(main_dir,data_filename,cls_filename))):
self.dataset.append([os.path.join(main_dir,
data_filename,cls_filename,img_data),i ])
#3.计算图片长度,方便后面迭代
def __len__(self):
return len(self.dataset)#为了获取图片长度,方便迭代
#4、取出图片路径,并打开,便于做数据预处理
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=self.data_process(Image.open(img))
return img_data,label
#5.数据处理,数据增强、加噪声等等
def data_process(self,x):
return transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5,),std=(0.5,))])(x)
``边栏推荐
- 【语义分割】Mapillary 数据集简介
- [overview] image classification network
- D3.js vertical relationship diagram (with arrows and text description of connecting lines)
- [tensorrt] convert pytorch into deployable tensorrt
- 【网络设计】ConvNeXt:A ConvNet for the 2020s
- 第三周周报 ResNet+ResNext
- 迁移学习——Transitive Transfer Learning
- 【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
- Valuable blog and personal experience collection (continuous update)
- [semantic segmentation] full attention network for semantic segmentation
猜你喜欢

虚假新闻检测论文阅读(三):Semi-supervised Content-based Detection of Misinformation via Tensor Embeddings

tensorboard使用

【Transformer】TransMix: Attend to Mix for Vision Transformers

mysql 的show profiles 使用。

四、One-hot和损失函数的应用
![[clustmaps] visitor statistics](/img/1a/173664a633fd14ea56696dd824acb6.png)
[clustmaps] visitor statistics

Yum local source production

【pycharm】pycharm远程连接服务器

C # judge whether the user accesses by mobile phone or computer

【ML】机器学习模型之PMML--概述
随机推荐
Reporting Services- Web Service
[DL] introduction and understanding of tensor
通过简单的脚本在Linux环境实现Mysql数据库的定时备份(Mysqldump命令备份)
Detailed explanation of atomic operation class atomicinteger in learning notes of concurrent programming
【Transformer】SOFT: Softmax-free Transformer with Linear Complexity
虚假新闻检测论文阅读(三):Semi-supervised Content-based Detection of Misinformation via Tensor Embeddings
A preliminary study on fastjason's autotype
虚假新闻检测论文阅读(二):Semi-Supervised Learning and Graph Neural Networks for Fake News Detection
【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
第三周周报 ResNet+ResNext
mysql 的show profiles 使用。
Interesting talk about performance optimization thread pool: is the more threads open, the better?
Show profiles of MySQL is used.
这些你一定要知道的进程知识
[tensorrt] convert pytorch into deployable tensorrt
研究生新生培训第二周:卷积神经网络基础
【目标检测】KL-Loss:Bounding Box Regression with Uncertainty for Accurate Object Detection
Operation commands in anaconda, such as removing old environment, adding new environment, viewing environment, installing library, cleaning cache, etc
Nifi changed UTC time to CST time
关于Flow的原理解析