本文尝试解释mmdet中的Normalize
mmdet中的Normalize使用的是MMCV中的imnormalize函数实现的
路径如下:
mmdetection/mmdet/datasets/pipelines/transforms.pymmdetection/mmdet/datasets/pipelines/transforms.py
@PIPELINES.register_module()class Normalize:"""Normalize the image.Added key is "img_norm_cfg".Args:mean (sequence): Mean values of 3 channels.std (sequence): Std values of 3 channels.to_rgb (bool): Whether to convert the image from BGR to RGB,default is true."""def __init__(self, mean, std, to_rgb=True):self.mean = np.array(mean, dtype=np.float32)self.std = np.array(std, dtype=np.float32)self.to_rgb = to_rgbdef __call__(self, results):"""Call function to normalize images.Args:results (dict): Result dict from loading pipeline.Returns:dict: Normalized results, 'img_norm_cfg' key is added intoresult dict."""for key in results.get('img_fields', ['img']):results[key] = mmcv.imnormalize(results[key], self.mean, self.std,self.to_rgb)results['img_norm_cfg'] = dict(mean=self.mean, std=self.std, to_rgb=self.to_rgb)return resultsdef __repr__(self):repr_str = self.__class__.__name__repr_str += f'(mean={self.mean}, std={self.std}, to_rgb={self.to_rgb})'return repr_str
MMCV中的normalize函数
Normalize an image with mean and std.
mmcv的imnormalize是使用opencv实现的,通过cv2.subtract(img, mean, img)
def imnormalize(img, mean, std, to_rgb=True):"""Normalize an image with mean and std.Args:img (ndarray): Image to be normalized.mean (ndarray): The mean to be used for normalize.std (ndarray): The std to be used for normalize.to_rgb (bool): Whether to convert to rgb.Returns:ndarray: The normalized image."""img = img.copy().astype(np.float32)return imnormalize_(img, mean, std, to_rgb)def imnormalize_(img, mean, std, to_rgb=True):"""Inplace normalize an image with mean and std.Args:img (ndarray): Image to be normalized.mean (ndarray): The mean to be used for normalize.std (ndarray): The std to be used for normalize.to_rgb (bool): Whether to convert to rgb.Returns:ndarray: The normalized image."""# cv2 inplace normalization does not accept uint8assert img.dtype != np.uint8mean = np.float64(mean.reshape(1, -1))stdinv = 1 / np.float64(std.reshape(1, -1))if to_rgb:cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplacecv2.subtract(img, mean, img) # inplacecv2.multiply(img, stdinv, img) # inplacereturn img
Imdenormalize
def imdenormalize(img, mean, std, to_bgr=True):assert img.dtype != np.uint8mean = mean.reshape(1, -1).astype(np.float64)std = std.reshape(1, -1).astype(np.float64)img = cv2.multiply(img, std) # make a copycv2.add(img, mean, img) # inplaceif to_bgr:cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img) # inplacereturn img
