You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

functional.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import collections.abc
  10. import functools
  11. import random
  12. import cv2
  13. import numpy as np
  14. def wrap_keepdims(func):
  15. """Wraper to keep the dimension of input images unchanged"""
  16. @functools.wraps(func)
  17. def wrapper(image, *args, **kwargs):
  18. if len(image.shape) != 3:
  19. raise ValueError(
  20. "image must have 3 dims, but got {} dims".format(len(image.shape))
  21. )
  22. ret = func(image, *args, **kwargs)
  23. if len(ret.shape) == 2:
  24. ret = ret[:, :, np.newaxis]
  25. return ret
  26. return wrapper
  27. @wrap_keepdims
  28. def to_gray(image):
  29. r"""
  30. Change BGR format image's color space to gray
  31. :param image: Input BGR format image, with (H, W, C) shape
  32. :return: Gray format image, with (H, W, C) shape
  33. """
  34. return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  35. @wrap_keepdims
  36. def to_bgr(image):
  37. r"""
  38. Change gray format image's color space to BGR
  39. :param image: input Gray format image, with (H, W, C) shape
  40. :return: BGR format image, with (H, W, C) shape
  41. """
  42. return cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
  43. @wrap_keepdims
  44. def pad(input, size, value):
  45. r"""
  46. Pad input data with *value* and given *size*
  47. :param input: Input data, with (H, W, C) shape
  48. :param size: Padding size of input data, it could be integer or sequence.
  49. If it's an integer, the input data will be padded in four directions.
  50. If it's a sequence contains two integer, the bottom and right side
  51. of input data will be padded.
  52. If it's a sequence contains four integer, the top, bottom, left, right
  53. side of input data will be padded with given size.
  54. :param value: Padding value of data, could be a sequence of int or float.
  55. if it's float value, the dtype of image will be casted to float32 also.
  56. :return: Padded image
  57. """
  58. if isinstance(size, int):
  59. size = (size, size, size, size)
  60. elif isinstance(size, collections.abc.Sequence) and len(size) == 2:
  61. size = (0, size[0], 0, size[1])
  62. if np.array(value).dtype == float:
  63. input = input.astype(np.float32)
  64. return cv2.copyMakeBorder(input, *size, cv2.BORDER_CONSTANT, value=value)
  65. @wrap_keepdims
  66. def flip(image, flipCode):
  67. r"""
  68. Accordding to the flipCode (the type of flip), flip the input image
  69. :param image: Input image, with (H, W, C) shape
  70. :param flipCode: code that indicates the type of flip.
  71. 1 : Flip horizontally
  72. 0 : Flip vertically
  73. -1 : Flip horizontally and vertically
  74. :return: BGR format image, with (H, W, C) shape
  75. """
  76. return cv2.flip(image, flipCode=flipCode)
  77. @wrap_keepdims
  78. def resize(input, size, interpolation=cv2.INTER_LINEAR):
  79. r"""
  80. resize the input data to given size
  81. :param input: Input data, could be image or masks, with (H, W, C) shape
  82. :param size: Target size of input data, with (height, width) shape.
  83. :param interpolation: Interpolation method.
  84. :return: Resized data, with (H, W, C) shape
  85. """
  86. if len(size) != 2:
  87. raise ValueError("resize needs (h, w), but got {}".format(size))
  88. if isinstance(interpolation, collections.abc.Sequence):
  89. interpolation = random.choice(interpolation)
  90. return cv2.resize(input, size[::-1], interpolation=interpolation)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台