Skip to content

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 requested review from zhouzaida and mzr1996 August 24, 2022 07:49
@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 force-pushed the yancong/dev-accuracy branch from f00eabb to ddc5138 Compare August 24, 2022 09:08
@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-accuracy branch from ddc5138 to 6fdf113 Compare August 24, 2022 09:13
@zhouzaida zhouzaida requested a review from tonysy September 6, 2022 06:26
@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-accuracy branch from fdb2de2 to a37c153 Compare September 6, 2022 07:44
@ice-tong ice-tong force-pushed the yancong/dev-base_metric branch from 2b68268 to 61c4c7a Compare September 7, 2022 04:36
@ice-tong ice-tong force-pushed the yancong/dev-accuracy branch from a37c153 to cc82251 Compare September 7, 2022 04:51
@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 force-pushed the yancong/dev-accuracy branch from cc82251 to d951c4c Compare September 13, 2022 05:33
@ice-tong ice-tong force-pushed the yancong/dev-base_metric branch from ceb2f3d to b4b4c23 Compare September 15, 2022 02:18
@ice-tong ice-tong force-pushed the yancong/dev-accuracy branch from 3e81490 to fbd0635 Compare September 15, 2022 11:44
@ice-tong ice-tong changed the base branch from yancong/dev-base_metric to main September 15, 2022 11:44
@ice-tong ice-tong force-pushed the yancong/dev-accuracy branch from e48d94a to 29177a2 Compare September 29, 2022 12:33
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


@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

@ice-tong ice-tong force-pushed the yancong/dev-accuracy branch from 502d4bb to d7e0ae7 Compare October 10, 2022 10:57
@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