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

[Feature] Add ImageClassificationInferencer. #1261

Merged
merged 5 commits into from
Feb 8, 2023

Conversation

mzr1996
Copy link
Member

@mzr1996 mzr1996 commented Dec 13, 2022

Motivation

See open-mmlab/mmengine#773 for more details about the inferencer design in MMEngine. And this PR is also depend on the inference branch of MMEngine.

In a word, the inferencer is a enhanced inference_model but split the whole process into preprocess, forward, visualize and postprocess four methods.

Modification

Implement ImageClassificationInferencer, refactor inference_model and our demo.

BC-breaking (Optional)

Almost not, but the demo/image_demo.py is modified and we need to also change our documentation later.

Use cases (Optional)

To use the ImageClassificationInferencer

>>> from mmcls import ImageClassificationInferencer
>>> inferencer = ImageClassificationInferencer('resnet50_8xb32_in1k')
>>> results = inferencer('demo/demo.JPEG')
>>> print(results[0]['pred_class'])
sea snake

To inference multiple images by batch on CUDA

>>> from mmcls import ImageClassificationInferencer
>>> inferencer = ImageClassificationInferencer('resnet50_8xb32_in1k', device='cuda')
>>> imgs = ['demo/demo.JPEG'] * 100
>>> results = inferencer(imgs, batch_size=16)
Inference ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.5 it/s
>>> print(results[99]['pred_class'])
sea snake

The original demo/image_demo.py also supports model name by now

python demo/image_demo.py demo/demo.JPEG resnet50_8xb32_in1k

Output:

{
  "pred_label": 65,
  "pred_score": 0.6649367809295654,
  "pred_class": "sea snake"
}

Checklist

Before PR:

  • Pre-commit or other linting tools are used to fix the potential lint issues.
  • Bug fixes are fully covered by unit tests, the case that causes the bug should be added in the unit tests.
  • The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, like docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with those projects, like MMDet or MMSeg.
  • CLA has been signed and all committers have signed the CLA in this PR.

@mzr1996 mzr1996 requested a review from tonysy December 13, 2022 05:21
for input_ in inputs:
if isinstance(input_, str):
data_info = loader({'img_path': input_, 'to_rgb': True})
elif isinstance(input_, np.ndarray):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to add the shape check of the numpy array?

to find a pre-defined weight from the model you specified
(only work if the ``model`` is a model name). Defaults to None.
device (str, optional): Device to run inference. If None, use CPU.
Defaults to None.
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about add some usage cases in doc-string?

Copy link
Collaborator

@tonysy tonysy left a comment

Choose a reason for hiding this comment

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

As inline comments

@mzr1996 mzr1996 requested a review from tonysy January 18, 2023 08:09
@codecov
Copy link

codecov bot commented Jan 18, 2023

Codecov Report

Base: 0.02% // Head: 86.85% // Increases project coverage by +86.82% 🎉

Coverage data is based on head (16d4a6d) compared to base (b8b31e9).
Patch has no changes to coverable lines.

❗ Current head 16d4a6d differs from pull request most recent head d21e071. Consider uploading reports for the commit d21e071 to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##           dev-1.x    #1261       +/-   ##
============================================
+ Coverage     0.02%   86.85%   +86.82%     
============================================
  Files          121      166       +45     
  Lines         8217    13510     +5293     
  Branches      1368     2148      +780     
============================================
+ Hits             2    11734    +11732     
+ Misses        8215     1420     -6795     
- Partials         0      356      +356     
Flag Coverage Δ
unittests 86.85% <ø> (+86.82%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmcls/datasets/transforms/compose.py
mmcls/evaluation/metrics/multi_task.py 94.59% <0.00%> (ø)
mmcls/models/retrievers/base.py 100.00% <0.00%> (ø)
mmcls/apis/model.py 87.09% <0.00%> (ø)
mmcls/utils/progress.py 66.66% <0.00%> (ø)
mmcls/models/heads/margin_head.py 89.13% <0.00%> (ø)
mmcls/models/tta/score_tta.py 100.00% <0.00%> (ø)
mmcls/models/utils/norm.py 80.00% <0.00%> (ø)
mmcls/models/backbones/swin_transformer_v2.py 89.63% <0.00%> (ø)
mmcls/models/backbones/levit.py 96.06% <0.00%> (ø)
... and 157 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@tonysy tonysy left a comment

Choose a reason for hiding this comment

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

As inline comments

docs/en/get_started.md Show resolved Hide resolved
@mzr1996 mzr1996 requested a review from tonysy February 6, 2023 08:43
@Ezra-Yu Ezra-Yu mentioned this pull request Feb 8, 2023
6 tasks
@mzr1996 mzr1996 merged commit a3f2eff into open-mmlab:dev-1.x Feb 8, 2023
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.

None yet

2 participants