forked from DenisDsh/PyTorch-Deep-CORAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
88 lines (66 loc) · 2.09 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
import numpy as np
import matplotlib.pyplot as plt
import torch
def imshow(image_tensor, mean, std, title=None):
"""
Imshow for normalized Tensors.
Useful to visualize data from data loader
"""
image = image_tensor.numpy().transpose((1, 2, 0))
image = std * image + mean
image = np.clip(image, 0, 1)
plt.imshow(image)
if title is not None:
plt.title(title)
plt.pause(0.001) # pause a bit so that plots are updated
def accuracy(output, target):
_, predicted = torch.max(output.data, 1)
total = target.size(0)
correct = (predicted == target).sum().item()
accuracy = correct/total
return accuracy
class Tracker:
def __init__(self):
self.data = {}
def track(self, name, *monitors):
l = Tracker.ListStorage(monitors)
self.data.setdefault(name, []).append(l)
return l
def to_dict(self):
return {k: list(map(list, v)) for k, v in self.data.items()}
class ListStorage:
def __init__(self, monitors=[]):
self.data = []
self.monitors = monitors
for monitor in self.monitors:
setattr(self, monitor.name, monitor)
def append(self, item):
for monitor in self.monitors:
monitor.update(item)
self.data.append(item)
def __iter__(self):
return iter(self.data)
class MeanMonitor:
name = 'mean'
def __init__(self):
self.n = 0
self.total = 0
def update(self, value):
self.total += value
self.n += 1
@property
def value(self):
return self.total / self.n
class MovingMeanMonitor:
name = 'mean'
def __init__(self, momentum=0.9):
self.momentum = momentum
self.first = True
self.value = None
def update(self, value):
if self.first:
self.value = value
self.first = False
else:
m = self.momentum
self.value = m * self.value + (1 - m) * value