-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
241 lines (183 loc) · 7.66 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
import os
import math
import logging
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from data.transform import _CIFAR_MEAN, _CIFAR_STD, _IMAGENET_MEAN, _IMAGENET_STD
from pathlib import Path
from typing import Optional
from collections import OrderedDict
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
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 accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0)
res.append(correct_k.mul_(1. / batch_size))
return res
def one_hot(
labels: torch.Tensor,
num_classes: int,
device: Optional[torch.device] = None,
dtype: Optional[torch.dtype] = None,
eps: float = 1e-6,
) -> torch.Tensor:
r"""Converts an integer label x-D tensor to a one-hot (x+1)-D tensor.
Args:
labels: tensor with labels of shape :math:`(N, *)`, where N is batch size.
Each value is an integer representing correct classification.
num_classes: number of classes in labels.
device: the desired device of returned tensor.
dtype: the desired data type of returned tensor.
Returns:
the labels in one hot tensor of shape :math:`(N, C, *)`,
Examples:
>>> labels = torch.LongTensor([[[0, 1], [2, 0]]])
>>> one_hot(labels, num_classes=3)
tensor([[[[1.0000e+00, 1.0000e-06],
[1.0000e-06, 1.0000e+00]],
<BLANKLINE>
[[1.0000e-06, 1.0000e+00],
[1.0000e-06, 1.0000e-06]],
<BLANKLINE>
[[1.0000e-06, 1.0000e-06],
[1.0000e+00, 1.0000e-06]]]])
"""
if not isinstance(labels, torch.Tensor):
raise TypeError("Input labels type is not a torch.Tensor. Got {}".format(type(labels)))
if not labels.dtype == torch.int64:
raise ValueError("labels must be of the same dtype torch.int64. Got: {}".format(labels.dtype))
if num_classes < 1:
raise ValueError("The number of classes must be bigger than one." " Got: {}".format(num_classes))
shape = labels.shape
one_hot = torch.zeros((shape[0], num_classes) + shape[1:], device=device, dtype=dtype)
return one_hot.scatter_(1, labels.unsqueeze(1), 1.0) + eps
def dice_similarity_coefficient(input: torch.Tensor, target: torch.Tensor, index: list, eps: float = 1e-8) -> torch.Tensor:
r"""Criterion that computes Sørensen-Dice Coefficient.
According to [1], we compute the Sørensen-Dice Coefficient as follows:
.. math::
\text{Dice}(x, class) = \frac{2 |X| \cap |Y|}{|X| + |Y|}
Where:
- :math:`X` expects to be the scores of each class.
- :math:`Y` expects to be the one-hot tensor with the class labels.
Reference:
[1] https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
Args:
input: logits tensor with shape :math:`(N, C, H, W)` where C = number of classes.
labels: labels tensor with shape :math:`(N, H, W)` where each value
is :math:`0 ≤ targets[i] ≤ C−1`.
eps: Scalar to enforce numerical stabiliy.
Return:
the computed Sørensen-Dice Coefficient.
Example:
>>> N = 5 # num_classes
>>> input = torch.randn(1, N, 3, 5, requires_grad=True)
>>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
>>> output = dice_loss(input, target)
>>> output.backward()
"""
if not isinstance(input, torch.Tensor):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(type(input)))
if not len(input.shape) == 4:
raise ValueError("Invalid input shape, we expect BxNxHxW. Got: {}".format(input.shape))
if not input.shape[-2:] == target.shape[-2:]:
raise ValueError("input and target shapes must be the same. Got: {} and {}".format(input.shape, target.shape))
if not input.device == target.device:
raise ValueError(
"input and target must be in the same device. Got: {} and {}".format(input.device, target.device)
)
# compute softmax over the classes axis
input_soft: torch.Tensor = F.softmax(input, dim=1)
# create the labels one hot tensor
target_one_hot: torch.Tensor = one_hot(target, num_classes=input.shape[1], device=input.device, dtype=input.dtype)
# compute the actual dice score
dims = (2, 3)
intersection = torch.sum(input_soft * target_one_hot, dims)
cardinality = torch.sum(input_soft + target_one_hot, dims)
dice_score = 2.0 * intersection / (cardinality + eps)
return torch.mean(dice_score, dim=0)[index]
def unnormalize(config, tensor):
name = config.DATASET.NAME
if name == 'imagenet':
mean, std = _IMAGENET_MEAN, _IMAGENET_STD
elif 'cifar' in name:
mean, std = _CIFAR_MEAN, _CIFAR_STD
else:
mean, std = [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]
for t, m, s in zip(tensor, mean, std):
t.mul_(s).add_(m)
return tensor
def create_logger(cfg, cfg_name, phase='train'):
root_output_dir = Path(cfg.OUTPUT_DIR)
# set up logger
if not root_output_dir.exists():
print('=> creating {}'.format(root_output_dir))
root_output_dir.mkdir()
dataset = cfg.DATASET.NAME
model = cfg.MODEL.NAME
cfg_name = os.path.basename(cfg_name).split('.')[0]
time_str = time.strftime('%Y-%m-%d-%H-%M')
cfg_name = '{}_{}'.format(cfg_name, time_str)
final_output_dir = root_output_dir / dataset / cfg_name
print('=> creating {}'.format(final_output_dir))
final_output_dir.mkdir(parents=True, exist_ok=True)
log_file = '{}.log'.format(phase)
final_log_file = final_output_dir / log_file
head = '%(asctime)-15s %(message)s'
logging.basicConfig(filename=str(final_log_file),
format=head)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
console = logging.StreamHandler()
logging.getLogger('').addHandler(console)
tensorboard_log_dir = Path(cfg.LOG_DIR) / dataset / model / \
(cfg_name + '_' + time_str)
print('=> creating {}'.format(tensorboard_log_dir))
tensorboard_log_dir.mkdir(parents=True, exist_ok=True)
return logger, str(final_output_dir), str(tensorboard_log_dir)
def save_checkpoint(states, is_best, output_dir, filename='checkpoint.pth'):
latest_path = os.path.join(output_dir, 'latest.pth')
if os.path.islink(latest_path):
os.remove(latest_path)
os.symlink(os.path.join(output_dir, filename), latest_path)
if is_best and 'state_dict' in states.keys():
torch.save(states['state_dict'], os.path.join(output_dir, 'model_best.pth'))
def load_checkpoint(model_path, model):
checkpoint = torch.load(model_path)
state_dict = checkpoint
# create new OrderedDict that does not contain `module.`
new_state_dict = OrderedDict()
for k, v in state_dict.items():
if 'module.' in k:
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)
return model