当前位置:网站首页>3、 How to customize data sets?
3、 How to customize data sets?
2022-07-29 06:07:00 【My hair is messy】
List of articles
Preface
MNIST Data, the most basic data set, has been destroyed by the students who are on the path of programming apes , So today I will teach you how to make a custom dataset .
One 、 Defined datasets , No pretreatment .
Here are some Inline code slice .
import os
from torch.utils.data import Dataset ,DataLoader
from PIL import Image
#1. Create dataset class , Use torch.utils.data Medium Dataset Method .
class My_Dataset(Dataset):
#2. Loop to find the file path , And add labels
def __init__(self,main_dir,data_type,transforms):
self.dataset=[]# The empty list is the database with a new label
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. Calculate picture length , Convenient for later iterations
def __len__(self):
return len(self.dataset)# To get the length of the picture , Easy to iterate
#4、 Take out the image path , And open , Convenient for data preprocessing
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=Image.open(img)
img_data=self.transforms(img_data)
return img_data,labelExample :@TOC
Two 、 Define datasets , And do data preprocessing .
Including rotation 、 tailoring 、 Into a tensor 、 expand 、 Regularization and so on .
1. Pretreatment part
#4、 Take out the image path , And open , Convenient for data preprocessing
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=self.data_process(Image.open(img))
return img_data,label
#5. Data processing , Data to enhance 、 Add noise, etc
def data_process(self,x):
return transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5,),std=(0.5,))])(x)2. Define data process
The code is as follows ( Example ):
import os
from torchvision import transforms
from torch.utils.data import Dataset ,DataLoader
from PIL import Image
#1. Create dataset class , Use torch.utils.data Medium Dataset Method .
class My_Dataset(Dataset):
#2. Loop to find the file path , And add labels
def __init__(self,main_dir,data_type):
self.dataset=[]# The empty list is the database with a new label
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. Calculate picture length , Convenient for later iterations
def __len__(self):
return len(self.dataset)# To get the length of the picture , Easy to iterate
#4、 Take out the image path , And open , Convenient for data preprocessing
def __getitem__(self, index):
img,label=self.dataset[index]
img_data=self.data_process(Image.open(img))
return img_data,label
#5. Data processing , Data to enhance 、 Add noise, etc
def data_process(self,x):
return transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5,),std=(0.5,))])(x)
``边栏推荐
- 一、迁移学习与fine-tuning有什么区别?
- [semantic segmentation] full attention network for semantic segmentation
- 【网络设计】ConvNeXt:A ConvNet for the 2020s
- 【语义分割】语义分割综述
- 【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs
- Reporting Services- Web Service
- 研究生新生培训第三周:ResNet+ResNeXt
- ML16-神经网络(2)
- Detailed explanation of MySQL statistical function count
- C connect to SharePoint online webservice
猜你喜欢

虚假新闻检测论文阅读(五):A Semi-supervised Learning Method for Fake News Detection in Social Media

Yum local source production

预训练语言模型的使用方法

【Transformer】TransMix: Attend to Mix for Vision Transformers

【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer

【TensorRT】将 PyTorch 转化为可部署的 TensorRT

【Transformer】ACMix:On the Integration of Self-Attention and Convolution

一、常见损失函数的用法

【ML】机器学习模型之PMML--概述

Ribbon learning notes II
随机推荐
【bug】XLRDError: Excel xlsx file; not supported
[pycharm] pycharm remote connection server
Interesting talk about performance optimization thread pool: is the more threads open, the better?
Wechat built-in browser prohibits caching
GA-RPN:引导锚点的建议区域网络
【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
迁移学习——Transitive Transfer Learning
NLP领域的AM模型
Reporting service 2016 custom authentication
迁移学习——Robust Visual Domain Adaptation with Low-Rank Reconstruction
【Attention】Visual Attention Network
Briefly talk about the difference between pendingintent and intent
【TensorRT】将 PyTorch 转化为可部署的 TensorRT
虚假新闻检测论文阅读(四):A novel self-learning semi-supervised deep learning network to detect fake news on...
Reporting Services- Web Service
【目标检测】Generalized Focal Loss V1
clion+opencv+aruco+cmake配置
Transformer回顾+理解
ASM插桩:学完ASM Tree api,再也不用怕hook了
迁移学习——Low-Rank Transfer Subspace Learning