-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRLE.py
86 lines (72 loc) · 2.5 KB
/
RLE.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
"""
Script with utility functions related to the submission: RLE encoding and submission preparation
"""
from skimage.morphology import label
from skimage.transform import resize
import pandas as pd
import numpy as np
def rle_encoding(x):
dots = np.where(x.T.flatten() == 1)[0]
run_lengths = []
prev = -2
for b in dots:
if (b>prev+1): run_lengths.extend((b + 1, 0))
run_lengths[-1] += 1
prev = b
return run_lengths
def prob_to_rles(x, cutoff=0.5):
lab_img = label(x > cutoff)
#lab_img = x
for i in range(1, lab_img.max() + 1):
yield rle_encoding(lab_img == i)
def prepareSubmission_new(test_names,predictions,submission_filename):
new_test_ids = []
rles = []
for n, id_ in enumerate(test_names[0:]):
print('Resizing back: '+str(n))
rle = list(prob_to_rles(predictions[n]))
rles.extend(rle)
new_test_ids.extend([id_] * len(rle))
# Create submission DataFrame
sub = pd.DataFrame()
sub['ImageId'] = new_test_ids
sub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))
sub.to_csv(submission_filename, index=False)
def prepareSubmission(test_names,predictions,original_sizes,submission_filename):
new_test_ids = []
rles = []
for n, id_ in enumerate(test_names[0:]):
print('Resizing back: '+str(n))
final_predictions = resize(predictions[n],original_sizes[n],mode='constant',preserve_range=True)
rle = list(prob_to_rles(final_predictions))
rles.extend(rle)
new_test_ids.extend([id_] * len(rle))
# Create submission DataFrame
sub = pd.DataFrame()
sub['ImageId'] = new_test_ids
sub['EncodedPixels'] = pd.Series(rles).apply(lambda x: ' '.join(str(y) for y in x))
sub.to_csv(submission_filename, index=False)
def unique(list1):
# intilize a null list
unique_list = []
# traverse for all elements
for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
return unique_list
def searchAppearence(l,w):
for i in range(0,len(l)):
if l[i] == w:
return True
else:
pass
return False
def findNonAppeared(list_all,list_some):
non_app = []
for i in range(0,len(list_all)):
if searchAppearence(list_some,list_all[i]) == True:
pass
else:
non_app = non_app + [list_all[i]]
return non_app