MCPcopy Create free account
hub / github.com/PeizeSun/TransTrack / SmoothedValue

Class SmoothedValue

util/misc.py:52–111  ·  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

50 from torchvision.ops.misc import _output_size
51
52class SmoothedValue(object):
53 """Track a series of values and provide access to smoothed values over a
54 window or the global series average.
55 """
56
57 def __init__(self, window_size=20, fmt=None):
58 if fmt is None:
59 fmt = "{median:.4f} ({global_avg:.4f})"
60 self.deque = deque(maxlen=window_size)
61 self.total = 0.0
62 self.count = 0
63 self.fmt = fmt
64
65 def update(self, value, n=1):
66 self.deque.append(value)
67 self.count += n
68 self.total += value * n
69
70 def synchronize_between_processes(self):
71 """
72 Warning: does not synchronize the deque!
73 """
74 if not is_dist_avail_and_initialized():
75 return
76 t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
77 dist.barrier()
78 dist.all_reduce(t)
79 t = t.tolist()
80 self.count = int(t[0])
81 self.total = t[1]
82
83 @property
84 def median(self):
85 d = torch.tensor(list(self.deque))
86 return d.median().item()
87
88 @property
89 def avg(self):
90 d = torch.tensor(list(self.deque), dtype=torch.float32)
91 return d.mean().item()
92
93 @property
94 def global_avg(self):
95 return self.total / self.count
96
97 @property
98 def max(self):
99 return max(self.deque)
100
101 @property
102 def value(self):
103 return self.deque[-1]
104
105 def __str__(self):
106 return self.fmt.format(
107 median=self.median,
108 avg=self.avg,
109 global_avg=self.global_avg,

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected