当前位置:网站首页>Pytorch load data
Pytorch load data
2022-07-06 03:19:00 【Up and down black】
One 、 Load data
- Load a single image in the dataset
from PIL import Image
img_path="D:\\pycharm\\PycharmProjects\\learn_torch\\hymenoptera_data\\train\\ants\\0013035.jpg"
img = Image.open(img_path)
img.show()
- Load dataset list
import os
dir_path = "hymenoptera_data/train/ants"
img_path_list = os.listdir(dir_path)
stay pycharm Medium Python console( controller ) Run in , The effect is more intuitive .
Two 、 Application pytorch Load data
torch.utils yes torch Common toolbox .
Want to according to idx Get the corresponding picture , Get the picture address first list( obtain dataset All under data).
from torch.utils.data import Dataset
- Definition MyData class
class MyData(Dataset):
''' Define global variables '''
def __init__(self, root_dir, label_dir):
self.root_dir = root_dir # root_dir Is the directory of the dataset
self.label_dir = label_dir # label_dir Is the catalog of labels
self.path = os.path.join(self.root_dir, self.label_dir)
self.img_path = os.listdir(self.path)
''' get data '''
def __getitem__(self, idx):
img_name = self.img_path[idx]
img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
img = Image.open(img_item_path)
label = self.label_dir
return img, label
''' The length of the data set '''
def __len__(self):
return len(self.img_path)
- Test defined MyData class
root_dir = "hymenoptera_data/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir, ants_label_dir)
bees_dataset = MyData(root_dir, bees_label_dir)
Method 1 :
Can be found in Python Medium test :
ants_dataset[0]
Method 2 :
img, label = ants_dataset[0]
img.show()
Add :
train_dataset = ants_dataset + bees_dataset # The whole training set ( A collection of ant data sets and bee data sets )
When the data set is insufficient , Data sets can be supplemented in this way
边栏推荐
- Is there a completely independent localization database technology
- My C language learning records (blue bridge) -- files and file input and output
- [risc-v] external interrupt
- 出现Permission denied的解决办法(750权限谨慎使用)
- NR modulation 1
- The next industry outlet: NFT digital collection, is it an opportunity or a foam?
- MPLS experiment
- XSS challenges bypass the protection strategy for XSS injection
- Leetcode problem solving -- 173 Binary search tree iterator
- Audio audiorecord binder communication mechanism
猜你喜欢
MySQL Server层四个日志
11. Container with the most water
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
Apt installation ZABBIX
Analyze 菜单分析
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
My C language learning record (blue bridge) -- on the pointer
NR modulation 1
Performance analysis of user login TPS low and CPU full
[ruoyi] set theme style
随机推荐
[concept] Web basic concept cognition
How to choose PLC and MCU?
jsscript
C # create self host webservice
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Software design principles
Sign SSL certificate as Ca
SAP ALV单元格级别设置颜色
[network security interview question] - how to penetrate the test file directory through
深入刨析的指针(题解)
真机无法访问虚拟机的靶场,真机无法ping通虚拟机
SAP ALV颜色代码对应颜色(整理)
Idea push rejected solution
NR modulation 1
Daily question brushing plan-2-13 fingertip life
codeforces每日5題(均1700)-第六天
XSS challenges绕过防护策略进行 XSS 注入
[Li Kou] the second set of the 280 Li Kou weekly match
Mysqldump data backup
Reverse repackaging of wechat applet