当前位置:网站首页>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 # 整个训练集(蚂蚁数据集和蜜蜂数据集的集合)在数据集不够时,可以用这种方法补充数据集
边栏推荐
- Audio audiorecord binder communication mechanism
- Inherit day01
- Crazy, thousands of netizens are exploding the company's salary
- Getting started with applet cloud development - getting user search content
- Derivation of anti Park transform and anti Clarke transform formulas for motor control
- Modeling specifications: naming conventions
- SAP ALV单元格级别设置颜色
- Summary of Bible story reading
- EDCircles: A real-time circle detector with a false detection control 翻译
- Jenkins basic knowledge ----- detailed explanation of 03pipeline code
猜你喜欢

Eight super classic pointer interview questions (3000 words in detail)

What is the investment value of iFLYTEK, which does not make money?

真机无法访问虚拟机的靶场,真机无法ping通虚拟机

MySQL advanced notes

Analyze menu analysis

Sign SSL certificate as Ca

Reverse repackaging of wechat applet

Explore pointers and pointer types in depth

记录一下逆向任务管理器的过程

three.js网页背景动画液态js特效
随机推荐
电机控制反Park变换和反Clarke变换公式推导
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
Self made CA certificate and SSL certificate using OpenSSL
Descriptor implements ORM model
Problems encountered in 2022 work IV
Summary of Bible story reading
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
What is the investment value of iFLYTEK, which does not make money?
Taobao focus map layout practice
深度解析指针与数组笔试题
Apt installation ZABBIX
Recommended foreign websites for programmers to learn
Reverse repackaging of wechat applet
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
Analyze menu analysis
Selenium share
resulttype和resultmap的区别和应用场景
Research on cooperative control of industrial robots
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
[ruoyi] set theme style