This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prep.py
91 lines (72 loc) · 3.51 KB
/
prep.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
import logging
from ops.ops import load_opt_image, load_SAR_image, load_json, filter_outliers, load_label_image, load_SAR_DN_image
import fiona
import os
import numpy as np
from skimage.util import view_as_windows
import logging
import tqdm
def prep_tile(tile_id):
conf = load_json(os.path.join('conf', 'conf.json'))
patch_size = conf['patch_size']
min_perc = conf['min_perc']
n_opt_layers = conf['n_opt_layers']
n_sar_layers = conf['n_sar_layers']
train_step = int((1-conf['patch_overlap'])*patch_size)
opt_path = os.path.join('img', 'GEE_imgs_opt')
sar_path = os.path.join('img', 'GEE_imgs_sar')
label_path = os.path.join('img', 'labels')
cmap_scl_path = os.path.join('img', 'GEE_scl')
patches_path = os.path.join('img', 'patches')
logging.basicConfig(
filename='02-prep.txt',
level=logging.INFO,
filemode='a',
format='%(asctime)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S'
)
logging.info(f'Preparing {tile_id} tile')
opt = np.concatenate([
filter_outliers(load_opt_image(os.path.join(opt_path, f'{tile_id}_opt_2019.tif'))),
filter_outliers(load_opt_image(os.path.join(opt_path, f'{tile_id}_opt_2020.tif')))
], axis=2)
sar = np.concatenate([
filter_outliers(load_SAR_image(os.path.join(sar_path, f'{tile_id}_sar_2019.tif'))),
filter_outliers(load_SAR_image(os.path.join(sar_path, f'{tile_id}_sar_2020.tif'))),
], axis=2)
cmap_scl_19 = load_opt_image(os.path.join(cmap_scl_path, f'{tile_id}_cloud_scl_2019.tif'))
cmap_scl_20 = load_opt_image(os.path.join(cmap_scl_path, f'{tile_id}_cloud_scl_2020.tif'))
cmap = np.concatenate([
np.expand_dims(cmap_scl_19[:,:,1], axis=-1),
np.expand_dims(cmap_scl_20[:,:,1], axis=-1)
], axis=2)
scl = np.concatenate([
np.expand_dims(cmap_scl_19[:,:,0], axis=-1),
np.expand_dims(cmap_scl_20[:,:,0], axis=-1)
], axis=2)
del cmap_scl_19, cmap_scl_20
label = np.expand_dims(load_label_image(os.path.join(label_path, f'label_{tile_id}.tif')), axis=-1)
shape = label.shape[:2]
idx_matrix = np.arange(shape[0]*shape[1]).reshape(shape)
label_patches = view_as_windows(label, (patch_size, patch_size, 1), train_step).reshape((-1, patch_size, patch_size, 1))
idx_patches = view_as_windows(idx_matrix, (patch_size, patch_size), train_step).reshape((-1, patch_size, patch_size))
keep_patches = np.mean((label_patches == 1), axis=(1,2)).squeeze() >= min_perc
idx_patches = idx_patches[keep_patches]
np.savez(os.path.join(patches_path, f'{tile_id}.npz'),
opt = opt.reshape((-1,n_opt_layers)),
sar = sar.reshape((-1, n_sar_layers)),
cmap = cmap.reshape((-1,2)),
scl = scl.reshape((-1,2)),
label = label.reshape((-1,1)),
patches_idx = idx_patches,
shape = shape
)
#np.save(os.path.join(patches_path, f'{tile_id}_opt.npy'), opt)
#np.save(os.path.join(patches_path, f'{tile_id}_sar.npy'), sar)
#np.save(os.path.join(patches_path, f'{tile_id}_cmap.npy'), cmap)
#np.save(os.path.join(patches_path, f'{tile_id}_scl.npy'), scl)
#np.save(os.path.join(patches_path, f'{tile_id}_label.npy'), label)
#np.save(os.path.join(patches_path, f'{tile_id}_patches_idx.npy'), idx_patches)
#np.save(os.path.join(patches_path, f'{tile_id}_shape.npy'), shape)
logging.info(f'Tile {tile_id} produced {idx_patches.shape[0]} patches')
return opt.mean(), opt.std(), sar.mean(), sar.std(), cmap.mean(), cmap.std()