首頁 >後端開發 >Python教學 >pytorch中的隨機克羅普(1)

pytorch中的隨機克羅普(1)

Barbara Streisand
Barbara Streisand原創
2025-01-30 12:12:10236瀏覽

給我買咖啡☕

*備忘錄:

  • 我的帖子解釋了牛津iiitpet()。

Randomcrop()可以隨機裁剪圖像,如下所示:

*備忘錄:

    >初始化的第一個參數是大小(必需類型:int或tuple/list/list(int)或size()): *備忘錄:
    • 是[高度,寬度]。
    • 必須是1< = x。
    • 元組/列表必須是具有1或2個元素的1D。
    • 單個值(int或tuple/list(int))是指[size,size]。
    • >初始化的第二個參數是填充(可選默認:非類型:int或tuple/list(int)): *備忘錄:
    是[左上,右,底部],可以從[左右,底部]或[左右右下]轉換。 一個元組/列表必須是1D,具有1、2或4個元素。
  • 單個值(int或tuple/list(int))是指[填充,填充,填充,填充]。
    • > double值(元組/列表(int))表示[填充[0],填充[1],填充[0],填充[1]。
    • 初始化的第三個參數是pad_if_needed(可選默認:false-type:bool):
    • 如果是錯誤的,並且大小小於原始圖像或填充圖像的填充圖像,則出現錯誤。
    • >
    • 如果它的真實且大小小於原始圖像或填充圖像的填充圖像,則沒有錯誤,則該圖像被隨機填充以變為尺寸。
  • 初始化的第四個參數是填充(可選默認:0型:int,float或tuple/tuple/list(int或float)): *備忘錄:
    • >它可以更改圖像的背景。 *當圖像被正面填充時,可以看到背景。
    • 元組/列表必須是具有1或3個元素的1D。
  • 初始化的第五個參數是padding_mode(可選默認:'constant'-type:str)。 *可以將其設置為 *'常數','edge',“反射”或“對稱”。
  • >
      第一個參數是img(必需類型:pil圖像或張量(int)): *備忘錄:
    • 張量必須為2D或3D。
    • 不使用img =。
  • 建議根據V1或V2使用V2?我應該使用哪一個?
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomCrop

randomcrop = RandomCrop(size=100)
randomcrop = RandomCrop(size=100,
                        padding=None,
                        pad_if_needed=False, 
                        fill=0,
                        padding_mode='constant')
randomcrop
# RandomCrop(size=(100, 100),
#            pad_if_needed=False,
#            fill=0,
#            padding_mode=constant)

randomcrop.size
# (100, 100)

print(randomcrop.padding)
# None

randomcrop.pad_if_needed
# False

randomcrop.fill
# 0

randomcrop.padding_mode
# 'constant'

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

s300_data = OxfordIIITPet( # `s` is size.
    root="data",
    transform=RandomCrop(size=300)
    # transform=RandomCrop(size=[300, 300])
)

s200_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=200)
)

s100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=100)
)

s50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=50)
)

s10_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=10)
)

s1_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=1)
)

s200_300_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[200, 300])
)

s300_200_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[300, 200])
)

s300p100_data = OxfordIIITPet( # `p` is padding.
    root="data",
    transform=RandomCrop(size=300, padding=100)
    # transform=RandomCrop(size=300, padding=[100, 100])
    # transform=RandomCrop(size=300, padding=[100, 100, 100, 100])
)

s300p200_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=200)
)

s700_594p100origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100)
)

s300p100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100)
)

s600_594p100_50origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[600, 594], padding=[100, 50])
)

s300p100_50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=[100, 50])
)

s650_494p25_50_75_100origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[650, 494], padding=[25, 50, 75, 100])
)

s300p25_50_75_100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=[25, 50, 75, 100])
)

s300_194pn100origin_data = OxfordIIITPet( # `n` is negative.
    root="data",
    transform=RandomCrop(size=[300, 194], padding=-100)
)

s150pn100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=150, padding=-100)
)

s300_294pn50n100origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[300, 294], padding=[-50, -100])
)

s150pn50n100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=150, padding=[-50, -100])
)

s350_294pn25n50n75n100origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[350, 294], padding=[-25, -50, -75, -100])
)

s150pn25n50n75n100_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=150, padding=[-25, -50, -75, -100])
)

s600_444p25_50origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[600, 444], padding=[25, 50])
)

s200p25_50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=200, padding=[25, 50])
)

s400_344pn25n50origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[400, 344], padding=[-25, -50])
)

s200pn25n50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=200, padding=[-25, -50])
)

s400_444p25n50origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[400, 444], padding=[25, -50])
)

s200p25n50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=200, padding=[25, -50])
)

s600_344pn25_50origin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[600, 344], padding=[-25, 50])
)

s200pn25_50_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=200, padding=[-25, 50])
)

s700_594p100fgrayorigin_data = OxfordIIITPet( # `f` is fill.
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100, fill=150)
    # transform=RandomCrop(size=[700, 594], padding=100, fill=[150])
)

s300p100fgray_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, fill=150)
)

s700_594p100fpurpleorigin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100, fill=[160, 32, 240])
)

s300p100fpurple_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, fill=[160, 32, 240])
)

s700_594p100pmconstorigin_data = OxfordIIITPet( # `pm` is padding_mode.
    root="data",                                # `const` is constant.
    transform=RandomCrop(size=[700, 594], padding=100, padding_mode='constant')
)

s300p100pmconst_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, padding_mode='constant')
)

s700_594p100pmedgeorigin_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100, padding_mode='edge')
)

s300p100pmedge_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, padding_mode='edge')
)

s700_594p100pmrefleorigin_data = OxfordIIITPet( # `refle` is reflect.
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100, padding_mode='reflect')
)

s300p100pmrefle_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, padding_mode='reflect')
)

s700_594p100pmsymmeorigin_data = OxfordIIITPet( # `symme` is symmetric.
    root="data",
    transform=RandomCrop(size=[700, 594], padding=100, 
                         padding_mode='symmetric')
)

s300p100pmsymme_data = OxfordIIITPet(
    root="data",
    transform=RandomCrop(size=300, padding=100, padding_mode='symmetric')
)

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 in range(1, 6):
        plt.subplot(1, 5, i)
        plt.imshow(X=data[0][0])
    plt.tight_layout()
    plt.show()

plt.figure(figsize=(7, 9))
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images1(data=origin_data, main_title="s500_394origin_data")
show_images1(data=s300_data, main_title="s300_data")
show_images1(data=s200_data, main_title="s200_data")
show_images1(data=s100_data, main_title="s100_data")
show_images1(data=s50_data, main_title="s50_data")
show_images1(data=s10_data, main_title="s10_data")
show_images1(data=s1_data, main_title="s1_data")
show_images1(data=s200_300_data, main_title="s200_300_data")
show_images1(data=s300_200_data, main_title="s300_200_data")
print()
show_images1(data=s700_594p100origin_data,
             main_title="s700_594p100origin_data")
show_images1(data=s300p100_data, main_title="s300p100_data")
print()
show_images1(data=s600_594p100_50origin_data,
             main_title="s600_594p100_50origin_data")
show_images1(data=s300p100_50_data, main_title="s300p100_50_data")
print()
show_images1(data=s650_494p25_50_75_100origin_data,
             main_title="s650_494p25_50_75_100origin_data")
show_images1(data=s300p25_50_75_100_data, 
             main_title="s300p25_50_75_100_data")
print()
show_images1(data=s300_194pn100origin_data,
             main_title="s300_194pn100origin_data")
show_images1(data=s150pn100_data, 
             main_title="s150pn100_data")
print()
show_images1(data=s300_294pn50n100origin_data,
             main_title="s300_294pn50n100origin_data")
show_images1(data=s150pn50n100_data, 
             main_title="s150pn50n100_data")
print()
show_images1(data=s350_294pn25n50n75n100origin_data,
             main_title="s350_294pn25n50n75n100origin_data")
show_images1(data=s150pn25n50n75n100_data, 
             main_title="s150pn25n50n75n100_data")
print()
show_images1(data=s600_444p25_50origin_data,
             main_title="s600_444p25_50origin_data")
show_images1(data=s200p25_50_data, 
             main_title="s200p25_50_data")
print()
show_images1(data=s400_344pn25n50origin_data,
             main_title="s400_344pn25n50origin_data")
show_images1(data=s200pn25n50_data, 
             main_title="s200pn25n50_data")
print()
show_images1(data=s400_444p25n50origin_data,
             main_title="s400_444p25n50origin_data")
show_images1(data=s200p25n50_data, 
             main_title="s200p25n50_data")
print()
show_images1(data=s600_344pn25_50origin_data,
             main_title="s600_344pn25_50origin_data")
show_images1(data=s200pn25_50_data, 
             main_title="s200pn25_50_data")
print()
show_images1(data=s700_594p100fgrayorigin_data,
             main_title="s700_594p100fgrayorigin_data")
show_images1(data=s300p100fgray_data, main_title="s300p100fgray_data")
print()
show_images1(data=s700_594p100fpurpleorigin_data,
             main_title="s700_594p100fpurpleorigin_data")
show_images1(data=s300p100fpurple_data, main_title="s300p100fpurple_data")
print()
show_images1(data=s700_594p100pmconstorigin_data,
             main_title="s700_594p100pmconstorigin_data")
show_images1(data=s300p100pmconst_data, main_title="s300p100pmconst_data")
print()
show_images1(data=s700_594p100pmedgeorigin_data,
             main_title="s700_594p100pmedgeorigin_data")
show_images1(data=s300p100pmedge_data, main_title="s300p100pmedge_data")
print()
show_images1(data=s700_594p100pmrefleorigin_data,
             main_title="s700_594p100pmrefleorigin_data")
show_images1(data=s300p100pmrefle_data, main_title="s300p100pmrefle_data")
print()
show_images1(data=s700_594p100pmsymmeorigin_data,
             main_title="s700_594p100pmsymmeorigin_data")
show_images1(data=s300p100pmsymme_data, main_title="s300p100pmsymme_data")

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, s=None, p=None,
                 pin=False, f=0, pm='constant'):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    temp_s = s
    im = data[0][0]
    for i in range(1, 6):
        plt.subplot(1, 5, i)
        if not temp_s:
            s = [im.size[1], im.size[0]]
        rc = RandomCrop(size=s, padding=p, # Here
                        pad_if_needed=pin, fill=f, padding_mode=pm)
        plt.imshow(X=rc(im)) # Here
    plt.tight_layout()
    plt.show()

plt.figure(figsize=(7, 9))
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
show_images2(data=origin_data, main_title="s500_394origin_data")
show_images2(data=origin_data, main_title="s300_data", s=300)
show_images2(data=origin_data, main_title="s200_data", s=200)
show_images2(data=origin_data, main_title="s100_data", s=100)
show_images2(data=origin_data, main_title="s50_data", s=50)
show_images2(data=origin_data, main_title="s10_data", s=10)
show_images2(data=origin_data, main_title="s1_data", s=1)
show_images2(data=origin_data, main_title="s200_300_data", s=[200, 300])
show_images2(data=origin_data, main_title="s300_200_data", s=[300, 200])
print()
show_images2(data=origin_data, main_title="s700_594p100origin_data",
             s=[700, 594], p=100)
show_images2(data=origin_data, main_title="s300p100_data", s=300, p=100)
print()
show_images2(data=origin_data, main_title="s600_594p100_50origin_data",
             s=[600, 594], p=[100, 50])
show_images2(data=origin_data, main_title="s300p100_50_data", s=300,
             p=[100, 50])
print()
show_images2(data=origin_data, main_title="s650_494p25_50_75_100origin_data",
             s=[650, 494], p=[25, 50, 75, 100])
show_images2(data=origin_data, main_title="s300p25_50_75_100_data", s=300, 
             p=[25, 50, 75, 100])
print()
show_images2(data=origin_data, main_title="s300_194pn100origin_data",
             s=[300, 194], p=-100)
show_images2(data=origin_data, main_title="s150pn100_data", s=150, p=-100)
print()
show_images2(data=origin_data, main_title="s300_294pn50n100origin_data",
             s=[300, 294], p=[-50, -100])
show_images2(data=origin_data, main_title="s150pn50n100_data", s=150,
             p=[-50, -100])
print()
show_images2(data=origin_data, main_title="s350_294pn25n50n75n100origin_data",
             s=[350, 294], p=[-25, -50, -75, -100])
show_images2(data=origin_data, main_title="s150pn25n50n75n100_data", s=150,
             p=[-25, -50, -75, -100])
print()
show_images2(data=origin_data, main_title="s600_444p25_50origin_data",
             s=[600, 444], p=[25, 50])
show_images2(data=origin_data, main_title="s200p25_50_data", s=200,
             p=[25, 50])
print()
show_images2(data=origin_data, main_title="s400_344pn25n50origin_data",
             s=[400, 344], p=[-25, -50])
show_images2(data=origin_data, main_title="s200pn25n50_data", s=200,
             p=[-25, -50])
print()
show_images2(data=origin_data, main_title="s400_444p25n50origin_data",
             s=[400, 444], p=[25, -50])
show_images2(data=origin_data, main_title="s200p25n50_data", s=200,
             p=[25, -50])
print()
show_images2(data=origin_data, main_title="s600_344pn25_50origin_data",
             s=[600, 344], p=[-25, 50])
show_images2(data=origin_data, main_title="s200pn25_50_data", s=200,
             p=[-25, 50])
print()
show_images2(data=origin_data, main_title="s700_594p100fgrayorigin_data", 
             s=[700, 594], p=100, f=150)
show_images2(data=origin_data, main_title="s300p100fgray_data", s=300,
             p=100, f=150)
print()
show_images2(data=origin_data, main_title="s700_594p100fpurpleorigin_data",
             s=[700, 594], p=100, f=[160, 32, 240])
show_images2(data=origin_data, main_title="s300p100fpurple_data", s=300,
             p=100, f=[160, 32, 240])
print()
show_images2(data=origin_data, main_title="s700_594p100pmconstorigin_data",
             s=[700, 594], p=100, pm='constant')
show_images2(data=origin_data, main_title="s300p100pmconst_data", s=300, 
             p=100, pm='constant')
print()
show_images2(data=origin_data, main_title="s700_594p100pmedgeorigin_data",
             s=[700, 594], p=100, pm='edge')
show_images2(data=origin_data, main_title="s300p100pmedge_data", s=300, 
             p=100, pm='edge')
print()
show_images2(data=origin_data, main_title="s700_594p100pmrefleorigin_data",
             s=[700, 594], p=100, pm='reflect')
show_images2(data=origin_data, main_title="s300p100pmrefle_data", s=300, 
             p=100, pm='reflect')
print()
show_images2(data=origin_data, main_title="s700_594p100pmsymmeorigin_data",
             s=[700, 594], p=100, pm='symmetric')
show_images2(data=origin_data, main_title="s300p100pmsymme_data", s=300, 
             p=100, pm='symmetric')

Image description

Image description

Image description

Image description

Image description

Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description


Image description

Image description

以上是pytorch中的隨機克羅普(1)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn