本文整理汇总了Python中torchvision.transforms.functional.crop方法的典型用法代码示例。如果您正苦于以下问题:Python functional.crop方法的具体用法?Python functional.crop怎么用?Python functional.crop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.transforms.functional
的用法示例。
在下文中一共展示了functional.crop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_params
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def get_params(img_size, crop_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img_size (tuple): size of image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
w, h = img_size
th = crop_size
tw = crop_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
示例2: _random_crop
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def _random_crop(self, img_list):
"""Performs random square crop of fixed size.
Works with list so that all items get the same cropped window (e.g. for buffers).
"""
w, h = img_list[0].size
assert w >= self.crop_size and h >= self.crop_size, \
f'Error: Crop size: {self.crop_size}, Image size: ({w}, {h})'
cropped_imgs = []
i = np.random.randint(0, h - self.crop_size + 1)
j = np.random.randint(0, w - self.crop_size + 1)
for img in img_list:
# Resize if dimensions are too small
if min(w, h) < self.crop_size:
img = tvF.resize(img, (self.crop_size, self.crop_size))
# Random crop
cropped_imgs.append(tvF.crop(img, i, j, self.crop_size, self.crop_size))
return cropped_imgs
示例3: __getitem__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def __getitem__(self, index):
"""Retrieves image from folder and corrupts it."""
# Load PIL image
img_path = os.path.join(self.root_dir, self.imgs[index])
img = Image.open(img_path).convert('RGB')
# Random square crop
if self.crop_size != 0:
img = self._random_crop([img])[0]
# Corrupt source image
tmp = self._corrupt(img)
source = tvF.to_tensor(self._corrupt(img))
# Corrupt target image, but not when clean targets are requested
if self.clean_targets:
target = tvF.to_tensor(img)
else:
target = tvF.to_tensor(self._corrupt(img))
return source, target
示例4: get_params
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def get_params(img, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img (PIL Image): Image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
w, h = img.size
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
示例5: _instance_process
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def _instance_process(self, img, params):
if params is None:
img.img = img.img.resize((self.width, self.height), self.interpolation)
if img.x is not None:
img.x = img.x.resize((self.width, self.height), self.interpolation)
if img.y is not None:
img.y = img.y.resize((self.width, self.height), self.interpolation)
else:
new_width, new_height, x1, y1 = params
img.img = img.img.resize((new_width, new_height), self.interpolation)
img.img = img.img.crop((x1, y1, x1 + self.width, y1 + self.height))
if img.x is not None:
img.x = img.x.resize((new_width, new_height), self.interpolation)
img.x = img.x.crop((x1, y1, x1 + self.width, y1 + self.height))
if img.y is not None:
img.y = img.y.resize((new_width, new_height), self.interpolation)
img.y = img.y.crop((x1, y1, x1 + self.width, y1 + self.height))
return img
示例6: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def __call__(self, img):
image_width = img.size[0]
image_height = img.size[1]
h, w = (self.size, self.size)
if self.crop_position == 'c':
i = int(round((image_height - h) / 2.))
j = int(round((image_width - w) / 2.))
elif self.crop_position == 'tl':
i = 0
j = 0
elif self.crop_position == 'tr':
i = 0
j = image_width - self.size
elif self.crop_position == 'bl':
i = image_height - self.size
j = 0
elif self.crop_position == 'br':
i = image_height - self.size
j = image_width - self.size
img = F.crop(img, i, j, h, w)
return img
示例7: get_params
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def get_params(data, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img (PIL Image): Image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
hr, lr = data
w, h = hr.size
th, tw = output_size
if w == tw or h == th:
return 0, 0, h, w
if w < tw or h < th:
th, tw = h//2, w//2
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
示例8: get_params
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def get_params(img, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img (PIL Image): Image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
w, h = img.size
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
示例9: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def __call__(self, img, target):
w, h = img.size
w_range = w - self.size[0]
h_range = h - self.size[1]
if w_range > 0:
left = random.randint(0, w_range - 1)
else:
left = 0
if h_range > 0:
top = random.randint(0, h_range - 1)
else:
top = 0
height = min(h - top, self.size[1])
width = min(w - left, self.size[0])
img = F.crop(img, top, left, height, width)
target = F.crop(target, top, left, height, width)
return img, target
示例10: resized_crop
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def resized_crop(img, i, j, h, w, size, interpolation='BILINEAR'):
"""Crop the given CV Image and resize it to desired size. Notably used in RandomResizedCrop.
Args:
img (np.ndarray): Image to be cropped.
i: Upper pixel coordinate.
j: Left pixel coordinate.
h: Height of the cropped image.
w: Width of the cropped image.
size (sequence or int): Desired output size. Same semantics as ``scale``.
interpolation (str, optional): Desired interpolation. Default is
``BILINEAR``.
Returns:
np.ndarray: Cropped image.
"""
assert _is_numpy_image(img), 'img should be CV Image'
img = crop(img, i, j, h, w)
img = resize(img, size, interpolation)
return img
示例11: pil_transform
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [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)
示例12: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def __call__(self, img_dict):
keys = ['rgb', 'ir', 'depth']
for k in keys:
img = img_dict[k]
w, h = img.size
crop_h, crop_w = self.size
if crop_w > w or crop_h > h:
raise ValueError("Requested crop size {} is bigger than input size {}".format(self.size,
(h, w)))
if self.crop_index == 0:
img_dict[k] = F.center_crop(img, (crop_h, crop_w))
elif self.crop_index == 1:
img_dict[k] = img.crop((0, 0, crop_w, crop_h))
elif self.crop_index == 2:
img_dict[k] = img.crop((w - crop_w, 0, w, crop_h))
elif self.crop_index == 3:
img_dict[k] = img.crop((0, h - crop_h, crop_w, h))
elif self.crop_index == 4:
img_dict[k] = img.crop((w - crop_w, h - crop_h, w, h))
else:
raise ValueError("Requested crop index is not in range(5)")
return img_dict
示例13: get_params
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [as 别名]
def get_params(frames, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
frames: a list of PIL Image
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
w, h = frames[0].size
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th)
j = random.randint(0, w - tw)
return i, j, th, tw
示例14: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [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
示例15: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import crop [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)