mindformers.pipeline.ImageClassificationPipeline

class mindformers.pipeline.ImageClassificationPipeline(model: Union[str, mindformers.models.base_model.BaseModel, mindspore.train.model.Model], image_processor: Optional[mindformers.models.base_processor.BaseImageProcessor] = None, **kwargs)[源代码]

Pipeline for image classification

参数
  • model (Union[str, BaseModel]) – The model used to perform task, the input could be a supported model name, or a model instance inherited from BaseModel.

  • image_processor (Optional[BaseImageProcessor]) – The image_processor of model, it could be None if the model do not need image_processor.

引发
  • TypeError – If input model and image_processor’s types are not corrected.

  • ValueError – If the input model is not in support list.

实际案例

>>> import numpy as np
>>> from mindformers.pipeline import ImageClassificationPipeline
>>> from mindformers import ViTImageProcessor
>>> processor = ViTImageProcessor(size=224)
>>> classifier = ImageClassificationPipeline(
...     model='vit_base_p16',
...     image_processor=processor,
...     top_k=5
...     )
>>> classifier(np.uint8(np.random.random((5, 3, 255, 255))))
    [[{'score': 0.0016654134, 'label': 'matchstick'},
    {'score': 0.0015071577, 'label': 'theater curtain'},
    {'score': 0.0014839625, 'label': 'ocarina'},
    {'score': 0.0014319294, 'label': 'abaya'},
    {'score': 0.0014109017, 'label': 'bottlecap'}],
    ..., {'score': 0.0014109018, 'label': 'bottlecap'}]]
forward(model_inputs: dict, **forward_params)[源代码]

The Forward Process of Model

参数
  • model_inputs (dict) – The output of preprocess.

  • forward_params (dict) – The parameter dict for model forward.

postprocess(model_outputs, **postprocess_params)[源代码]

Postprocess

参数
  • model_outputs (dict) – Outputs of forward process.

  • top_k (int) – Return top_k probs of result.

返回

classification results.

preprocess(inputs: Union[str, PIL.Image.Image, mindspore.common.tensor.Tensor, numpy.ndarray], **preprocess_params)[源代码]

The Preprocess For Task

参数
  • inputs (Union[url, PIL.Image, tensor, numpy]) – The image to be classified.

  • preprocess_params (dict) – The parameter dict for preprocess.

返回

Processed image.