forked from developmentseed/ml-hv-grid-pub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
140 lines (107 loc) · 3.88 KB
/
Copy pathutils.py
File metadata and controls
140 lines (107 loc) · 3.88 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
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
140
"""
utils.py
@author: Development Seed
Utility functions for printing training details
"""
import shutil
import pprint
import numpy as np
import matplotlib as mpl
from keras.models import model_from_yaml
from pygeotile.tile import Tile
import config as cf
def print_start_details(start_time):
"""Print config at the start of a run."""
pp = pprint.PrettyPrinter(indent=4)
print('Start time: ' + start_time.strftime('%d/%m %H:%M:%S'))
print('\nDatasets used:')
pp.pprint(cf.dataset_fpaths)
print('\nTraining details:')
pp.pprint(cf.train_params)
print('\nModel details:')
pp.pprint(cf.model_params)
print('\n\n' + '=' * 40)
def print_end_details(start_time, end_time):
"""Print runtime information."""
run_time = end_time - start_time
hours, remainder = divmod(run_time.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
print('\n\n' + '=' * 40)
print('End time: ' + end_time.strftime('%d/%m %H:%M:%S'))
print('Total runtime: %i:%02i:%02i' % (hours, minutes, seconds))
def copy_filenames_to_dir(file_list, dst_dir):
"""Copy a list of filenames (like images) to new directory."""
for file_name in file_list:
print('Copying: %s to %s' % (file_name, dst_dir))
shutil.copy(file_name, dst_dir)
print('Done.')
def save_model_yaml(model, model_fpath):
from keras.models import model_from_yaml
"""Save pre-trained Keras model."""
with open(model_fpath, "w") as yaml_file:
yaml_file.write(model.to_yaml())
def load_model(model_fpath, weights_fpath):
"""Load a model from yaml architecture and h5 weights."""
assert model_fpath[-5:] == '.yaml'
assert weights_fpath[-3:] == '.h5'
with open(model_fpath, "r") as yaml_file:
yaml_architecture = yaml_file.read()
model = model_from_yaml(yaml_architecture)
model.load_weights(weights_fpath)
return model
def make_geo_feature(tile_dict, pred, thresh_vals, thresh_cols, coord_prec=5,
cmap=None):
"""Create a GeoJSON feature
Parameters
----------
tile_dict: dict
Dict with `x`, `y`, `z` defined
pred: float
Model's predicted probability for having a feature of interest.
thresh_vals: list
Thresholds for indicated that tile should be mapped
thresh_cols: list
Hex color codes for each threshold
coord_prec: int
Number of decimals to keep for prediction score
cmap: function
Maps value to color hex key
"""
# Convert tile to lat/lon bounds
tile = Tile.from_google(google_x=tile_dict['x'], google_y=tile_dict['y'], zoom=tile_dict['z'])
# Get lower-left (most SW) point and upper-right (most NE) point
ll = list(tile.bounds[0])
ur = list(tile.bounds[1])
# Round pred score to save bytes
for di in range(len(ll)):
ll[di] = np.around(ll[di], decimals=coord_prec)
ur[di] = np.around(ur[di], decimals=coord_prec)
# GeoJSON uses long/lat
bbox = [[[ll[1], ll[0]],
[ur[1], ll[0]],
[ur[1], ur[0]],
[ll[1], ur[0]],
[ll[1], ll[0]]]]
# From pred, get fill color
if cmap is None:
for ii, upper_thresh in enumerate(thresh_vals):
if pred <= upper_thresh:
fill = thresh_cols[ii]
break
else:
fill = mpl.colors.to_hex(cmap.to_rgba(pred))
# Create properties
properties = {'X': tile_dict['x'],
'Y': tile_dict['y'],
'Z': tile_dict['z'],
'pred': pred,
'fill': fill}
#'fill-opacity': 0.5,
#'stroke-width': 1,
#'stroke-opacity': 0.5}
# Generate feature dict
feature = dict(geometry={'coordinates': bbox,
'type': 'Polygon'},
type='Feature',
properties=properties)
return feature