Rumah > pembangunan bahagian belakang > Tutorial Python > RandomCrop dalam Pytorch (1)

RandomCrop dalam Pytorch (1)

Barbara Streisand
Lepaskan: 2025-01-30 12:12:10
asal
236 orang telah melayarinya

beli saya kopi ☕

*Memos:

  • Catatan saya menerangkan oxfordiiitpet ().

RandomCrop () boleh menanam imej secara rawak seperti yang ditunjukkan di bawah:

*Memos:

  • hujah pertama untuk permulaan adalah saiz (jenis yang diperlukan: int atau tuple/senarai (int) atau saiz ()): *Memo:
    • ia [ketinggian, lebar].
    • Ia mesti 1 & lt; = x.
    • tuple/senarai mestilah 1D dengan 1 atau 2 elemen.
    • nilai tunggal (int atau tuple/senarai (int)) bermaksud [saiz, saiz].
  • Argumen ke-2 untuk permulaan adalah padding (opsyenal-lalai: tiada jenis: int atau tuple/list (int)): *Memo:
    • Ia [kiri, atas, kanan, bawah] yang boleh ditukar dari [kiri-kanan, atas-bawah] atau [kiri-atas-kanan-bawah].
    • tuple/senarai mestilah 1D dengan 1, 2 atau 4 elemen.
    • nilai tunggal (int atau tuple/senarai (int)) bermaksud [padding, padding, padding, padding].
    • Nilai ganda (tuple/senarai (int)) bermaksud [padding [0], padding [1], padding [0], padding [1]].
  • hujah ke-3 untuk permulaan adalah pad_if_needed (opsyenal-lalai: jenis palsu: bool):
    • Jika ia palsu dan saiz lebih kecil daripada imej asal atau imej empuk dengan padding, ada ralat.
    • Jika ia benar dan saiznya lebih kecil daripada imej asal atau imej empuk dengan padding, tidak ada kesilapan, maka imejnya secara rawak menjadi saiz.
  • Argumen ke-4 untuk permulaan adalah mengisi (opsyenal-lalai: 0-jenis: int, float atau tuple/list (int atau float)): *Memo:
    • Ia boleh mengubah latar belakang imej. *Latar belakang dapat dilihat apabila imej positif empuk.
    • tuple/senarai mestilah 1D dengan 1 atau 3 elemen.
  • Argumen ke-5 untuk inisialisasi adalah padding_mode (opsyenal-lalai: 'constant'-type: str). *'tetap', 'tepi', 'mencerminkan' atau 'simetri' boleh ditetapkan kepadanya.
  • hujah pertama adalah IMG (jenis yang diperlukan: PIL Image atau Tensor (int)): *Memo:
    • Tensor mestilah 2D atau 3D.
    • Jangan gunakan img =.
  • V2 disyorkan untuk digunakan mengikut V1 atau V2? Yang mana yang harus saya gunakan?.
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')
Salin selepas log masuk

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

Atas ialah kandungan terperinci RandomCrop dalam Pytorch (1). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan