Example #1
0
 def compute_transformation(self, input: Tensor, params: Dict[str, Tensor]) -> Tensor:
     if self.flags["cropping_mode"] == "resample":
         transform: Tensor = get_perspective_transform(params["src"].to(input), params["dst"].to(input))
         return transform
     if self.flags["cropping_mode"] == "slice":  # Skip the computation for slicing.
         return self.identity_matrix(input)
     raise NotImplementedError(f"Not supported type: {self.flags['cropping_mode']}.")
Example #2
0
    def compute_transformation(self, input: Tensor,
                               params: Dict[str, Tensor]) -> Tensor:
        if params["output_size"] == input.shape[-2:]:
            return eye_like(3, input)

        transform: Tensor = get_perspective_transform(params["src"],
                                                      params["dst"])
        transform = transform.expand(input.shape[0], -1, -1)
        return transform
Example #3
0
def apply_perspective(input: torch.Tensor,
                      params: Dict[str, torch.Tensor],
                      return_transform: bool = False) -> UnionType:
    r"""Perform perspective transform of the given torch.Tensor or batch of tensors.

    Args:
        input (torch.Tensor): Tensor to be transformed with shape BxCxHxW.
        start_points (torch.Tensor): Tensor containing [top-left, top-right, bottom-right,
        bottom-left] of the orignal image with shape Bx4x2.
        end_points (torch.Tensor): Tensor containing [top-left, top-right, bottom-right,
        bottom-left] of the transformed image with shape Bx4x2.
        return_transform (bool): if ``True`` return the matrix describing the transformation
        applied to each. Default: False.

    Returns:
        torch.Tensor: Perspectively transformed tensor.
    """

    if not torch.is_tensor(input):
        raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}")

    device: torch.device = input.device
    dtype: torch.dtype = input.dtype

    # arrange input data
    x_data: torch.Tensor = input.view(-1, *input.shape[-3:])

    batch_size, _, height, width = x_data.shape

    # compute the homography between the input points
    transform: torch.Tensor = get_perspective_transform(
        params['start_points'], params['end_points']).to(device, dtype)

    out_data: torch.Tensor = x_data.clone()

    # process valid samples
    mask = params['batch_prob'].to(device)

    # TODO: look for a workaround for this hack. In CUDA it fails when no elements found.

    if bool(mask.sum() > 0):
        # apply the computed transform
        height, width = x_data.shape[-2:]
        out_data[mask] = warp_perspective(x_data[mask], transform[mask],
                                          (height, width))

    if return_transform:
        return out_data.view_as(input), transform

    return out_data.view_as(input)
Example #4
0
 def compute_transformation(self, input: Tensor,
                            params: Dict[str, Tensor]) -> Tensor:
     return get_perspective_transform(params["start_points"].to(input),
                                      params["end_points"].to(input))