-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmission_to_mask.py
More file actions
54 lines (43 loc) · 1.22 KB
/
submission_to_mask.py
File metadata and controls
54 lines (43 loc) · 1.22 KB
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
#!/usr/bin/python
import os
import sys
import Image
import math
import matplotlib.image as mpimg
import numpy as np
label_file = 'dummy_submission.csv'
h = 16
w = h
imgwidth = int(math.ceil((600.0/w))*w)
imgheight = int(math.ceil((600.0/h))*h)
nc = 3
# Convert an array of binary labels to a uint8
def binary_to_uint8(img):
rimg = (img * 255).round().astype(np.uint8)
return rimg
def reconstruct_from_labels(image_id):
im = np.zeros((imgwidth, imgheight), dtype=np.uint8)
f = open(label_file)
lines = f.readlines()
image_id_str = '%.3d_' % image_id
for i in range(1, len(lines)):
line = lines[i]
if not image_id_str in line:
continue
tokens = line.split(',')
id = tokens[0]
prediction = int(tokens[1])
tokens = id.split('_')
i = int(tokens[1])
j = int(tokens[2])
je = min(j+w, imgwidth)
ie = min(i+h, imgheight)
if prediction == 0:
adata = np.zeros((w,h))
else:
adata = np.ones((w,h))
im[j:je, i:ie] = binary_to_uint8(adata)
Image.fromarray(im).save('prediction_' + '%.3d' % image_id + '.png')
return im
for i in range(1, 5):
reconstruct_from_labels(i)