-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_unet_helpers.py
474 lines (356 loc) · 14.7 KB
/
test_unet_helpers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import rasterio
import glob
import os,sys
from skimage.transform import rescale, resize, downscale_local_mean
from matplotlib import pyplot as plt
import numpy as np
from scipy import misc
import fiona
# import geopandas as gpd
from shapely.geometry import shape
import shapely
from rasterio.mask import mask
from pyproj import Proj, transform
import torch, torchvision
import rasterio
import torch.nn as nn
import torch.nn.init as init
from torch.autograd import Variable
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import random
from math import log10
from collections import OrderedDict
def load_pretrained_weights(model, weight_path):
"""Load pretrianed weights to model
Incompatible layers (unmatched in name or size) will be ignored
Args:
- model (nn.Module): network model, which must not be nn.DataParallel
- weight_path (str): path to pretrained weights
"""
checkpoint = torch.load(weight_path)
if 'state_dict' in checkpoint:
state_dict = checkpoint['state_dict']
else:
state_dict = checkpoint
model_dict = model.state_dict()
new_state_dict = OrderedDict()
matched_layers, discarded_layers = [], []
for k, v in state_dict.items():
# If the pretrained state_dict was saved as nn.DataParallel,
# keys would contain "module.", which should be ignored.
if k.startswith('module.'):
k = k[7:]
if k in model_dict and model_dict[k].size() == v.size():
new_state_dict[k] = v
matched_layers.append(k)
else:
discarded_layers.append(k)
model_dict.update(new_state_dict)
model.load_state_dict(model_dict)
if len(matched_layers) == 0:
print('** ERROR: the pretrained weights "{}" cannot be loaded, please check the key names manually (ignored and continue)'.format(weight_path))
else:
print('Successfully loaded pretrained weights from "{}"'.format(weight_path))
if len(discarded_layers) > 0:
print("* The following layers are discarded due to unmatched keys or layer size: {}".format(discarded_layers))
# convert bounding boxes into string required for DG CatalogImage
# ex. bbox = [-110.85299491882326,32.167148499672855,-110.84870338439943,32.170236308395644] WGS84
def rioBoundBoxUTM_toWGS84(bounds_obj, src_crs, wgs84='4326'):
wgs = Proj(init='epsg:{}'.format(wgs84))
p2 = Proj(init=str(src_crs['init']))
try:
min_x, min_y = transform(p2, wgs, bounds_obj.left, bounds_obj.bottom)
max_x, max_y = transform(p2, wgs, bounds_obj.right, bounds_obj.top)
except:
min_x, min_y = transform(p2, wgs, bounds_obj[0], bounds_obj[1])
max_x, max_y = transform(p2, wgs, bounds_obj[2], bounds_obj[3])
return min_x, min_y, max_x, max_y
def rioBoundBoxWGS84_toUTM(bounds_obj, src_crs, wgs84='4326'):
#print(src_crs['init'])
wgs = Proj(init='epsg:{}'.format(wgs84))
#p2 = Proj(init=str(src_crs['init']))
p2 = Proj(init='epsg:{}'.format(str(src_crs['init'])))
min_x, min_y = transform(wgs, p2, bounds_obj[0], bounds_obj[1])
max_x, max_y = transform(wgs, p2, bounds_obj[2], bounds_obj[3])
return min_x, min_y, max_x, max_y
# define a function to check the image bounds of the two datasets
def imageIntersectionTest(dg_bounds, planet_bounds):
res = ''
if dg_bounds[0] > planet_bounds[0]:
res += ' DG xmin is gt PL xmin'
if dg_bounds[1] > planet_bounds[1]:
res += ' DG ymin is gt PL ymin'
if dg_bounds[2] < planet_bounds[2]:
res += ' DG xmax is lt PL xmax'
if dg_bounds[3] < planet_bounds[3]:
res += ' DG ymax is lt PL ymax'
return res
## define a function to get the chip dimensions from a larger bounding box and a chip dimension
def generateChipBoxesUTM(bbox, box_dim):
# xmin, ymin, xmax, ymax = bbox.bounds
# xmin_chips = np.arange(xmin, xmax - box_dim, box_dim)
# xmax_chips = np.arange(xmin + box_dim, xmax, box_dim)
# ymin_chips = np.arange(ymin, ymax - box_dim, box_dim)
# ymax_chips = np.arange(ymin + box_dim, ymax, box_dim)
# try a for loop
for_loop_result = []
for l_x in np.arange(xmin, xmax-box_dim, box_dim):
for l_y in np.arange(ymin, ymax-box_dim, box_dim):
res = [l_x, l_y, l_x + box_dim, l_y + box_dim ]
for_loop_result.append(res)
return for_loop_result
def generateChipBoxesUTM_WGS84(bbox, box_dim, crs):
# get bounds
xmin, ymin, xmax, ymax = bbox.bounds
print(xmax-xmin)
print(ymax-ymin)
# try a for loop
utm_chips = []
wgs84_chips = []
# construct chip bounds
for l_x in np.arange(xmin, xmax-box_dim, box_dim):
for l_y in np.arange(ymin, ymax-box_dim, box_dim):
res = [l_x, l_y, l_x + box_dim, l_y + box_dim ]
utm_chips.append(res)
arg = box(res[0], res[1], res[2], res[3]).bounds
wgs84_chips.append( rioBoundBoxUTM_toWGS84(arg, crs))
return utm_chips, wgs84_chips
def chip_planet_image(impath, bbox, out_file):
first = (bbox[0], bbox[1]) # xmin, ymin
second = (bbox[0], bbox[3]) # xmin, ymax
third = (bbox[2], bbox[3]) # xmax, ymax
fourth = (bbox[2], bbox[1]) # xmax, ymin
## construct the geometry for rasterio.mask.mask
bbox_geom = shapely.geometry.Polygon([first, second, third, fourth, first])
geom = [shapely.geometry.mapping(bbox_geom)]
# read the image
with rasterio.open(impath, 'r') as src:
out_image, out_transform = mask(src, geom, crop=True)
out_meta = src.meta.copy()
# get some pixel metrics
num_nonzero = np.count_nonzero(out_image[0,:,:])
num_pixels = out_image[0,:,:].size
ratio = float(num_nonzero) / float(num_pixels)
## skip this one if not enough pixels
if ratio < 0.95:
return False
else: #continue with the writing
# update the metadata
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
# write the file
with rasterio.open(out_file, "w", **out_meta) as dest:
dest.write(out_image)
return True
def chip_dg_image(dg_scene, bbox, dg_out_file):
print('bbox {}'.format(bbox))
# get the aoi
img_aoi = dg_scene.aoi(bbox=bbox)
# save the file
# print(dg_out_file)
img_aoi.geotiff(path=dg_out_file)
return
def assignRC(rio_obj, samp_pt, window_size=64, inproj='epsg:4326', outproj='epsg:32613'):
# project the point to source crs
outProj = Proj(init=outproj)
inProj = Proj(init=inproj)
x1,y1 = samp_pt
x2,y2 = transform(inProj,outProj,x1,y1)
# get the row column
temp = rio_obj.index(x2,y2)
r,c = [int(c) for c in temp]
return (samp_pt, r,c)
def verifyWindow(rio_obj, samp_pt, window_size=64, inproj='epsg:4326', outproj='epsg:32613'):
# project the point to source crs
outProj = Proj(init=outproj)
inProj = Proj(init=inproj)
x1,y1 = samp_pt
x2,y2 = transform(inProj,outProj,x1,y1)
# get the row column
temp = rio_obj.index(x2,y2)
r,c = [int(c) for c in temp]
# check what the sample window would look like
r_start = int(r - window_size/2)
r_end = int(r_start + window_size)
c_start = int(c - window_size/2)
c_end = int(c_start + window_size)
try:
arr = rio_obj.read()
win_arr = arr[:,r_start:r_end, c_start:c_end]
# if it is all zero or all NaN and at least 95% data
test_arr = win_arr[0]
test_nan = np.isnan(np.mean(test_arr))
test_zero = np.mean(test_arr) == 0
# print('here')
# plt.imshow(test_arr)
# plt.colorbar()
# plt.show()
# get some pixel metrics
num_nonzero = test_arr.size
num_pixels = test_arr[test_arr == 3].size
ratio = float(num_pixels) / float(num_nonzero)
# print(ratio)
# empirically choose no-data ratio of 10% of window
if (test_nan or test_zero or (ratio > 0.10)) :
# if either of the tests fail, we don't want that window
pass
else:
return [samp_pt, r,c]
except Exception as e:
print(e)
# should only happen if start/end coordinates are outside of image bounds.
# in that case, we don't want it
pass
## calculate the lon/lat of the random coordinates
def calcXYfromRC(aff, coords):
col = coords[1]
row = coords[0]
# get the origin (why is DG storing Affine transform this way???)
ox = aff[2]
oy = aff[5]
# calculate lon / lat
cx = ox + aff[0]*col
cy = oy + aff[4]*row
return (cx,cy)
# lonlat_MS = [calcXYfromRC(img_2m.affine, pair) for pair in coords]
# lonlat_PAN = [calcXYfromRC(image_05m.affine, pair) for pair in coords_pan]
def checkWindow(rio_obj, samp_pt, window_size=64, inproj='epsg:4326', outproj='epsg:32613'):
try:
# project the point to source crs
outProj = Proj(init=outproj)
inProj = Proj(init=inproj)
x1,y1 = samp_pt
x2,y2 = transform(inProj,outProj,x1,y1)
# get the row column
temp = rio_obj.index(x2,y2)
temp = rio_obj.index(x1,y1) # not sure why this needs to happen...????? the above line worked before.
r,c = [int(c) for c in temp]
# check what the sample window would look like
r_start = int(r - window_size/2)
r_end = int(r_start + window_size)
c_start = int(c - window_size/2)
c_end = int(c_start + window_size)
arr = rio_obj.read()
win_arr = arr[:,r_start:r_end, c_start:c_end]
# if it is all zero or all NaN and at least 95% data
test_arr = win_arr[0]
test_nan = np.isnan(np.mean(test_arr))
test_zero = np.mean(test_arr) == 0
# get some pixel metrics
num_nonzero = test_arr.size
num_pixels = test_arr[test_arr == 3].size
ratio = float(num_pixels) / float(num_nonzero)
# print(ratio)
# empirically choose no-data ratio of 10% of window
if (test_nan or test_zero or (ratio > 0.10)) :
# if either of the tests fail, we don't want that window
return True
else:
return False
except Exception as e:
#print(e)
return True
class gtDatasetSampler2(Dataset):
"""DG Dataset"""
def __init__(self, gtfile, coord_pair, window_size=64, transform=None):
"""
Args:
image_dir(string): the folder containing the DG images
transform (callable, optional): Optional transform to be applies
"""
self.image_file = gtfile
self.transform = transform
self.coords = coord_pair
self.window_size = window_size
def __getitem__(self, idx):
with rasterio.open(self.image_file, 'r') as src:
temp = src.read()
# get the window
r,c = self.coords[idx]
r_start = int(r - self.window_size/2)
r_end = int(r_start + self.window_size)
c_start = int(c - self.window_size/2)
c_end = int(c_start + self.window_size)
# extract the window
img_arr = temp[0,r_start:r_end, c_start:c_end]
img_arr = np.expand_dims(img_arr, axis=0)
# set no data to 0
img_arr[img_arr == 3] = 0
# convert to tensor
img_arr = torch.from_numpy(img_arr).float()
return img_arr
def __len__(self):
return len(self.coords)
class DigitalGlobeSampler(Dataset):
"""DG Dataset"""
def __init__(self, cat_img, coord_pair, window_size=64, transform=None, comb='bgr'):
"""
Args:
image_dir(string): the folder containing the DG images
transform (callable, optional): Optional transform to be applies
"""
self.image = cat_img
self.transform = transform
self.coords = coord_pair
self.window_size = window_size
self.bgrn = [1,2,4,6]
self.bgr = [1,2,4]
def __getitem__(self, idx):
# get the window
r,c = self.coords[idx]
r_start = int(r - self.window_size/2)
r_end = int(r_start + self.window_size)
c_start = int(c - self.window_size/2)
c_end = int(c_start + self.window_size)
# extract the window
img_arr = self.image[:, r_start:r_end, c_start:c_end].compute()
if self.transform:
img_arr = self.transform(img_arr)
return img_arr
def __len__(self):
return len(self.coords)
class DigitalGlobeSamplerTensor(Dataset):
"""DG Dataset"""
def __init__(self, cat_img, coord_pair, window_size=64, transform=None, comb='bgr'):
"""
Args:
image_dir(string): the folder containing the DG images
transform (callable, optional): Optional transform to be applies
"""
self.image = cat_img
self.transform = transform
self.coords = coord_pair
self.window_size = window_size
self.bgrn = [1,2,4,6]
self.bgr = [1,2,4]
self.comb = comb
def __getitem__(self, idx):
# get the window
r,c = self.coords[idx]
r_start = int(r - self.window_size/2)
r_end = int(r_start + self.window_size)
c_start = int(c - self.window_size/2)
c_end = int(c_start + self.window_size)
# extract the window
img_arr = self.image[:, r_start:r_end, c_start:c_end].compute()
if self.comb=='bgr' and self.transform and self.transform.transforms[0].mean.__len__()==3:
#img_arr = np.rollaxis(img_arr, 0,3)
#img_arr = img_arr[:,:,self.bgr]
img_arr = img_arr[self.bgr,:,:]
img_arr = self.transform(torch.from_numpy(img_arr))
return img_arr
elif self.comb=='bgrn' and self.transform and self.transform.transforms[0].mean.__len__()==4:
#img_arr = np.rollaxis(img_arr, 0,3)
#img_arr = img_arr[:,:,self.bgrn]
img_arr = img_arr[self.bgrn,:,:]
img_arr = self.transform(torch.from_numpy(img_arr))
return img_arr
else:
# print('here')
return self.transform(torch.from_numpy(img_arr))
def __len__(self):
return len(self.coords)