-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
282 lines (211 loc) · 10.1 KB
/
utils.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
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import pickle5 as pickle
import torch
import torch.nn as nn
from tqdm import tqdm
from loss import *
##########################################################
# Functions about Loss #
##########################################################
def build_loss_func(loss_dict, device):
loss_compute_dict = {}
for key, val in loss_dict.items():
key = key.lower()
func = None
# Task : Regression
if key == 'huber':
func = HuberLoss(device=device)
if key == 'mse':
func = nn.MSELoss()
if key == 'l1':
func = nn.L1Loss()
if key == 'probbasedmse':
func = ProbBasedMSE(device=device)
# Task : Classification
if key == 'crossentropy':
func = nn.CrossEntropyLoss()
if func == None:
raise NotImplementedError(f"{key} is not implemented yet.")
weight = val
loss_compute_dict[key] = {'func': func.to(device), 'weight': weight}
return loss_compute_dict
def compute_loss(loss_func, pred, gt_reg, gt_cls):
total_loss = 0
assert pred.get_device() == gt_reg.get_device(), \
"Prediction & GT tensor must be in same device"
if gt_cls != None:
assert pred.get_device() == gt_cls.get_device(), \
"Prediction & GT tensor must be in same device"
for loss_name, loss_dict in loss_func.items():
if loss_name in ['crossentropy']:
loss = loss_dict['func'](pred, gt_cls)
else:
loss = loss_dict['func'](pred, gt_reg)
loss *= loss_dict['weight']
total_loss += loss
return total_loss
##########################################################
# Functions about Optim #
##########################################################
def build_optim(cfg, model):
optim_name = cfg['train']['optim']
lr = cfg['train']['lr']
optim_name = optim_name.lower()
optim = None
if optim_name == 'sgd':
optim = torch.optim.SGD(model.parameters(), lr=lr)
if optim_name == 'adam':
optim = torch.optim.Adam(model.parameters(), lr=lr)
if optim_name == 'adamw':
optim = torch.optim.AdamW(model.parameters(), lr=lr)
# Add optimizer if you want
if optim != None:
return optim
else:
raise NotImplementedError(f"{optim_name} is not implemented yet.")
##########################################################
# Functions about Scheduler #
##########################################################
def build_scheduler(cfg, optimizer):
scheduler_dict = cfg['train']['scheduler']
sch_name = list(scheduler_dict.keys())[0]
sch_settings = scheduler_dict[sch_name]
sch_name = sch_name.lower()
if sch_name == 'multisteplr':
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=sch_settings['milestones'], gamma=sch_settings['gamma']
)
if sch_name == 'exponentiallr':
scheduler = torch.optim.lr_scheduler.ExponentialLR(
optimizer, gamma=sch_settings['gamma']
)
if sch_name == 'cossineannealinglr':
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, T_max=sch_settings['T_max'], eta_min=sch_settings['eta_min']
)
# Add optimizer if you want
if sch_name != None:
return scheduler
else:
raise NotImplementedError(f"{sch_name} is not implemented yet.")
##########################################################
# Visualize #
##########################################################
def visualize(model, input_size, device, epoch, save_path):
path = 'data_samples/samples'
if not os.path.exists(path):
path = os.getcwd() + path
random_pkl_path = 'data_samples/random_reference.pkl'
if not os.path.exists(random_pkl_path):
random_pkl_path = os.getcwd() + random_pkl_path
fix_pkl_path = 'data_samples/fix_reference.pkl'
if not os.path.exists(fix_pkl_path):
fix_pkl_path = os.getcwd() + fix_pkl_path
assert os.path.exists(path) == True
assert os.path.exists(random_pkl_path) == True
assert os.path.exists(fix_pkl_path) == True
model.eval()
with torch.no_grad():
cos_mean_random = np.zeros(100)
cos_mean_fix = np.zeros(100)
with open(random_pkl_path, 'rb') as f:
real_mean_random = pickle.load(f)
with open(fix_pkl_path, 'rb') as f:
real_mean_fix = pickle.load(f)
print(f"Epoch #{epoch + 1 } >>>> Visualize :")
iter = 0
for subpath in tqdm(os.listdir(path)):
if os.path.splitext(subpath)[-1] not in ['.png', '.jpg']:
sample_path = os.path.join(path, subpath)
iter += 1
for i in range(1, 101):
try:
blurred_img_fix = cv2.resize(cv2.imread(os.path.join(sample_path, f'fix_{i}.png')),
(input_size, input_size), interpolation=cv2.INTER_AREA) / 255
blurred_img_random = cv2.resize(cv2.imread(os.path.join(sample_path, f'random_{i}.png')),
(input_size, input_size), interpolation=cv2.INTER_AREA) / 255
blurred_img_fix = torch.Tensor(blurred_img_fix).permute(2, 0, 1).unsqueeze(0).to(device)
estimated_fix = model(blurred_img_fix)
blurred_img_random = torch.Tensor(blurred_img_random).permute(2, 0, 1).unsqueeze(0).to(device)
estimated_random = model(blurred_img_random)
except:
estimated_random = (cos_mean_random[i - 1] / iter)
estimated_fix = (cos_mean_fix[i - 1] / iter)
cos_mean_random[i - 1] += estimated_random.item()
cos_mean_fix[i - 1] += estimated_fix.item()
break
cos_mean_fix /= 30
cos_mean_random /= 30
plt.figure(figsize=(24, 7))
plt.subplot(1, 2, 1)
plt.plot(cos_mean_fix, 'k', linewidth=2, label="Estimated(Fix $\\theta$)")
plt.plot(real_mean_fix, 'k--', linewidth=2, label="Real(Fix $\\theta$)")
plt.legend(fontsize=15)
plt.subplot(1, 2, 2)
plt.plot(cos_mean_random, 'k', linewidth=2, label="Estimated(Random $\\theta$)")
plt.plot(real_mean_random, 'k--', linewidth=2, label="Real(Random $\\theta$)")
plt.legend(fontsize=15)
plt.savefig(f"{save_path}/graph_{epoch}.png")
return cos_mean_fix, cos_mean_random
def visualize_cls(model, input_size, device, epoch, cls_num, save_path):
path = 'data_samples/samples'
if not os.path.exists(path):
path = os.getcwd() + path
random_pkl_path = 'data_samples/random_reference.pkl'
if not os.path.exists(random_pkl_path):
random_pkl_path = os.getcwd() + random_pkl_path
fix_pkl_path = 'data_samples/fix_reference.pkl'
if not os.path.exists(fix_pkl_path):
fix_pkl_path = os.getcwd() + fix_pkl_path
assert os.path.exists(path) == True
assert os.path.exists(random_pkl_path) == True
assert os.path.exists(fix_pkl_path) == True
model.eval()
with torch.no_grad():
cos_mean_random = np.zeros(100)
cos_mean_fix = np.zeros(100)
with open(random_pkl_path, 'rb') as f:
real_mean_random = pickle.load(f)
with open(fix_pkl_path, 'rb') as f:
real_mean_fix = pickle.load(f)
iter = 0
for subpath in tqdm(os.listdir(path)):
if os.path.splitext(subpath)[-1] not in ['.png', '.jpg']:
sample_path = os.path.join(path, subpath)
iter += 1
for i in range(1, 101):
try:
blurred_img_fix = cv2.resize(cv2.imread(os.path.join(sample_path, f'fix_{i}.png')),
(input_size, input_size), interpolation=cv2.INTER_AREA) / 255
blurred_img_random = cv2.resize(cv2.imread(os.path.join(sample_path, f'random_{i}.png')),
(input_size, input_size), interpolation=cv2.INTER_AREA) / 255
blurred_img_fix = torch.Tensor(blurred_img_fix).permute(2, 0, 1).unsqueeze(0).to(device)
estimated_fix = model(blurred_img_fix)
_, predicted_fix = torch.max(estimated_fix.data, 1)
blurred_img_random = torch.Tensor(blurred_img_random).permute(2, 0, 1).unsqueeze(0).to(device)
estimated_random = model(blurred_img_random)
_, predicted_random = torch.max(estimated_random.data, 1)
estimated_random = predicted_random*(1/cls_num)
estimated_fix = predicted_fix*(1/cls_num)
except:
estimated_random = (cos_mean_random[i - 1] / iter)
estimated_fix = (cos_mean_fix[i - 1] / iter)
cos_mean_random[i - 1] += estimated_random.item()
cos_mean_fix[i - 1] += estimated_fix.item()
cos_mean_fix /= 30
cos_mean_random /= 30
plt.figure(figsize=(24, 7))
plt.subplot(1, 2, 1)
plt.plot(cos_mean_fix, 'k', linewidth=2, label="Estimated(Fix $\\theta$)")
plt.plot(real_mean_fix, 'k--', linewidth=2, label="Real(Fix $\\theta$)")
plt.legend(fontsize=15)
plt.subplot(1, 2, 2)
plt.plot(cos_mean_random, 'k', linewidth=2, label="Estimated(Random $\\theta$)")
plt.plot(real_mean_random, 'k--', linewidth=2, label="Real(Random $\\theta$)")
plt.legend(fontsize=15)
plt.savefig(f"{save_path}/graph_{epoch}_cls_{cls_num}_visualize.png")
plt.close()
return cos_mean_fix, cos_mean_random