PyTorch
作為一款深度學(xué)習(xí)框架,已經(jīng)幫助我們實(shí)現(xiàn)了很多很多的功能了,包括數(shù)據(jù)的讀取和轉(zhuǎn)換了,那么這一章節(jié)就介紹一下PyTorch
內(nèi)置的數(shù)據(jù)讀取模塊吧
import zipfile # 解壓 import pandas as pd # 操作數(shù)據(jù) import os # 操作文件或文件夾 import cv2 # 圖像操作庫(kù) import matplotlib.pyplot as plt # 圖像展示庫(kù) from torch.utils.data import Dataset # PyTorch內(nèi)置對(duì)象 from torchvision import transforms # 圖像增廣轉(zhuǎn)換庫(kù) PyTorch內(nèi)置 import torch
數(shù)據(jù)下載到此處
我們先初步編寫一個(gè)腳本來(lái)實(shí)現(xiàn)圖片的展示
# 解壓文件到指定目錄 def unzip_file(root_path, filename): full_path = os.path.join(root_path, filename) file = zipfile.ZipFile(full_path) file.extractall(root_path) unzip_file(root_path, zip_filename) # 讀入csv文件 face_landmarks = pd.read_csv(os.path.join(extract_path, csv_filename)) # pandas讀出的數(shù)據(jù)如想要操作索引 使用iloc image_name = face_landmarks.iloc[:,0] landmarks = face_landmarks.iloc[:,1:] # 展示 def show_face(extract_path, image_file, face_landmark): plt.imshow(plt.imread(os.path.join(extract_path, image_file)), cmap='gray') point_x = face_landmark.to_numpy()[0::2] point_y = face_landmark.to_numpy()[1::2] plt.scatter(point_x, point_y, c='r', s=6) show_face(extract_path, image_name.iloc[1], landmarks.iloc[1])
實(shí)現(xiàn)MyDataset
使用內(nèi)置庫(kù)是我們的代碼更加的規(guī)范,并且可讀性也大大增加
繼承Dataset,需要我們實(shí)現(xiàn)的有兩個(gè)地方:
__len__
返回?cái)?shù)據(jù)的長(zhǎng)度,實(shí)例化調(diào)用len()
時(shí)返回__getitem__
給定數(shù)據(jù)的索引返回對(duì)應(yīng)索引的數(shù)據(jù)如:a[0]transform
數(shù)據(jù)的額外操作時(shí)調(diào)用class FaceDataset(Dataset): def __init__(self, extract_path, csv_filename, transform=None): super(FaceDataset, self).__init__() self.extract_path = extract_path self.csv_filename = csv_filename self.transform = transform self.face_landmarks = pd.read_csv(os.path.join(extract_path, csv_filename)) def __len__(self): return len(self.face_landmarks) def __getitem__(self, idx): image_name = self.face_landmarks.iloc[idx,0] landmarks = self.face_landmarks.iloc[idx,1:].astype('float32') point_x = landmarks.to_numpy()[0::2] point_y = landmarks.to_numpy()[1::2] image = plt.imread(os.path.join(self.extract_path, image_name)) sample = {'image':image, 'point_x':point_x, 'point_y':point_y} if self.transform is not None: sample = self.transform(sample) return sample
測(cè)試功能是否正常
face_dataset = FaceDataset(extract_path, csv_filename) sample = face_dataset[0] plt.imshow(sample['image'], cmap='gray') plt.scatter(sample['point_x'], sample['point_y'], c='r', s=2) plt.title('face')
內(nèi)置的在torchvision.transforms
模塊下,由于我們的數(shù)據(jù)結(jié)構(gòu)不能滿足內(nèi)置模塊的要求,我們就必須自己實(shí)現(xiàn)
圖片的縮放,由于縮放后人臉的標(biāo)注位置也應(yīng)該發(fā)生對(duì)應(yīng)的變化,所以要自己實(shí)現(xiàn)對(duì)應(yīng)的變化
class Rescale(object): def __init__(self, out_size): assert isinstance(out_size,tuple) or isinstance(out_size,int), 'out size isinstance int or tuple' self.out_size = out_size def __call__(self, sample): image, point_x, point_y = sample['image'], sample['point_x'], sample['point_y'] new_h, new_w = self.out_size if isinstance(self.out_size,tuple) else (self.out_size, self.out_size) new_image = cv2.resize(image,(new_w, new_h)) h, w = image.shape[0:2] new_y = new_h / h * point_y new_x = new_w / w * point_x return {'image':new_image, 'point_x':new_x, 'point_y':new_y}
將數(shù)據(jù)轉(zhuǎn)換為torch
認(rèn)識(shí)的數(shù)據(jù)格式因此,就必須轉(zhuǎn)換為tensor
注意
: cv2
和matplotlib
讀出的圖片默認(rèn)的shape為N H W C
,而torch
默認(rèn)接受的是N C H W
因此使用tanspose
轉(zhuǎn)換維度,torch
轉(zhuǎn)換多維度使用permute
class ToTensor(object): def __call__(self, sample): image, point_x, point_y = sample['image'], sample['point_x'], sample['point_y'] new_image = image.transpose((2,0,1)) return {'image':torch.from_numpy(new_image), 'point_x':torch.from_numpy(point_x), 'point_y':torch.from_numpy(point_y)}
測(cè)試
transform = transforms.Compose([Rescale((1024, 512)), ToTensor()]) face_dataset = FaceDataset(extract_path, csv_filename, transform=transform) sample = face_dataset[0] plt.imshow(sample['image'].permute((1,2,0)), cmap='gray') plt.scatter(sample['point_x'], sample['point_y'], c='r', s=2) plt.title('face')
data_loader = DataLoader(face_dataset, batch_size=4, shuffle=True, num_workers=0) for i in data_loader: print(i['image'].shape) break
torch.Size([4, 3, 1024, 512])
注意
: windows
環(huán)境盡量不使用num_workers
會(huì)發(fā)生報(bào)錯(cuò)
這節(jié)使用內(nèi)置的數(shù)據(jù)讀取模塊,幫助我們規(guī)范代碼,也幫助我們簡(jiǎn)化代碼,加速讀取數(shù)據(jù)也可以加速訓(xùn)練,數(shù)據(jù)的增廣可以大大的增加我們的訓(xùn)練精度,所以本節(jié)也是訓(xùn)練中比較重要環(huán)節(jié)
到此這篇關(guān)于PyTorch數(shù)據(jù)讀取的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyTorch數(shù)據(jù)讀取內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:錦州 安慶 日照 天水 西安 白城 隨州 股票
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyTorch數(shù)據(jù)讀取的實(shí)現(xiàn)示例》,本文關(guān)鍵詞 PyTorch,數(shù)據(jù),讀,取的,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。