Pytorch使用transforms
更新時間:2022年01月25日 12:53:44 作者:游客26024
這篇文章主要介紹了Pytorch使用transforms,tansforms功能,通俗地講,類似于在計算機視覺流程里的圖像預處理部分的數(shù)據(jù)增強。下面來看看文章的具體內(nèi)容介紹吧,需要的朋友可以參考一下
首先,這次講解的tansforms功能,通俗地講,類似于在計算機視覺流程里的圖像預處理部分的數(shù)據(jù)增強。
transforms的原理:
說明:圖片(輸入)通過工具得到結(jié)果(輸出),這個工具,就是transforms模板工具,(tool=transforms.ToTensor()具體工具),使用工具result=tool(圖片)

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

接下來,上代碼:
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é)果:

相關文章
pyqt 實現(xiàn)QlineEdit 輸入密碼顯示成圓點的方法
今天小編就為大家分享一篇pyqt 實現(xiàn)QlineEdit 輸入密碼顯示成圓點的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Centos下實現(xiàn)安裝Python3.6和Python2共存
這篇文章主要介紹了Centos下實現(xiàn)安裝Python3.6和Python2共存,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
基于opencv和pillow實現(xiàn)人臉識別系統(tǒng)(附demo)
人臉識別就是一個程序能識別給定圖像或視頻中的人臉,本文主要介紹了opencv和pillow實現(xiàn)人臉識別系統(tǒng),本文不涉及分類器、訓練識別器等算法原理,感興趣的可以了解一下2021-11-11

