本文主要介紹py torch . data(py torch中的數(shù)據(jù)集),下面一起看看py torch . data(py torch中的數(shù)據(jù)集)相關(guān)資訊。
本節(jié)參考小土丘的pytorch入門視頻教程。建議學(xué)習(xí)時(shí)多讀源代碼。你可以通過源代碼中的注釋快速找出一個(gè)類或函數(shù)的功能以及輸入輸出的類型。
dataset#借用dataset可以快速訪問深度學(xué)習(xí)所需的數(shù)據(jù)。例如,我們需要訪問以下培訓(xùn)數(shù)據(jù):
其中,訓(xùn)練數(shù)據(jù)集存儲(chǔ)在train中,螞蟻和蜜蜂都是它們所包含的圖片數(shù)據(jù)的文件夾名和標(biāo)簽,驗(yàn)證數(shù)據(jù)集存儲(chǔ)在val中。
如果我們希望數(shù)據(jù)集類實(shí)現(xiàn)以下數(shù)據(jù)訪問形式:
dataset = my dataset( 根目錄 , 標(biāo)簽?zāi)夸?)img,label = dataset [0] #我們只需要繼承dataset類,并重寫__getitem__(必需的)和__len__(建議的)方法。可以通過在交互模式下執(zhí)行以下語句來查看詳細(xì)信息:
從torch.utils.data導(dǎo)入數(shù)據(jù)集幫助(dataset) #還是執(zhí)行數(shù)據(jù)集??#或者在pycharm中按住ctrl點(diǎn)擊dataset直接查看源代碼。輸出如下所示:
表示: class : ` dataset &;的抽象類。所有表示從鍵到數(shù)據(jù)樣本的映射的數(shù)據(jù)集都應(yīng)該對(duì)其進(jìn)行子類化。所有子類都應(yīng)該覆蓋: methe cho 6-@ . com `_ _ getitem _ _ `,支持為給定鍵提取數(shù)據(jù)樣本。子類還可以選擇覆蓋: methe cho 6-@ . com `_ _ len _ _ `,這將返回?cái)?shù)據(jù)集大小的許多: class cho 6-@ . com `~torch . utils . data . sampl:class: ` ~ torch . utils . data . data loader `的實(shí)現(xiàn)和默認(rèn)選項(xiàng).定義mydataset類:# import os from torch . utils . data import dataset from torch . utils . data . dataset import t _ co from pil import image class my datas: d: 得到了所有的圖像文件名:參數(shù)root_dir:數(shù)據(jù)路徑:參數(shù)lab:標(biāo)簽 self . root _ d . ir = root _ dir self . label _ dir = label _ dir self . data _ dir = os . path . join(self . root _ dir,self . label _ dir)self . img _ names = os . listdir(self . data _ dir)def _ _ getitem _ _(self,index)-t_co: 只能通過下標(biāo)訪問數(shù)據(jù)集:參數(shù)ind:下標(biāo):r: img(pil)。標(biāo)簽 img _ nam: return len(self . img _ names)使用我的數(shù)據(jù)集。class #以交互模式導(dǎo)入mydataset并執(zhí)行以下語句:
ants _ dataset = my dataset( 數(shù)據(jù)集/訓(xùn)練 , 螞蟻 )#創(chuàng)建數(shù)據(jù)集bees _ dataset = my dataset( 數(shù)據(jù)集/訓(xùn)練 , 蜜蜂 )img,label = ants _ dataset[47]# access data len(bees _ dataset)# get dataset length img . show# display picture dataset = ants _ dataset bees _ dataset # add dataset tensor board # tensor board可以用來記錄一些數(shù)據(jù)(標(biāo)量、圖片等。)以日志的形式在訓(xùn)練模型時(shí)生成,同時(shí)也可以將記錄的數(shù)據(jù)可視化,方便我們對(duì)現(xiàn)有模型進(jìn)行分析。比如借助tensorboard,記錄并可視化損失函數(shù)隨訓(xùn)練周期的下降過程。
要使用tensorboard,必須首先通過以下命令將其安裝在conda環(huán)境中:
conda安裝tensorboard記錄標(biāo)量)#記錄標(biāo)量需要使用torch.utils.tensorboard中summarywriter類的add_scalar方法
創(chuàng)建并執(zhí)行以下python腳本:
來自火炬。utils。tensorboard導(dǎo)入摘要writer #參數(shù):log_dir,存儲(chǔ)日志的路徑名writer = summary writer( 日志 )#記錄曲線y = x 2 #參數(shù)1:標(biāo)簽,數(shù)據(jù)標(biāo)識(shí)符#參數(shù)2: scalar_valu: writer中i的x軸數(shù)據(jù)。add _ scalar( y = x 2 ,i * * 2,i) #調(diào)用close確保數(shù)據(jù)刷新到磁盤寫入器。close腳本成功執(zhí)行后,將在腳本文件所在的當(dāng)前路徑下創(chuàng)建一個(gè)logs文件夾,并生成一個(gè)日志文件。成功生成日志后,在conda環(huán)境中執(zhí)行以下命令以啟動(dòng)tensorboard可視化服務(wù):
#指示日志存儲(chǔ)的位置和服務(wù)啟動(dòng)時(shí)使用的端口。默認(rèn)端口為6006#多人使用同一臺(tái)服務(wù)器時(shí),應(yīng)避免以下兩個(gè)tensorboa值相同的參數(shù)。rd-logdir = second/logs-port = 6008該命令成功執(zhí)行后,會(huì)給出以下提示:
在瀏覽器中打開上述鏈接以查看可視化效果:
注:tensorboard是根據(jù)不同標(biāo)簽可視化的。如果多次寫入的日志具有相同的標(biāo)記,可能會(huì)導(dǎo)致可視化出現(xiàn)問題。只需刪除日志并重新生成它,然后重新啟動(dòng)可視化服務(wù)。
image)# image可以通過使用torch.utils.tensorboard中summarywriter類的add_image方法來記錄
該方法定義如下:
def add _ image (self,tag,img _ tensor,global _ step = none,walltime = none,dataformats = chw )# tag (string)同上# img _ tensor (torch。張量、numpy.array或string/blobnam:圖像數(shù)據(jù)# global _ st: step # data formats(string):圖像數(shù)據(jù)格式。如果不是chw,您需要指定創(chuàng)建并執(zhí)行以下python腳本:
從torch.utils.tensorboard導(dǎo)入摘要writer從pil導(dǎo)入圖像import numpy as np writer = summary writer( 日志 )img _ path = e: \ \ code \ \ python \ \ py torch-learn \ \ dataset \ \ train \ \ ants \ \ 0013035 . jpg # img是pil . jpegimageplugin . jpegimagefile type img = image . open(img _ path)img _ array = np . array(img)writer . add _ image( 螞蟻 ,img _ array,0,數(shù)據(jù)格式= hwc )后編劇的劇本。close執(zhí)行成功,只需像之前一樣啟動(dòng)可視化服務(wù)。
如果日志中相同標(biāo)簽標(biāo)識(shí)的數(shù)據(jù)中有多個(gè)global _ steps,可以顯示如下:拖動(dòng)軸可以看到global _ steps之間的圖像變化。
轉(zhuǎn)換#transforms是位于torchvision包中的一個(gè)模塊,模塊中有很多類可以把要輸入神經(jīng)網(wǎng)絡(luò)的數(shù)據(jù)進(jìn)行轉(zhuǎn)換,以滿足網(wǎng)絡(luò)的需要。
totensor#這個(gè)類可以將pil圖像或numpy.ndarray轉(zhuǎn)換成張量,以便于將圖像輸入神經(jīng)網(wǎng)絡(luò)。下面的代碼做了一個(gè)簡(jiǎn)單的例子:將pil圖像轉(zhuǎn)換成張量后,借助tensorboard可以直接寫入日志文件:
從torchvision導(dǎo)入轉(zhuǎn)換從pil導(dǎo)入圖像從torch.utils.t:/code/python/pytorch-learn/dataset/train/.螞蟻 \ /6743948 _ 2b8c096dda.jpg img = image.open (img _ path) #將pil圖像轉(zhuǎn)換為張量類型# 1。劃分輸入數(shù)據(jù)形狀w,h,c —— c,w,h# 2。將所有數(shù)字除以255。將數(shù)據(jù)規(guī)范化到[0,1] #實(shí)例就像方法一樣被調(diào)用,其中實(shí)際調(diào)用的是totensor類中的__call__方法。# mor = trans _ to _ tensor(img)#將張量圖像寫入日志writer = summary writer( 日志 )使用tensorboard。writer . add _ image( img _張量 ,img _ tensor,1) writer.close啟動(dòng)tensorbard可視化服務(wù)并在瀏覽器中訪問:
normalize#normalize類可以通過均值和標(biāo)準(zhǔn)差來歸一化張量圖像。這種構(gòu)造函數(shù)定義如下:
#平均(sequenc:表示圖像的每個(gè)通道的平均值的序列# std (sequenc:表示圖像的每個(gè)通道的標(biāo)準(zhǔn)偏差的序列# inplac:是否原位修改tensordef _ _ init _ _ (self,mean,std,inplac:),這個(gè)類的具體歸一化操作如下:
output[channel]=(input[channel]-mean[channel])/std[channel]對(duì)于任意通道,如果原始值為[0,1],傳入均值為0.5,std為0.5,則歸一化運(yùn)算為:(value-0.5)/0.5 = 2*value-1,帶入。
創(chuàng)建并執(zhí)行以下腳本:
從torchvision導(dǎo)入轉(zhuǎn)換從pil導(dǎo)入圖像從torch.utils.tensorboard導(dǎo)入摘要writer#在轉(zhuǎn)換模塊trans _ to _ tenor = transforms中創(chuàng)建totensor類。totensor #在transforms模塊中創(chuàng)建normalize類。三個(gè)通道的均值和標(biāo)準(zhǔn)差都是0.5 trans _ normalize = transforms . normalize([0.5,0.5,0.5],[0.5,0.5],0.5)#獲取pil圖像img _ path = :/code/python/pytorch-learn/dataset/train/ants\ /6743948 _ 2b 8 c 096 dda . jpg img = image.open (img _ path) #將pil圖像轉(zhuǎn)換為張量類型# 1。將所有數(shù)字除以255,將數(shù)據(jù)歸一化為[0,1]img _ tensor = trans _ to _ tensor(img)# normalize tensor img _ normalize = trans _ normalize(img _ tensor)print(type(img _ normalize))# class ;火炬。張量與數(shù)學(xué)。;= #使用t。傳感器板將張量圖像寫入日志日志 )writer . add _ image( 正常化 ,img _ normalize,1) writer.clos:要求的序列或輸出大小#如果siz:創(chuàng)建并執(zhí)行以下腳本:
從torchvision導(dǎo)入轉(zhuǎn)換從pil導(dǎo)入圖像從torch.utils.tensorboard導(dǎo)入摘要writer# totensor在轉(zhuǎn)換模塊trans _ to _ tensor = transforms中創(chuàng)建的類。totensor #在轉(zhuǎn)換模塊trans _ normalize = transforms中創(chuàng)建normalize類。normalize ([0.5,0.5,0.5],[0.5,0.5]) #創(chuàng)建resize類trans _ resize _ 52x 52 = transforms . resize((80,40)) trans _ resize _ 52 = transforms。獲取pil圖像img _ path = :/code/python/pytorch-learn/dataset/train/ants\ /6743948 _ 2b8c096。mg_path)#用tensor board writer = summary writer( 日志 )#將pil圖像轉(zhuǎn)換成張量類型# 1。輸入數(shù)據(jù)形狀w,h,c —— c,w,h# 2。將所有數(shù)字除以255,將數(shù)據(jù)歸一化為[0,1]img _ tensor = trans _ to _ tensor(img)#第0步:記錄張量圖像writer . add _ image( img-tensor-normalize-resize _ 52x 52,img _ tensor,0)#歸一化張量img _ normalize = trans _ normalize(img _ tensor)#第一步:記錄歸一化的圖像寫入器。添加圖像( img-tensor-normalize-resize _ 52x 52,img_normalize,1)# resize img _ resize _ 52x 52 = trans _ resize _ 52x 52(img _ normalize)#第二步:記錄(52,52)圖像寫入器。調(diào)整大小后添加圖像( img-tensor-normalize-resize _ 52x 52,img _ resize _ 52x52,2) # pair (52,52)52)resize張量再次執(zhí)行(52)的resize img _ resize 52 = trans _ resize 52(img _ resize 52 x52)#第三步:記錄(52)。調(diào)整writer . add _ image( img-tensor-normalize-resize _ 52x 52,img _ resize _ 52,3) writer.close啟動(dòng)tensorbard可視化服務(wù)并在瀏覽器中訪問它:
compose#在上一節(jié)的例子中,我們的圖像處理基于一系列的變換。為了在每次轉(zhuǎn)換執(zhí)行后記錄圖像,我們?cè)诿績(jī)纱无D(zhuǎn)換之間插入一個(gè)圖像記錄操作。如果我們不這樣做。;不需要在每次轉(zhuǎn)換執(zhí)行后記錄圖像,那么我們可以通過compose順序執(zhí)行一系列轉(zhuǎn)換。
compose類的構(gòu)造函數(shù)定義如下:
# trans froms(list of transform objects): # list d:)這個(gè)類的核心思想在于__call__函數(shù):
def _ _ call _ _ (s: for t in s: img = t(img)return img with compose,為了得到上一節(jié)的最終結(jié)果,我們可以將上一節(jié)的腳本簡(jiǎn)化如下:
從torchvision導(dǎo)入轉(zhuǎn)換從pil導(dǎo)入圖像從torch.utils.tensorboard導(dǎo)入摘要writer# totensor在轉(zhuǎn)換模塊trans _ to _ tensor = transforms中創(chuàng)建的類。totensor #在轉(zhuǎn)換模塊trans _ normalize = transforms中創(chuàng)建normalize類。normalize ([0.5,0.5,0.5],[0.5,0.5]) #創(chuàng)建resize類trans _ resize _ 80x 40 = transforms . resize((80,40))trans _ resize _ 200 = transforms。resize (200) #創(chuàng)建合成類trans _ compose = transforms。compose ([trans _ to _ tensor,trans _ normalize,trans _ resize _ 80x40,trans _ resize _ 200]) #獲取pil圖像img _ path = e:/code/python/pytorch-learn/dataset/train/ants\ /6743948 _ 2b8c096dda。g _ path)# execute compose img _ compose = trans _ compose(img)# record合成的圖像writer = summary writer( 日志 ).writer . add _ image( img-tensor-normalize-resize _ 80x 40-resize _ 200,img _ compose,0) writer.close啟動(dòng)tensorbard可視化服務(wù)并在瀏覽器中訪問它:
transforms模塊中還有許多其他轉(zhuǎn)換類,它們可以在需要時(shí)查詢文檔。在查詢文檔時(shí),理解類或函數(shù)的功能以及輸入和輸出類型是很重要的。
dataset和transforms的組合使用#在pytorch官網(wǎng)首頁(yè),按照不同的模塊提供文檔,包括:核心模塊、音頻模塊、文本模塊、可視化模塊等。
在torchvision.datasets中,提供了對(duì)常用數(shù)據(jù)集的支持,可以幫助我們快速下載、解壓和使用數(shù)據(jù)集:
以cifar10數(shù)據(jù)集為例,按照以下文檔使用:
根據(jù)文檔,如果要使用cifar10數(shù)據(jù)集,應(yīng)該使用torch vision . datasets . cifs ar 10類,該類的構(gòu)造函數(shù)需要傳入以下參數(shù):
# root (string):數(shù)據(jù)集所在的目錄# train (bool,可選):tru測(cè)試集# transform (callable,optional):function/transform # target _ transform(callable,optional):目標(biāo)的function/transform # download(bool,optional):如果為tru: str,train: bool = tru: optional[callabl: optional[callabl:·布爾= fals:根據(jù)文檔中的__getitem__函數(shù),我們知道當(dāng)通過下標(biāo)訪問dataset時(shí),會(huì)返回(image,target),即圖像和圖像的類型。
創(chuàng)建以下腳本并執(zhí)行它(使用訓(xùn)練集):
導(dǎo)入torchvision# create transform,將pil圖像轉(zhuǎn)換為tensor data _ trans = torch vision . transforms . pose([torch vision . transforms . tot tensor])#建議下載始終為真。即使提前下載了數(shù)據(jù)集,如果下載很慢也可以復(fù)制控制臺(tái)輸出的下載地址。然后在迅雷#下載后將壓縮包復(fù)制到根目錄下下載地址也可以在火炬威視的源代碼中查看。數(shù)據(jù)集。cifar10train _ set =火炬視覺。數(shù)據(jù)集。cifar 10(root = 。/dataset ,train = true,transform=data_trans,download=true)img,target = train _ set[0]print(type(img))# class ;torch.tensor = 打印(目標(biāo))# 6 #[ ;飛機(jī)與航空。;, 汽車與汽車。;,鳥 , 貓和貓。;, 鹿 , 狗和貓。;, 青蛙 , 馬與馬。;, 船舶與航空公司。;, 卡車 ]打印(train _ set。classes [target]) # frog創(chuàng)建以下腳本并執(zhí)行它(。
import torch vision test _ set = torch vision . datasets . cifar 10(root = 。/dataset ,train=false,download=true)img,target = test _ set[0]img . showprint(type(img))# class ;pil.image.image= print(target)# 3 print(test _ set . class[target]) # catdataload:數(shù)據(jù)集類型dataset # batch _ siz:批處理大小,d: 1 # shuffl:每個(gè)歷元后是否打亂數(shù)據(jù),d: fals:加載數(shù)據(jù)時(shí)使用的子進(jìn)程數(shù),0表示使用主進(jìn)程加載,d: 0。可以設(shè)置為cpu核心數(shù)# drop _ last (bool,可選):)。當(dāng)數(shù)據(jù)個(gè)數(shù)不能被batch_siz:為fals: dataset[t _ co],batch_siz:可選[int] = 1,shuffl: bool = fals:可選[sampler] = none,batch_sampl:可選[sampler[sequenc: int = 0,collat:可選[_collate_fn_t] = none,pin _ memory: bool = false,drop _ last : boo= none,multiprocessing _ context = none,generator = none,*,prefetch_factor: int = 2,p:布爾= fals:創(chuàng)建并執(zhí)行以下腳本:
導(dǎo)入torch vision from torch . utils . data導(dǎo)入dataloader from torch . utils . t: trans _ to _ tensor = torch vision . transforms . to tensortrain _ dataset = torch vision . datasets . cifar 10(root = 。/dataset ,transform = trans _ to _ tensor,download = true) # dataloader根據(jù)batch_size封裝img和target,如圖,train _ data loader = data loader(dataset = train _ dataset,batch_siz:的epoch,用于:的batch_num,batch _ data # print(epoch, : ,batch_num) batch_img,batch _ target = batch _ data writer . add _ images( cifr 10-batch 64-no _ shuffle-no _ drop _ last _ epoch { } 。format (epoch),batch _ img,batch _ num)writer . clos:。如果num_work: #這里插入了訓(xùn)練網(wǎng)絡(luò)的全部代碼...if _ _ nam:火車2。如果修改后代碼仍然報(bào)錯(cuò):os:[win:。
進(jìn)入高級(jí)系統(tǒng)設(shè)置:
點(diǎn)擊:高級(jí)-性能-設(shè)置:
點(diǎn)擊:高級(jí)-更改:
如下圖,將d盤的虛擬內(nèi)存調(diào)整到系統(tǒng)管理的大小(重啟后生效)。如果仍然報(bào)告錯(cuò)誤,您可以將大小自定義為一個(gè)很大的值,如10gb-100gb。
腳本成功執(zhí)行后,啟動(dòng)tensorbard可視化服務(wù)并在瀏覽器中訪問:
step=170時(shí):(日志比較大,可能需要在瀏覽器中刷新幾次才能加載滿)。
當(dāng)步驟=781時(shí):
因?yàn)槲覀冊(cè)趧?chuàng)建dataloader時(shí),參數(shù)shuffle=false使得epoch0和epoch1之間同批次提取的圖像相同;參數(shù)drop_last=false使最后一批圖像即使小于64也不會(huì)被丟棄。
修改腳本,使shuffle=true,drop_last=true,然后再次執(zhí)行腳本并啟動(dòng)tensorbard可視化服務(wù)。瀏覽器中的訪問結(jié)果如下:
如圖所示,從epoch0和epoch1之間的同一批次(780)提取的圖像不同,丟棄第781批次。
標(biāo)簽:
圖像數(shù)據(jù)
了解更多py torch . data(py torch中的數(shù)據(jù)集)相關(guān)內(nèi)容請(qǐng)關(guān)注本站點(diǎn)。
手機(jī)如何控制電腦操作(手機(jī)怎么控制電腦屏幕,電腦不用操作)
靜電導(dǎo)致電腦死機(jī)(電腦老有靜電怎么回事)
shell環(huán)境變量的定義(shell設(shè)置環(huán)境變量即時(shí)生效)
ipad air3重量是多少(iPadair3重量多少千克)
win10系統(tǒng)u盤安裝制作(win10系統(tǒng)u盤安裝步驟)
pytorch .data(pytorch中dataset)
5g網(wǎng)絡(luò)什么時(shí)候普及全國(guó)2020(5g網(wǎng)絡(luò)什么時(shí)候普及全國(guó)各地)
聯(lián)想拯救者保修條例(聯(lián)想拯救者保修期幾年)
微信怎么讓訂閱號(hào)成列表狀態(tài)(訂閱號(hào)信息怎么變成列表)
oppo的輸入法怎么切換(oppo手機(jī)輸入法怎么切換別的輸入法)
戴爾筆記本電源鍵亮但黑屏怎么辦,我的戴爾筆記本黑屏怎么辦但是還在運(yùn)作開機(jī)的指示燈也亮著 搜
ps軟件官方下載中文版手機(jī)版(ps軟件下載手機(jī)版安卓官方網(wǎng)站)
oppor9splus怎么截屏
ipad屏幕不亮但有聲音觸屏能用(ipad屏幕不亮但有聲音換屏幕要多少錢)
諾基亞6120ci怎么下載軟件,怎么在諾基亞上下載軟件
格力風(fēng)管機(jī)e6是什么故障如何排除的(格力風(fēng)管機(jī)e6是什么故障代碼怎么解決)
臺(tái)式機(jī)硬盤如何重新分區(qū),電腦硬盤重新分區(qū)方法
電腦硬盤維修費(fèi)用大概是多少錢啊
手機(jī)如何做無線攝像頭(手機(jī)做wifi攝像頭)
oppo怎么關(guān)相機(jī)的聲音了,怎么關(guān)閉相機(jī)拍照的聲音