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

[Feat] Add Accuracy #6

Merged
merged 1 commit into from
Oct 10, 2022
Merged

[Feat] Add Accuracy #6

merged 1 commit into from
Oct 10, 2022

Conversation

ice-tong
Copy link
Collaborator

@ice-tong ice-tong commented Aug 24, 2022

Motivation

Adding accuracy metric and test case.

Modification

  • mmeval/classification/accuracy.py
  • tests/unittest/classification/test_accuracy.py

MMCls

@ice-tong ice-tong changed the base branch from main to yancong/dev-dispatch August 24, 2022 07:46
@ice-tong ice-tong force-pushed the yancong/dev-dispatch branch 2 times, most recently from 062e847 to e47d1de Compare August 24, 2022 08:39
@ice-tong ice-tong changed the base branch from yancong/dev-dispatch to yancong/dev-base_metric August 24, 2022 09:08
@ice-tong ice-tong force-pushed the yancong/dev-base_metric branch 2 times, most recently from cdc0b86 to 2b68268 Compare September 6, 2022 07:31
@ice-tong ice-tong force-pushed the yancong/dev-base_metric branch 2 times, most recently from 3de7c93 to 7247528 Compare September 13, 2022 05:30
@ice-tong ice-tong changed the base branch from yancong/dev-base_metric to main September 15, 2022 11:44
Comment on lines 142 to 143
for pred, label in zip(predictions, labels):
self._results.append((pred, label))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to only reserve the first top-k results only in the intermediate results.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intermediate results can be very large if the number of classes is large.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, use corrects as the intermediate results.

Comment on lines 139 to 141
for pred, label in zip(predictions, labels):
corrects = self.compute_correct(pred, label)
self._results.append(corrects)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why calculate corrects one by one instead of a batch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, calculate corrects in batch now.

Comment on lines +42 to +75
if torch is not None:
values, indices = _torch_topk(torch.from_numpy(inputs), k, dim=axis)
return values.numpy(), indices.numpy()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How fast does the pytorch compared with numpy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This depends on the size of the input. The larger the size of the input, the greater the speedup.

The following is a simple benchmark.

import torch
import numpy as np
import time


def numpy_topk(inputs, k, axis=None, use_torch=False):
    if use_torch:
        values, indices = torch.from_numpy(inputs).topk(k, dim=axis)
        return values.numpy(), indices.numpy()

    indices = np.argsort(inputs, axis=axis)
    indices = np.take(indices, np.arange(k), axis=axis)
    values = np.take_along_axis(inputs, indices, axis=axis)
    return values, indices


def test(shape, k, axis):
    print('Test setting: ', shape, k, axis)
    arr = np.random.rand(*shape)
    t1 = time.time()
    numpy_topk(arr, k, axis, use_torch=False)
    t2 = time.time()
    numpy_topk(arr, k, axis, use_torch=True)
    t3 = time.time()
    print(f'custom impl numpy topk cost: {t2-t1}')
    print(f'torch impl numpy topk cost:  {t3-t2}')


if __name__ == "__main__":
    test((100, 100), k=4, axis=1)
    test((100, 1000), k=4, axis=1)
    test((100, 1000), k=10, axis=1)
    test((1000, 1000), k=4, axis=1)
    test((10000, 1000), k=4, axis=1)

Got the following outputs:

Test setting:  (100, 100) 4 1
custom impl numpy topk cost: 0.0005044937133789062
torch impl numpy topk cost:  0.0046596527099609375
Test setting:  (100, 1000) 4 1
custom impl numpy topk cost: 0.005578756332397461
torch impl numpy topk cost:  0.004395961761474609
Test setting:  (100, 1000) 10 1
custom impl numpy topk cost: 0.005606651306152344
torch impl numpy topk cost:  0.0003619194030761719
Test setting:  (1000, 1000) 4 1
custom impl numpy topk cost: 0.05330657958984375
torch impl numpy topk cost:  0.001957416534423828
Test setting:  (10000, 1000) 4 1
custom impl numpy topk cost: 0.5171260833740234
torch impl numpy topk cost:  0.0032396316528320312

.pre-commit-config.yaml Outdated Show resolved Hide resolved
mmeval/classification/accuracy.py Show resolved Hide resolved

@overload # type: ignore
@dispatch
def compute_corrects(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a private method if the method won't be used by users.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@zhouzaida zhouzaida merged commit 48003ec into main Oct 10, 2022
@zhouzaida zhouzaida deleted the yancong/dev-accuracy branch October 29, 2022 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants