当前位置:网站首页>pytorch加载数据
pytorch加载数据
2022-07-06 03:18:00 【七上八下的黑】
一、 加载数据
- 加载数据集里的单张图片
from PIL import Image
img_path="D:\\pycharm\\PycharmProjects\\learn_torch\\hymenoptera_data\\train\\ants\\0013035.jpg"
img = Image.open(img_path)
img.show()- 加载数据集列表
import os
dir_path = "hymenoptera_data/train/ants"
img_path_list = os.listdir(dir_path)在pycharm中的Python console(控制器)中运行,效果更直观。

二、运用pytorch加载数据
torch.utils 是torch常用的工具箱。
想根据 idx 获取相应的图片的话,先获取这个图片地址的list(获取dataset下的所有data)。
from torch.utils.data import Dataset- 定义 MyData 类
class MyData(Dataset):
'''定义全局变量'''
def __init__(self, root_dir, label_dir):
self.root_dir = root_dir # root_dir 是数据集的目录
self.label_dir = label_dir # label_dir 是标签的目录
self.path = os.path.join(self.root_dir, self.label_dir)
self.img_path = os.listdir(self.path)
'''获取数据'''
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
'''数据集的长度'''
def __len__(self):
return len(self.img_path)- 测试定义的 MyData 类
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)方法一:
可在Python中测试:
ants_dataset[0]
方法二:
img, label = ants_dataset[0]
img.show()补充:
train_dataset = ants_dataset + bees_dataset # 整个训练集(蚂蚁数据集和蜜蜂数据集的集合)在数据集不够时,可以用这种方法补充数据集
边栏推荐
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- 手写数据库客户端
- 继承day01
- Summary of Bible story reading
- Buuctf question brushing notes - [geek challenge 2019] easysql 1
- Codeworks 5 questions per day (1700 average) - day 6
- XSS challenges bypass the protection strategy for XSS injection
- 建模规范:命名规范
- Performance test method of bank core business system
- Taobao focus map layout practice
猜你喜欢

My C language learning record (blue bridge) -- on the pointer

银行核心业务系统性能测试方法

Installation and use tutorial of cobaltstrike-4.4-k8 modified version

#PAT#day10

下一个行业风口:NFT 数字藏品,是机遇还是泡沫?

Mysqldump data backup

Idea push rejected solution

Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)
[concept] Web basic concept cognition

Tomb. Weekly update of Finance (February 7 - February 13)
随机推荐
建模规范:命名规范
指针笔试题~走近大厂
Codeworks 5 questions per day (1700 average) - day 6
出现Permission denied的解决办法(750权限谨慎使用)
Function knowledge points
MySQL advanced notes
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
codeforces每日5题(均1700)-第六天
暑期刷题-Day3
Getting started with applet cloud development - getting user search content
Analyze 菜单分析
Performance test method of bank core business system
Who is the winner of PTA
Distributed service framework dobbo
NR modulation 1
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
canvas切积木小游戏代码
Problems encountered in 2022 work IV
Overview of OCR character recognition methods