当前位置:网站首页>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 # 整个训练集(蚂蚁数据集和蜜蜂数据集的集合)在数据集不够时,可以用这种方法补充数据集
边栏推荐
- 八道超经典指针面试题(三千字详解)
- Codeworks 5 questions per day (1700 average) - day 6
- 下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
- What are the principles of software design (OCP)
- SD卡报错“error -110 whilst initialising SD card
- 1. Dynamic parameters of function: *args, **kwargs
- Precautions for single chip microcomputer anti reverse connection circuit
- My C language learning record (blue bridge) -- on the pointer
- Overview of OCR character recognition methods
- canvas切积木小游戏代码
猜你喜欢

svg拖动点裁剪图片js特效

Getting started with applet cloud development - getting user search content

Redo file corruption repair

Overview of OCR character recognition methods

I sorted out a classic interview question for my job hopping friends

2022工作中遇到的问题四

4. File modification

深入刨析的指针(题解)
How to do function test well

Research on cooperative control of industrial robots
随机推荐
codeforces每日5題(均1700)-第六天
Software design principles
Eight super classic pointer interview questions (3000 words in detail)
codeforces每日5题(均1700)-第六天
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
EDCircles: A real-time circle detector with a false detection control 翻译
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
ASU & OSU | model based regularized off-line meta reinforcement learning
Mysql database operation
StrError & PERROR use yyds dry inventory
建模规范:命名规范
mysqldump数据备份
如何做好功能测试
[ruoyi] ztree custom icon (iconskin attribute)
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Getting started with applet cloud development - getting user search content
canvas切积木小游戏代码
Quartz misfire missed and compensated execution
. Net 6 and Net core learning notes: Important issues of net core
MySQL learning notes-10-tablespace recycling