MCPcopy
hub / github.com/fundamentalvision/Deformable-DETR / SmoothedValue

Class SmoothedValue

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

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

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected