logger.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import inspect
  3. from mmengine.logging import print_log
  4. def get_caller_name():
  5. """Get name of caller method."""
  6. # this_func_frame = inspect.stack()[0][0] # i.e., get_caller_name
  7. # callee_frame = inspect.stack()[1][0] # e.g., log_img_scale
  8. caller_frame = inspect.stack()[2][0] # e.g., caller of log_img_scale
  9. caller_method = caller_frame.f_code.co_name
  10. try:
  11. caller_class = caller_frame.f_locals['self'].__class__.__name__
  12. return f'{caller_class}.{caller_method}'
  13. except KeyError: # caller is a function
  14. return caller_method
  15. def log_img_scale(img_scale, shape_order='hw', skip_square=False):
  16. """Log image size.
  17. Args:
  18. img_scale (tuple): Image size to be logged.
  19. shape_order (str, optional): The order of image shape.
  20. 'hw' for (height, width) and 'wh' for (width, height).
  21. Defaults to 'hw'.
  22. skip_square (bool, optional): Whether to skip logging for square
  23. img_scale. Defaults to False.
  24. Returns:
  25. bool: Whether to have done logging.
  26. """
  27. if shape_order == 'hw':
  28. height, width = img_scale
  29. elif shape_order == 'wh':
  30. width, height = img_scale
  31. else:
  32. raise ValueError(f'Invalid shape_order {shape_order}.')
  33. if skip_square and (height == width):
  34. return False
  35. caller = get_caller_name()
  36. print_log(
  37. f'image shape: height={height}, width={width} in {caller}',
  38. logger='current')
  39. return True