搜尋
首頁後端開發Python教學PyTorch 中的隨機透視

請我喝杯咖啡☕

*備忘錄:

  • 我的貼文解釋了 RandomRotation()。
  • 我的帖子解釋了 RandomAffine()。
  • 我的貼文解釋了 RandomHorizo​​ntalFlip()。
  • 我的貼文解釋了 RandomVerticalFlip()。
  • 我的帖子解釋了 OxfordIIITPet()。

RandomPerspective() 可以對零個或多個影像進行透視變換,如下所示:

*備忘錄:

  • 初始化的第一個參數是 Distortion_scale(可選-預設:0.5-類型:int 或 float): *備註:
    • 可以進行透視變換。
    • 必須是 0
  • 初始化的第二個參數是 p(可選-預設:0.5-型別:int 或 float): *備註:
    • 是每張影像是否經過透視變換的機率。
    • 必須是 0
  • 初始化的第三個參數是插值(Optional-Default:InterpolationMode.BILINEAR-Type:InterpolationMode)。
  • 初始化的第四個參數是 fill(Optional-Default:0-Type:int, float or tuple/list(int or float)): *備註:
    • 它可以改變影像的背景。 *對影像進行透視變換時可以看到背景。
    • 元組/列表必須是具有 3 個元素的一維。
  • 有第一個參數(必需類型:PIL 影像或張量(int))。 *它必須是 3D 張量。
  • v2建議依照V1還是V2使用?我應該使用哪一個?
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomPerspective
from torchvision.transforms.functional import InterpolationMode

randompers = RandomPerspective()
randompers = RandomPerspective(distortion_scale=0.5,
                               p=0.5,
                               interpolation=InterpolationMode.BILINEAR,
                               fill=0)
randompers
# RandomPerspective(p=0.5,
#                   distortion_scale=0.5,
#                   interpolation=InterpolationMode.BILINEAR,
#                   fill=0)

randompers.distortion_scale
# 0.5

randompers.p
# 0.5

randompers.interpolation
# <interpolationmode.bilinear:>

randompers.fill
# 0

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=RandomPerspective(distortion_scale=0)
    # transform=RandomPerspective(p=0)
)

dis02p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(distortion_scale=0.2, p=1)
)

dis06p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(distortion_scale=0.6, p=1)
)

dis1p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(distortion_scale=1, p=1)
)

p1_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(p=1)
)

p05_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(p=0.5)
)

p1fillgray_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(p=1, fill=150)
)

p1fillpurple_data = OxfordIIITPet(
    root="data",
    transform=RandomPerspective(p=1, fill=[160, 32, 240])
)

import matplotlib.pyplot as plt

def show_images1(data, main_title=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        plt.imshow(X=im)
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images1(data=origin_data, main_title="origin_data")
show_images1(data=dis02p1_data, main_title="dis02p1_data")
show_images1(data=dis06p1_data, main_title="dis06p1_data")
show_images1(data=dis1p1_data, main_title="dis1p1_data")
show_images1(data=p1_data, main_title="p1_data")
show_images1(data=p05_data, main_title="p05_data")
show_images1(data=p1fillgray_data, main_title="p1fillgray_data")
show_images1(data=p1fillpurple_data, main_title="p1fillpurple_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, d=0.5, prob=0.5, f=0):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        rp = RandomPerspective(distortion_scale=d, p=prob, fill=f) # Here
        plt.imshow(X=rp(im)) # Here
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title="origin_data", d=0)
show_images2(data=origin_data, main_title="dis02p1_data", d=0.2, prob=1)
show_images2(data=origin_data, main_title="dis06p1_data", d=0.6, prob=1)
show_images2(data=origin_data, main_title="dis1p1_data", d=1, prob=1)
show_images2(data=origin_data, main_title="p1_data", prob=1)
show_images2(data=origin_data, main_title="p05_data", prob=0.5)
show_images2(data=origin_data, main_title="p1fillgray_data", prob=1, f=150)
show_images2(data=origin_data, main_title="p1fillpurple_data", prob=1,
             f=[160, 32, 240])
</interpolationmode.bilinear:>

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Image description

以上是PyTorch 中的隨機透視的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python的混合方法:編譯和解釋合併Python的混合方法:編譯和解釋合併May 08, 2025 am 12:16 AM

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增強效率和通用性。

了解python的' for”和' then”循環之間的差異了解python的' for”和' then”循環之間的差異May 08, 2025 am 12:11 AM

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

Python串聯列表與重複Python串聯列表與重複May 08, 2025 am 12:09 AM

在Python中,可以通過多種方法連接列表並管理重複元素:1)使用 運算符或extend()方法可以保留所有重複元素;2)轉換為集合再轉回列表可以去除所有重複元素,但會丟失原有順序;3)使用循環或列表推導式結合集合可以去除重複元素並保持原有順序。

Python列表串聯性能:速度比較Python列表串聯性能:速度比較May 08, 2025 am 12:09 AM

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

您如何將元素插入python列表中?您如何將元素插入python列表中?May 08, 2025 am 12:07 AM

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

Python是否列表動態陣列或引擎蓋下的鏈接列表?Python是否列表動態陣列或引擎蓋下的鏈接列表?May 07, 2025 am 12:16 AM

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他們areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

如何從python列表中刪除元素?如何從python列表中刪除元素?May 07, 2025 am 12:15 AM

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)刪除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

試圖運行腳本時,應該檢查是否會遇到'權限拒絕”錯誤?試圖運行腳本時,應該檢查是否會遇到'權限拒絕”錯誤?May 07, 2025 am 12:12 AM

toresolvea“ dermissionded”錯誤Whenrunningascript,跟隨台詞:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境