当前位置:网站首页>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 # 整个训练集(蚂蚁数据集和蜜蜂数据集的集合)
在数据集不够时,可以用这种方法补充数据集
边栏推荐
- 【概念】Web 基础概念认知
- Taobao focus map layout practice
- Polymorphic day02
- Who is the winner of PTA
- Leetcode problem solving -- 98 Validate binary search tree
- Is there a completely independent localization database technology
- [Li Kou] the second set of the 280 Li Kou weekly match
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
- Prototype design
猜你喜欢
XSS challenges bypass the protection strategy for XSS injection
深入刨析的指针(题解)
Apt installation ZABBIX
如何做好功能测试
Linear programming matlab
暑期刷题-Day3
Jenkins basic knowledge ----- detailed explanation of 03pipeline code
Recommended foreign websites for programmers to learn
JS音乐在线播放插件vsPlayAudio.js
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
随机推荐
Selenium share
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
如何做好功能测试
Research on cooperative control of industrial robots
Getting started with applet cloud development - getting user search content
Installation and use tutorial of cobaltstrike-4.4-k8 modified version
C # create self host webservice
How to choose PLC and MCU?
[ruoyi] ztree custom icon (iconskin attribute)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
NR modulation 1
Leetcode problem solving -- 99 Restore binary search tree
Some problem records of AGP gradle
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
Analyze menu analysis
svg拖动点裁剪图片js特效
Redo file corruption repair
How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)