本文整理匯總了Python中torchvision.transforms.functional.pad方法的典型用法代碼示例。如果您正苦於以下問題:Python functional.pad方法的具體用法?Python functional.pad怎麽用?Python functional.pad使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torchvision.transforms.functional
的用法示例。
在下文中一共展示了functional.pad方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: pad_func
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def pad_func(self, img, params):
if self.padding is not None:
img.img = F.pad(img.img, self.padding, self.fill, self.padding_mode)
if img.x is not None:
img.x = F.pad(img.x, self.padding, self.fill, self.padding_mode)
if img.y is not None:
img.y = F.pad(img.y, self.padding, self.fill, self.padding_mode)
if self.pad_if_needed and img.img.size[0] < self.size[1]:
img.img = F.pad(img.img, (self.size[1] - img.img.size[0], 0), self.fill, self.padding_mode)
if img.x is not None:
img.x = F.pad(img.x, (self.size[1] - img.img.size[0], 0), self.fill, self.padding_mode)
if img.y is not None:
img.y = F.pad(img.y, (self.size[1] - img.img.size[0], 0), self.fill, self.padding_mode)
if self.pad_if_needed and img.img.size[1] < self.size[0]:
img.img = F.pad(img.img, (0, self.size[0] - img.img.size[1]), self.fill, self.padding_mode)
if img.x is not None:
img.x = F.pad(img.x, (0, self.size[0] - img.img.size[1]), self.fill, self.padding_mode)
if img.y is not None:
img.y = F.pad(img.y, (0, self.size[0] - img.img.size[1]), self.fill, self.padding_mode)
return img
示例2: _get_params
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def _get_params(self, images):
"""
Args:
img (PIL Image) list: Image to be cropped.
Returns:
PIL Image list: Cropped image.
"""
while isinstance(images, (tuple, list)):
images = images[0]
img = images.img
if self.padding is not None:
img = F.pad(img, self.padding, self.fill, self.padding_mode)
# pad the width if needed
if self.pad_if_needed and img.size[0] < self.size[1]:
img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
# pad the height if needed
if self.pad_if_needed and img.size[1] < self.size[0]:
img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)
return self.get_params(img, self.size)
示例3: undo_transform
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def undo_transform(self, sample):
rdict = {}
input_data = sample['input']
fh, fw, w, h = self.get_params(sample)
th, tw = self.size
pad_left = fw
pad_right = w - pad_left - tw
pad_top = fh
pad_bottom = h - pad_top - th
padding = (pad_left, pad_top, pad_right, pad_bottom)
input_data = F.pad(input_data, padding)
rdict['input'] = input_data
sample.update(rdict)
return sample
示例4: cv_transform
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def cv_transform(img):
# img = resize(img, size=(100, 300))
# img = to_tensor(img)
# img = normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# img = pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant')
# img = pad(img, padding=(100, 100, 100, 100), fill=5, padding_mode='symmetric')
# img = crop(img, -40, -20, 1000, 1000)
# img = center_crop(img, (310, 300))
# img = resized_crop(img, -10.3, -20, 330, 220, (500, 500))
# img = hflip(img)
# img = vflip(img)
# tl, tr, bl, br, center = five_crop(img, 100)
# img = adjust_brightness(img, 2.1)
# img = adjust_contrast(img, 1.5)
# img = adjust_saturation(img, 2.3)
# img = adjust_hue(img, 0.5)
# img = adjust_gamma(img, gamma=3, gain=0.1)
# img = rotate(img, 10, resample='BILINEAR', expand=True, center=None)
# img = to_grayscale(img, 3)
# img = affine(img, 10, (0, 0), 1, 0, resample='BICUBIC', fillcolor=(255,255,0))
# img = gaussion_noise(img)
# img = poisson_noise(img)
img = salt_and_pepper(img)
return to_tensor(img)
示例5: pil_transform
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def pil_transform(img):
# img = functional.resize(img, size=(100, 300))
# img = functional.to_tensor(img)
# img = functional.normalize(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# img = functional.pad(img, padding=(10, 10, 20, 20), fill=(255, 255, 255), padding_mode='constant')
# img = functional.pad(img, padding=(100, 100, 100, 100), padding_mode='symmetric')
# img = functional.crop(img, -40, -20, 1000, 1000)
# img = functional.center_crop(img, (310, 300))
# img = functional.resized_crop(img, -10.3, -20, 330, 220, (500, 500))
# img = functional.hflip(img)
# img = functional.vflip(img)
# tl, tr, bl, br, center = functional.five_crop(img, 100)
# img = functional.adjust_brightness(img, 2.1)
# img = functional.adjust_contrast(img, 1.5)
# img = functional.adjust_saturation(img, 2.3)
# img = functional.adjust_hue(img, 0.5)
# img = functional.adjust_gamma(img, gamma=3, gain=0.1)
# img = functional.rotate(img, 10, resample=PIL.Image.BILINEAR, expand=True, center=None)
# img = functional.to_grayscale(img, 3)
# img = functional.affine(img, 10, (0, 0), 1, 0, resample=PIL.Image.BICUBIC, fillcolor=(255,255,0))
return functional.to_tensor(img)
示例6: _maybe_pad
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def _maybe_pad(self, frame: Image):
if self.padding is not None:
frame = F.pad(frame, self.padding, self.fill, self.padding_mode)
# pad the width if needed
frame_width = frame.size[0]
desired_width = self.size[1]
if self.pad_if_needed and frame_width < desired_width:
horizontal_padding = desired_width - frame_width
frame = F.pad(frame, (horizontal_padding, 0), self.fill, self.padding_mode)
# pad the height if needed
frame_height = frame.size[1]
desired_height = self.size[0]
if self.pad_if_needed and frame_height < desired_height:
vertical_padding = desired_height - frame_height
frame = F.pad(frame, (0, vertical_padding), self.fill, self.padding_mode)
return frame
示例7: pad
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def pad(img, padding, mode='constant', fill=0):
if isinstance(padding, int):
padding = (padding, padding, padding, padding)
elif len(padding) == 2:
padding = (padding[0], padding[1], padding[0], padding[1])
else:
assert len(padding) == 4
if mode == 'constant':
img_new = TF.pad(img, padding, fill=fill)
else:
np_padding = ((padding[1], padding[3]), (padding[0], padding[2]), (0, 0))
img_new = Image.fromarray(np.pad(
np.array(img), np_padding, mode=mode
))
return img_new
示例8: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def __call__(self, frames):
"""
Args:
frames: a list of PIL Image
Returns:
a list of PIL Image: Cropped images.
"""
i, j, h, w = self.get_params(frames, self.size)
out_frames = []
for frame in frames:
if self.padding is not None:
frame = F.pad(frame, self.padding, self.fill, self.padding_mode)
# pad the width if needed
if self.pad_if_needed and frame.size[0] < self.size[1]:
frame = F.pad(frame, (int((1 + self.size[1] - frame.size[0]) / 2), 0), self.fill, self.padding_mode)
# pad the height if needed
if self.pad_if_needed and frame.size[1] < self.size[0]:
frame = F.pad(frame, (0, int((1 + self.size[0] - frame.size[1]) / 2)), self.fill, self.padding_mode)
out_frames.append(F.crop(frame, i, j, h, w))
return out_frames
示例9: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def __call__(self, img):
"""
Args:
img (PIL Image): Image to be padded.
Returns:
PIL Image: Padded image.
"""
w, h = img.size
m = self.multiple
nw = (w // m + int((w % m) != 0)) * m
nh = (h // m + int((h % m) != 0)) * m
padw = nw - w
padh = nh - h
out = vf.pad(img, (0, 0, padw, padh), self.fill, self.padding_mode)
return out
示例10: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def __call__(self, img):
"""
Args:
img (PIL Image): Image to be cropped.
Returns:
PIL Image: Cropped image.
"""
if self.padding is not None:
img = F.pad(img, self.padding, self.fill, self.padding_mode)
# pad the width if needed
if self.pad_if_needed and img.size[0] < self.size[1]:
img = F.pad(img, (self.size[1] - img.size[0], 0), self.fill, self.padding_mode)
# pad the height if needed
if self.pad_if_needed and img.size[1] < self.size[0]:
img = F.pad(img, (0, self.size[0] - img.size[1]), self.fill, self.padding_mode)
i, j, h, w = self.get_params(img, self.size)
return F.crop(img, i, j, h, w)
示例11: __call__
# 需要導入模塊: from torchvision.transforms import functional [as 別名]
# 或者: from torchvision.transforms.functional import pad [as 別名]
def __call__(self, image, bboxes):
if self.padding is not None:
image = F.pad(image, self.padding, self.fill, self.padding_mode)
bboxes = bboxes.pad(padding)
# pad the width if needed
if self.pad_if_needed and image.size[0] < self.size[1]:
image = F.pad(image, (self.size[1] - image.size[0], 0), self.fill, self.padding_mode)
bboxes = bboxes.pad((self.size[1] - image.size[0], 0))
# pad the height if needed
if self.pad_if_needed and image.size[1] < self.size[0]:
image = F.pad(image, (0, self.size[0] - image.size[1]), self.fill, self.padding_mode)
bboxes = bboxes.pad((0, self.size[0] - image.size[1]))
i, j, h, w = self.get_params(image, self.size)
return F.crop(image, i, j, h, w), bboxes.crop((i, j, i+h, j+w))