(self, img: NdarrayOrTensor, sub=None, div=None)
| 886 | return x.item() if x.numel() == 1 else x |
| 887 | |
| 888 | def _normalize(self, img: NdarrayOrTensor, sub=None, div=None) -> NdarrayOrTensor: |
| 889 | img, *_ = convert_data_type(img, dtype=torch.float32) |
| 890 | |
| 891 | if self.nonzero: |
| 892 | slices = img != 0 |
| 893 | masked_img = img[slices] |
| 894 | if not slices.any(): |
| 895 | return img |
| 896 | else: |
| 897 | slices = None |
| 898 | masked_img = img |
| 899 | |
| 900 | _sub = sub if sub is not None else self._mean(masked_img) |
| 901 | if isinstance(_sub, (torch.Tensor, np.ndarray)): |
| 902 | _sub, *_ = convert_to_dst_type(_sub, img) |
| 903 | if slices is not None: |
| 904 | _sub = _sub[slices] |
| 905 | |
| 906 | _div = div if div is not None else self._std(masked_img) |
| 907 | if np.isscalar(_div): |
| 908 | if _div == 0.0: |
| 909 | _div = 1.0 |
| 910 | elif isinstance(_div, (torch.Tensor, np.ndarray)): |
| 911 | _div, *_ = convert_to_dst_type(_div, img) |
| 912 | if slices is not None: |
| 913 | _div = _div[slices] |
| 914 | _div[_div == 0.0] = 1.0 |
| 915 | |
| 916 | if slices is not None: |
| 917 | img[slices] = (masked_img - _sub) / _div |
| 918 | else: |
| 919 | img = (img - _sub) / _div |
| 920 | return img |
| 921 | |
| 922 | def __call__(self, img: NdarrayOrTensor) -> NdarrayOrTensor: |
| 923 | """ |
no test coverage detected