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.

utils.py 9.0 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Interpolation Mode, Resampling Filters
  16. """
  17. from enum import Enum, IntEnum
  18. class Inter(IntEnum):
  19. """
  20. Interpolation Modes.
  21. Possible enumeration values are: Inter.NEAREST, Inter.ANTIALIAS, Inter.LINEAR, Inter.BILINEAR, Inter.CUBIC,
  22. Inter.BICUBIC, Inter.AREA, Inter.PILCUBIC.
  23. - Inter.NEAREST: means interpolation method is nearest-neighbor interpolation.
  24. - Inter.ANTIALIAS: means the interpolation method is antialias interpolation.
  25. - Inter.LINEAR: means interpolation method is bilinear interpolation, here is the same as Inter.BILINEAR.
  26. - Inter.BILINEAR: means interpolation method is bilinear interpolation.
  27. - Inter.CUBIC: means the interpolation method is bicubic interpolation, here is the same as Inter.BICUBIC.
  28. - Inter.BICUBIC: means the interpolation method is bicubic interpolation.
  29. - Inter.AREA: means interpolation method is pixel area interpolation.
  30. - Inter.PILCUBIC: means interpolation method is bicubic interpolation like implemented in pillow, input
  31. should be in 3 channels format.
  32. """
  33. NEAREST = 0
  34. ANTIALIAS = 1
  35. BILINEAR = LINEAR = 2
  36. BICUBIC = CUBIC = 3
  37. AREA = 4
  38. PILCUBIC = 5
  39. class Border(str, Enum):
  40. """
  41. Padding Mode, Border Type.
  42. Possible enumeration values are: Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC.
  43. - Border.CONSTANT: means it fills the border with constant values.
  44. - Border.EDGE: means it pads with the last value on the edge.
  45. - Border.REFLECT: means it reflects the values on the edge omitting the last value of edge.
  46. - Border.SYMMETRIC: means it reflects the values on the edge repeating the last value of edge.
  47. Note: This class derived from class str to support json serializable.
  48. """
  49. CONSTANT: str = "constant"
  50. EDGE: str = "edge"
  51. REFLECT: str = "reflect"
  52. SYMMETRIC: str = "symmetric"
  53. class ImageBatchFormat(IntEnum):
  54. """
  55. Data Format of images after batch operation.
  56. Possible enumeration values are: ImageBatchFormat.NHWC, ImageBatchFormat.NCHW.
  57. - ImageBatchFormat.NHWC: in orders like, batch N, height H, width W, channels C to store the data.
  58. - ImageBatchFormat.NCHW: in orders like, batch N, channels C, height H, width W to store the data.
  59. """
  60. NHWC = 0
  61. NCHW = 1
  62. class ConvertMode(IntEnum):
  63. """The color conversion code"""
  64. COLOR_BGR2BGRA = 0
  65. COLOR_RGB2RGBA = COLOR_BGR2BGRA
  66. COLOR_BGRA2BGR = 1
  67. COLOR_RGBA2RGB = COLOR_BGRA2BGR
  68. COLOR_BGR2RGBA = 2
  69. COLOR_RGB2BGRA = COLOR_BGR2RGBA
  70. COLOR_RGBA2BGR = 3
  71. COLOR_BGRA2RGB = COLOR_RGBA2BGR
  72. COLOR_BGR2RGB = 4
  73. COLOR_RGB2BGR = COLOR_BGR2RGB
  74. COLOR_BGRA2RGBA = 5
  75. COLOR_RGBA2BGRA = COLOR_BGRA2RGBA
  76. COLOR_BGR2GRAY = 6
  77. COLOR_RGB2GRAY = 7
  78. COLOR_GRAY2BGR = 8
  79. COLOR_GRAY2RGB = COLOR_GRAY2BGR
  80. COLOR_GRAY2BGRA = 9
  81. COLOR_GRAY2RGBA = COLOR_GRAY2BGRA
  82. COLOR_BGRA2GRAY = 10
  83. COLOR_RGBA2GRAY = 11
  84. class SliceMode(IntEnum):
  85. """
  86. Mode to Slice Tensor into multiple parts.
  87. Possible enumeration values are: SliceMode.PAD, SliceMode.DROP.
  88. - SliceMode.PAD: pad some pixels before slice the Tensor if needed.
  89. - SliceMode.DROP: drop remainder pixels before slice the Tensor if needed.
  90. """
  91. PAD = 0
  92. DROP = 1
  93. class AutoAugmentPolicy(IntEnum):
  94. """
  95. AutoAugment policy for different datasets.
  96. Possible enumeration values are: AutoAugmentPolicy.IMAGENET, AutoAugmentPolicy.CIFAR10,
  97. AutoAugmentPolicy.SVHN.
  98. Each policy contains 25 pairs of augmentation operations. When using AutoAugment, each image is randomly
  99. transformed with one of these operation pairs. Each pair has 2 different operations. The following shows
  100. all of these augmentation operations, including operation names with their probabilities and random params.
  101. - AutoAugmentPolicy.IMAGENET: dataset auto augment policy for ImageNet.
  102. Augmentation operations pair:
  103. [(("Posterize", 0.4, 8), ("Rotate", 0.6, 9)), (("Solarize", 0.6, 5), ("AutoContrast", 0.6, None)),
  104. (("Equalize", 0.8, None), ("Equalize", 0.6, None)), (("Posterize", 0.6, 7), ("Posterize", 0.6, 6)),
  105. (("Equalize", 0.4, None), ("Solarize", 0.2, 4)), (("Equalize", 0.4, None), ("Rotate", 0.8, 8)),
  106. (("Solarize", 0.6, 3), ("Equalize", 0.6, None)), (("Posterize", 0.8, 5), ("Equalize", 1.0, None)),
  107. (("Rotate", 0.2, 3), ("Solarize", 0.6, 8)), (("Equalize", 0.6, None), ("Posterize", 0.4, 6)),
  108. (("Rotate", 0.8, 8), ("Color", 0.4, 0)), (("Rotate", 0.4, 9), ("Equalize", 0.6, None)),
  109. (("Equalize", 0.0, None), ("Equalize", 0.8, None)), (("Invert", 0.6, None), ("Equalize", 1.0, None)),
  110. (("Color", 0.6, 4), ("Contrast", 1.0, 8)), (("Rotate", 0.8, 8), ("Color", 1.0, 2)),
  111. (("Color", 0.8, 8), ("Solarize", 0.8, 7)), (("Sharpness", 0.4, 7), ("Invert", 0.6, None)),
  112. (("ShearX", 0.6, 5), ("Equalize", 1.0, None)), (("Color", 0.4, 0), ("Equalize", 0.6, None)),
  113. (("Equalize", 0.4, None), ("Solarize", 0.2, 4)), (("Solarize", 0.6, 5), ("AutoContrast", 0.6, None)),
  114. (("Invert", 0.6, None), ("Equalize", 1.0, None)), (("Color", 0.6, 4), ("Contrast", 1.0, 8)),
  115. (("Equalize", 0.8, None), ("Equalize", 0.6, None))]
  116. - AutoAugmentPolicy.CIFAR10: dataset auto augment policy for Cifar10.
  117. Augmentation operations pair:
  118. [(("Invert", 0.1, None), ("Contrast", 0.2, 6)), (("Rotate", 0.7, 2), ("TranslateX", 0.3, 9)),
  119. (("Sharpness", 0.8, 1), ("Sharpness", 0.9, 3)), (("ShearY", 0.5, 8), ("TranslateY", 0.7, 9)),
  120. (("AutoContrast", 0.5, None), ("Equalize", 0.9, None)), (("ShearY", 0.2, 7), ("Posterize", 0.3, 7)),
  121. (("Color", 0.4, 3), ("Brightness", 0.6, 7)), (("Sharpness", 0.3, 9), ("Brightness", 0.7, 9)),
  122. (("Equalize", 0.6, None), ("Equalize", 0.5, None)), (("Contrast", 0.6, 7), ("Sharpness", 0.6, 5)),
  123. (("Color", 0.7, 7), ("TranslateX", 0.5, 8)), (("Equalize", 0.3, None), ("AutoContrast", 0.4, None)),
  124. (("TranslateY", 0.4, 3), ("Sharpness", 0.2, 6)), (("Brightness", 0.9, 6), ("Color", 0.2, 8)),
  125. (("Solarize", 0.5, 2), ("Invert", 0.0, None)), (("Equalize", 0.2, None), ("AutoContrast", 0.6, None)),
  126. (("Equalize", 0.2, None), ("Equalize", 0.6, None)), (("Color", 0.9, 9), ("Equalize", 0.6, None)),
  127. (("AutoContrast", 0.8, None), ("Solarize", 0.2, 8)), (("Brightness", 0.1, 3), ("Color", 0.7, 0)),
  128. (("Solarize", 0.4, 5), ("AutoContrast", 0.9, None)), (("TranslateY", 0.9, 9), ("TranslateY", 0.7, 9)),
  129. (("AutoContrast", 0.9, None), ("Solarize", 0.8, 3)), (("Equalize", 0.8, None), ("Invert", 0.1, None)),
  130. (("TranslateY", 0.7, 9), ("AutoContrast", 0.9, None))]
  131. - AutoAugmentPolicy.SVHN: dataset auto augment policy for SVHN.
  132. Augmentation operations pair:
  133. [(("ShearX", 0.9, 4), ("Invert", 0.2, None)), (("ShearY", 0.9, 8), ("Invert", 0.7, None)),
  134. (("Equalize", 0.6, None), ("Solarize", 0.6, 6)), (("Invert", 0.9, None), ("Equalize", 0.6, None)),
  135. (("Equalize", 0.6, None), ("Rotate", 0.9, 3)), (("ShearX", 0.9, 4), ("AutoContrast", 0.8, None)),
  136. (("ShearY", 0.9, 8), ("Invert", 0.4, None)), (("ShearY", 0.9, 5), ("Solarize", 0.2, 6)),
  137. (("Invert", 0.9, None), ("AutoContrast", 0.8, None)), (("Equalize", 0.6, None), ("Rotate", 0.9, 3)),
  138. (("ShearX", 0.9, 4), ("Solarize", 0.3, 3)), (("ShearY", 0.8, 8), ("Invert", 0.7, None)),
  139. (("Equalize", 0.9, None), ("TranslateY", 0.6, 6)), (("Invert", 0.9, None), ("Equalize", 0.6, None)),
  140. (("Contrast", 0.3, 3), ("Rotate", 0.8, 4)), (("Invert", 0.8, None), ("TranslateY", 0.0, 2)),
  141. (("ShearY", 0.7, 6), ("Solarize", 0.4, 8)), (("Invert", 0.6, None), ("Rotate", 0.8, 4)),
  142. (("ShearY", 0.3, 7), ("TranslateX", 0.9, 3)), (("ShearX", 0.1, 6), ("Invert", 0.6, None)),
  143. (("Solarize", 0.7, 2), ("TranslateY", 0.6, 7)), (("ShearY", 0.8, 4), ("Invert", 0.8, None)),
  144. (("ShearX", 0.7, 9), ("TranslateY", 0.8, 3)), (("ShearY", 0.8, 5), ("AutoContrast", 0.7, None)),
  145. (("ShearX", 0.7, 2), ("Invert", 0.1, None))]
  146. """
  147. IMAGENET = 0
  148. CIFAR10 = 1
  149. SVHN = 2