本文尝试解释mmdet中的Normalize
mmdet中的Normalize使用的是MMCV中的imnormalize函数实现的

路径如下:

mmdetection/mmdet/datasets/pipelines/transforms.pymmdetection/mmdet/datasets/pipelines/transforms.py

  1. @PIPELINES.register_module()
  2. class Normalize:
  3. """Normalize the image.
  4. Added key is "img_norm_cfg".
  5. Args:
  6. mean (sequence): Mean values of 3 channels.
  7. std (sequence): Std values of 3 channels.
  8. to_rgb (bool): Whether to convert the image from BGR to RGB,
  9. default is true.
  10. """
  11. def __init__(self, mean, std, to_rgb=True):
  12. self.mean = np.array(mean, dtype=np.float32)
  13. self.std = np.array(std, dtype=np.float32)
  14. self.to_rgb = to_rgb
  15. def __call__(self, results):
  16. """Call function to normalize images.
  17. Args:
  18. results (dict): Result dict from loading pipeline.
  19. Returns:
  20. dict: Normalized results, 'img_norm_cfg' key is added into
  21. result dict.
  22. """
  23. for key in results.get('img_fields', ['img']):
  24. results[key] = mmcv.imnormalize(results[key], self.mean, self.std,
  25. self.to_rgb)
  26. results['img_norm_cfg'] = dict(
  27. mean=self.mean, std=self.std, to_rgb=self.to_rgb)
  28. return results
  29. def __repr__(self):
  30. repr_str = self.__class__.__name__
  31. repr_str += f'(mean={self.mean}, std={self.std}, to_rgb={self.to_rgb})'
  32. return repr_str

MMCV中的normalize函数

Normalize an image with mean and std.
mmcv的imnormalize是使用opencv实现的,通过cv2.subtract(img, mean, img)

  1. def imnormalize(img, mean, std, to_rgb=True):
  2. """Normalize an image with mean and std.
  3. Args:
  4. img (ndarray): Image to be normalized.
  5. mean (ndarray): The mean to be used for normalize.
  6. std (ndarray): The std to be used for normalize.
  7. to_rgb (bool): Whether to convert to rgb.
  8. Returns:
  9. ndarray: The normalized image.
  10. """
  11. img = img.copy().astype(np.float32)
  12. return imnormalize_(img, mean, std, to_rgb)
  13. def imnormalize_(img, mean, std, to_rgb=True):
  14. """Inplace normalize an image with mean and std.
  15. Args:
  16. img (ndarray): Image to be normalized.
  17. mean (ndarray): The mean to be used for normalize.
  18. std (ndarray): The std to be used for normalize.
  19. to_rgb (bool): Whether to convert to rgb.
  20. Returns:
  21. ndarray: The normalized image.
  22. """
  23. # cv2 inplace normalization does not accept uint8
  24. assert img.dtype != np.uint8
  25. mean = np.float64(mean.reshape(1, -1))
  26. stdinv = 1 / np.float64(std.reshape(1, -1))
  27. if to_rgb:
  28. cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) # inplace
  29. cv2.subtract(img, mean, img) # inplace
  30. cv2.multiply(img, stdinv, img) # inplace
  31. return img

Imdenormalize

  1. def imdenormalize(img, mean, std, to_bgr=True):
  2. assert img.dtype != np.uint8
  3. mean = mean.reshape(1, -1).astype(np.float64)
  4. std = std.reshape(1, -1).astype(np.float64)
  5. img = cv2.multiply(img, std) # make a copy
  6. cv2.add(img, mean, img) # inplace
  7. if to_bgr:
  8. cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img) # inplace
  9. return img