MCPcopy Create free account
hub / github.com/UVA-Computer-Vision-Lab/FrameINO / AverageMeter

Class AverageMeter

preprocess/SpaTrackV2_code/models/utils.py:34–95  ·  view source on GitHub ↗

Computes and stores the average and current value

Source from the content-addressed store, hash-verified

32 COUNT = 3
33
34class AverageMeter(object):
35 """Computes and stores the average and current value"""
36
37 def __init__(self, name, fmt=":f", summary_type=Summary.AVERAGE):
38 self.name = name
39 self.fmt = fmt
40 self.summary_type = summary_type
41 self.reset()
42
43 def reset(self):
44 self.val = 0
45 self.avg = 0
46 self.sum = 0
47 self.count = 0
48
49 def update(self, val, n=1):
50 self.val = val
51 self.sum += val * n
52 self.count += n
53 self.avg = self.sum / self.count
54
55 def all_reduce(self):
56 device = "cuda" if torch.cuda.is_available() else "cpu"
57 if isinstance(self.sum, np.ndarray):
58 total = torch.tensor(
59 self.sum.tolist()
60 + [
61 self.count,
62 ],
63 dtype=torch.float32,
64 device=device,
65 )
66 else:
67 total = torch.tensor(
68 [self.sum, self.count], dtype=torch.float32, device=device
69 )
70
71 dist.all_reduce(total, dist.ReduceOp.SUM, async_op=False)
72 if total.shape[0] > 2:
73 self.sum, self.count = total[:-1].cpu().numpy(), total[-1].cpu().item()
74 else:
75 self.sum, self.count = total.tolist()
76 self.avg = self.sum / (self.count + 1e-5)
77
78 def __str__(self):
79 fmtstr = "{name} {val" + self.fmt + "} ({avg" + self.fmt + "})"
80 return fmtstr.format(**self.__dict__)
81
82 def summary(self):
83 fmtstr = ""
84 if self.summary_type is Summary.NONE:
85 fmtstr = ""
86 elif self.summary_type is Summary.AVERAGE:
87 fmtstr = "{name} {avg:.3f}"
88 elif self.summary_type is Summary.SUM:
89 fmtstr = "{name} {sum:.3f}"
90 elif self.summary_type is Summary.COUNT:
91 fmtstr = "{name} {count:.3f}"

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected