PyTorch資料讀取

2021-08-20 16:45:40 字數 1819 閱讀 1156

torch.utils.data.dataloader(torch.utils.data.dataset,batch_size,shuffle,num_workers,pin_memory)

關鍵是這兩個類:

torch.utils.data.dataloader

torch.utils.data.dataset

import torchvision.transforms as transforms

train_loader = torch.utils.data.dataloader(

imagelist(root=opt.root_path, filelist=opt.train_list,

transform=transforms.compose([

transforms.totensor(), #將讀取的變為tensor型別,很重要

])),

batch_size=opt.batch_size, shuffle=true,

num_workers=opt.workers, pin_memory=true)

寫乙個類作為資料讀取器,繼承torch.utils.data.dataset

#load_imglist.py

import torch.utils.data

from pil import image

import os

def default_list_reader(filelist):

imglist =

with open(filelist, 'r') as file:

for line in file.readlines():

imgpath, label = line.strip().split(' ')

return imglist

class imagelist(torch.utils.data.dataset):

def __init__(self, root, filelist, transform=none):

self.root = root

self.imglist = default_list_reader(filelist)

self.transform = transform

def __getitem__(self, index):

imgpath, target = self.imglist[index]

print(imgpath)

img_loc=os.path.join(self.root, imgpath)

img = image.open(img_loc).convert('l') #預設讀取彩色圖象,這兒轉化為rgb影象

if self.transform is not none:

img = self.transform(img)

return img, target

def __len__(self):

return len(self.imglist)

for i,(input,target) in enumerate(train_loader):

print(i,target)

print(input.shape)

輸出的最後乙個結果為

(1093, 

928[torch.longtensor of size 1]

)(1l, 1l, 64l, 64l)

輸出的tensor是4維,將影象自動加了一維。

pytorch資料讀取

pytorch資料讀取機制 sampler生成索引index,根據索引從dataset中獲取和標籤 1.torch.utils.data.dataloader 功能 構建可迭代的資料裝在器 dataset dataset類,決定資料從哪讀取及如何讀取 batchsize 批大小 num works ...

pytorch讀取coco資料集

yolov3 an incremental improvement 原理在該篇部落格就寫的很詳細了,這裡就不贅述了 bin bash credit clone coco api git clone cd coco mkdir images cd images download images wget...

Pytorch 讀取大資料集

記錄一下pytorch讀取大型資料集的要點 pytorch 讀取大資料集的一般方法 class mydataset data.dataset def init self,root filepath self.root root init 中讀取檔案路徑而非檔案本體 self.imgs list se...