Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit d76d5c7

Browse files
abhinavnmagicbfineran
authored andcommitted
Compute overall accuracy using number of correct predictions (#1341)
* Trying out accuracy fix * Computing Accuracy using num_correct * minor fixes * Moving mutiplication by 100 to accuracy computation, training log fix * code quality edits
1 parent e054e12 commit d76d5c7

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/sparseml/pytorch/torchvision/train.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,17 @@ def train_one_epoch(
139139
# Reset ema buffer to keep copying weights during warmup period
140140
model_ema.n_averaged.fill_(0)
141141

142-
acc1, acc5 = utils.accuracy(output, target, topk=(1, 5))
142+
acc1, num_correct_1, acc5, num_correct_5 = utils.accuracy(
143+
output, target, topk=(1, 5)
144+
)
143145
batch_size = image.shape[0]
144146
metric_logger.update(loss=loss.item(), lr=optimizer.param_groups[0]["lr"])
145-
metric_logger.meters["acc1"].update(acc1.item(), n=batch_size)
146-
metric_logger.meters["acc5"].update(acc5.item(), n=batch_size)
147+
metric_logger.meters["acc1"].update(
148+
acc1.item(), n=batch_size, total=num_correct_1
149+
)
150+
metric_logger.meters["acc5"].update(
151+
acc5.item(), n=batch_size, total=num_correct_5
152+
)
147153
metric_logger.meters["imgs_per_sec"].update(
148154
batch_size / (time.time() - start_time)
149155
)
@@ -181,13 +187,19 @@ def evaluate(
181187
output = output[0]
182188
loss = criterion(output, target)
183189

184-
acc1, acc5 = utils.accuracy(output, target, topk=(1, 5))
190+
acc1, num_correct_1, acc5, num_correct_5 = utils.accuracy(
191+
output, target, topk=(1, 5)
192+
)
185193
# FIXME need to take into account that the datasets
186194
# could have been padded in distributed setup
187195
batch_size = image.shape[0]
188196
metric_logger.update(loss=loss.item())
189-
metric_logger.meters["acc1"].update(acc1.item(), n=batch_size)
190-
metric_logger.meters["acc5"].update(acc5.item(), n=batch_size)
197+
metric_logger.meters["acc1"].update(
198+
acc1.item(), n=batch_size, total=num_correct_1
199+
)
200+
metric_logger.meters["acc5"].update(
201+
acc5.item(), n=batch_size, total=num_correct_5
202+
)
191203
num_processed_samples += batch_size
192204
# gather the stats from all processes
193205

src/sparseml/pytorch/torchvision/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ def __init__(self, window_size=20, fmt=None):
2828
self.count = 0
2929
self.fmt = fmt
3030

31-
def update(self, value, n=1):
31+
def update(self, value, n=1, total=None):
3232
self.deque.append(value)
3333
self.count += n
34-
self.total += value * n
34+
if total is not None:
35+
self.total += total
36+
else:
37+
self.total += value * n
3538

3639
def synchronize_between_processes(self):
3740
"""
@@ -210,6 +213,7 @@ def accuracy(output, target, topk=(1,)):
210213
for k in topk:
211214
correct_k = correct[:k].flatten().sum(dtype=torch.float32)
212215
res.append(correct_k * (100.0 / batch_size))
216+
res.append(correct_k * 100.0)
213217
return res
214218

215219

0 commit comments

Comments
 (0)