MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / SmoothedValue

Class SmoothedValue

SwissArmyTransformer/examples/yolos/util/misc.py:26–85  ·  view source on GitHub ↗

Track a series of values and provide access to smoothed values over a window or the global series average.

Source from the content-addressed store, hash-verified

24
25
26class SmoothedValue(object):
27 """Track a series of values and provide access to smoothed values over a
28 window or the global series average.
29 """
30
31 def __init__(self, window_size=20, fmt=None):
32 if fmt is None:
33 fmt = "{median:.4f} ({global_avg:.4f})"
34 self.deque = deque(maxlen=window_size)
35 self.total = 0.0
36 self.count = 0
37 self.fmt = fmt
38
39 def update(self, value, n=1):
40 self.deque.append(value)
41 self.count += n
42 self.total += value * n
43
44 def synchronize_between_processes(self):
45 """
46 Warning: does not synchronize the deque!
47 """
48 if not is_dist_avail_and_initialized():
49 return
50 t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
51 dist.barrier()
52 dist.all_reduce(t)
53 t = t.tolist()
54 self.count = int(t[0])
55 self.total = t[1]
56
57 @property
58 def median(self):
59 d = torch.tensor(list(self.deque))
60 return d.median().item()
61
62 @property
63 def avg(self):
64 d = torch.tensor(list(self.deque), dtype=torch.float32)
65 return d.mean().item()
66
67 @property
68 def global_avg(self):
69 return self.total / self.count
70
71 @property
72 def max(self):
73 return max(self.deque)
74
75 @property
76 def value(self):
77 return self.deque[-1]
78
79 def __str__(self):
80 return self.fmt.format(
81 median=self.median,
82 avg=self.avg,
83 global_avg=self.global_avg,

Callers 1

log_everyMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected