Shortcuts

ImageClassificationInferencer

class mmcls.apis.ImageClassificationInferencer(model, weights=None, device=None, classes=None)[源代码]

The inferencer for image classification.

参数
  • model (BaseModel | str | Config) – A model name or a path to the confi file, or a BaseModel object. The model name can be found by ImageClassificationInferencer.list_models() and you can also query it in 模型库统计.

  • weights (str, optional) – Path to the checkpoint. If None, it will try 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 or the device of the input model. Defaults to None.

示例

  1. Use a pre-trained model in MMClassification to inference an image.

    >>> from mmcls import ImageClassificationInferencer
    >>> inferencer = ImageClassificationInferencer('resnet50_8xb32_in1k')
    >>> inferencer('demo/demo.JPEG')
    [{'pred_score': array([...]),
      'pred_label': 65,
      'pred_score': 0.6649367809295654,
      'pred_class': 'sea snake'}]
    
  2. Use a config file and checkpoint to inference multiple images on GPU, and save the visualization results in a folder.

    >>> from mmcls import ImageClassificationInferencer
    >>> inferencer = ImageClassificationInferencer(
            model='configs/resnet/resnet50_8xb32_in1k.py',
            weights='https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_8xb32_in1k_20210831-ea4938fc.pth',
            device='cuda')
    >>> inferencer(['demo/dog.jpg', 'demo/bird.JPEG'], show_dir="./visualize/")
    
__call__(inputs, return_datasamples=False, batch_size=1, **kwargs)[源代码]

Call the inferencer.

参数
  • inputs (InputsType) – Inputs for the inferencer.

  • return_datasamples (bool) – Whether to return results as BaseDataElement. Defaults to False.

  • batch_size (int) – Batch size. Defaults to 1.

  • rescale_factor (float, optional) – Rescale the image by the rescale factor for visualization. This is helpful when the image is too large or too small for visualization. Defaults to None.

  • draw_score (bool) – Whether to draw the prediction scores of prediction categories. Defaults to True.

  • show (bool) – Whether to display the visualization result in a window. Defaults to False.

  • show_dir (str, optional) – If not None, save the visualization results in the specified directory. Defaults to None.

返回

The inference results.

返回类型

list

static list_models(pattern=None)[源代码]

List all available model names.

参数

pattern (str | None) – A wildcard pattern to match model names.

返回

a list of model names.

返回类型

List[str]

Read the Docs v: mmcls-1.x
Versions
latest
stable
mmcls-1.x
mmcls-0.x
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.