Skip to content

Commit

Permalink
Compute overall accuracy using number of correct predictions (#1341)
Browse files Browse the repository at this point in the history
* Trying out accuracy fix

* Computing Accuracy using num_correct

* minor fixes

* Moving mutiplication by 100 to accuracy computation, training log fix

* code quality edits
  • Loading branch information
abhinavnmagic authored and bfineran committed Feb 3, 2023
1 parent e054e12 commit d76d5c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/sparseml/pytorch/torchvision/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ def train_one_epoch(
# Reset ema buffer to keep copying weights during warmup period
model_ema.n_averaged.fill_(0)

acc1, acc5 = utils.accuracy(output, target, topk=(1, 5))
acc1, num_correct_1, acc5, num_correct_5 = utils.accuracy(
output, target, topk=(1, 5)
)
batch_size = image.shape[0]
metric_logger.update(loss=loss.item(), lr=optimizer.param_groups[0]["lr"])
metric_logger.meters["acc1"].update(acc1.item(), n=batch_size)
metric_logger.meters["acc5"].update(acc5.item(), n=batch_size)
metric_logger.meters["acc1"].update(
acc1.item(), n=batch_size, total=num_correct_1
)
metric_logger.meters["acc5"].update(
acc5.item(), n=batch_size, total=num_correct_5
)
metric_logger.meters["imgs_per_sec"].update(
batch_size / (time.time() - start_time)
)
Expand Down Expand Up @@ -181,13 +187,19 @@ def evaluate(
output = output[0]
loss = criterion(output, target)

acc1, acc5 = utils.accuracy(output, target, topk=(1, 5))
acc1, num_correct_1, acc5, num_correct_5 = utils.accuracy(
output, target, topk=(1, 5)
)
# FIXME need to take into account that the datasets
# could have been padded in distributed setup
batch_size = image.shape[0]
metric_logger.update(loss=loss.item())
metric_logger.meters["acc1"].update(acc1.item(), n=batch_size)
metric_logger.meters["acc5"].update(acc5.item(), n=batch_size)
metric_logger.meters["acc1"].update(
acc1.item(), n=batch_size, total=num_correct_1
)
metric_logger.meters["acc5"].update(
acc5.item(), n=batch_size, total=num_correct_5
)
num_processed_samples += batch_size
# gather the stats from all processes

Expand Down
8 changes: 6 additions & 2 deletions src/sparseml/pytorch/torchvision/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ def __init__(self, window_size=20, fmt=None):
self.count = 0
self.fmt = fmt

def update(self, value, n=1):
def update(self, value, n=1, total=None):
self.deque.append(value)
self.count += n
self.total += value * n
if total is not None:
self.total += total
else:
self.total += value * n

def synchronize_between_processes(self):
"""
Expand Down Expand Up @@ -210,6 +213,7 @@ def accuracy(output, target, topk=(1,)):
for k in topk:
correct_k = correct[:k].flatten().sum(dtype=torch.float32)
res.append(correct_k * (100.0 / batch_size))
res.append(correct_k * 100.0)
return res


Expand Down

0 comments on commit d76d5c7

Please sign in to comment.