pytorch如何對(duì)image和label同時(shí)進(jìn)行隨機(jī)翻轉(zhuǎn)
pytorch對(duì)image和label同時(shí)進(jìn)行隨機(jī)翻轉(zhuǎn)
在使用pytorch進(jìn)行數(shù)據(jù)增廣時(shí)碰到一個(gè)坑,對(duì)image和label分別進(jìn)行增廣,網(wǎng)絡(luò)訓(xùn)練結(jié)果一直很差,查了很久才找到,原來分別image和label進(jìn)行隨機(jī)翻轉(zhuǎn)操作時(shí),是不同步的,記錄之,防止以后再犯。
所以在對(duì)數(shù)據(jù)進(jìn)行隨機(jī)增廣操作時(shí),需要指定的參數(shù),
代碼如下:
image_path = '/home/org/19.bmp'
label_path = '/home/mask/19.png'
image = Image.open(image_path)
print(image.size)
label = Image.open(label_path)
plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(image)
plt.subplot(2, 2, 2)
plt.imshow(label)
p = np.random.choice([0, 1])#在0,1二者中隨機(jī)取一個(gè),
print(p)
transform = transforms.Compose([
transforms.RandomHorizontalFlip(p),#指定是否翻轉(zhuǎn)
transforms.ToTensor()
])
img = transform(image)
lab = transform(label)
unloader = transforms.ToPILImage()
img = unloader(img)
lab = unloader(lab)
# print(img.shape)
plt.subplot(2, 2, 3)
plt.imshow(img)
plt.subplot(2, 2, 4)
plt.imshow(lab)
plt.show()結(jié)果如下:

pytorch image to tensor
from PIL import Image
import os.path as osp
import os
from torchvision.transforms import ToTensor
test_path = '../dataset/BSD500/images/test'
images_ls = os.listdir(test_path)
images_all_path = []
for image in images_ls:
s_images_path = osp.join(test_path, image)
images_all_path.append(s_images_path)
# print(images_all_path)
image = Image.open(images_all_path[0]).convert('YCbCr')
# image.show()
# print(image)
y, cb, cr = image.split()
print(y)
print(cb)
print(cr)
# y.show()
# cb.show()
image_to_tensor = ToTensor()
y = image_to_tensor(y)
print(y.shape)總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python列表排序用?sort()和sorted()的區(qū)別
這篇文章主要介紹了python列表排序用?sort()和sorted()的區(qū)別,主要比較?Python?中用于列表排序的兩種函數(shù)?sort()?和?sorted(),選擇合適的排序函數(shù),下文詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-03-03
Python優(yōu)化技巧之利用ctypes提高執(zhí)行速度
ctypes是Python的一個(gè)外部庫(kù),提供和C語(yǔ)言兼容的數(shù)據(jù)類型,可以很方便地調(diào)用C DLL中的函數(shù)。今天我們就來詳細(xì)探討下ctypes庫(kù)的使用技巧2016-09-09
python實(shí)現(xiàn)讀取圖像文件并獲取到像素?cái)?shù)組的4種方法詳解
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)讀取圖像文件并獲取到像素?cái)?shù)組的4種方法并進(jìn)行對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-03-03
Pandas中DataFrame.replace()函數(shù)的實(shí)現(xiàn)
DataFrame.replace()用于替換DataFrame中的指定值,本文主要介紹了Pandas中DataFrame.replace()函數(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Python實(shí)戰(zhàn)之實(shí)現(xiàn)截圖識(shí)別文字
本文主要介紹了通過python實(shí)現(xiàn)截圖識(shí)別圖中文字的功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以學(xué)習(xí)一下2021-11-11
Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作
這篇文章主要介紹了Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Django項(xiàng)目使用ckeditor詳解(不使用admin)
今天小編就為大家分享一篇Django項(xiàng)目使用ckeditor詳解(不使用admin),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
淺談pytorch卷積核大小的設(shè)置對(duì)全連接神經(jīng)元的影響
今天小編就為大家分享一篇淺談pytorch卷積核大小的設(shè)置對(duì)全連接神經(jīng)元的影響,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python SQLAlchemy基本操作和常用技巧(包含大量實(shí)例,非常好)
這篇文章主要介紹了Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量實(shí)例,非常好的一個(gè)學(xué)習(xí)SQLAlchemy的教程,需要的朋友可以參考下2014-05-05

