π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Merged with bug fixes for bounding box points order.
Jian Sf7905486ed1d
diff --cc kornia/augmentation/param_gen.pyindex a2b21c5c6,4199e3e51..fdecbb6e6--- a/kornia/augmentation/param_gen.py+++ b/kornia/augmentation/param_gen.py@@@ -1,6 -1,5 +1,6 @@@- from typing import Tuple, List, Union, Dict, Optional+ from typing import Tuple, List, Union, Dict, Optional, cast+import numpy as npimport torchfrom torch.distributions import Uniformdiff --cc kornia/geometry/transform/crop.pyindex e391cd149,2acd44dd3..455a84ff2--- a/kornia/geometry/transform/crop.py+++ b/kornia/geometry/transform/crop.py@@@ -21,7 -22,7 +22,7 @@@ def crop_and_resize(tensor: torch.Tensoboxes (torch.Tensor): a tensor containing the coordinates of thebounding boxes to be extracted. The tensor must have the shapeof Bx4x2, where each box is defined in the following (clockwise)-- order: top-left, top-right, bottom-right and bottom-left. The++ order: top-left, top-right, bottom-left and bottom-right. Thecoordinates must be in the x, y order.size (Tuple[int, int]): a tuple with the height and width that will beused to resize the extracted patches.@@@ -63,30 -64,23 +64,23 @@@dst_w: torch.Tensor = torch.tensor(size[1])# [x, y] origin-- # top-left, top-right, bottom-right, bottom-left- points_src: torch.Tensor = boxes.to(- tensor.device).to(tensor.dtype)++ # top-left, top-right, bottom-left, bottom-right+ points_src: torch.Tensor = boxes# [x, y] destination-- # top-left, top-right, bottom-right, bottom-left- points_dst: torch.Tensor = _get_perspective_dst(- size).repeat(points_src.shape[0], 1, 1).to(tensor.device).to(tensor.dtype)-- # warping needs data in the shape of BCHW- is_unbatched: bool = tensor.ndimension() == 3- if is_unbatched:- tensor = torch.unsqueeze(tensor, dim=0)-- patches = _crop_wrapper(tensor, points_src, points_dst)-- # return in the original shape- if is_unbatched:- patches = torch.squeeze(patches, dim=0)++ # top-left, top-right, bottom-left, bottom-right+ points_dst: torch.Tensor = torch.tensor([[+ [0, 0],- [dst_w - 1, 0],- [dst_w - 1, dst_h - 1],- [0, dst_h - 1],++ [0, dst_w - 1],++ [dst_h - 1, 0],++ [dst_h - 1, dst_w - 1],+ ]]).repeat(points_src.shape[0], 1, 1)- return patches+ return crop_by_boxes(tensor, points_src, points_dst, return_transform=return_transform)- def center_crop(tensor: torch.Tensor, size: Tuple[int, int]) -> torch.Tensor:+ def center_crop(tensor: torch.Tensor, size: Tuple[int, int],+ return_transform: bool = False) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:r"""Crops the given tensor at the center.Args:@@@ -122,26 -116,52 +116,48 @@@.format(size))# unpack input sizes- dst_h: torch.Tensor = torch.tensor(size[0])- dst_w: torch.Tensor = torch.tensor(size[1])- src_h: torch.Tensor = torch.tensor(tensor.shape[-2])- src_w: torch.Tensor = torch.tensor(tensor.shape[-1])+ dst_h, dst_w = size+ src_h, src_w = tensor.shape[-2:]# compute start/end offsets- dst_h_half: torch.Tensor = dst_h / 2- dst_w_half: torch.Tensor = dst_w / 2- src_h_half: torch.Tensor = src_h / 2- src_w_half: torch.Tensor = src_w / 2+ dst_h_half = dst_h / 2+ dst_w_half = dst_w / 2+ src_h_half = src_h / 2+ src_w_half = src_w / 2- start_x: torch.Tensor = src_h_half - dst_h_half- start_y: torch.Tensor = src_w_half - dst_w_half-- end_x: torch.Tensor = start_x + dst_w - 1- end_y: torch.Tensor = start_y + dst_h - 1+ start_x = src_h_half - dst_h_half+ start_y = src_w_half - dst_w_half++ end_x = start_x + dst_w - 1++ end_y = start_y + dst_h - 1# [y, x] origin# top-left, top-right, bottom-left, bottom-right- points_src: torch.Tensor = _get_perspective_src(start_x, start_y, size).to(tensor.device).to(tensor.dtype)+ points_src: torch.Tensor = torch.tensor([[+ [start_y, start_x],+ [start_y, end_x],+ [end_y, start_x],+ [end_y, end_x],+ ]])# [y, x] destination# top-left, top-right, bottom-left, bottom-right- points_dst: torch.Tensor = _get_perspective_dst(size).to(tensor.device).to(tensor.dtype)+ points_dst: torch.Tensor = torch.tensor([[+ [0, 0],+ [0, dst_w - 1],+ [dst_h - 1, 0],+ [dst_h - 1, dst_w - 1],+ ]])++ return crop_by_boxes(tensor, points_src, points_dst, return_transform=return_transform)++ def crop_by_boxes(tensor, src_box, dst_box,+ return_transform: bool = False) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:+ """A wrapper performs crop transform with bounding boxes.++ """- print(tensor.ndimension(), tensor.ndimension() in [3, 4])+ if tensor.ndimension() not in [3, 4]:+ raise TypeError("Only tensor with shape (C, H, W) and (B, C, H, W) supported. Got %s" % str(tensor.shape))# warping needs data in the shape of BCHWis_unbatched: bool = tensor.ndimension() == 3if is_unbatched:@@@ -153,40 -183,47 +179,46 @@@if is_unbatched:patches = torch.squeeze(patches, dim=0)- return patches--- def _get_perspective_src(start_x: int, start_y: int, size: Tuple[int, int]) -> torch.Tensor:+ if return_transform:+ return patches, dst_trans_src- end_x = start_x + size[1] - 1- end_y = start_y + size[0] - 1-- # [y, x] origin- # top-left, top-right, bottom-left, bottom-right- return torch.tensor([[- [start_y, start_x],- [start_y, end_x],- [end_y, start_x],- [end_y, end_x],- ]])+ return patches- def _get_perspective_dst(size: Tuple[int, int]) -> torch.Tensor:- # [y, x] destination- # top-left, top-right, bottom-left, bottom-right- return torch.tensor([[- [0, 0],- [size[1] - 1, 0],- [size[1] - 1, size[0] - 1],- [0, size[0] - 1],- ]])-+ def _infer_bounding_box(boxes: torch.Tensor) -> Tuple[int, int]:+ r"""Auto-infer the output sizes.- def _crop_wrapper(input: torch.Tensor, src: torch.Tensor, dst: torch.Tensor) -> torch.Tensor:- dst_trans_src: torch.Tensor = get_perspective_transform(src, dst)- dst_trans_src = dst_trans_src.repeat(input.shape[0], 1, 1)+ Args:+ boxes (torch.Tensor): a tensor containing the coordinates of the+ bounding boxes to be extracted. The tensor must have the shape+ of Bx4x2, where each box is defined in the following (clockwise)- order: top-left, top-right, bottom-right and bottom-left. The++ order: top-left, top-right, bottom-left and bottom-right. The+ coordinates must be in the x, y order.- size = (int(dst.squeeze()[-1][-1] + 1), int(dst.squeeze()[1][0] + 1))+ Returns:+ torch.Tensor: tensor containing the patches with shape BxN1xN2- patches: torch.Tensor = warp_perspective(input, dst_trans_src, size)- return patches+ Example:+ >>> boxes = torch.tensor([[+ [1., 1.],+ [1., 2.],+ [2., 1.],+ [2., 2.],+ ]]) # 1x4x2+ >>> _infer_bounding_box(boxes)+ (2, 2)+ """-+ assert (boxes[:, 1, 1] - boxes[:, 0, 1] + 1).equal(boxes[:, 3, 1] - boxes[:, 2, 1] + 1), \+ "Boxes must have be square, while get widths %s and %s" % \+ (str(boxes[:, 1, 1] - boxes[:, 0, 1] + 1), str(boxes[:, 3, 1] - boxes[:, 2, 1] + 1))- assert (boxes[:, 2, 0] - boxes[:, 0, 0]).equal(boxes[:, 3, 0] - boxes[:, 1, 0]), \++ assert (boxes[:, 2, 0] - boxes[:, 0, 0] + 1).equal(boxes[:, 3, 0] - boxes[:, 1, 0] + 1), \+ "Boxes must have be square, while get heights %s and %s" % \+ (str(boxes[:, 2, 0] - boxes[:, 0, 0] + 1), str(boxes[:, 3, 0] - boxes[:, 1, 0] + 1))+ assert len((boxes[:, 1, 1] - boxes[:, 0, 1] + 1).unique()) == 1, \+ "Boxes can only have one widths, got %s" % str((boxes[:, 1, 1] - boxes[:, 0, 1] + 1).unique())- assert len((boxes[:, 2, 0] - boxes[:, 0, 0]).unique()) == 1, \++ assert len((boxes[:, 2, 0] - boxes[:, 0, 0] + 1).unique()) == 1, \+ "Boxes can only have one heights, got %s" % str((boxes[:, 2, 0] - boxes[:, 0, 0] + 1).unique())+- height = (boxes[:, 2, 0] - boxes[:, 0, 0]).unique().data.item()- width = (boxes[:, 1, 1] - boxes[:, 0, 1]).unique().data.item()++ height = (boxes[:, 2, 0] - boxes[:, 0, 0] + 1).unique().data.item()++ width = (boxes[:, 1, 1] - boxes[:, 0, 1] + 1).unique().data.item()+ return (height, width)