r"""Rotate a tensor by given angle. Args: inp(Tensor): input image, format must be NCHW or NHWC. angle(float): rotation angle of the image. format(str, optional): format of the input tensor, currently only supports NCHW and NHWC. interp_mode(str, optional): inter
(
inp: Tensor,
angle: float = 0.0,
format: str = "NCHW",
interp_mode: str = "bilinear",
)
| 715 | |
| 716 | |
| 717 | def rotate( |
| 718 | inp: Tensor, |
| 719 | angle: float = 0.0, |
| 720 | format: str = "NCHW", |
| 721 | interp_mode: str = "bilinear", |
| 722 | ) -> Tensor: |
| 723 | r"""Rotate a tensor by given angle. |
| 724 | |
| 725 | Args: |
| 726 | inp(Tensor): input image, format must be NCHW or NHWC. |
| 727 | angle(float): rotation angle of the image. |
| 728 | format(str, optional): format of the input tensor, currently only supports NCHW and NHWC. |
| 729 | interp_mode(str, optional): interpoloation mode, currently only supports bilinear for NCHW format |
| 730 | and area mode for NHWC format. |
| 731 | |
| 732 | Returns: |
| 733 | rotated result. |
| 734 | """ |
| 735 | if format == "NCHW": |
| 736 | assert ( |
| 737 | interp_mode == "bilinear" |
| 738 | ), "Currently NCHW format only supports bilinear mode." |
| 739 | interp_mode = interp_mode[2:] |
| 740 | height, width = inp.shape[2:] |
| 741 | elif format == "NHWC": |
| 742 | assert interp_mode == "area", "Currently NHWC format only supports area mode." |
| 743 | height, width = inp.shape[1:3] |
| 744 | |
| 745 | angle = angle / 180.0 * np.pi |
| 746 | |
| 747 | rotate_matrix = Tensor( |
| 748 | [ |
| 749 | [ |
| 750 | [ |
| 751 | cos(angle), |
| 752 | -sin(angle), |
| 753 | (1 - cos(angle)) * ((width - 1) / 2) |
| 754 | + sin(angle) * ((height - 1) / 2), |
| 755 | ], |
| 756 | [ |
| 757 | sin(angle), |
| 758 | cos(angle), |
| 759 | -sin(angle) * ((width - 1) / 2) |
| 760 | + (1 - cos(angle)) * ((height - 1) / 2), |
| 761 | ], |
| 762 | ] |
| 763 | ] |
| 764 | ) |
| 765 | |
| 766 | out_shape = (height, width) |
| 767 | output = warp_affine( |
| 768 | inp, |
| 769 | rotate_matrix, |
| 770 | out_shape, |
| 771 | border_mode="constant", |
| 772 | format=format, |
| 773 | interp_mode=interp_mode, |
| 774 | ) |