Get importance map for different weight modes. Args: patch_size: Size of the required importance map. This should be either H, W [,D]. mode: {``"constant"``, ``"gaussian"``} How to blend output of overlapping windows. Defaults to ``"constant"``. - ``"con
(
patch_size: tuple[int, ...],
mode: BlendMode | str = BlendMode.CONSTANT,
sigma_scale: Sequence[float] | float = 0.125,
device: torch.device | int | str = "cpu",
dtype: torch.dtype | str | None = torch.float32,
)
| 1057 | |
| 1058 | |
| 1059 | def compute_importance_map( |
| 1060 | patch_size: tuple[int, ...], |
| 1061 | mode: BlendMode | str = BlendMode.CONSTANT, |
| 1062 | sigma_scale: Sequence[float] | float = 0.125, |
| 1063 | device: torch.device | int | str = "cpu", |
| 1064 | dtype: torch.dtype | str | None = torch.float32, |
| 1065 | ) -> torch.Tensor: |
| 1066 | """Get importance map for different weight modes. |
| 1067 | |
| 1068 | Args: |
| 1069 | patch_size: Size of the required importance map. This should be either H, W [,D]. |
| 1070 | mode: {``"constant"``, ``"gaussian"``} |
| 1071 | How to blend output of overlapping windows. Defaults to ``"constant"``. |
| 1072 | |
| 1073 | - ``"constant``": gives equal weight to all predictions. |
| 1074 | - ``"gaussian``": gives less weight to predictions on edges of windows. |
| 1075 | |
| 1076 | sigma_scale: Sigma_scale to calculate sigma for each dimension |
| 1077 | (sigma = sigma_scale * dim_size). Used for gaussian mode only. |
| 1078 | device: Device to put importance map on. |
| 1079 | dtype: Data type of the output importance map. |
| 1080 | |
| 1081 | Raises: |
| 1082 | ValueError: When ``mode`` is not one of ["constant", "gaussian"]. |
| 1083 | |
| 1084 | Returns: |
| 1085 | Tensor of size patch_size. |
| 1086 | |
| 1087 | """ |
| 1088 | mode = look_up_option(mode, BlendMode) |
| 1089 | device = torch.device(device) |
| 1090 | if mode == BlendMode.CONSTANT: |
| 1091 | importance_map = torch.ones(patch_size, device=device, dtype=torch.float) |
| 1092 | elif mode == BlendMode.GAUSSIAN: |
| 1093 | sigma_scale = ensure_tuple_rep(sigma_scale, len(patch_size)) |
| 1094 | sigmas = [i * sigma_s for i, sigma_s in zip(patch_size, sigma_scale)] |
| 1095 | |
| 1096 | for i in range(len(patch_size)): |
| 1097 | x = torch.arange( |
| 1098 | start=-(patch_size[i] - 1) / 2.0, end=(patch_size[i] - 1) / 2.0 + 1, dtype=torch.float, device=device |
| 1099 | ) |
| 1100 | x = torch.exp(x**2 / (-2 * sigmas[i] ** 2)) # 1D gaussian |
| 1101 | importance_map = importance_map.unsqueeze(-1) * x[(None,) * i] if i > 0 else x |
| 1102 | else: |
| 1103 | raise ValueError( |
| 1104 | f"Unsupported mode: {mode}, available options are [{BlendMode.CONSTANT}, {BlendMode.CONSTANT}]." |
| 1105 | ) |
| 1106 | # handle non-positive weights |
| 1107 | min_non_zero = max(torch.min(importance_map).item(), 1e-3) |
| 1108 | importance_map = torch.clamp_(importance_map.to(torch.float), min=min_non_zero).to(dtype) |
| 1109 | return importance_map |
| 1110 | |
| 1111 | |
| 1112 | def is_supported_format(filename: Sequence[PathLike] | PathLike, suffixes: Sequence[str]) -> bool: |
no test coverage detected
searching dependent graphs…