Pytorch使用transforms
首先,這次講解的tansforms功能,通俗地講,類(lèi)似于在計(jì)算機(jī)視覺(jué)流程里的圖像預(yù)處理部分的數(shù)據(jù)增強(qiáng)。
transforms的原理:
說(shuō)明:圖片(輸入)通過(guò)工具得到結(jié)果(輸出),這個(gè)工具,就是transforms模板工具,(tool=transforms.ToTensor()具體工具),使用工具result=tool(圖片)

tansforms的調(diào)用與使用,由下圖可得:
- 先創(chuàng)建一個(gè)
transforms.Tensor(),使用from torchvision import transforms調(diào)包 transforms去調(diào)init函數(shù)- init去調(diào)用真正的
transforms類(lèi),里面就有很多的方法(綠色五角星標(biāo)注),例如:resize,ToTensor,CenterCrop(從這些方法可以看出,許多都是數(shù)據(jù)增強(qiáng)的方法)。

接下來(lái),上代碼:
import os from torchvision import transforms from PIL import Image root_path = "D:\\data\\basic\\Image" label_path = "aligned" # 1.獲取aligned第一張圖的名字 img_dir = os.path.join(root_path, label_path) img_list = os.listdir(img_dir) img_path = img_list[0] # 2.獲取aligned第一張圖的路徑 img = os.path.join(root_path, label_path, img_path) # 3.使用python自帶的PIL獲取圖片 img = Image.open(img) # 4.將PIL利用transforms轉(zhuǎn)換成ToTensor to_tensor = transforms.ToTensor() # 創(chuàng)建totensor () img = to_tensor(img) # 使用to_tensor直接將圖片的PIL轉(zhuǎn)化為tensor print(img) # transforms
代碼結(jié)果:

相關(guān)文章
pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法
今天小編就為大家分享一篇pyqt 實(shí)現(xiàn)QlineEdit 輸入密碼顯示成圓點(diǎn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Centos下實(shí)現(xiàn)安裝Python3.6和Python2共存
這篇文章主要介紹了Centos下實(shí)現(xiàn)安裝Python3.6和Python2共存,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Python變量及數(shù)據(jù)類(lèi)型用法原理匯總
這篇文章主要介紹了Python變量及數(shù)據(jù)類(lèi)型用法原理匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
基于opencv和pillow實(shí)現(xiàn)人臉識(shí)別系統(tǒng)(附demo)
人臉識(shí)別就是一個(gè)程序能識(shí)別給定圖像或視頻中的人臉,本文主要介紹了opencv和pillow實(shí)現(xiàn)人臉識(shí)別系統(tǒng),本文不涉及分類(lèi)器、訓(xùn)練識(shí)別器等算法原理,感興趣的可以了解一下2021-11-11

