Shortcuts

ImgDataPreprocessor

class mmengine.model.ImgDataPreprocessor(mean=None, std=None, pad_size_divisor=1, pad_value=0, bgr_to_rgb=False, rgb_to_bgr=False, non_blocking=False)[source]

Image pre-processor for normalization and bgr to rgb conversion.

Accepts the data sampled by the dataloader, and preprocesses it into the format of the model input. ImgDataPreprocessor provides the basic data pre-processing as follows

  • Collates and moves data to the target device.

  • Converts inputs from bgr to rgb if the shape of input is (3, H, W).

  • Normalizes image with defined std and mean.

  • Pads inputs to the maximum size of current batch with defined pad_value. The padding size can be divisible by a defined pad_size_divisor

  • Stack inputs to batch_inputs.

For ImgDataPreprocessor, the dimension of the single inputs must be (3, H, W).

Note

ImgDataPreprocessor and its subclass is built in the constructor of BaseDataset.

Parameters:
  • mean (Sequence[float or int], optional) – The pixel mean of image channels. If bgr_to_rgb=True it means the mean value of R, G, B channels. If the length of mean is 1, it means all channels have the same mean value, or the input is a gray image. If it is not specified, images will not be normalized. Defaults None.

  • std (Sequence[float or int], optional) – The pixel standard deviation of image channels. If bgr_to_rgb=True it means the standard deviation of R, G, B channels. If the length of std is 1, it means all channels have the same standard deviation, or the input is a gray image. If it is not specified, images will not be normalized. Defaults None.

  • pad_size_divisor (int) – The size of padded image should be divisible by pad_size_divisor. Defaults to 1.

  • pad_value (float or int) – The padded pixel value. Defaults to 0.

  • bgr_to_rgb (bool) – whether to convert image from BGR to RGB. Defaults to False.

  • rgb_to_bgr (bool) – whether to convert image from RGB to RGB. Defaults to False.

  • non_blocking (bool) – Whether block current process when transferring data to device. New in version v0.3.0.

Note

if images do not need to be normalized, std and mean should be both set to None, otherwise both of them should be set to a tuple of corresponding values.

forward(data, training=False)[source]

Performs normalization, padding and bgr2rgb conversion based on BaseDataPreprocessor.

Parameters:
  • data (dict) – Data sampled from dataset. If the collate function of DataLoader is pseudo_collate, data will be a list of dict. If collate function is default_collate, data will be a tuple with batch input tensor and list of data samples.

  • training (bool) – Whether to enable training time augmentation. If subclasses override this method, they can perform different preprocessing strategies for training and testing based on the value of training.

Returns:

Data in the same format as the model input.

Return type:

dict or list