class mmengine.infer.BaseInferencer(model=None, weights=None, device=None, scope=None)[源代码]

Base inferencer for downstream tasks.

The BaseInferencer provides the standard workflow for inference as follows:

  1. Preprocess the input data by preprocess().

  2. Forward the data to the model by forward(). BaseInferencer assumes the model inherits from mmengine.models.BaseModel and will call model.test_step in forward() by default.

  3. Visualize the results by visualize().

  4. Postprocess and return the results by postprocess().

When we call the subclasses inherited from BaseInferencer (not overriding __call__), the workflow will be executed in order.

All subclasses of BaseInferencer could define the following class attributes for customization:

  • preprocess_kwargs: The keys of the kwargs that will be passed to preprocess().

  • forward_kwargs: The keys of the kwargs that will be passed to forward()

  • visualize_kwargs: The keys of the kwargs that will be passed to visualize()

  • postprocess_kwargs: The keys of the kwargs that will be passed to postprocess()

All attributes mentioned above should be a set of keys (strings), and each key should not be duplicated. Actually, __call__() will dispatch all the arguments to the corresponding methods according to the xxx_kwargs mentioned above, therefore, the key in sets should be unique to avoid ambiguous dispatching.


If subclasses defined the class attributes mentioned above with duplicated keys, an AssertionError will be raised during import process.

Subclasses inherited from BaseInferencer should implement _init_pipeline(), visualize() and postprocess():

  • _init_pipeline: Return a callable object to preprocess the input data.

  • visualize: Visualize the results returned by forward().

  • postprocess: Postprocess the results returned by forward() and visualize().

  • model (str, optional) – Path to the config file or the model name defined in metafile. Take the mmdet metafile as an example, the model could be retinanet_r18_fpn_1x_coco or its alias. If model is not specified, user must provide the weights saved by MMEngine which contains the config string. Defaults to None.

  • weights (str, optional) – Path to the checkpoint. If it is not specified and model is a model name of metafile, the weights will be loaded from metafile. Defaults to None.

  • device (str, optional) – Device to run inference. If None, the available device will be automatically used. Defaults to None.

  • scope (str, optional) – The scope of the model. Defaults to None.




Since Inferencer could be used to infer batch data, collate_fn should be defined. If collate_fn is not defined in config file, the collate_fn will be pseudo_collate by default.

forward(inputs, **kwargs)[源代码]

Feed the inputs to the model.


inputs (Union[dict, tuple]) –



static list_models(scope=None, patterns='.*')[源代码]

List models defined in metafile of corresponding packages.

  • scope (str, optional) – The scope to which the model belongs. Defaults to None.

  • patterns (str, optional) – Regular expressions for the searched models. Once matched with Alias or Name filed in metafile, corresponding model will be added to the return list. Defaults to ‘.*’.


Model dict with model name and its alias.



abstract postprocess(preds, visualization, return_datasample=False, **kwargs)[源代码]

Process the predictions and visualization results from forward and visualize.

This method should be responsible for the following tasks:

  1. Convert datasamples into a json-serializable dict if needed.

  2. Pack the predictions and visualization results and return them.

  3. Dump or log the predictions.

Customize your postprocess by overriding this method. Make sure postprocess will return a dict with visualization results and inference results.

  • preds (List[Dict]) – Predictions of the model.

  • visualization (np.ndarray) – Visualized predictions.

  • return_datasample (bool) – Whether to return results as datasamples. Defaults to False.


Inference and visualization results with key predictions and visualization

  • visualization (Any): Returned by visualize()

  • predictions (dict or DataSample): Returned by forward() and processed in postprocess(). If return_datasample=False, it usually should be a json-serializable dict containing only basic data elements such as strings and numbers.



preprocess(inputs, batch_size=1, **kwargs)[源代码]

Process the inputs into a model-feedable format.

Customize your preprocess by overriding this method. Preprocess should return an iterable object, of which each item will be used as the input of model.test_step.

BaseInferencer.preprocess will return an iterable chunked data, which will be used in __call__ like this:

def __call__(self, inputs, batch_size=1, **kwargs):
    chunked_data = self.preprocess(inputs, batch_size, **kwargs)
    for batch in chunked_data:
        preds = self.forward(batch, **kwargs)
  • inputs (InputsType) – Inputs given by user.

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


Any – Data processed by the pipeline and collate_fn.

abstract visualize(inputs, preds, show=False, **kwargs)[源代码]

Visualize predictions.

Customize your visualization by overriding this method. visualize should return visualization results, which could be np.ndarray or any other objects.

  • inputs (list) – Inputs preprocessed by _inputs_to_list().

  • preds (Any) – Predictions of the model.

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


Visualization results.



Read the Docs v: v0.7.0
On Read the Docs
Project Home

Free document hosting provided by Read the Docs.