当前位置:网站首页>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 # 整个训练集(蚂蚁数据集和蜜蜂数据集的集合)在数据集不够时,可以用这种方法补充数据集
边栏推荐
- Overview of OCR character recognition methods
- 继承day01
- jsscript
- Deep parsing pointer and array written test questions
- 适合程序员学习的国外网站推荐
- Redis cache breakdown, cache penetration, cache avalanche
- 下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
- Audio-AudioRecord Binder通信机制
- 深度解析指针与数组笔试题
- . Net 6 and Net core learning notes: Important issues of net core
猜你喜欢

深入刨析的指针(题解)

SAP ALV颜色代码对应颜色(整理)

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

OCR文字识别方法综述

js凡客banner轮播图js特效

Software design principles

The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
![[kubernetes series] learn the exposed application of kubernetes service security](/img/61/4564230feeb988886fe595e3125ef4.png)
[kubernetes series] learn the exposed application of kubernetes service security

Research on cooperative control of industrial robots

Reverse repackaging of wechat applet
随机推荐
适合程序员学习的国外网站推荐
Redo file corruption repair
[risc-v] external interrupt
NR modulation 1
这些不太会
Function knowledge points
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
codeforces每日5題(均1700)-第六天
Performance test method of bank core business system
My C language learning record (blue bridge) -- under the pointer
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Detailed use of dbutils # yyds dry goods inventory #
Tidb ecological tools (backup, migration, import / export) collation
SAP ALV颜色代码对应颜色(整理)
1003 emergency (25 points), "DIJ deformation"
NR modulation 1
1.16 - 校验码
How to do function test well
Game theory matlab