-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils.py
252 lines (200 loc) · 7.63 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
from math import cos, pi
import torch
from torchvision import transforms
from PIL import Image
import torch.nn as nn
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def statistics(pred, y, thresh):
batch_size = pred.size(0)
class_nb = pred.size(1)
pred = pred >= thresh
pred = pred.long()
statistics_list = []
for j in range(class_nb):
TP = 0
FP = 0
FN = 0
TN = 0
for i in range(batch_size):
if pred[i][j] == 1:
if y[i][j] == 1:
TP += 1
elif y[i][j] == 0:
FP += 1
else:
assert False
elif pred[i][j] == 0:
if y[i][j] == 1:
FN += 1
elif y[i][j] == 0:
TN += 1
else:
assert False
else:
assert False
statistics_list.append({'TP': TP, 'FP': FP, 'TN': TN, 'FN': FN})
return statistics_list
def calc_f1_score(statistics_list):
f1_score_list = []
for i in range(len(statistics_list)):
TP = statistics_list[i]['TP']
FP = statistics_list[i]['FP']
FN = statistics_list[i]['FN']
precise = TP / (TP + FP + 1e-20)
recall = TP / (TP + FN + 1e-20)
f1_score = 2 * precise * recall / (precise + recall + 1e-20)
f1_score_list.append(f1_score)
mean_f1_score = sum(f1_score_list) / len(f1_score_list)
return mean_f1_score, f1_score_list
def calc_acc(statistics_list):
acc_list = []
for i in range(len(statistics_list)):
TP = statistics_list[i]['TP']
FP = statistics_list[i]['FP']
FN = statistics_list[i]['FN']
TN = statistics_list[i]['TN']
acc = (TP+TN)/(TP+TN+FP+FN)
acc_list.append(acc)
mean_acc_score = sum(acc_list) / len(acc_list)
return mean_acc_score, acc_list
def update_statistics_list(old_list, new_list):
if not old_list:
return new_list
assert len(old_list) == len(new_list)
for i in range(len(old_list)):
old_list[i]['TP'] += new_list[i]['TP']
old_list[i]['FP'] += new_list[i]['FP']
old_list[i]['TN'] += new_list[i]['TN']
old_list[i]['FN'] += new_list[i]['FN']
return old_list
def BP4D_infolist(list):
infostr = {'AU1: {:.2f} AU2: {:.2f} AU4: {:.2f} AU6: {:.2f} AU7: {:.2f} AU10: {:.2f} AU12: {:.2f} AU14: {:.2f} AU15: {:.2f} AU17: {:.2f} AU23: {:.2f} AU24: {:.2f} '.format(100.*list[0],100.*list[1],100.*list[2],100.*list[3],100.*list[4],100.*list[5],100.*list[6],100.*list[7],100.*list[8],100.*list[9],100.*list[10],100.*list[11])}
return infostr
def DISFA_infolist(list):
infostr = {'AU1: {:.2f} AU2: {:.2f} AU4: {:.2f} AU6: {:.2f} AU9: {:.2f} AU12: {:.2f} AU25: {:.2f} AU26: {:.2f} '.format(100.*list[0],100.*list[1],100.*list[2],100.*list[3],100.*list[4],100.*list[5],100.*list[6],100.*list[7])}
return infostr
def adjust_learning_rate(optimizer, epoch, epochs, init_lr, iteration, num_iter):
current_iter = iteration + epoch * num_iter
max_iter = epochs * num_iter
lr = init_lr * (1 + cos(pi * current_iter / max_iter)) / 2
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class PlaceCrop(object):
"""Crops the given PIL.Image at the particular index.
Args:
size (sequence or int): Desired output size of the crop. If size is an
int instead of sequence like (w, h), a square crop (size, size) is
made.
"""
def __init__(self, size, start_x, start_y):
if isinstance(size, int):
self.size = (int(size), int(size))
else:
self.size = size
self.start_x = start_x
self.start_y = start_y
def __call__(self, img):
"""
Args:
img (PIL.Image): Image to be cropped.
Returns:
PIL.Image: Cropped image.
"""
th, tw = self.size
return img.crop((self.start_x, self.start_y, self.start_x + tw, self.start_y + th))
class SetFlip(object):
def __init__(self, flip):
self.flip = flip
def __call__(self, img):
"""
Args:
img (PIL.Image): Image to be flipped.
Returns:
PIL.Image: Randomly flipped image.
"""
if self.flip:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
return img
class image_train(object):
def __init__(self, img_size=256, crop_size=224):
self.img_size = img_size
self.crop_size = crop_size
def __call__(self, img, flip, offset_x, offset_y):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transform = transforms.Compose([
transforms.Resize(self.img_size),
PlaceCrop(self.crop_size, offset_x, offset_y),
SetFlip(flip),
transforms.ColorJitter(brightness=0.4,
contrast=0.4,
saturation=0.4,
hue=0),
transforms.ToTensor(),
normalize
])
img = transform(img)
return img
class image_test(object):
def __init__(self, img_size=256, crop_size=224):
self.img_size = img_size
self.crop_size = crop_size
def __call__(self, img):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
transform = transforms.Compose([
transforms.Resize(self.img_size),
transforms.CenterCrop(self.crop_size),
transforms.ToTensor(),
normalize
])
img = transform(img)
return img
def load_state_dict(model,path):
checkpoints = torch.load(path,map_location=torch.device('cpu'))
state_dict = checkpoints['state_dict']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if 'module.' in k:
k = k[7:] # remove `module.`
new_state_dict[k] = v
# load params
model.load_state_dict(new_state_dict,strict=False)
return model
class WeightedAsymmetricLoss(nn.Module):
def __init__(self, eps=1e-8, disable_torch_grad=True, weight=None):
super(WeightedAsymmetricLoss, self).__init__()
self.disable_torch_grad = disable_torch_grad
self.eps = eps
self.weight = weight
def forward(self, x, y):
xs_pos = x
xs_neg = 1 - x
# Basic CE calculation
los_pos = y * torch.log(xs_pos.clamp(min=self.eps))
los_neg = (1 - y) * torch.log(xs_neg.clamp(min=self.eps))
# Asymmetric Focusing
if self.disable_torch_grad:
torch.set_grad_enabled(False)
neg_weight = 1 - xs_neg
if self.disable_torch_grad:
torch.set_grad_enabled(True)
loss = los_pos + neg_weight * los_neg
if self.weight is not None:
loss = loss * self.weight.view(1,-1)
loss = loss.mean(dim=-1)
return -loss.mean()