-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.py
287 lines (224 loc) · 9.54 KB
/
tile.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
import os
import rasterio
import numpy as np
import re
import time
import random
import zarr
import shutil
import matplotlib.pyplot as plt
import gc
CITY = 'aleppo'
DATA_DIR = "../data"
TILE_SIZE = (128,128)
PRE_IMAGE_INDEX=[0,1]
SUFFIX = "im"
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--city", help="City")
parser.add_argument("--data_dir", help="Data Dir")
args = parser.parse_args()
if args.city:
CITY = args.city
if args.data_dir:
DATA_DIR = args.data_dir
def search_data(pattern:str='.*', directory:str='../data') -> list:
'''Sorted list of files in a directory matching a regular expression'''
files = list()
for root, _, file_names in os.walk(directory):
for file_name in file_names:
files.append(os.path.join(root, file_name))
files = list(filter(re.compile(pattern).search, files))
files.sort()
# if len(files) == 1: files = files[0]
return files
def pattern(city:str='.*', type:str='.*', date:str='.*', ext:str='tif') -> str:
'''Regular expressions for search_data'''
return f'^.*{city}/.*/{type}_{date}\.{ext}$'
def read_raster(source:str, band:int=None, window=None, dtype:str='uint8', profile:bool=False) -> np.ndarray:
'''Reads a raster as a numpy array'''
raster = rasterio.open(source)
if band is not None:
image = raster.read(band, window=window)
image = np.expand_dims(image, 0)
else:
image = raster.read(window=window)
# print(image.shape)
# image = image.transpose([1, 2, 0]).astype(dtype)
image = image.transpose([1, 2, 0]).astype(dtype)
if profile:
return image, raster.profile
else:
return image
def tile_sequences(images:np.ndarray, tile_size:tuple=(128, 128)) -> np.ndarray:
'''Converts images to sequences of tiles'''
n_images, image_width, image_height, n_bands = images.shape
tile_width, tile_height = tile_size
assert image_width % tile_width == 0
assert image_height % tile_height == 0
n_tiles_width = (image_width // tile_width)
n_tiles_height = (image_height // tile_height)
sequence = images.reshape(n_images, n_tiles_width, tile_width, n_tiles_height, tile_height, n_bands)
sequence = np.moveaxis(sequence.swapaxes(2, 3), 0, 2)
sequence = sequence.reshape(-1, n_images, tile_width, tile_height, n_bands)
return sequence
def sample_split(images:np.ndarray, samples:dict) -> list:
'''Splits the data structure into multiple samples'''
print(np.unique(samples))
samples = [images[samples == value, ...] for value in np.unique(samples)]
# print(len(samples))
# print(samples)
return samples
def read_zarr(city, suffix, path="../data"):
path = f'{path}/{city}/others/{city}_{suffix}.zarr'
return zarr.open(path)
def save_zarr(data, city, suffix, path="../data"):
path = f'{path}/{city}/others/{city}_{suffix}.zarr'
if not os.path.exists(path):
zarr.save(path, data)
else:
za = zarr.open(path, mode='a')
za.append(data)
def delete_zarr_if_exists(city, suffix, path="../data"):
path = f'{path}/{city}/others/{city}_{suffix}.zarr'
if os.path.exists(path):
shutil.rmtree(path)
# images = search_data(pattern(city=CITY, type='image'), directory=DATA_DIR)
pre_images = search_data(pattern='^.*tif', directory=f'{DATA_DIR}/{CITY}/images/pre')
post_images = search_data(pattern='^.*tif', directory=f'{DATA_DIR}/{CITY}/images/post')
labels = search_data(pattern(city=CITY, type='label'), directory=DATA_DIR)
samples = read_raster(f'{DATA_DIR}/{CITY}/others/{CITY}_samples.tif')
print(pre_images)
image_dates = sorted([el.split("image_")[1].split('.tif')[0] for el in [*post_images]])
label_dates = sorted([el.split("label_")[1].split('.tif')[0] for el in labels])
# # print(image_dates)
# # print(label_dates)
remove = []
for la in label_dates:
if la.replace("-", "_") not in image_dates:
# print(la, "not in image_dates" )
remove.append(label_dates.index(la))
_ = []
_labels = []
for i, dt in enumerate(label_dates):
if i not in remove:
_.append(dt)
_labels.append(labels[i])
label_dates = sorted(_)
labels = sorted(_labels)
# print(len(post_images), len(labels))
# print(image_dates, label_dates)
suffixes = ["im_tr_pre", "im_va_pre", "im_te_pre", "im_tr_post", "im_va_post", "im_te_post", "la_tr", "la_va", "la_te"]
for s in suffixes:
delete_zarr_if_exists(CITY, s, DATA_DIR)
f = open(f"{DATA_DIR}/{CITY}/others/metadata.txt", "a")
print(f'{len(post_images)} post images; {len(pre_images)} pre images')
f.write("######## Tiling Step\n\n")
f.write(f'{len(post_images)} post images; {len(pre_images)} pre images\n')
# # empty = np.empty((0, TILE_SIZE[0]*TILE_SIZE[1]))
# # images_tr = images_va = images_te = empty
# # labels_tr = labels_va = labels_te = np.empty((0,))
for j, pre_image in enumerate(pre_images):
print(f"Reading pre image.. {pre_images[j]}")
pre_image = read_raster(pre_images[j])
print(f"Tiling pre image.. {pre_images[j]}")
pre_image = tile_sequences(np.array([pre_image]), TILE_SIZE)
for i in range(len(post_images)):
label = labels[i]
label = read_raster(label, 1)
label = np.squeeze(label.flatten())
unc = np.where(label == 99)
label = np.delete(label, unc, 0)
image = post_images[i]
print(f"Tiling post image.. {post_images[i]}")
image = read_raster(image)
a = image
image = tile_sequences(np.array([image]))
image = np.squeeze(image)
image = np.delete(image, unc, 0)
_pre_image = np.delete(pre_image, unc, 0)
samples_min_unc = np.delete(samples.flatten(), unc)
print("!!", _pre_image.shape)
print("!!", samples_min_unc.shape)
_, pre_image_tr, pre_image_va, pre_image_te = sample_split(_pre_image, samples_min_unc)
_, image_tr, image_va, image_te = sample_split(image, samples_min_unc) # for smaller samples there is no noanalysis class
_, label_tr, label_va, label_te = sample_split(label, samples_min_unc)
# pre_image_tr, pre_image_va, pre_image_te = sample_split(_pre_image, samples_min_unc)
# image_tr, image_va, image_te = sample_split(image, samples_min_unc) # for smaller samples there is no noanalysis class
# label_tr, label_va, label_te = sample_split(label, samples_min_unc)
# image_tr = image_tr.reshape(*image_tr.shape)
# print(pre_image_tr.shape)
# print(image_tr.shape)
pre_image_tr = np.squeeze(pre_image_tr)
save_zarr(pre_image_tr, CITY, 'im_tr_pre', path=DATA_DIR)
save_zarr(image_tr, CITY, 'im_tr_post', path=DATA_DIR)
save_zarr(label_tr, CITY, 'la_tr', path=DATA_DIR)
pre_image_va = np.squeeze(pre_image_va)
save_zarr(pre_image_va, CITY, 'im_va_pre', path=DATA_DIR)
save_zarr(image_va, CITY, 'im_va_post', path=DATA_DIR)
save_zarr(label_va, CITY, 'la_va', path=DATA_DIR)
pre_image_te = np.squeeze(pre_image_te)
save_zarr(pre_image_te, CITY, 'im_te_pre', path=DATA_DIR)
save_zarr(image_te, CITY, 'im_te_post', path=DATA_DIR)
save_zarr(label_te, CITY, 'la_te', path=DATA_DIR)
# save_zarr(image_tr, CITY, 'im_snn_tr_tt', path=DATA_DIR)
# save_zarr(pre_image_tr, CITY, 'im_snn_tr_t0', path=DATA_DIR)
print("Sanity Check 1: Training")
tr_pre = read_zarr(CITY, "im_tr_pre", DATA_DIR)
tr_post = read_zarr(CITY, "im_tr_post", DATA_DIR)
la_tr = read_zarr(CITY, "la_tr", DATA_DIR)
index = random.randint(0,tr_pre.shape[0] - 10)
# print("Pre", tr_pre.shape)
# print("Post",tr_post.shape)
# print(la_tr.shape)
fig, ax = plt.subplots(1,1,dpi=200)
ax.imshow(a)
plt.suptitle("Original Image")
plt.savefig(f"{DATA_DIR}/{CITY}/others/orig.png")
del a
fig, ax = plt.subplots(2,5,dpi=200, figsize=(25,10))
ax = ax.flatten()
for i, image in enumerate(tr_pre[index:index+5]):
ax[i].imshow(image)
for i, image in enumerate(tr_post[index:index+5]):
ax[i+5].imshow(image)
for i, label in enumerate(la_tr[index:index+5]):
ax[i].set_title(label==1)
plt.suptitle("Training set (sample images; top=pre, bottom=post)")
plt.savefig(f"{DATA_DIR}/{CITY}/others/tr_samples.png")
print("Sanity Check 2: Testing")
te_pre = read_zarr(CITY, "im_te_pre", DATA_DIR)
te_post = read_zarr(CITY, "im_te_post", DATA_DIR)
la_te = read_zarr(CITY, "la_te", DATA_DIR)
index = random.randint(0,te_pre.shape[0] - 10)
# print("Pre", te_pre.shape)
# print("Post",te_post.shape)
# print(la_te.shape)
fig, ax = plt.subplots(2,5,dpi=200, figsize=(25,10))
ax = ax.flatten()
for i, image in enumerate(te_pre[index:index+5]):
ax[i].imshow(image)
for i, image in enumerate(te_post[index:index+5]):
ax[i+5].imshow(image)
plt.suptitle("Test set (sample images; top=pre, bottom=post)")
plt.savefig(f"{DATA_DIR}/{CITY}/others/te_samples.png")
print("Sanity Check 3: Validation")
va_pre = read_zarr(CITY, "im_va_pre", DATA_DIR)
va_post = read_zarr(CITY, "im_va_post", DATA_DIR)
la_va = read_zarr(CITY, "la_va", DATA_DIR)
index = random.randint(0,va_pre.shape[0] - 10)
# print("Pre", va_pre.shape)
# print("Post",va_post.shape)
# print(la_va.shape)
fig, ax = plt.subplots(2,5,dpi=200, figsize=(25,10))
ax = ax.flatten()
for i, image in enumerate(va_pre[index:index+5]):
ax[i].imshow(image)
for i, image in enumerate(va_post[index:index+5]):
ax[i+5].imshow(image)
plt.suptitle("Validation set (sample images; top=pre, bottom=post)")
plt.savefig(f"{DATA_DIR}/{CITY}/others/va_samples.png")
f.write(f"Training set: {tr_pre.shape[0]} observations\n")
f.write(f"Validation set: {va_pre.shape[0]} observations\n")
f.write(f"Test set: {te_pre.shape[0]} observations\n\n")
f.close()