当前位置:网站首页>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
边栏推荐
- 教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
- MySQL Server层四个日志
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- Yyds dry inventory what is test driven development
- IPv6 jobs
- Modeling specifications: naming conventions
- Advanced learning of MySQL -- Fundamentals -- isolation level of transactions
- 银行核心业务系统性能测试方法
- three.js网页背景动画液态js特效
- SD card reports an error "error -110 whilst initializing SD card
猜你喜欢

Map sorts according to the key value (ascending plus descending)

Codeforces 5 questions par jour (1700 chacune) - jour 6

华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】

Crazy, thousands of netizens are exploding the company's salary

电机控制反Park变换和反Clarke变换公式推导

Analyze 菜单分析

【RISC-V】外部中断
![[network security interview question] - how to penetrate the test file directory through](/img/48/be645442c8ff4cc5417c115963b217.jpg)
[network security interview question] - how to penetrate the test file directory through

Exness foreign exchange: the governor of the Bank of Canada said that the interest rate hike would be more moderate, and the United States and Canada fell slightly to maintain range volatility

MPLS experiment
随机推荐
【概念】Web 基础概念认知
Distributed service framework dobbo
Some problem records of AGP gradle
下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
Descriptor implements ORM model
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
MySQL advanced notes
这些不太会
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
SD card reports an error "error -110 whilst initializing SD card
继承day01
How to choose PLC and MCU?
Is there a completely independent localization database technology
Performance analysis of user login TPS low and CPU full
暑期刷题-Day3
NR modulation 1
Redis cache breakdown, cache penetration, cache avalanche
Game theory matlab
Leetcode problem solving -- 98 Validate binary search tree