Repository navigation
Expand file tree
/
Copy pathtest_sbi.py
More file actions
202 lines (175 loc) · 4.77 KB
/
Copy pathtest_sbi.py
File metadata and controls
202 lines (175 loc) · 4.77 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
198
199
200
201
202
import gc
import os
from time import time
import numpy as np
import pandas as pd
import torch
from method.aoc_rpca import AOC_RPCA
from utils.data_util import bounded_shape, resize_rpca_matrix
from utils.method_util import background_metrics_matrix, get_recovery_metrics_L
from utils.sbi import SBI
from utils.seed_util import set_seed
from utils.train_util import train
start = time()
global_seed = 1
set_seed(seed=global_seed)
cuda = True
rgb = True
n = 100
n_comp = 1
layer = 15
epoch = 1000
epoch_print = 500
epoch_resample = 40
lr = 5e-2
max_side = 160
sbi_root = os.path.join(os.getcwd(), "data")
data_list = ["IBMtest2"]
# data_list = ["CAVIAR1", "HumanBody2", "IBMtest2"]
seed_dict = {
"CAVIAR1": 1,
"HumanBody2": 1,
"IBMtest2": 1,
}
param_dict = {
"CAVIAR1": {
"ConfigID": "bl_0p1_bh_0p5_sim_0p2",
"beta_l": 0.1,
"beta_h": 0.5,
"sim_thresh": 0.2,
"layer": 15,
},
"HumanBody2": {
"ConfigID": "bl_0p1_bh_0p5_sim_0p3",
"beta_l": 0.05,
"beta_h": 0.5,
"sim_thresh": 0.1,
"layer": 30,
},
"IBMtest2": {
"ConfigID": "bl_0p1_bh_0p5_sim_0p3",
"beta_l": 0.1,
"beta_h": 0.5,
"sim_thresh": 0.3,
"layer": 30,
},
}
method_name = "AOC_RPCA"
basic_metrics_column = ["Basic-MSE", "Basic-NRMSE", "Basic-PSNR", "Basic-SSIM"]
sbi_metrics_column = ["AGE", "pEPs", "pCEPs", "MS-SSIM", "PSNR"]
if rgb:
n_comp *= 3
device = torch.device("cuda") if cuda and torch.cuda.is_available() else torch.device("cpu")
sbi = SBI(sbi_root)
all_summary_rows = []
for data_i in data_list:
print("-" * 80)
print(f"Dataset: {data_i}")
seed_i = seed_dict.get(data_i, global_seed)
params = param_dict[data_i]
layer_i = int(params.get("layer", layer))
data, L_true, shape = sbi.load(
mode="random",
name=data_i,
rgb=rgb,
seed=seed_i,
n=n,
shuffle=False,
normalize=True,
)
old_shape = shape
new_shape = bounded_shape(shape, max_side)
if new_shape != shape:
data = resize_rpca_matrix(data, shape, new_shape, rgb)
L_true = resize_rpca_matrix(L_true, shape, new_shape, rgb)
shape = new_shape
print(f"Downsampled from {old_shape} to {shape}")
frame_num = data.shape[1] // 3 if rgb else data.shape[1]
print(f"Data shape: {shape}, matrix shape: {data.shape}, frames: {frame_num}")
print(f"Config: {params}")
set_seed(seed=seed_i)
data_tensor = torch.from_numpy(data).to(device=device, dtype=torch.float32)
model = AOC_RPCA(
layer_i,
n_comp,
data.shape[1],
device=device,
ths=np.max(np.abs(data)),
c=4,
self_att=True,
restrict_channel=3 if rgb else 1,
sim_thresh=params["sim_thresh"],
beta_l=params["beta_l"],
beta_h=params["beta_h"],
)
model.to(device)
model.print_log()
train_start = time()
train(
model=model,
layer=layer_i,
data_tensor=data_tensor,
epoch=epoch,
epoch_resample=epoch_resample,
epoch_print=epoch_print,
lr=lr,
)
model.eval()
with torch.no_grad():
L_list, S_list = model(data_tensor, model_eval=True)
time_cost = time() - train_start
L_pred = L_list[-1].cpu().detach().numpy()
L_true_norm = np.linalg.norm(L_true, "fro")
l_loss = np.linalg.norm(L_pred - L_true, ord="fro") ** 2 / L_true_norm ** 2
basic_metric = get_recovery_metrics_L(L_true, L_pred, shape, rgb=rgb)
sbi_metric = background_metrics_matrix(L_true, L_pred, shape, rgb=rgb)
print("SBI-style metrics:")
print(sbi_metric.to_string(index=False))
all_summary_rows.append(
[
data_i,
method_name,
params["ConfigID"],
params["beta_l"],
params["beta_h"],
params["sim_thresh"],
layer_i,
seed_i,
frame_num,
shape[0],
shape[1],
time_cost,
l_loss,
*basic_metric,
*sbi_metric.iloc[0].to_list(),
]
)
del model, data_tensor, L_list, S_list, L_pred
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
summary_df = pd.DataFrame(
all_summary_rows,
columns=[
"Dataset",
"Method",
"ConfigID",
"beta_l",
"beta_h",
"sim_thresh",
"Layer",
"Seed",
"Frames",
"Height",
"Width",
"Time",
"Loss",
*basic_metrics_column,
*sbi_metrics_column,
],
)
print("-" * 80)
print("SBI AOC Summary")
show_df = summary_df[["Dataset", "AGE", "pEPs", "pCEPs", "MS-SSIM", "PSNR"]]
print(show_df.to_string(index=False))
print(f"Total time cost: {time() - start:.3f}s")