-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
86 lines (73 loc) · 2.06 KB
/
helper.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import torch
import numpy as np
import tifffile as tiff
from torch import nn
class RGB(nn.Module):
IMAGE_RGB_MEAN = [0.485, 0.456, 0.406] # [0.5, 0.5, 0.5]
IMAGE_RGB_STD = [0.229, 0.224, 0.225] # [0.5, 0.5, 0.5]
def __init__(self, ):
super(RGB, self).__init__()
self.register_buffer('mean', torch.zeros(1, 3, 1, 1))
self.register_buffer('std', torch.ones(1, 3, 1, 1))
self.mean.data = torch.FloatTensor(self.IMAGE_RGB_MEAN).view(self.mean.shape)
self.std.data = torch.FloatTensor(self.IMAGE_RGB_STD).view(self.std.shape)
def forward(self, x):
x = (x - self.mean) / self.std
return x
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def rle_encode(mask):
m = mask.T.flatten()
m = np.concatenate([[0], m, [0]])
run = np.where(m[1:] != m[:-1])[0] + 1
run[1::2] -= run[::2]
rle = ' '.join(str(r) for r in run)
return rle
def read_tiff(image_file, mode='rgb'):
image = tiff.imread(image_file)
image = image.squeeze()
if image.shape[0] == 3:
image = image.transpose(1, 2, 0)
if mode=='bgr':
image = image[:,:,::-1]
image = np.ascontiguousarray(image)
return image
organ_meta = dotdict(
kidney = dotdict(
label = 1,
um = 0.5000,
ftu ='glomeruli',
),
prostate = dotdict(
label = 2,
um = 6.2630,
ftu ='glandular acinus',
),
largeintestine = dotdict(
label = 3,
um = 0.2290,
ftu ='crypt',
),
spleen = dotdict(
label = 4,
um = 0.4945,
ftu ='white pulp',
),
lung = dotdict(
label = 5,
um = 0.7562,
ftu ='alveolus',
),
)
organ_to_label = {k: organ_meta[k].label for k in organ_meta.keys()}
label_to_organ = {v:k for k,v in organ_to_label.items()}
def image_to_tensor(image, mode='rgb'):
if mode=='bgr' :
image = image[:,:,::-1]
x = image.transpose(2,0,1)
x = np.ascontiguousarray(x)
x = torch.tensor(x)
return x