PyTorch如何創(chuàng)建自己的數(shù)據(jù)集
PyTorch創(chuàng)建自己的數(shù)據(jù)集
圖片文件在同一的文件夾下
思路是繼承 torch.utils.data.Dataset,并重點(diǎn)重寫(xiě)其 __getitem__方法,示例代碼如下:
class ImageFolder(Dataset):
? ? def __init__(self, folder_path):
? ? ? ? self.files = sorted(glob.glob('%s/*.*' % folder_path))
? ? def __getitem__(self, index):
? ? ? ? path = self.files[index % len(self.files)]
? ? ? ? img = np.array(Image.open(path))
? ? ? ? h, w, c = img.shape
? ? ? ? pad = ((40, 40), (4, 4), (0, 0))
? ? ? ? # img = np.pad(img, pad, 'constant', constant_values=0) / 255
? ? ? ? img = np.pad(img, pad, mode='edge') / 255.0
? ? ? ? img = torch.from_numpy(img).float()
? ? ? ? patches = np.reshape(img, (3, 10, 128, 11, 128))
? ? ? ? patches = np.transpose(patches, (0, 1, 3, 2, 4))
? ? ? ? return img, patches, path
? ? def __len__(self):
? ? ? ? return len(self.files)圖片文件在不同的文件夾下
比如我們有數(shù)據(jù)如下:
─── data
├── train
│ ├── 0.jpg
│ └── 1.jpg
├── test
│ ├── 0.jpg
│ └── 1.jpg
└── val
├── 1.jpg
└── 2.jpg
此時(shí)我們只需要將以上代碼稍作修改即可,修改的代碼如下:
self.files = sorted(glob.glob('%s/**/*.*' % folder_path, recursive=True))其他代碼不變。
pytorch常用數(shù)據(jù)集的使用
對(duì)于pytorch數(shù)據(jù)集的使用,示例代碼如下:
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import Compose
from torchvision import transforms
import torchvision
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
dataset_transform = Compose([transforms.ToTensor()])
# 關(guān)于官方數(shù)據(jù)集的使用還是關(guān)鍵要看pytorch的官方文檔
train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=False,transform=dataset_transform,download=True)
# 查看測(cè)試數(shù)據(jù)集中的第一個(gè)數(shù)據(jù)
# print(test_set[0])
# 查看測(cè)試數(shù)據(jù)集中的分類(lèi)情況
# print(test_set.classes)
#
# 取出第一個(gè)數(shù)據(jù)中的圖片(img)和分類(lèi)結(jié)果(target)
# img,target = test_set[0]
# 查看圖片數(shù)據(jù)的類(lèi)型
# print(img)
# print(target)
# 輸出類(lèi)別
# print(test_set.classes[target])
# 查看圖片
# img.show()
# 使用tensorboard顯示tensor數(shù)據(jù)類(lèi)型的圖片
writer = SummaryWriter("logs")
for i in range(10):
# 取出數(shù)據(jù)中的圖片(img)和分類(lèi)結(jié)果(target)
img,target = test_set[i]
writer.add_image("test_set",img,i)
writer.close()上述代碼運(yùn)行結(jié)果在tensorboard可視化:

代碼
train_set = torchvision.datasets.CIFAR10(root="./CIFAR10",train=True,transform=dataset_transform,download=True)
常用參數(shù)講解
root:根目錄,存放數(shù)據(jù)集的位置train:若為T(mén)rue,則劃分為訓(xùn)練數(shù)據(jù)集,若為False,則劃分為測(cè)試數(shù)據(jù)集transform:指定輸入數(shù)據(jù)集處理方式download:若為T(mén)rue,則會(huì)將數(shù)據(jù)集下載到root指定的目錄下,否則不會(huì)下載
官方文檔對(duì)參數(shù)的解釋?zhuān)?/p>
root (string) – Root directory of dataset where directory cifar-10-batches-py exists or will be saved to if download is set to True.
train (bool, optional) – If True, creates dataset from training set, otherwise creates from test set.
transform (callable, optional) – A function/transform that takes in an PIL image and returns a transformed version. E.g, transforms.RandomCrop
target_transform (callable, optional) – A function/transform that takes in the target and transforms it.
download (bool, optional) – If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again.
注意:
- 關(guān)于官方數(shù)據(jù)集的使用還是關(guān)鍵要看pytorch的官方文檔
- 下載數(shù)據(jù)集的細(xì)節(jié)之處:知道下載鏈接(下載鏈接可以在源碼中查看)之后可以不用使用代碼下載了,使用迅雷來(lái)下載可能會(huì)更快。
- 要學(xué)會(huì)使用Pycharm中的ctrl+p和ctrl+alt這兩個(gè)快捷鍵
- pytorch官網(wǎng)
- pytorch官方數(shù)據(jù)集(下載數(shù)據(jù)集方法)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python爬蟲(chóng) requests-html的使用
這篇文章主要介紹了python爬蟲(chóng) requests-html的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python調(diào)用win32接口進(jìn)行截圖的示例
這篇文章主要介紹了python調(diào)用win32接口進(jìn)行截圖的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
完美解決pycharm導(dǎo)入自己寫(xiě)的py文件爆紅問(wèn)題
今天小編就為大家分享一篇完美解決pycharm導(dǎo)入自己寫(xiě)的py文件爆紅問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
用Python實(shí)現(xiàn)zip密碼破解實(shí)例
大家好,本篇文章主要講的是用Python實(shí)現(xiàn)zip密碼破解實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
python 遠(yuǎn)程統(tǒng)計(jì)文件代碼分享
享一個(gè)Python獲取遠(yuǎn)程文件大小的函數(shù)代碼,簡(jiǎn)單實(shí)用,是學(xué)習(xí)Python編程的基礎(chǔ)實(shí)例。2015-05-05
Python PaddlePaddle機(jī)器學(xué)習(xí)之求解線性模型
這篇文章主要介紹了Python PaddlePaddle機(jī)器學(xué)習(xí)之求解線性模型,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
使用Protocol Buffers的C語(yǔ)言拓展提速Python程序的示例
這篇文章主要介紹了使用Protocol Buffers的C語(yǔ)言拓展提速Python程序的示例,使用C拓展Python是Python編程進(jìn)階中的重要技巧,需要的朋友可以參考下2015-04-04

