Models — MMSegmentation 1.2.2 documentation (original) (raw)

We usually define a neural network in a deep learning task as a model, and this model is the core of an algorithm. MMEngine abstracts a unified model BaseModel to standardize the interfaces for training, testing and other processes. All models implemented by MMSegmentation inherit from BaseModel, and in MMSegmentation we implemented forward and added some functions for the semantic segmentation algorithm.

Common components

Segmentor

In MMSegmentation, we abstract the network architecture as a Segmentor, it is a model that contains all components of a network. We have already implemented EncoderDecoder and CascadeEncoderDecoder, which typically consist of Data preprocessor, Backbone, Decode head and Auxiliary head.

Data preprocessor

Data preprocessor is the part that copies data to the target device and preprocesses the data into the model input format.

Backbone

Backbone is the part that transforms an image to feature maps, such as a ResNet-50 without the last fully connected layer.

Neck

Neck is the part that connects the backbone and heads. It performs some refinements or reconfigurations on the raw feature maps produced by the backbone. An example is Feature Pyramid Network (FPN).

Decode head

Decode head is the part that transforms the feature maps into a segmentation mask, such as PSPNet.

Auxiliary head

Auxiliary head is an optional component that transforms the feature maps into segmentation masks which only used for computing auxiliary losses.

Basic interfaces

MMSegmentation wraps BaseModel and implements the BaseSegmentor class, which mainly provides the interfaces forward, train_step, val_step and test_step. The following will introduce these interfaces in detail.

forward

EncoderDecoder dataflow CascadeEncoderDecoder dataflow

The forward method returns losses or predictions of training, validation, testing, and a simple inference process.

The method should accept three modes: “tensor”, “predict” and “loss”:

Note: SegDataSample is a data structure interface of MMSegmentation, it is used as an interface between different components. SegDataSample implements the abstract data element mmengine.structures.BaseDataElement, please refer to the SegDataSample documentation and data element documentation in MMEngine for more information.

Note that this method doesn’t handle either backpropagation or optimizer updating, which are done in the method train_step.

Parameters:

Returns:

prediction modes

We briefly describe the fields of the model’s configuration in the config documentation, here we elaborate on the model.test_cfg field. model.test_cfg is used to control forward behavior, the forward method in "predict" mode can run in two modes:

train_step

The train_step method calls the forward interface of the loss mode to get the loss dict. The BaseModel class implements the default model training process including preprocessing, model forward propagation, loss calculation, optimization, and back-propagation.

Parameters:

Note: OptimWrapper provides a common interface for updating parameters, please refer to optimizer wrapper documentation in MMEngine for more information.

Returns:

train_step dataflow

val_step

The val_step method calls the forward interface of the predict mode and returns the prediction result, which is further passed to the process interface of the evaluator and the after_val_iter interface of the Hook.

Parameters:

Returns:

test_step/val_step dataflow

test_step

The BaseModel implements test_step the same as val_step.

Data Preprocessor

The SegDataPreProcessor implemented by MMSegmentation inherits from the BaseDataPreprocessor implemented by MMEngine and provides the functions of data preprocessing and copying data to the target device.

The runner carries the model to the specified device during the construction stage, while the data is carried to the specified device by the SegDataPreProcessor in train_step, val_step, and test_step, and the processed data is further passed to the model.

The parameters of the SegDataPreProcessor constructor:

The data will be processed as follows:

The parameters of the forward method:

The returns of the forward method: