-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
177 lines (144 loc) · 5.31 KB
/
Copy pathutils.py
File metadata and controls
177 lines (144 loc) · 5.31 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
import logging
import os
import shutil
from collections import OrderedDict
import torch
from torch import distributed as dist
from torch import nn
from torch.nn import functional as F
from sklearn.metrics import confusion_matrix
import numpy as np
logger = logging.getLogger(__name__)
def reduce_tensor(tensor, n):
rt = tensor.clone()
dist.all_reduce(rt, op=dist.ReduceOp.SUM)
rt /= n
return rt
def create_loss_fn(args):
# if args.label_smoothing > 0:
# criterion = SmoothCrossEntropyV2(alpha=args.label_smoothing)
# else:
criterion = nn.CrossEntropyLoss(label_smoothing=args.label_smoothing)
return criterion # .to(args.device)
def module_load_state_dict(model, state_dict):
try:
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
except:
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = f'module.{k}' # add `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
def model_load_state_dict(model, state_dict):
try:
model.load_state_dict(state_dict)
except:
module_load_state_dict(model, state_dict)
def save_checkpoint(args, state, is_best, finetune=False):
os.makedirs(args.save_path, exist_ok=True)
if finetune:
name = f'{args.name}_finetune'
else:
name = args.name
filename = f'{args.save_path}/{name}_last.pth.tar'
torch.save(state, filename, _use_new_zipfile_serialization=False)
if is_best:
shutil.copyfile(filename, f'{args.save_path}/{args.name}_best.pth.tar')
def accuracy(output, target, topk=(1,)):
output = output.to(torch.device('cpu'))
target = target.to(torch.device('cpu'))
maxk = max(topk)
batch_size = target.shape[0]
_, idx = output.sort(dim=1, descending=True)
pred = idx.narrow(1, 0, maxk).t()
correct = pred.eq(target.reshape(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(dim=0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def metrics(args, confmat):
tpr_cm = np.zeros(args.num_classes)
tnr_cm = np.zeros(args.num_classes)
f1_cm = np.zeros(args.num_classes)
acc_cm = np.zeros(args.num_classes)
# determine accuracy metrics
for y in range(args.num_classes):
# true positive rate
pos = np.sum(confmat[:, y]) # number of instances in the positive class
tp = confmat[y, y] # correctly classified positive incidents
fp = np.sum(confmat[y, :]) - tp # incorrectly classified negative instances
tpr = tp / pos # true positive classification rate
tpr_cm[y] = tpr
# true negative rate
tn = np.trace(confmat) - tp # correctly classified negative instances
tnr = tn / (tn + fp) # true negative rate
tnr_cm[y] = tnr
# f1 score
ppv = tp / (tp + fp) # positve prediction value
f1 = 2 * ((ppv * tpr) / (ppv + tpr)) # f1 score
f1_cm[y] = f1
tot = np.sum(confmat[y, :])
acc = tp / tot
acc_cm[y] = acc
# dice similarity coefficient (dsc)
# dsc = 2 * tp / (2 * tp + fp + tn)
# dsc_cm[fold, y] = dsc
return tpr_cm, tnr_cm, f1_cm, acc_cm
class SmoothCrossEntropy(nn.Module):
def __init__(self, alpha=0.1):
super(SmoothCrossEntropy, self).__init__()
self.alpha = alpha
def forward(self, logits, labels):
if self.alpha == 0:
loss = F.cross_entropy(logits, labels)
else:
num_classes = logits.shape[-1]
alpha_div_k = self.alpha / num_classes
target_probs = F.one_hot(labels, num_classes=num_classes).float() * \
(1. - self.alpha) + alpha_div_k
loss = (-(target_probs * torch.log_softmax(logits, dim=-1)).sum(dim=-1)).mean()
return loss
class SmoothCrossEntropyV2(nn.Module):
"""
NLL loss with label smoothing.
"""
def __init__(self, label_smoothing=0.1):
"""
Constructor for the LabelSmoothing module.
:param smoothing: label smoothing factor`
"""
super().__init__()
assert label_smoothing < 1.0
self.smoothing = label_smoothing
self.confidence = 1. - label_smoothing
def forward(self, x, target):
if self.smoothing == 0:
loss = F.cross_entropy(x, target)
else:
logprobs = F.log_softmax(x, dim=-1)
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1))
nll_loss = nll_loss.squeeze(1)
smooth_loss = -logprobs.mean(dim=-1)
loss = (self.confidence * nll_loss + self.smoothing * smooth_loss).mean()
return loss
class AverageMeter(object):
"""Computes and stores the average and current value
Imported from https://github.com/pytorch/examples/blob/master/imagenet/main.py#L247-L262
"""
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