Heim >Backend-Entwicklung >Python-Tutorial >Randomcrop in Pytorch (1)

Randomcrop in Pytorch (1)

Barbara Streisand
Barbara StreisandOriginal
2025-01-30 12:12:10239Durchsuche

Kaufen Sie mir einen Kaffee

*Memos:

  • Mein Beitrag erklärt Oxfordiiitpet ().

randomCrop () kann ein Bild zufällig wie unten gezeigt aufnehmen:

*Memos:

  • Das erste Argument für die Initialisierung ist die Größe (Erforderlich: int oder tuple/list (int) oder size ()): *Memos:
    • Es ist [Höhe, Breite].
    • Es muss 1 & lt; = x.
    • sein
    • Eine Tupel/Liste muss der 1D mit 1 oder 2 Elementen sein.
    • Ein einzelner Wert (int oder tuple/list (int)) bedeutet [Größe, Größe].
  • Das 2. Argument für die Initialisierung ist die Polsterung (optional-default: none-type: int oder tuple/list (int)): *Memos:
    • Es ist [links, oben, rechts, unten], die aus [links-rechts, oben bottom] oder [links-top-right-bottom] konvertiert werden können.
    • Ein Tupel/eine Liste muss der 1D mit 1, 2 oder 4 Elementen sein.
    • Ein einzelner Wert (int oder tuple/list (int)) bedeutet [Polsterung, Polsterung, Polsterung, Polsterung].
    • Doppelwerte (Tuple/List (int)) bedeutet [Polster [0], Polster [1], Polster [0], Polster [1]].
  • Das 3. Argument für die Initialisierung ist pad_if_needed (Optional-Default: False-Typ: Bool):
    • Wenn es falsch ist und die Größe kleiner als ein Originalbild oder das gepolsterte Bild durch Polsterung ist, gibt es Fehler.
    • Wenn es wahr ist und die Größe kleiner als ein Originalbild oder das gepolsterte Bild durch Polsterung ist, gibt es keinen Fehler, dann wird das Bild zufällig gepolstert, um zu Größe zu werden.
  • Das 4. Argument für die Initialisierung ist gefüllt (Optional-Default: 0-Typ: INT, Float oder Tupel/List (int oder Float)): *Memos:
    • Es kann den Hintergrund eines Bildes ändern. *Der Hintergrund ist zu sehen, wenn ein Bild positiv gepolstert ist.
    • Ein Tupel/eine Liste muss die 1D mit 1 oder 3 Elementen sein.
  • Das 5. Argument für die Initialisierung ist padding_mode (optional-default: 'Constant'-Typ: str). *'Konstant', 'Edge', 'reflektiert' oder 'symmetrisch' kann darauf eingestellt werden.
  • Das erste Argument ist IMG (Erforderlich: Pil-Bild oder Tensor (int)): *Memos:
    • Ein Tensor muss 2D oder 3D.
    • sein
    • Verwenden Sie nicht img =.
  • V2 wird empfohlen, nach V1 oder V2 zu verwenden? Welches soll ich verwenden?
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

Image description


Image description

Image description

Das obige ist der detaillierte Inhalt vonRandomcrop in Pytorch (1). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn