forked from mehdiec/luminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
34 lines (24 loc) · 1.08 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch
from albumentations.core.transforms_interface import DualTransform
from torchvision.transforms.functional import to_tensor
from pathaia.util.types import NDImage
from typing import Callable, Tuple, Dict
from deep_learning.transforms import ToTensor
ID_TO_CLASS = {0: "Luminal A", 1: "Luminal B", 2: "other"}
class ToTensor(DualTransform):
def __init__(
self, transpose_mask: bool = False, always_apply: bool = True, p: float = 1
):
super().__init__(always_apply=always_apply, p=p)
self.transpose_mask = transpose_mask
@property
def targets(self) -> Dict[str, Callable[[NDImage], torch.Tensor]]:
return {"image": self.apply, "mask": self.apply_to_mask}
def apply(self, img: NDImage, **params) -> torch.Tensor:
return to_tensor(img)
def apply_to_mask(self, mask: NDImage, **params) -> torch.Tensor:
if self.transpose_mask and mask.ndim == 3:
mask = mask.transpose(2, 0, 1)
return torch.from_numpy(mask)
def get_transform_init_args_names(self) -> Tuple[str]:
return ("transpose_mask",)