Shortcuts

BaseInferencer

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

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.

  • show_progress (bool) – Control whether to display the progress bar during the inference process. Defaults to True. New in version 0.7.4.

备注

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 (dict | tuple) –

返回类型:

Any

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.

返回类型:

dict

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.

返回类型:

dict

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.

返回类型:

List[np.ndarray]

Read the Docs v: latest
Versions
latest
stable
v0.10.3
v0.10.2
v0.10.1
v0.10.0
v0.9.1
v0.9.0
v0.8.5
v0.8.4
v0.8.3
v0.8.2
v0.8.1
v0.8.0
v0.7.4
v0.7.3
v0.7.2
v0.7.1
v0.7.0
v0.6.0
v0.5.0
v0.4.0
v0.3.0
v0.2.0
Downloads
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.