-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
51 lines (36 loc) · 1.28 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
EPS = 1e-10
def dice(true, pred):
true = true.astype(bool)
pred = pred.astype(bool)
intersection = (true & pred).sum()
im_sum = true.sum() + pred.sum()
return 2.0 * intersection / (im_sum + EPS)
def dice_all(true, pred):
return np.mean([dice(t, p) for t, p in zip(true, pred)])
def rle_encode(img):
'''
img: numpy array, 1 - mask, 0 - background
Returns run length as string formated
from: https://www.kaggle.com/kmader/baseline-u-net-model-part-1
'''
pixels = img.flatten()
pixels = np.concatenate([[0], pixels, [0]])
runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x) for x in runs)
def rle_decode(mask_rle, shape=(320, 240)):
'''
mask_rle: run-length as string formated (start length)
shape: (height,width) of array to return
Returns numpy array, 1 - mask, 0 - background
from: https://www.kaggle.com/kmader/baseline-u-net-model-part-1
'''
s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape).T