pytorch深度神經(jīng)網(wǎng)絡(luò)入門準(zhǔn)備自己的圖片數(shù)據(jù)
正文
圖片數(shù)據(jù)一般有兩種情況:
1、所有圖片放在一個(gè)文件夾內(nèi),另外有一個(gè)txt文件顯示標(biāo)簽。
2、不同類別的圖片放在不同的文件夾內(nèi),文件夾就是圖片的類別。
針對(duì)這兩種不同的情況,數(shù)據(jù)集的準(zhǔn)備也不相同,第一種情況可以自定義一個(gè)Dataset,第二種情況直接調(diào)用torchvision.datasets.ImageFolder來(lái)處理。下面分別進(jìn)行說(shuō)明:
一、所有圖片放在一個(gè)文件夾內(nèi)
這里以mnist數(shù)據(jù)集的10000個(gè)test為例, 我先把test集的10000個(gè)圖片保存出來(lái),并生著對(duì)應(yīng)的txt標(biāo)簽文件。
先在當(dāng)前目錄創(chuàng)建一個(gè)空文件夾mnist_test, 用于保存10000張圖片,接著運(yùn)行代碼:
import torch
import torchvision
import matplotlib.pyplot as plt
from skimage import io
mnist_test= torchvision.datasets.MNIST(
'./mnist', train=False, download=True
)
print('test set:', len(mnist_test))
f=open('mnist_test.txt','w')
for i,(img,label) in enumerate(mnist_test):
img_path="./mnist_test/"+str(i)+".jpg"
io.imsave(img_path,img)
f.write(img_path+' '+str(label)+'\n')
f.close()經(jīng)過上面的操作,10000張圖片就保存在mnist_test文件夾里了,并在當(dāng)前目錄下生成了一個(gè)mnist_test.txt的文件,大致如下:

前期工作就裝備好了,接著就進(jìn)入正題了:
from torchvision import transforms, utils
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt
from PIL import Image
def default_loader(path):
return Image.open(path).convert('RGB')
class MyDataset(Dataset):
def __init__(self, txt, transform=None, target_transform=None, loader=default_loader):
fh = open(txt, 'r')
imgs = []
for line in fh:
line = line.strip('\n')
line = line.rstrip()
words = line.split()
imgs.append((words[0],int(words[1])))
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
self.loader = loader
def __getitem__(self, index):
fn, label = self.imgs[index]
img = self.loader(fn)
if self.transform is not None:
img = self.transform(img)
return img,label
def __len__(self):
return len(self.imgs)
train_data=MyDataset(txt='mnist_test.txt', transform=transforms.ToTensor())
data_loader = DataLoader(train_data, batch_size=100,shuffle=True)
print(len(data_loader))
def show_batch(imgs):
grid = utils.make_grid(imgs)
plt.imshow(grid.numpy().transpose((1, 2, 0)))
plt.title('Batch from dataloader')
for i, (batch_x, batch_y) in enumerate(data_loader):
if(i<4):
print(i, batch_x.size(),batch_y.size())
show_batch(batch_x)
plt.axis('off')
plt.show()自定義了一個(gè)MyDataset, 繼承自torch.utils.data.Dataset。然后利用torch.utils.data.DataLoader將整個(gè)數(shù)據(jù)集分成多個(gè)批次。
二、不同類別的圖片放在不同的文件夾內(nèi)
同樣先準(zhǔn)備數(shù)據(jù),這里以flowers數(shù)據(jù)集為例
提取 鏈接: https://pan.baidu.com/s/1dcAsOOZpUfWNYR77JGXPHA?pwd=mwg6
花總共有五類,分別放在5個(gè)文件夾下。大致如下圖:

我的路徑是d:/flowers/.
數(shù)據(jù)準(zhǔn)備好了,就開始準(zhǔn)備Dataset吧,這里直接調(diào)用torchvision里面的ImageFolder
import torch
import torchvision
from torchvision import transforms, utils
import matplotlib.pyplot as plt
img_data = torchvision.datasets.ImageFolder('D:/bnu/database/flower',
transform=transforms.Compose([
transforms.Scale(256),
transforms.CenterCrop(224),
transforms.ToTensor()])
)
print(len(img_data))
data_loader = torch.utils.data.DataLoader(img_data, batch_size=20,shuffle=True)
print(len(data_loader))
def show_batch(imgs):
grid = utils.make_grid(imgs,nrow=5)
plt.imshow(grid.numpy().transpose((1, 2, 0)))
plt.title('Batch from dataloader')
for i, (batch_x, batch_y) in enumerate(data_loader):
if(i<4):
print(i, batch_x.size(), batch_y.size())
show_batch(batch_x)
plt.axis('off')
plt.show()以上就是pytorch深度神經(jīng)網(wǎng)絡(luò)入門準(zhǔn)備自己的圖片數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于pytorch圖片數(shù)據(jù)準(zhǔn)備的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- PyTorch中的神經(jīng)網(wǎng)絡(luò) Mnist 分類任務(wù)
- 使用Pytorch構(gòu)建第一個(gè)神經(jīng)網(wǎng)絡(luò)模型?附案例實(shí)戰(zhàn)
- pytorch簡(jiǎn)單實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)功能
- Pytorch卷積神經(jīng)網(wǎng)絡(luò)遷移學(xué)習(xí)的目標(biāo)及好處
- Pytorch深度學(xué)習(xí)經(jīng)典卷積神經(jīng)網(wǎng)絡(luò)resnet模塊訓(xùn)練
- Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實(shí)踐
- PyTorch實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的搭建詳解
- Pytorch神經(jīng)網(wǎng)絡(luò)參數(shù)管理方法詳細(xì)講解
相關(guān)文章
python 對(duì)給定可迭代集合統(tǒng)計(jì)出現(xiàn)頻率,并排序的方法
今天小編就為大家分享一篇python 對(duì)給定可迭代集合統(tǒng)計(jì)出現(xiàn)頻率,并排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-10-10
Python實(shí)戰(zhàn)之IQ測(cè)試系統(tǒng)的實(shí)現(xiàn)
通常,智商測(cè)試測(cè)驗(yàn)一個(gè)人在數(shù)字、空間、邏輯、詞匯、創(chuàng)造、記憶等方面的能力。本文將利用Python實(shí)現(xiàn)一個(gè)IQ測(cè)試系統(tǒng),感興趣的可以了解一下2022-09-09
python數(shù)據(jù)可視化plt庫(kù)實(shí)例詳解
這篇文章主要介紹了python可視化數(shù)據(jù)plt庫(kù)實(shí)例,下面使用pycharm環(huán)境給大家詳細(xì)介紹,文中提到j(luò)upyter和pycharm環(huán)境的差別,需要的朋友可以參考下2021-06-06
OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn)
今天小編就為大家分享一篇OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-11-11
python中return的返回和執(zhí)行實(shí)例
今天小編就為大家分享一篇python中return的返回和執(zhí)行實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12
教你如何用python操作攝像頭以及對(duì)視頻流的處理
這篇文章主要介紹了教你如何用python操作攝像頭以及對(duì)視頻流的處理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

