pytorch如何對image和label同時進行隨機翻轉
更新時間:2023年09月09日 11:38:29 作者:叫我小二吧
這篇文章主要介紹了pytorch如何對image和label同時進行隨機翻轉問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
pytorch對image和label同時進行隨機翻轉
在使用pytorch進行數據增廣時碰到一個坑,對image和label分別進行增廣,網絡訓練結果一直很差,查了很久才找到,原來分別image和label進行隨機翻轉操作時,是不同步的,記錄之,防止以后再犯。
所以在對數據進行隨機增廣操作時,需要指定的參數,
代碼如下:
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二者中隨機取一個, print(p) transform = transforms.Compose([ transforms.RandomHorizontalFlip(p),#指定是否翻轉 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()
結果如下:
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)
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
ubuntu16.04升級Python3.5到Python3.7的方法步驟
這篇文章主要介紹了ubuntu16.04升級Python3.5到Python3.7的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Python使用面向對象方式創(chuàng)建線程實現12306售票系統(tǒng)
目前python 提供了幾種多線程實現方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對thread做了一些包裝,可以更加方便的被使用2015-12-12python3之模塊psutil系統(tǒng)性能信息使用
psutil是個跨平臺庫,能夠輕松實現獲取系統(tǒng)運行的進程和系統(tǒng)利用率,這篇文章主要介紹了python3之模塊psutil系統(tǒng)性能信息使用,感興趣的小伙伴們可以參考一下2018-05-05