| # -------------------------------------------------------- | |
| # X-Decoder -- Generalized Decoding for Pixel, Image, and Language | |
| # Copyright (c) 2022 Microsoft | |
| # Licensed under The MIT License [see LICENSE for details] | |
| # Written by Xueyan Zou ([email protected]) | |
| # -------------------------------------------------------- | |
| import math | |
| class AverageMeter(object): | |
| """Computes and stores the average and current value.""" | |
| def __init__(self): | |
| self.reset() | |
| def reset(self): | |
| self.val = 0 | |
| self.avg = 0 | |
| self.sum = 0 | |
| self.count = 0 | |
| def update(self, val, n=1, decay=0): | |
| self.val = val | |
| if decay: | |
| alpha = math.exp(-n / decay) # exponential decay over 100 updates | |
| self.sum = alpha * self.sum + (1 - alpha) * val * n | |
| self.count = alpha * self.count + (1 - alpha) * n | |
| else: | |
| self.sum += val * n | |
| self.count += n | |
| self.avg = self.sum / self.count | |