-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare_submission.py
50 lines (42 loc) · 1.49 KB
/
prepare_submission.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
import os
from PIL import Image
import numpy as np
def rle_encode(img):
'''
img: numpy array, 1 - mask, 0 - background
Returns run length as string formated
'''
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):
'''
mask_rle: run-length as string formated (start length)
shape: (height,width) of array to return
Returns numpy array, 1 - mask, 0 - background
'''
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)
def create_rles():
"""Used for Kaggle submission: predicts and encode all test images"""
dir = 'data/test_preds/'
N = len(list(os.listdir(dir)))
with open('submission_file.csv', 'w') as f:
f.write('ImageClassId,rle_mask\n')
for index, i in enumerate(os.listdir(dir)):
# print('{}/{}'.format(index, N))
mask = Image.open(dir + i)
mask = mask.resize((1024, 1024), resample=Image.NEAREST)
mask = np.array(mask)
for x in range(1, 25):
enc = rle_encode(mask == x)
f.write(f"{i.split('_')[0]}_{x},{enc}\n")
create_rles()