-
Notifications
You must be signed in to change notification settings - Fork 10
/
preprocessing.py
145 lines (106 loc) · 4.01 KB
/
preprocessing.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
import numpy as np
from pathlib import Path
import cv2
import tifffile
import yaml
from utils import paths
def load_cities(dataset: str) -> list:
dirs = paths.load_paths()
cities_file = Path(dirs.OSCD_ROOT) / 'images' / f'{dataset}.txt'
with open(cities_file, 'r') as f:
cities = f.read()[:-1].split(',')
return cities
CITIES = ['aguasclaras', 'bercy', 'bordeaux', 'nantes', 'paris', 'rennes', 'saclay_e', 'abudhabi', 'cupertino',
'pisa', 'beihai', 'hongkong', 'beirut', 'mumbai', 'brasilia', 'montpellier', 'norcia', 'rio', 'saclay_w',
'valencia', 'dubai', 'lasvegas', 'milano', 'chongqing']
ORBITS = {
'aguasclaras': [24],
'bercy': [59, 8, 110],
'bordeaux': [30, 8, 81],
'nantes': [30, 81],
'paris': [59, 8, 110],
'rennes': [30, 81],
'saclay_e': [59, 8],
'abudhabi': [130],
'cupertino': [35, 115, 42],
'pisa': [15, 168],
'beihai': [157],
'hongkong': [11, 113],
'beirut': [14, 87],
'mumbai': [34],
'brasilia': [24],
'montpellier': [59, 37],
'norcia': [117, 44, 22, 95],
'rio': [155],
'saclay_w': [59, 8, 110],
'valencia': [30, 103, 8, 110],
'dubai': [130, 166],
'lasvegas': [166, 173],
'milano': [66, 168],
'chongqing': [55, 164]
}
def get_band(file: Path) -> str:
return file.stem.split('_')[-1]
def combine_bands(folder: Path) -> np.ndarray:
bands = ['B01', 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B09', 'B10', 'B11', 'B12']
n_bands = len(bands)
# using blue band as reference (10 m) to create img
blue_file = folder / 'B02.tif'
blue = tifffile.imread(str(blue_file))
img = np.ndarray((*blue.shape, n_bands), dtype=np.float32)
for i, band in enumerate(bands):
band_file = folder / f'{band}.tif'
arr = tifffile.imread(str(band_file))
band_h, band_w = arr.shape
# up-sample 20 m bands
# arr = cv2.resize(arr, (w, h), interpolation=cv2.INTER_CUBIC)
# rescaling image to [0, 1]
arr = np.clip(arr / 10000, a_min=0, a_max=1)
img[:, :, i] = arr
return img
def process_city(city: str):
print(f'Preprocessing {city}')
dirs = paths.load_paths()
new_parent = Path(dirs.PREPROCESSED_ROOT) / city
new_parent.mkdir(exist_ok=True)
# image data
for t in [1, 2]:
# get data
from_folder = Path(dirs.OSCD_ROOT) / 'images' / city / f'imgs_{t}_rect'
img = combine_bands(from_folder)
# save data
to_folder = new_parent / 'sentinel2'
to_folder.mkdir(exist_ok=True)
save_file = to_folder / f'sentinel2_{city}_t{t}.npy'
np.save(save_file, img)
test_cities = load_cities('test')
dataset = 'test' if city in test_cities else 'train'
from_label_file = Path(dirs.OSCD_ROOT) / f'{dataset}_labels' / city / 'cm' / f'{city}-cm.tif'
label = tifffile.imread(str(from_label_file))
label = label - 1
to_label_file = new_parent / 'label' / f'urbanchange_{city}.npy'
to_label_file.parent.mkdir(exist_ok=True)
np.save(to_label_file, label)
def add_sentinel1(city: str, orbit: int):
dirs = paths.load_paths()
test_cities = load_cities('test')
dataset = 'test' if city in test_cities else 'train'
label_file = Path(dirs.OSCD_ROOT) / f'{dataset}_labels' / city / 'cm' / f'{city}-cm.tif'
label = tifffile.imread(str(label_file))
h, w = label.shape
for t in [1, 2]:
s1_file = Path(dirs.SENTINEL1_DATA) / f'sentinel1_{city}_{orbit}_t{t}.tif'
img = tifffile.imread(str(s1_file))
img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC)
img = img[:, :, None]
# save data
to_folder = Path(dirs.PREPROCESSED_ROOT) / city / 'sentinel1'
to_folder.mkdir(exist_ok=True)
save_file = to_folder / f'sentinel1_{city}_{orbit}_t{t}.npy'
np.save(save_file, img)
if __name__ == '__main__':
for city in CITIES:
process_city(city)
orbits = ORBITS[city]
for orbit in orbits:
add_sentinel1(city, orbit)