本文整理汇总了Python中torchvision.transforms.functional.resize方法的典型用法代码示例。如果您正苦于以下问题:Python functional.resize方法的具体用法?Python functional.resize怎么用?Python functional.resize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.transforms.functional
的用法示例。
在下文中一共展示了functional.resize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resize
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def resize(image, boxes, dims=(300, 300), return_percent_coords=True):
"""
Resize image. For the SSD300, resize to (300, 300).
Since percent/fractional coordinates are calculated for the bounding boxes (w.r.t image dimensions) in this process,
you may choose to retain them.
:param image: image, a PIL Image
:param boxes: bounding boxes in boundary coordinates, a tensor of dimensions (n_objects, 4)
:return: resized image, updated bounding box coordinates (or fractional coordinates, in which case they remain the same)
"""
# Resize image
new_image = FT.resize(image, dims)
# Resize bounding boxes
old_dims = torch.FloatTensor([image.width, image.height, image.width, image.height]).unsqueeze(0)
new_boxes = boxes / old_dims # percent coordinates
if not return_percent_coords:
new_dims = torch.FloatTensor([dims[1], dims[0], dims[1], dims[0]]).unsqueeze(0)
new_boxes = new_boxes * new_dims
return new_image, new_boxes
示例2: _random_crop
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [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: _instance_process
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [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
示例4: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, img, mask):
if self.padding > 0:
img = ImageOps.expand(img, border=self.padding, fill=0)
mask = ImageOps.expand(mask, border=self.padding, fill=0)
assert img.size == mask.size
w, h = img.size
th, tw = self.size
if w == tw and h == th:
return img, mask
if w < tw or h < th:
return img.resize((tw, th), Image.BILINEAR), mask.resize(
(tw, th), Image.NEAREST)
x1 = random.randint(0, w - tw)
y1 = random.randint(0, h - th)
return img.crop((x1, y1, x1 + tw, y1 + th)
), mask.crop((x1, y1, x1 + tw, y1 + th))
示例5: resized_crop
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [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
示例6: cv_transform
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [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)
示例7: pil_transform
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [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)
示例8: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, img_dict):
if np.random.rand() < self.p:
data_get_func = img_dict['meta']['get_item_func']
curr_idx = img_dict['meta']['idx']
max_idx = img_dict['meta']['max_idx']
other_idx = np.random.randint(0, max_idx)
data4augm = data_get_func(other_idx)
while (curr_idx == other_idx) or (self.same_label and data4augm['label'] != img_dict['label']):
other_idx = np.random.randint(0, max_idx)
data4augm = data_get_func(other_idx)
alpha = np.random.rand()
keys = ['rgb', 'depth', 'ir']
for key in keys:
img_dict[key] = Image.blend(data4augm[key].resize(img_dict[key].size),
img_dict[key],
alpha=alpha)
if not self.same_label:
img_dict['label'] = alpha * img_dict['label'] + (1 - alpha) * data4augm['label']
return img_dict
示例9: __getitem__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __getitem__(self, index):
# Apply transforms to the image.
image = torch.FloatTensor(self.nc,self.out_img_size, self.out_img_size).fill_(-1.)
# Get the individual images.
randbox = random.randrange(len(self.metadata['images'][index]))
imglabel = np.zeros(10, dtype=np.int)
boxlabel = np.zeros(10, dtype=np.int)
for i,bb in enumerate(self.metadata['images'][index]):
imid = random.randrange(self.num_data)
bbox = [int(bc*self.out_img_size) for bc in bb]
img, label = self.dataset[imid]
scImg = FN.resize(img,(bbox[3],bbox[2]))
image[:, bbox[1]:bbox[1]+bbox[3], bbox[0]:bbox[0]+bbox[2]] = FN.normalize(FN.to_tensor(scImg), mean=(0.5,)*self.nc, std=(0.5,)*self.nc)
#imglabel[label] = 1
if i == randbox:
outBox = FN.normalize(FN.to_tensor(FN.resize(scImg, (self.bbox_out_size, self.bbox_out_size))), mean=(0.5,)*self.nc, std=(0.5,)*self.nc)
mask = torch.zeros(1,self.out_img_size,self.out_img_size)
mask[0,bbox[1]:bbox[1]+bbox[3],bbox[0]:bbox[0]+bbox[2]] = 1.
outbbox = bbox
#boxlabel[label]=1
#return image[[0,0,0],::], torch.FloatTensor([1]), outBox[[0,0,0],::], torch.FloatTensor([1]), mask, torch.IntTensor(outbbox)
return image, torch.FloatTensor([1]), outBox, torch.FloatTensor([1]), mask, torch.IntTensor(outbbox)
示例10: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, image, target):
size = self.get_size(image.size)
image = F.resize(image, size)
target = target.resize(image.size)
return image, target
示例11: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, image, target):
if -1 not in self.force_test_scale:
size = tuple(force_test_scale)
else:
size = self.get_size(image.size)
if self.preprocess_type == "random_crop":
size = self.reset_size(image.size, size)
image = F.resize(image, size)
target = target.resize(image.size)
return image, target
示例12: resize_image
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def resize_image(image, desired_width=768, desired_height=384, random_pad=False):
"""Resizes an image keeping the aspect ratio mostly unchanged.
Returns:
image: the resized image
window: (x1, y1, x2, y2). If max_dim is provided, padding might
be inserted in the returned image. If so, this window is the
coordinates of the image part of the full image (excluding
the padding). The x2, y2 pixels are not included.
scale: The scale factor used to resize the image
padding: Padding added to the image [left, top, right, bottom]
"""
# Default window (x1, y1, x2, y2) and default scale == 1.
w, h = image.size
width_scale = desired_width / w
height_scale = desired_height / h
scale = min(width_scale, height_scale)
# Resize image using bilinear interpolation
if scale != 1:
image = functional.resize(image, (round(h * scale), round(w * scale)))
w, h = image.size
y_pad = desired_height - h
x_pad = desired_width - w
top_pad = random.randint(0, y_pad) if random_pad else y_pad // 2
left_pad = random.randint(0, x_pad) if random_pad else x_pad // 2
padding = (left_pad, top_pad, x_pad - left_pad, y_pad - top_pad)
assert all([x >= 0 for x in padding])
image = functional.pad(image, padding)
window = [left_pad, top_pad, w + left_pad, h + top_pad]
return image, window, scale, padding
示例13: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, image, target):
image = F.resize(image, self.resize_shape)
target = F.resize(
target, self.resize_shape, interpolation=Image.NEAREST
)
return image, target
示例14: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, image, target):
image = F.resize(image, self.resize_shape)
return image, target
示例15: __call__
# 需要导入模块: from torchvision.transforms import functional [as 别名]
# 或者: from torchvision.transforms.functional import resize [as 别名]
def __call__(self, image, target=None):
size = self.get_size(image.size)
image = F.resize(image, size)
if target is None:
return image
target = target.resize(image.size)
return image, target