Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute overall accuracy using number of correct predictions #1341

Merged
merged 5 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
natuan marked this conversation as resolved.
Show resolved Hide resolved
return res


Expand Down