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實(shí)現(xiàn)自動(dòng)生成圖文并茂的數(shù)據(jù)分析
這篇文章主要介紹了利用Python實(shí)現(xiàn)自動(dòng)生成圖文并茂的數(shù)據(jù)分析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08ubuntu16.04升級(jí)Python3.5到Python3.7的方法步驟
這篇文章主要介紹了ubuntu16.04升級(jí)Python3.5到Python3.7的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python使用面向?qū)ο蠓绞絼?chuàng)建線程實(shí)現(xiàn)12306售票系統(tǒng)
目前python 提供了幾種多線程實(shí)現(xiàn)方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對(duì)thread做了一些包裝,可以更加方便的被使用2015-12-12python將圖片轉(zhuǎn)base64,實(shí)現(xiàn)前端顯示
今天小編就為大家分享一篇python將圖片轉(zhuǎn)base64,實(shí)現(xiàn)前端顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01一個(gè)Python最簡(jiǎn)單的接口自動(dòng)化框架
這篇文章主要為大家詳細(xì)介紹了一個(gè)Python最簡(jiǎn)單的接口自動(dòng)化框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01總結(jié)Python變量的相關(guān)知識(shí)
今天給大家?guī)淼氖顷P(guān)于Python基礎(chǔ)的相關(guān)知識(shí),文章圍繞著Python變量的相關(guān)知識(shí)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06python3之模塊psutil系統(tǒng)性能信息使用
psutil是個(gè)跨平臺(tái)庫(kù),能夠輕松實(shí)現(xiàn)獲取系統(tǒng)運(yùn)行的進(jìn)程和系統(tǒng)利用率,這篇文章主要介紹了python3之模塊psutil系統(tǒng)性能信息使用,感興趣的小伙伴們可以參考一下2018-05-05終于搞懂了Keras中multiloss的對(duì)應(yīng)關(guān)系介紹
這篇文章主要介紹了終于搞懂了Keras中multiloss的對(duì)應(yīng)關(guān)系介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06