濮阳杆衣贸易有限公司

主頁 > 知識庫 > Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結

Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結

熱門標簽:開封自動外呼系統(tǒng)怎么收費 手機網(wǎng)頁嵌入地圖標注位置 開封語音外呼系統(tǒng)代理商 電銷機器人的風險 應電話機器人打電話違法嗎 天津電話機器人公司 400電話辦理哪種 地圖標注線上如何操作 河北防封卡電銷卡

一、前言

確保安裝

  • scikit-image
  • numpy

二、Dataset

一個例子:

# 導入需要的包
import torch
import torch.utils.data.dataset as Dataset
import numpy as np
 
# 編造數(shù)據(jù)
Data = np.asarray([[1, 2], [3, 4],[5, 6], [7, 8]])
Label = np.asarray([[0], [1], [0], [2]])
# 數(shù)據(jù)[1,2],對應的標簽是[0],數(shù)據(jù)[3,4],對應的標簽是[1]
 
 
#創(chuàng)建子類
class subDataset(Dataset.Dataset):
    #初始化,定義數(shù)據(jù)內容和標簽
    def __init__(self, Data, Label):
        self.Data = Data
        self.Label = Label
    #返回數(shù)據(jù)集大小
    def __len__(self):
        return len(self.Data)
    #得到數(shù)據(jù)內容和標簽
    def __getitem__(self, index):
        data = torch.Tensor(self.Data[index])
        label = torch.IntTensor(self.Label[index])
        return data, label
 
# 主函數(shù)
if __name__ == '__main__':
    dataset = subDataset(Data, Label)
    print(dataset)
    print('dataset大小為:', dataset.__len__())
    print(dataset.__getitem__(0))
    print(dataset[0])

 輸出的結果

我們有了對Dataset的一個整體的把握,再來分析里面的細節(jié):

#創(chuàng)建子類
class subDataset(Dataset.Dataset):

創(chuàng)建子類時,繼承的時Dataset.Dataset,不是一個Dataset。因為Dataset是module模塊,不是class類,所以需要調用module里的class才行,因此是Dataset.Dataset!

lengetitem這兩個函數(shù),前者給出數(shù)據(jù)集的大小**,后者是用于查找數(shù)據(jù)和標簽。是最重要的兩個函數(shù),我們后續(xù)如果要對數(shù)據(jù)做一些操作基本上都是再這兩個函數(shù)的基礎上進行。

三、DatasetLoader

DataLoader(dataset,
           batch_size=1,
           shuffle=False,
           sampler=None,
           batch_sampler=None,
           num_works=0,
           clollate_fn=None,
           pin_memory=False,
           drop_last=False,
           timeout=0,
           worker_init_fn=None,
           multiprocessing_context=None)

功能:構建可迭代的數(shù)據(jù)裝載器;
dataset:Dataset類,決定數(shù)據(jù)從哪里讀取及如何讀??;數(shù)據(jù)集的路徑
batchsize:批大小;
num_works:是否多進程讀取數(shù)據(jù);只對于CPU
shuffle:每個epoch是否打亂;
drop_last:當樣本數(shù)不能被batchsize整除時,是否舍棄最后一批數(shù)據(jù);
Epoch:所有訓練樣本都已輸入到模型中,稱為一個Epoch;
Iteration:一批樣本輸入到模型中,稱之為一個Iteration;
Batchsize:批大小,決定一個Epoch中有多少個Iteration;

還是舉一個實例:

import torch
import torch.utils.data.dataset as Dataset
import torch.utils.data.dataloader as DataLoader
import numpy as np
 
Data = np.asarray([[1, 2], [3, 4],[5, 6], [7, 8]])
Label = np.asarray([[0], [1], [0], [2]])
#創(chuàng)建子類
class subDataset(Dataset.Dataset):
    #初始化,定義數(shù)據(jù)內容和標簽
    def __init__(self, Data, Label):
        self.Data = Data
        self.Label = Label
    #返回數(shù)據(jù)集大小
    def __len__(self):
        return len(self.Data)
    #得到數(shù)據(jù)內容和標簽
    def __getitem__(self, index):
        data = torch.Tensor(self.Data[index])
        label = torch.IntTensor(self.Label[index])
        return data, label
 
if __name__ == '__main__':
    dataset = subDataset(Data, Label)
    print(dataset)
    print('dataset大小為:', dataset.__len__())
    print(dataset.__getitem__(0))
    print(dataset[0])
 
    #創(chuàng)建DataLoader迭代器,相當于我們要先定義好前面說的Dataset,然后再用Dataloader來對數(shù)據(jù)進行一些操作,比如是否需要打亂,則shuffle=True,是否需要多個進程讀取數(shù)據(jù)num_workers=4,就是四個進程
 
    dataloader = DataLoader.DataLoader(dataset,batch_size= 2, shuffle = False, num_workers= 4)
    for i, item in enumerate(dataloader): #可以用enumerate來提取出里面的數(shù)據(jù)
        print('i:', i)
        data, label = item #數(shù)據(jù)是一個元組
        print('data:', data)
        print('label:', label)

四、將Dataset數(shù)據(jù)和標簽放在GPU上(代碼執(zhí)行順序出錯則會有bug)

這部分可以直接去看博客:Dataset和DataLoader

總結下來時有兩種方法解決

1.如果在創(chuàng)建Dataset的類時,定義__getitem__方法的時候,將數(shù)據(jù)轉變?yōu)镚PU類型。則需要將Dataloader里面的參數(shù)num_workers設置為0,因為這個參數(shù)是對于CPU而言的。如果數(shù)據(jù)改成了GPU,則只能單進程。如果是在Dataloader的部分,先多個子進程讀取,再轉變?yōu)镚PU,則num_wokers不用修改。就是上述__getitem__部分的代碼,移到Dataloader部分。

2.不過一般來講,數(shù)據(jù)集和標簽不會像我們上述編輯的那么簡單。一般再kaggle上的標簽都是存在CSV這種文件中。需要pandas的配合。

這個進階可以看:WRITING CUSTOM DATASETS, DATALOADERS AND TRANSFORMS,他是用人臉圖片作為數(shù)據(jù)和人臉特征點作為標簽。

到此這篇關于Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結的文章就介紹到這了,更多相關詳解Dataset和DataLoader內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Pytorch dataloader在加載最后一個batch時卡死的解決
  • pytorch鎖死在dataloader(訓練時卡死)
  • Pytorch 如何加速Dataloader提升數(shù)據(jù)讀取速度
  • pytorch DataLoader的num_workers參數(shù)與設置大小詳解
  • 我對PyTorch dataloader里的shuffle=True的理解
  • pytorch中DataLoader()過程中遇到的一些問題

標簽:六盤水 宿遷 常州 江蘇 山東 蘭州 駐馬店 成都

巨人網(wǎng)絡通訊聲明:本文標題《Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結》,本文關鍵詞  Pytorch,數(shù)據(jù),讀,取之,Dataset,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結》相關的同類信息!
  • 本頁收集關于Pytorch數(shù)據(jù)讀取之Dataset和DataLoader知識總結的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    开阳县| 凤庆县| 拉孜县| 武宁县| 沂源县| 白河县| 彭阳县| 曲水县| 栾川县| 华安县| 视频| 翁牛特旗| 左贡县| 个旧市| 嫩江县| 中西区| 伊川县| 岢岚县| 张家港市| 乌苏市| 丹东市| 遂宁市| 五常市| 治多县| 嘉兴市| 绥宁县| 南岸区| 新乐市| 资阳市| 罗甸县| 甘肃省| 内江市| 滨州市| 彰武县| 吕梁市| 宕昌县| 上林县| 武义县| 绥江县| 娄烦县| 禹城市|