-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_submission_rssrai.py
139 lines (112 loc) · 4.8 KB
/
make_submission_rssrai.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
import argparse
import csv
import json
import gzip
from functools import partial
from pathlib import Path
from multiprocessing.pool import Pool
import traceback
from typing import List, Tuple, Set
import cv2
import numpy as np
import utils_rssrai
from train_rssrai import Model, HyperParams, Image
logger = utils_rssrai.get_logger(__name__)
def main():
parser = argparse.ArgumentParser()
arg = parser.add_argument
arg('logdir', type=Path, help='Path to log directory')
#arg('output', type=str, help='Submission csv')
arg('--only', help='Only predict these image ids (comma-separated)')
arg('--threshold', type=float, default=0.5)
arg('--epsilon', type=float, default=2.0, help='smoothing')
arg('--min-area', type=float, default=50.0)
arg('--min-small-area', type=float, default=10.0)
arg('--masks-only', action='store_true', help='Do only mask prediction')
arg('--model-path', type=Path,
help='Path to a specific model (if the last is not desired)')
arg('--processes', type=int, default=30)
arg('--validation', action='store_true',
help='only validation images, check jaccard, '
'save masks and polygons as png')
arg('--valid-polygons', action='store_true', help='validation via polygons')
arg('--force-predict', action='store_true')
arg('--no-edges', action='store_true', help='disable prediction on edges')
arg('--buffer', type=float, help='do .buffer(x) on pred polygons')
args = parser.parse_args()
hps = HyperParams(**json.loads(
args.logdir.joinpath('hps.json').read_text()))
only = set(args.only.split(',')) if args.only else set()
store = args.logdir # type: Path
test_ids = set(['GF2_PMS1__20150902_L1A0001015646-MSS1',
'GF2_PMS1__20150902_L1A0001015648-MSS1',
'GF2_PMS1__20150912_L1A0001037899-MSS1',
'GF2_PMS1__20150926_L1A0001064469-MSS1',
'GF2_PMS1__20160327_L1A0001491484-MSS1',
'GF2_PMS1__20160430_L1A0001553848-MSS1',
'GF2_PMS1__20160623_L1A0001660727-MSS1',
'GF2_PMS1__20160627_L1A0001668483-MSS1',
'GF2_PMS1__20160704_L1A0001680853-MSS1',
'GF2_PMS1__20160801_L1A0001734328-MSS1'])
val_ids = set(['GF2_PMS1__20160421_L1A0001537716-MSS1',
'GF2_PMS2__20150217_L1A0000658637-MSS2'])
if only:
to_predict = only
elif args.validation:
to_predict = set(val_ids)
else:
to_predict = set(test_ids)
if not args.force_predict:
to_predict_masks = [
im_id for im_id in to_predict if not mask_path(store, im_id).exists()]
else:
to_predict_masks = to_predict
if to_predict_masks:
predict_masks(args, hps, store, to_predict_masks, args.threshold,
validation=args.validation, no_edges=args.no_edges)
if args.masks_only:
logger.info('Was building masks only, done.')
return
def mask_path(store: Path, im_id: str) -> Path:
return store.joinpath('{}.bin-mask.gz'.format(im_id))
def predict_masks(args, hps, store, to_predict: List[str], threshold: float,
validation: str=None, no_edges: bool=False):
logger.info('Predicting {} masks: {}'
.format(len(to_predict), ', '.join(sorted(to_predict))))
model = Model(hps=hps)
if args.model_path:
model.restore_snapshot(args.model_path)
else:
model.restore_last_snapshot(args.logdir)
def load_im(im_id):
data = model.preprocess_image(utils_rssrai.load_image(im_id))
if hps.n_channels != data.shape[0]:
data = data[:hps.n_channels]
if validation == 'square':
data = square(data, hps)
return Image(id=im_id, data=data)
def predict_mask(im):
logger.info(im.id)
return im, model.predict_image_mask(im.data, no_edges=no_edges)
im_masks = map(predict_mask, utils_rssrai.imap_fixed_output_buffer(
load_im, sorted(to_predict), threads=2))
for im, mask in im_masks:
assert mask.shape[1:] == im.data.shape[1:]
with gzip.open(str(mask_path(store, im.id)), 'wb') as f:
# TODO - maybe do (mask * 20).astype(np.uint8)
np.save(f, mask)
# for im, mask in utils_rssrai.imap_fixed_output_buffer(
# lambda _: next(im_masks), to_predict, threads=1):
# assert mask.shape[1:] == im.data.shape[1:]
# with gzip.open(str(mask_path(store, im.id)), 'wb') as f:
# # TODO - maybe do (mask * 20).astype(np.uint8)
# np.save(f, mask)
def square(x, hps):
if len(x.shape) == 2 or x.shape[2] <= 20:
return x[:hps.validation_square, :hps.validation_square]
else:
assert x.shape[0] <= 20
return x[:, :hps.validation_square, :hps.validation_square]
if __name__ == '__main__':
main()