当前位置:网站首页>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)
``边栏推荐
- 迁移学习—Geodesic Flow Kernel for Unsupervised Domain Adaptation
- Spring, summer, autumn and winter with Miss Zhang (2)
- Technology that deeply understands the principle of MMAP and makes big manufacturers love it
- 一、PyTorch Cookbook(常用代码合集)
- The difference between asyncawait and promise
- 【Transformer】ACMix:On the Integration of Self-Attention and Convolution
- 【Attention】Visual Attention Network
- Flutter正在被悄悄放弃?浅析Flutter的未来
- [semantic segmentation] full attention network for semantic segmentation
- 研究生新生培训第一周:深度学习和pytorch基础
猜你喜欢
![[semantic segmentation] overview of semantic segmentation](/img/79/0c22bd28206fee281fa754c336b3b4.png)
[semantic segmentation] overview of semantic segmentation

Transformer回顾+理解

Wechat applet source code acquisition (download with tools)

四、One-hot和损失函数的应用

Typical case of xdfs & Aerospace Institute HPC cluster

【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers

【目标检测】6、SSD

【Transformer】TransMix: Attend to Mix for Vision Transformers

"Full flash measurement" database acceleration solution

【卷积核设计】Scaling Up Your Kernels to 31x31: Revisiting Large Kernel Design in CNNs
随机推荐
一、常见损失函数的用法
Set automatic build in idea - change the code, and refresh the page without restarting the project
PyTorch的数据读取机制
Transformer回顾+理解
【pycharm】pycharm远程连接服务器
[DL] build convolutional neural network for regression prediction (detailed tutorial of data + code)
MarkDown简明语法手册
【Transformer】AdaViT: Adaptive Tokens for Efficient Vision Transformer
C # judge whether the user accesses by mobile phone or computer
Detailed explanation of MySQL statistical function count
[target detection] generalized focal loss v1
【Clustrmaps】访客统计
Briefly talk about the difference between pendingintent and intent
Flutter正在被悄悄放弃?浅析Flutter的未来
ROS常用指令
神经网络相关知识回顾(PyTorch篇)
Wechat built-in browser prohibits caching
Flink connector Oracle CDC synchronizes data to MySQL in real time (oracle19c)
ML10自学笔记-SVM
备份谷歌或其他浏览器插件