How to load a list of numpy arrays to pytorch dataset loader?(如何将 numpy 数组列表加载到 pytorch 数据集加载器?)
问题描述
我有一个巨大的 numpy 数组列表,其中每个数组代表一个图像,我想使用 torch.utils.data.Dataloader 对象加载它.但是 torch.utils.data.Dataloader 的文档提到它直接从文件夹加载数据.我如何根据我的原因修改它?我是 pytorch 的新手,任何帮助将不胜感激.我的单个图像的 numpy 数组看起来像这样.图片为RBG图片.
I have a huge list of numpy arrays, where each array represents an image and I want to load it using torch.utils.data.Dataloader object. But the documentation of torch.utils.data.Dataloader mentions that it loads data directly from a folder. How do I modify it for my cause? I am new to pytorch and any help would be greatly appreciated. my numpy array for a single image looks something like this. The image is RBG image.
[[[ 70 82 94]
[ 67 81 93]
[ 66 82 94]
...,
[182 182 188]
[183 183 189]
[188 186 192]]
[[ 66 80 92]
[ 62 78 91]
[ 64 79 95]
...,
[176 176 182]
[178 178 184]
[180 180 186]]
[[ 62 82 93]
[ 62 81 96]
[ 65 80 99]
...,
[169 172 177]
[173 173 179]
[172 172 178]]
...,
推荐答案
我认为 DataLoader 实际需要的是一个子类 Dataset
的输入.您可以编写自己的数据集类,子类化 Dataset
或使用 TensorDataset
,就像我在下面所做的那样:
I think what DataLoader actually requires is an input that subclasses Dataset
. You can either write your own dataset class that subclasses Dataset
or use TensorDataset
as I have done below:
import torch
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
my_x = [np.array([[1.0,2],[3,4]]),np.array([[5.,6],[7,8]])] # a list of numpy arrays
my_y = [np.array([4.]), np.array([2.])] # another list of numpy arrays (targets)
tensor_x = torch.Tensor(my_x) # transform to torch tensor
tensor_y = torch.Tensor(my_y)
my_dataset = TensorDataset(tensor_x,tensor_y) # create your datset
my_dataloader = DataLoader(my_dataset) # create your dataloader
对我有用.希望能帮到你.
Works for me. Hope it helps you.
这篇关于如何将 numpy 数组列表加载到 pytorch 数据集加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 numpy 数组列表加载到 pytorch 数据集加载器


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01