Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pytorch pad to square

import torchvision.transforms.functional as F

class SquarePad:
    def __call__(self, image):
        max_wh = max(image.size)
        p_left, p_top = [(max_wh - s) // 2 for s in image.size]
        p_right, p_bottom = [max_wh - (s+pad) for s, pad in zip(image.size, [p_left, p_top])]
        padding = (p_left, p_top, p_right, p_bottom)
        return F.pad(image, padding, 0, 'constant')

target_image_size = (224, 224)  # as an example
# now use it as the replacement of transforms.Pad class
transform=transforms.Compose([
    SquarePad(),
    transforms.Resize(target_image_size),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
Comment

PREVIOUS NEXT
Code Example
Python :: violajones python opencv 
Python :: round up 
Python :: the code panda 
Python :: not mutable data type in python 
Python :: least square fit straight line python 
Python :: python default summary statistics for all columns 
Python :: python structure like c 
Python :: performance of extend vs append loop 
Python :: python redirect console output to devnull 
Python :: python boolean ungleich 
Python :: matplotlib annotate align center 
Python :: insert in a sorted list python 
Python :: yesterday date in python 
Python :: start application from python 
Python :: pick random value from dictionary python 
Python :: python combine if statements 
Python :: python select random number from list 
Python :: python includes 
Python :: convert dictionary to string 
Python :: random forest classifier python 
Python :: function in function python 
Python :: calculation in python 
Python :: pandas to excel 
Python :: python round function example 
Python :: how to make capitalize text in python 
Python :: shallow copy deep copy python 
Python :: python code checker 
Python :: python create nested dictionary 
Python :: tkinter bg button 
Python :: on_delete django options 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =