欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 利用Python實現自動生成圖文并茂的數據分析

    利用Python實現自動生成圖文并茂的數據分析

    這篇文章主要介紹了利用Python實現自動生成圖文并茂的數據分析,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • ubuntu16.04升級Python3.5到Python3.7的方法步驟

    ubuntu16.04升級Python3.5到Python3.7的方法步驟

    這篇文章主要介紹了ubuntu16.04升級Python3.5到Python3.7的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • Python使用面向對象方式創(chuàng)建線程實現12306售票系統(tǒng)

    Python使用面向對象方式創(chuàng)建線程實現12306售票系統(tǒng)

    目前python 提供了幾種多線程實現方式 thread,threading,multithreading ,其中thread模塊比較底層,而threading模塊是對thread做了一些包裝,可以更加方便的被使用
    2015-12-12
  • python將圖片轉base64,實現前端顯示

    python將圖片轉base64,實現前端顯示

    今天小編就為大家分享一篇python將圖片轉base64,實現前端顯示,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 一個Python最簡單的接口自動化框架

    一個Python最簡單的接口自動化框架

    這篇文章主要為大家詳細介紹了一個Python最簡單的接口自動化框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 總結Python變量的相關知識

    總結Python變量的相關知識

    今天給大家?guī)淼氖顷P于Python基礎的相關知識,文章圍繞著Python變量的相關知識展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • python3之模塊psutil系統(tǒng)性能信息使用

    python3之模塊psutil系統(tǒng)性能信息使用

    psutil是個跨平臺庫,能夠輕松實現獲取系統(tǒng)運行的進程和系統(tǒng)利用率,這篇文章主要介紹了python3之模塊psutil系統(tǒng)性能信息使用,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Python json序列化解讀

    Python json序列化解讀

    這篇文章主要介紹了Python json序列化解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Python3中的循環(huán)語句示例詳解

    Python3中的循環(huán)語句示例詳解

    這篇文章主要介紹了Python3?循環(huán)語句,本文將詳細介紹Python3中的循環(huán)語句,給出各種循環(huán)的使用示例,以及運行結果的解釋,需要的朋友可以參考下
    2023-04-04
  • 終于搞懂了Keras中multiloss的對應關系介紹

    終于搞懂了Keras中multiloss的對應關系介紹

    這篇文章主要介紹了終于搞懂了Keras中multiloss的對應關系介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06

最新評論