Pytorch實戰(zhàn)之?dāng)?shù)據(jù)加載和處理詳解
Pytorch提供了許多工具來簡化和希望數(shù)據(jù)加載,使代碼更具可讀性。這里將專門講述transforms數(shù)據(jù)預(yù)處理方法,即數(shù)據(jù)增強。
數(shù)據(jù)增強又稱為數(shù)據(jù)增廣、數(shù)據(jù)擴增,它是對訓(xùn)練集進(jìn)行變換,使訓(xùn)練集更豐富,從而讓模型更具泛化能力。
# 在進(jìn)行下面代碼學(xué)習(xí)前需要安裝torchvision==0.8.2 !pip install torchvision==0.8.2 --user
from PIL import Image
from torchvision import transforms as T
import torch as t
to_tensor = T.ToTensor()
to_pil = T.ToPILImage()
cat = Image.open('./cat.jpeg') transforms——Crop


# torchvision.transforms.CenterCrop transforms = T.Compose([T.Resize(224),T.CenterCrop(224),T.ToTensor()]) # Resize:縮放 cat_t = transforms(cat) # 傳入transforms中的數(shù)據(jù)是PIL數(shù)據(jù),lena_t為tensor cat_t.shape # 3*224*224 ; 當(dāng)T.CenterCrop()的參數(shù)大于T.Resize()的參數(shù)時,周圍用0填充 to_pil(cat_t)

# torchvision.transforms.RandomCrop transforms = T.Compose([T.Resize(224),T.RandomCrop(224, padding=(16, 64)),T.ToTensor()]) # Resize:縮放 cat_t = transforms(cat) # 傳入transforms中的數(shù)據(jù)是PIL數(shù)據(jù),lena_t為tensor cat_t.shape # 3*224*224 ; 當(dāng)T.CenterCrop()的參數(shù)大于T.Resize()的參數(shù)時,周圍用0填充 to_pil(cat_t)

transforms——Flip

# torchvision.transforms.RandomHorizontalFlip transforms = T.Compose([T.Resize(224),T.RandomHorizontalFlip(p=0.5),T.ToTensor()]) # Resize:縮放 cat_t = transforms(cat) # 傳入transforms中的數(shù)據(jù)是PIL數(shù)據(jù),lena_t為tensor cat_t.shape # 3*224*224 ; 當(dāng)T.CenterCrop()的參數(shù)大于T.Resize()的參數(shù)時,周圍用0填充 to_pil(cat_t)

# torchvision.transforms.RandomRotation transforms = T.Compose([T.Resize(224),T.RandomRotation(30, center=(0, 0), expand=True),T.ToTensor()]) # Resize:縮放 cat_t = transforms(cat) # 傳入transforms中的數(shù)據(jù)是PIL數(shù)據(jù),lena_t為tensor cat_t.shape # 3*224*224 ; 當(dāng)T.CenterCrop()的參數(shù)大于T.Resize()的參數(shù)時,周圍用0填充 to_pil(cat_t)

圖像變換


transforms的操作

自定義transforms
自定義transforms要素:
1.僅接收一個參數(shù),返回一個參數(shù)
2.注意上下游的輸出與輸入
class Compose(object):
def __call__(self, img):
for t in transforms:
img = t(img)
return img通過類實現(xiàn)多參數(shù)傳入:
class YourTransforms(object):
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img):
for t in self.transforms:
img = t(img)
return img椒鹽噪聲又稱為脈沖噪聲,是一種隨機出現(xiàn)的白點或者黑點,白點稱為鹽噪聲,黑色為椒噪聲。
信噪比(Signal-Noise Rate,SNR)是衡量噪聲的比例,圖像中為圖像像素的占比。
class AddPepperNoise(object):
def __init__(self, snr, p):
self.snr = snr
self.p = p
def __call__(self, img):
# 添加椒鹽噪聲具體實現(xiàn)過程
img = None
return imgtransforms方法

到此這篇關(guān)于Pytorch實戰(zhàn)之?dāng)?shù)據(jù)加載和處理詳解的文章就介紹到這了,更多相關(guān)Pytorch數(shù)據(jù)加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練實例
在本篇文章里小編給大家整理的是關(guān)于Pytorch修改ResNet模型全連接層進(jìn)行直接訓(xùn)練相關(guān)知識點,有需要的朋友們參考下。2019-09-09
python包pdfkit(wkhtmltopdf)?將HTML轉(zhuǎn)換為PDF的操作方法
pdfkit,把HTML+CSS格式的文件轉(zhuǎn)換成PDF格式文檔的一種工具。它就是html轉(zhuǎn)成pdf工具包wkhtmltopdf的Python封裝。所以,必須手動安裝wkhtmltopdf,這篇文章主要介紹了python包pdfkit(wkhtmltopdf)將HTML轉(zhuǎn)換為PDF,需要的朋友可以參考下2022-04-04
python中l(wèi)ambda函數(shù) list comprehension 和 zip函數(shù)使用指南
這篇文章主要介紹了python中l(wèi)ambda函數(shù) list comprehension 和 zip函數(shù)使用方法,非常的實用,有需要的朋友可以參考下2014-09-09
Python利用多線程優(yōu)化for循環(huán)的技巧分享
多線程可以讓程序同時執(zhí)行多個任務(wù),從而提高整體運行效率,這篇文章將詳細(xì)介紹如何在Python中使用多線程來優(yōu)化for循環(huán),感興趣的可以了解下2025-02-02
django使用F方法更新一個對象多個對象字段的實現(xiàn)
這篇文章主要介紹了django使用F方法更新一個對象多個對象字段的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python wxPython創(chuàng)建文件復(fù)制工具
wxPython是一個功能強大的 GUI 框架,它允許開發(fā)者通過 Python 輕松構(gòu)建跨平臺的桌面應(yīng)用,本文將使用wxPython創(chuàng)建一個文件復(fù)制工具,感興趣的可以了解下2025-02-02
零基礎(chǔ)使用Python讀寫處理Excel表格的方法
這篇文章主要介紹了Python讀寫處理Excel表格,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

