forked from rogerxujiang/dstl_unet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_submission.py
51 lines (40 loc) · 1.35 KB
/
merge_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
51
__author__ = 'rogerjiang'
'''
This file merges the prediction files for all 10 classes and output a submission
file
'''
import pandas as pd
import os
from shapely import wkt
import sys
data_dir = os.path.join('.', 'submission')
if __name__ == '__main__':
files = []
for cl in range(10):
files.append(
pd.read_csv(
os.path.join(data_dir, 'class_{}.csv'.format(cl))))
df = pd.read_csv(
os.path.join(data_dir, '..', 'data/sample_submission.csv'))
length = (len(df) - 1) / 100
for idx, row in df.iterrows():
shape = wkt.loads(files[row[1] - 1].iloc[idx, 2])
if shape.is_valid:
df.iloc[idx, 2] = files[row[1] - 1].iloc[idx, 2]
else:
print \
'Index {}, ImageId {}, Class {} is not valid; ' \
'fixing it;\n'.format(idx, row[0], row[1])
shape1 = shape.buffer(0)
assert shape1.is_valid
df.iloc[idx, 2] = wkt.dumps(shape1)
if not idx % length:
sys.stdout.write(
'\r[' + '#' * (idx / length) + \
'-' * (100 - idx / length) + ']' + \
'Working on image No. {}'.format(idx))
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
df.to_csv(
os.path.join(data_dir, 'valid_submission.csv'), index=False)