-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_dwt.py
431 lines (343 loc) · 13.1 KB
/
run_dwt.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
from dwt.dwt import fuse_3dDWT, fuse_2dDWT
from dwt.average import fuse_average
import argparse
from os.path import join, isdir, isfile
from os import mkdir
from tqdm import tqdm
import glob
from typing import Dict, List, Callable
from transforms.get import get_transform
from transforms.resolution import Resolution
from transforms.bands import Bands
from transforms.compose import Compose
from metrics.ssim import metric_ssim
from metrics.sam import metric_sam
from metrics.psnr import metric_psnr
import numpy as np
from utils.image_id import image_id, mask_id
from utils.read_yaml import read_yaml
from datetime import datetime
def run_dwt(
msi_in_files: List[str],
hsi_in_files: List[str],
hsi_out_files: List[str],
mask_files: List[str] | None,
method: str,
wavelet: List[str] | str,
level: int,
metrics: List[str],
use_mask: bool | None,
dir: str,
transforms: Callable | None,
) -> Dict | None:
dir_str = ""
if method == "3d-dwt":
if isinstance(wavelet, str):
dir_str = wavelet
else:
for wav in wavelet:
dir_str += wav
dir_str += "-"
dir_str = dir_str[:-1]
elif method == "average":
dir_str = "average"
elif method == "2d-dwt":
dir_str = "2d"
if isinstance(wavelet, str):
dir_str += wavelet
else:
for wav in wavelet:
dir_str += wav
dir_str += "-"
dir_str = dir_str[:-1]
elif method == "baseline-msi":
dir_str = "baseline-msi"
elif method == "baseline-hsi":
dir_str = "baseline-hsi"
dir = dir + dir_str
if use_mask:
dir += "-mask"
if isdir(dir):
n_files = len(glob.glob(join(dir, "*.npy")))
if n_files == len(msi_in_files):
print("Results for this config have already been calculated.")
return None
print(f"{n_files} have already been calculated, resuming")
else:
n_files = 0
mkdir(dir)
with open(join(dir, "method.txt"), "w") as f:
f.write(
f"""Reading files from: {msi_in_files[0].split("/")[-3]}
{method} with wavelet(s): {wavelet}, level: {level}
metric(s): {metrics} stored in {dir}
"""
)
print(
f"""Reading files from: {msi_in_files[0].split("/")[-3]}
{method} with wavelet(s): {wavelet}, level: {level}
metric(s): {metrics} stored in {dir}
"""
)
for i in tqdm(range(n_files, len(msi_in_files))):
msi_in = np.load(msi_in_files[i])
hsi_in = np.load(hsi_in_files[i])
expected = np.load(hsi_out_files[i])
if transforms is not None:
msi_in, hsi_in, expected = transforms(msi_in, hsi_in, expected)
if method == "3d-dwt":
result = fuse_3dDWT(msi_in, hsi_in, wavelet, level, None)
elif method == "2d-dwt":
result = fuse_2dDWT(msi_in, hsi_in, wavelet, level, None)
elif method == "average":
result = fuse_average(msi_in, hsi_in, None)
elif method == "baseline-msi":
result = msi_in
elif method == "baseline-msi":
result = hsi_in
else:
print(f"[ERROR]: the method {method} not implemented")
exit(1)
id = image_id(msi_in_files[i])
results = {}
if use_mask:
if mask_files is not None:
mask = np.load(mask_files[i])
else:
print(
f"[ERROR]: mask flag is set to True but no mask files were provided"
)
exit(1)
if mask_id(mask_files[i]) != id:
print(f"[ERROR]: Mask id ({mask_id(mask_files[i])}) and image id ({id}) must be the same.")
exit(1)
mask = np.expand_dims(mask, np.argmin(result.shape))
result = result * mask
expected = expected * mask
if "ssim" in metrics:
results["ssim"] = metric_ssim(result, expected)
if "sam" in metrics:
results["sam"] = metric_sam(result, expected)
if "psnr" in metrics:
results["psnr"] = metric_psnr(expected, result)
save_image_result(id, results, dir)
def save_image_result(image_id: str, results: Dict, dir: str):
image_path = join(dir, image_id)
np.save(image_path, np.array(results))
def access_metrics(r):
# This is necessary to access a np array of type object
return r["ssim"], r["sam"], r["psnr"]
def calculate_mean(dir: str, metrics: List[str]) -> Dict:
files = sorted(glob.glob(dir + "*.npy"))
results = {}
for metric in metrics:
results[metric] = 0
for file in files:
ssim, sam, psnr = np.vectorize(access_metrics)(np.load(file, allow_pickle=True))
r = {"ssim": ssim, "sam": sam, "psnr": psnr}
for metric in metrics:
results[metric] += r[metric]
for metric in metrics:
results[metric] /= len(files)
return results
def calculate_deviation(dir: str, metrics: List[str], results: Dict) -> Dict:
files = sorted(glob.glob(dir + "*.npy"))
deviation = None
for file in files:
ssim, sam, psnr = np.vectorize(access_metrics)(np.load(file, allow_pickle=True))
r = {"ssim": ssim, "sam": sam, "psnr": psnr}
if deviation is None:
deviation = {}
for metric in metrics:
deviation[metric] = np.array((r[metric].shape))
deviation[metric] = (r[metric] - results[metric]) ** 2
else:
for metric in metrics:
deviation[metric] += (r[metric] - results[metric]) ** 2
for metric in metrics:
deviation[metric] /= len(files)
deviation[metric] = np.sqrt(deviation[metric])
return deviation
def save_results(results: Dict, dir: str):
date = datetime.today().strftime("%Y-%m-%d-%s")
with open(join(dir, "results" + date + ".txt"), "w") as f:
f.write(
f"""Method: {results['method']}
Wavelet: {results['wavelet']}
Level: {results['level']}
-----------------------\n"""
)
if "ssim" in results.keys():
metrics = results["ssim"]
f.write("SSIM\n")
f.write(f"Average: {metrics[0]}\n")
for i in range(1, len(metrics)):
f.write(f"Band {i-1}: {metrics[i]}\n")
f.write("-----------------------\n")
if "sam" in results.keys():
f.write("SAM\n")
f.write(f"{results['sam']}\n")
f.write("-----------------------\n")
if "psnr" in results.keys():
f.write("PSNR\n")
f.write(f"{results['psnr']}\n")
f.write("-----------------------")
def run_dwt_suite(dir: str):
config_files = sorted(glob.glob(join(dir, "*.yaml")))
for file in config_files:
config = read_yaml(file, False)
if config.get("run") is None or config["run"] != "true":
print(f"{file} is not set to run. Moving on to next one")
continue
print(f"Running {file}")
msi_in_files = sorted(glob.glob(join(config["msi_in_files"], "*.npy")))
hsi_in_files = sorted(glob.glob(join(config["hsi_in_files"], "*.npy")))
hsi_out_files = sorted(glob.glob(join(config["hsi_out_files"], "*.npy")))
mask_files = None
if config.get("mask"):
mask_files = sorted(glob.glob(join(config["mask_files"], "*.npy")))
run_dwt(
msi_in_files,
hsi_in_files,
hsi_out_files,
mask_files,
config["method"],
config["wavelet"].split(","),
config["level"],
config["metrics"].split(","),
config.get("mask"),
config["dir"],
get_transform(config["transforms"]),
)
print("Results calculated:")
def save_results_to_file(mean: Dict, deviation: Dict, dir: str):
with open(join(dir, "results.txt"), "w") as f:
for metric in mean.keys():
f.write(f"Metric: {metric}\n")
if mean[metric].shape:
for a, b in zip(mean[metric], deviation[metric]):
f.write(f"{a} +- {b}\n")
else:
f.write(f"{mean[metric]} +- {deviation[metric]}\n")
f.write("\n")
def aggregate_results(dir: str):
folders = sorted(glob.glob(join(dir, "*")))
results = {}
for folder in folders:
wav = folder.split("/")[-1]
filename = join(folder, "results.txt")
if isfile(filename):
with open(filename, "r") as f:
file = f.readlines()
results[wav] = {"ssim": file[1], "sam": file[65], "psnr": file[68]}
with open(join(dir, "results.txt"), "w") as f:
for key, value in results.items():
f.write(f"{key}\n")
f.write(f"{value}\n")
print(f"Saving results in {join(dir, 'results.txt')}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="fusion-runner",
description="Runs the given fusion method and calculates the given metrics",
)
parser.add_argument("-o", "--source", type=str, help="Source directory")
parser.add_argument("-m", "--method", type=str, help="Fusion method")
parser.add_argument("-w", "--wavelet", type=str, help="Wavelet")
parser.add_argument("-l", "--level", type=int, help="dwt level")
parser.add_argument("-d", "--dir", type=str, help="Where results are stored")
parser.add_argument("-e", "--metrics", type=str, help="Which metrics to compute")
parser.add_argument("-c", "--config", type=str, help="Path to config")
parser.add_argument("-s", "--suite", type=str, help="Path to configs")
parser.add_argument("-r", "--results", type=str, help="Calculate results")
parser.add_argument("-a", "--aggregate", type=str, help="Aggregate results")
parser.add_argument(
"-f", "--mask", type=bool, help="Whether to use metrics on mask or whole image"
)
args = parser.parse_args()
if args.aggregate is not None:
dir = args.aggregate
aggregate_results(dir)
exit(0)
if args.results is not None:
mean = calculate_mean(args.results, args.metrics.split(","))
deviation = calculate_deviation(args.results, args.metrics.split(","), mean)
mean_path = join(args.results, "mean")
deviation_path = join(args.results, "deviation")
save_results_to_file(mean, deviation, args.results)
print(f"results saved in {args.results}")
exit(0)
if args.suite is not None:
run_dwt_suite(args.suite)
exit(0)
if args.config is not None:
# Read from config file
config = read_yaml(args.config, False)
msi_in_files = sorted(glob.glob(join(config["msi_in_files"], "*.npy")))
hsi_in_files = sorted(glob.glob(join(config["hsi_in_files"], "*.npy")))
hsi_out_files = sorted(glob.glob(join(config["hsi_out_files"], "*.npy")))
mask_files = None
if config.get("mask"):
mask_files = sorted(glob.glob(join(config["mask_files"], "*.npy")))
results = run_dwt(
msi_in_files,
hsi_in_files,
hsi_out_files,
mask_files,
config["method"],
config["wavelet"].split(","),
config["level"],
config["metrics"].split(","),
config["mask"],
config["dir"],
get_transform(config["transforms"]),
)
else:
if not isdir(args.source):
print(f"ERROR: Directory {args.source} does not exist.")
exit(1)
msi_in_files = sorted(glob.glob(join(args.source, "msi_in/*.npy")))
hsi_in_files = sorted(glob.glob(join(args.source, "hsi_in/*.npy")))
hsi_out_files = sorted(glob.glob(join(args.source, "hsi_out/*.npy")))
mask_files = None
mask = args.mask
if mask:
mask_files = sorted(glob.glob(join(args.source, "masks/*.npy")))
method = args.method
if method != "3d-dwt":
print(f"ERROR: {method} not implemented")
exit(1)
if args.wavelet is not None:
wavelet = args.wavelet.split(",")
else:
print("ERROR: Must provide wavelet")
exit(1)
level = args.level
dir = args.dir
if args.metrics is not None:
metrics = args.metrics.split(",")
else:
print("ERROR: Must provide metrics")
exit(1)
transforms = Compose([Resolution(1024, 1024), Bands(61, 61, None)])
results = run_dwt(
msi_in_files,
hsi_in_files,
hsi_out_files,
mask_files,
method,
wavelet,
level,
metrics,
mask,
dir,
transforms,
)
if results is not None:
print("Results calculated:")
if "ssim" in results.keys():
print(f"SSIM: {results['ssim'][0]}")
if "sam" in results.keys():
print(f"sam: {results['sam']}")
if "psnr" in results.keys():
print(f"psnr: {results['psnr']}")
print("------------------")