-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval.py
211 lines (178 loc) · 9.51 KB
/
eval.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
import torch
import argparse
from os import path as osp
import os
from tqdm import tqdm
import numpy as np
import learn2learn as l2l
from sklearn.model_selection import train_test_split
import skimage.io as io
import logging
from src.core.config import update_cfg, get_cfg_defaults
from src.models.UNet import UNet
from src.models.metaHDR import evaluate_maml, evaluate_single_maml
from src.dataset.dataloader import DataGenerator
from src.core.loss import get_loss_func
from src.core.utils import create_logger
def main(args):
print("--- Evaluating on meta-test set ---")
device = 'cuda' if torch.cuda.is_available() else 'cpu'
source_directory = args.model_dir
use_last_flag = args.use_last
# Make evaluation folder for test images
evaluation_figure_output_dir = osp.join(source_directory, 'evaluation_output')
os.makedirs(evaluation_figure_output_dir, exist_ok=True)
# Make the subfolders
single_dir = os.path.join(evaluation_figure_output_dir, "single")
adapt_debevec_dir = os.path.join(evaluation_figure_output_dir, "adapt_debevec")
adapt_hdrcnn_dir = os.path.join(evaluation_figure_output_dir, "adapt_hdrcnn")
os.makedirs(single_dir, exist_ok=True)
os.makedirs(adapt_debevec_dir, exist_ok=True)
os.makedirs(adapt_hdrcnn_dir, exist_ok=True)
os.makedirs(evaluation_figure_output_dir, exist_ok=True)
# Grab config
if args.cfg is not None:
cfg = update_cfg(args.cfg)
else:
cfg = get_cfg_defaults()
# Make sure loss_func from config is valid, then get it
assert cfg.EVAL.LOSS_FUNC in ['ExpandNetLoss', 'HaarLoss', 'LPIPSLoss', 'LPIPSLoss_L1', 'SSIMLoss'], f"[CONFIG] evaluation loss function '{cfg.EVAL.LOSS_FUNC}' not valid"
loss_func = get_loss_func(cfg.EVAL.LOSS_FUNC)
# Grad test data -- all of it
dg = DataGenerator(num_exposures=cfg.EVAL.NUM_EXPOSURES)
all_test_data = dg.meta_test_data
# Create logger
logger = create_logger(evaluation_figure_output_dir, phase='eval', level=logging.CRITICAL)
logger.critical(f'GPU name -> {torch.cuda.get_device_name()}')
logger.critical(f'GPU feat -> {torch.cuda.get_device_properties("cuda")}')
# Grab model checkpoint
if use_last_flag:
model_path = osp.join(source_directory, 'model_last.pth.tar')
else:
model_path = osp.join(source_directory, 'model_best.pth.tar')
checkpoint = torch.load(model_path)
best_performance = checkpoint['performance']
best_epoch = checkpoint['epoch']
logger.critical(f"During training: Best Epoch: {best_epoch}, Best SSIM: {best_performance}")
# Define blank model to load weights into
model = UNet(in_size=3, out_size=3, num_filters=8).double().to(device)
meta_model = l2l.algorithms.MAML(model, lr=cfg.EVAL.TASK_LR)
logger.critical(f"Loading pre-trained model from --> {model_path}")
meta_model.load_state_dict(checkpoint['unet_state_dict'])
logger.critical(f"Successfully loaded pre-trained model from --> {model_path}")
# Perform single-shot evaluation
logger.critical("Performing Single-Shot Evaluation")
eval_single_ssim = 0.0
eval_single_psnr = 0.0
idx = 0
for i in tqdm(range(all_test_data.shape[0])):
for j in range(1, all_test_data.shape[1]):
input_test_image = all_test_data[np.newaxis, i, j]
input_test_label = all_test_data[np.newaxis, i, 0]
_, test_ssim, test_psnr = evaluate_single_maml(meta_model, loss_func, input_test_image, input_test_label, idx, device=device, visualize_flag=True, visualize_dir=single_dir)
eval_single_ssim+=test_ssim
eval_single_psnr+=test_psnr
idx += 1
eval_single_ssim /= (all_test_data.shape[0]*(all_test_data.shape[1]-1))
eval_single_psnr /= (all_test_data.shape[0]*(all_test_data.shape[1]-1))
# Perform adaptive evaluation
logger.critical("Performing Adaptive Evaluation using Debevec labels")
eval_adaptive_ssim = 0.0
eval_adaptive_psnr = 0.0
idx = 0
for curr_idx in tqdm(range(all_test_data.shape[0])):
cur_batch = all_test_data[np.newaxis, curr_idx]
tr_images, ts_images = [], []
tr_labels, ts_labels = [], []
for image_set in cur_batch:
# Train and Test for each set of exposures
tr, ts = train_test_split(np.arange(1, cfg.EVAL.NUM_EXPOSURES+1), test_size=1)
cur_tr_images, cur_tr_labels = [], []
for i in tr:
cur_tr_images.append(image_set[i, ...])
cur_tr_labels.append(image_set[0, ...])
tr_images.append(np.stack(cur_tr_images))
tr_labels.append(np.stack(cur_tr_labels))
cur_ts_images, cur_ts_labels = [], []
for i in ts:
cur_ts_images.append(image_set[i, ...])
cur_ts_labels.append(image_set[0, ...])
ts_images.append(np.stack(cur_ts_images))
ts_labels.append(np.stack(cur_ts_labels))
tr_images = np.stack(tr_images)
tr_labels = np.stack(tr_labels)
ts_images = np.stack(ts_images)
ts_labels = np.stack(ts_labels)
eval_train = np.stack([tr_images, tr_labels])
eval_test = np.stack([ts_images, ts_labels])
_, test_ssim, test_psnr = evaluate_maml(meta_model, loss_func, eval_train, eval_test, idx, cfg.EVAL.NUM_TASK_TR_ITER, device=device, model_type=cfg.TRAIN.MODEL, visualize_flag=True, visualize_dir=adapt_debevec_dir)
idx += 1
eval_adaptive_ssim += test_ssim
eval_adaptive_psnr += test_psnr
eval_adaptive_ssim /= all_test_data.shape[0]
eval_adaptive_psnr /= all_test_data.shape[0]
# Perform adaptive evaluation
logger.critical("Performing Adaptive Evaluation using HDRCNN labels")
eval_adaptive_hdrcnn_ssim = 0.0
eval_adaptive_hdrcnn_psnr = 0.0
# cur_indices = [1, 4, 11, 20, 23, 27, 36, 45, 48, \
# 53, 56, 65, 70, 73, 82, 84, 126, 137, \
# 138, 150, 162, 171, 173, 175, 194, 195, 209, \
# 221, 224, 230, 275, 288, 294, 306, 342, 360, \
# 375, 394, 397, 405, 430, 438, 439, 448, 450]
from pathlib import Path
load_dir = Path(__file__).parent/'data/LDR-HDR-pair_Dataset/TestOutputs' # /home/users/edwinpan/MetaHDR/data/LDR-HDR-pair_Dataset/TestOutputs
idx = 0
for curr_idx in tqdm(range(all_test_data.shape[0])):
cur_batch = all_test_data[np.newaxis, curr_idx]
tr_images, ts_images = [], []
tr_labels, ts_labels = [], []
for image_set in cur_batch:
# Train and Test for each set of exposures
tr, ts = train_test_split(np.arange(1, cfg.EVAL.NUM_EXPOSURES+1), test_size=1)
cur_tr_images, cur_tr_labels = [], []
for i in tr:
image_idx = curr_idx + 1
cur_tr_images.append(image_set[i, ...])
if i == 1: # exposure p2
image_idx += all_test_data.shape[0]
elif i == 2: #exposure 0
pass
elif i == 3: # expusre n2
image_idx += all_test_data.shape[0] * 2
cur_label = io.imread(load_dir/f'{image_idx:06d}_out.png').astype(np.float64) / 255
cur_tr_labels.append(cur_label)
tr_images.append(np.stack(cur_tr_images))
tr_labels.append(np.stack(cur_tr_labels))
cur_ts_images, cur_ts_labels = [], []
for i in ts:
cur_ts_images.append(image_set[i, ...])
cur_ts_labels.append(image_set[0, ...])
ts_images.append(np.stack(cur_ts_images))
ts_labels.append(np.stack(cur_ts_labels))
tr_images = np.stack(tr_images)
tr_labels = np.stack(tr_labels)
ts_images = np.stack(ts_images)
ts_labels = np.stack(ts_labels)
eval_train = np.stack([tr_images, tr_labels])
eval_test = np.stack([ts_images, ts_labels])
_, test_ssim, test_psnr = evaluate_maml(meta_model, loss_func, eval_train, eval_test, idx, cfg.EVAL.NUM_TASK_TR_ITER, device=device, model_type=cfg.TRAIN.MODEL, visualize_flag=True, visualize_dir=adapt_hdrcnn_dir)
idx += 1
eval_adaptive_hdrcnn_ssim += test_ssim
eval_adaptive_hdrcnn_psnr += test_psnr
eval_adaptive_hdrcnn_ssim /= all_test_data.shape[0]
eval_adaptive_hdrcnn_psnr /= all_test_data.shape[0]
logger.critical("[Evaluation Results] Average Single-Shot Evaluation SSIM : {:.3f}".format(eval_single_ssim))
logger.critical("[Evaluation Results] Average Single-Shot Evaluation PSNR : {:.3f}".format(eval_single_psnr))
logger.critical("[Evaluation Results] Average Debevec Adapted Evaluation SSIM : {:.3f}".format(eval_adaptive_ssim))
logger.critical("[Evaluation Results] Average Debevec Adapted Evaluation PSNR : {:.3f}".format(eval_adaptive_psnr))
logger.critical("[Evaluation Results] Average HDRCNN Adapted Evaluation SSIM : {:.3f}".format(eval_adaptive_hdrcnn_ssim))
logger.critical("[Evaluation Results] Average HDRCNN Adapted Evaluation PSNR : {:.3f}".format(eval_adaptive_hdrcnn_psnr))
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', type=str, help='Path to directory with outputs from MetaHDR training cycle.')
parser.add_argument('--cfg', type=str, help='cfg file path')
parser.add_argument('--use_last', dest='use_last', action='store_true', help='Use to evaluate the last model in training iters. Default is to evaluate the best model from training.')
args = parser.parse_args()
main(args)