search

Pad in PyTorch

Jan 18, 2025 am 08:10 AM

Buy Me a Coffee☕

*Memos:

  • My post explains OxfordIIITPet().

Pad() can add padding to zero or more images as shown below:

*Memos:

  • The 1st argument for initialization is padding(Required-Type:int or tuple/list(int)): *Memos:
    • It can do add padding.
    • A tuple/list must be the 1D with 2 or 4 elements.
  • The 2nd argument for initialization is fill(Optional-Default:0-Type:int, float or tuple/list(int or float)): *Memos:
    • It can change the background of images. *The background can be seen when adding padding for images.
    • A tuple/list must be the 1D with 3 elements.
  • The 3rd argument for initialization is padding_mode(Optional-Default:'constant'-Type:str). *'constant', 'edge', 'reflect' or 'symmetric' can be set to it.
  • There is the 1st argument(Required-Type:PIL Image or tensor(int)). *It must be a 3D or more D tensor.
  • v2 is recommended to use according to V1 or V2? Which one should I use?.
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import Pad

pad = Pad(padding=100)
pad = Pad(padding=100, fill=0, padding_mode='constant')

pad
# Pad(padding=100, fill=0, padding_mode=constant)

pad.padding
# 100

pad.fill
# 0

pad.padding_mode
# 'constant'

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=Pad(padding=0)
)

p50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=50)
)

p100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100)
)

p150_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=150)
)

m50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=-50)
)

m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=-100)
)

m150_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=-150)
)

p100p50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[100, 50])
)

m100m50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[-100, -50])
)

p100m50_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[100, -50])
)

p25p50p75p100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[25, 50, 75, 100])
)

m25m50m75m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[-25, -50, -75, -100])
)

p25m50p75m100_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=[25, -50, 75, -100])
)

p100fillgray_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, fill=150)
)

p100fillpurple_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, fill=[160, 32, 240])
)

p100edge_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, padding_mode="edge")
)

p100reflect_data = OxfordIIITPet(
    root="data",
    transform=Pad(padding=100, padding_mode="reflect")
)

p100symmetric_data = OxfordIIITPet(
    root="data",
    transform=Pad(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, (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=p50_data, main_title='p50_data')
show_images1(data=p100_data, main_title='p100_data')
show_images1(data=p150_data, main_title='p150_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=m50_data, main_title='m50_data')
show_images1(data=m100_data, main_title='m100_data')
show_images1(data=m150_data, main_title='m150_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p100p50_data, main_title='p100p50_data')
show_images1(data=m100m50_data, main_title='m100m50_data')
show_images1(data=p100m50_data, main_title='p100m50_data')
print()
show_images1(data=origin_data, main_title='origin_data')
show_images1(data=p25p50p75p100_data, main_title='p25p50p75p100_data')
show_images1(data=m25m50m75m100_data, main_title='m25m50m75m100_data')
show_images1(data=p25m50p75m100_data, main_title='p25m50p75m100_data')
print()
show_images1(data=p100fillgray_data, main_title='p100fillgray_data')
show_images1(data=p100fillpurple_data, main_title='p100fillpurple_data')
print()
show_images1(data=p100edge_data, main_title='p100edge_data')
show_images1(data=p100reflect_data, main_title='p100reflect_data')
show_images1(data=p100symmetric_data, main_title='p100symmetric_data')

# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, p=0, f=0, pm='constant'):
    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)
        pad = Pad(padding=p, fill=f, padding_mode=pm) # Here
        plt.imshow(X=pad(im)) # Here
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p50_data', p=50)
show_images2(data=origin_data, main_title='p100_data', p=100)
show_images2(data=origin_data, main_title='p150_data', p=150)
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='m50_data', p=-50)
show_images2(data=origin_data, main_title='m100_data', p=-100)
show_images2(data=origin_data, main_title='m150_data', p=-150)
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p100p50_data', p=[100, 50])
show_images2(data=origin_data, main_title='m100m50_data', p=[-100, -50])
show_images2(data=origin_data, main_title='p100m50_data', p=[100, -50])
print()
show_images2(data=origin_data, main_title='origin_data')
show_images2(data=origin_data, main_title='p25p50p75p100_data',
             p=[25, 50, 75, 100])
show_images2(data=origin_data, main_title='m25m50m75m100_data',
             p=[-25, -50, -75, -100])
show_images2(data=origin_data, main_title='p25m50p75m100_data',
             p=[25, -50, 75, -100])
print()
show_images2(data=origin_data, main_title='p100fillgray_data', p=100,
             f=[150])
show_images2(data=origin_data, main_title='p100fillpurple_data', p=100,
             f=[160, 32, 240])
print()
show_images2(data=origin_data, main_title='p100edge_data', p=100, 
             pm='edge')
show_images2(data=origin_data, main_title='p100reflect_data', p=100,
             pm='reflect')
show_images2(data=origin_data, main_title='p100symmetric_data', 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

The above is the detailed content of Pad in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Explain how memory is allocated for lists versus arrays in Python.Explain how memory is allocated for lists versus arrays in Python.May 03, 2025 am 12:10 AM

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

How do you specify the data type of elements in a Python array?How do you specify the data type of elements in a Python array?May 03, 2025 am 12:06 AM

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

What is NumPy, and why is it important for numerical computing in Python?What is NumPy, and why is it important for numerical computing in Python?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Discuss the concept of 'contiguous memory allocation' and its importance for arrays.Discuss the concept of 'contiguous memory allocation' and its importance for arrays.May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

How do you slice a Python list?How do you slice a Python list?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

What are some common operations that can be performed on NumPy arrays?What are some common operations that can be performed on NumPy arrays?May 02, 2025 am 12:09 AM

NumPyallowsforvariousoperationsonarrays:1)Basicarithmeticlikeaddition,subtraction,multiplication,anddivision;2)Advancedoperationssuchasmatrixmultiplication;3)Element-wiseoperationswithoutexplicitloops;4)Arrayindexingandslicingfordatamanipulation;5)Ag

How are arrays used in data analysis with Python?How are arrays used in data analysis with Python?May 02, 2025 am 12:09 AM

ArraysinPython,particularlythroughNumPyandPandas,areessentialfordataanalysis,offeringspeedandefficiency.1)NumPyarraysenableefficienthandlingoflargedatasetsandcomplexoperationslikemovingaverages.2)PandasextendsNumPy'scapabilitieswithDataFramesforstruc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)