-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
197 lines (174 loc) · 4.83 KB
/
Copy patheval.py
File metadata and controls
197 lines (174 loc) · 4.83 KB
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
import os
import json
import torch
import numpy as np
import pandas as pd
import models as model
from runner import Runner
from modules.loggers import make_logger
from modules.train_funcs import net_eval
from modules.data_utils import toComplex
from modules.data_utils import convert_to_serializable
from modules.metrics import (
count_net_params,
plot_final_spectrums,
plot_spectrums,
calculate_mean_red,
calculate_metrics,
compute_power,
plot_total_perf
)
def run_evaluation(
net,
test_loader,
criterion,
device,
filter,
data_type,
data_name,
CScaler,
FS,
FC_TX,
PIM_SFT,
PIM_BW,
noise,
logs,
step_logger,
path_dir_save,
):
_, pred, gt = net_eval(logs, net, test_loader, criterion, device)
logs = calculate_metrics(
pred,
gt,
noise,
filter,
data_type,
data_name,
CScaler,
FS,
PIM_SFT,
PIM_BW,
logs,
)
mean_reduction = calculate_mean_red(
list(logs["Reduction_level"].values())
)
step_logger.success(
f"Mean Reduction_level EVAL: {mean_reduction}"
)
step_logger.success(
f"Reduction_level EVAL: {convert_to_serializable(logs['Reduction_level'])}"
)
pred = CScaler.rescale(pred, key="Y")
gt = CScaler.rescale(gt, key="Y")
plot_spectrums(
toComplex(pred),
toComplex(gt),
FS,
FC_TX,
PIM_SFT,
PIM_BW,
0,
logs["Reduction_level"],
data_type,
path_dir_save,
cut=False,
phase_name='EVAL',
)
plot_final_spectrums(
toComplex(pred),
toComplex(gt),
toComplex(noise),
FS,
FC_TX,
PIM_SFT,
PIM_BW,
0,
data_type,
path_dir_save,
phase_name='EVAL',
)
powers = dict()
for key, value in (("gt", gt), ("err", gt - pred), ("noise", noise)):
compl = toComplex(value)
powers[key] = [
compute_power(
compl[:, id],
FS, PIM_SFT, PIM_BW,
data_type, data_name
)
for id in range(compl.shape[1])
]
plot_total_perf(
powers,
path_dir_save
)
logs['MEAN_REDUCTION'] = mean_reduction
return logs
if __name__ == "__main__":
step_logger = make_logger()
exp = Runner(load_exp=True)
loaded_config = exp.load_experiment()
exp.set_device()
exp.args.path_dir_save = os.path.join(exp.args.path_dir_save,'evaluation')
os.makedirs(exp.args.path_dir_save,exist_ok=True)
# Build Dataloaders
(
(train_loader, val_loader, test_loader),
input_size,
n_channels,
noise,
filter,
CScaler,
specs,
) = exp.load_resources()
net = model.CoreModel(
n_channels=n_channels,
input_size=input_size,
out_window=exp.args.out_window,
hidden_size=exp.args.PIM_hidden_size,
backbone_type=exp.args.PIM_backbone,
batch_size=exp.args.batch_size,
out_filtration=exp.args.out_filtration,
filter_path=exp.args.filter_path,
aux_loss_present=exp.args.use_aux_loss_if_present,
)
weights = torch.load(
loaded_config['path_save_file_best'],
map_location='cpu'
)
net.load_state_dict(weights, strict=True)
net = net.to(exp.device)
logger = make_logger()
n_net_pim_params = count_net_params(net)
logger.info(f"::: Number of PIM Model Parameters: {n_net_pim_params}")
pim_model_id = exp.gen_model_id(n_net_pim_params)
PandasWriter = exp.build_logger(pim_model_id)
exp.build_logger(model_id=pim_model_id)
criterion = exp.build_criterion()
###########################################################################################################
# EVALUATION
###########################################################################################################
logs = {}
results = run_evaluation(
net=net,
test_loader=test_loader,
criterion=criterion,
device=exp.device,
filter=filter,
data_type=exp.args.data_type,
data_name=exp.args.dataset_name,
CScaler=CScaler,
FS=loaded_config['FS'],
FC_TX=loaded_config['FC_TX'],
PIM_SFT=loaded_config['PIM_SFT'],
PIM_BW=loaded_config['PIM_BW'],
noise=noise['test'],
logs=logs,
step_logger=step_logger,
path_dir_save =exp.args.path_dir_save
)
results = convert_to_serializable(results)
print(results)
with open(exp.args.path_dir_save+'/results.json', "w") as f:
json.dump(results, f, indent=4)