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.

image_ops.h 15 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef GE_OP_MAGE_OPS_H_
  17. #define GE_OP_MAGE_OPS_H_
  18. #include "graph/operator_reg.h"
  19. namespace ge {
  20. REG_OP(AdjustHue)
  21. .INPUT(images, TensorType({DT_FLOAT}))
  22. .INPUT(delta, TensorType({DT_FLOAT}))
  23. .OUTPUT(y, TensorType({DT_FLOAT}))
  24. .OP_END_FACTORY_REG(AdjustHue)
  25. REG_OP(AdjustSaturation)
  26. .INPUT(images, TensorType({DT_FLOAT}))
  27. .INPUT(scale, TensorType({DT_FLOAT}))
  28. .OUTPUT(y, TensorType({DT_FLOAT}))
  29. .OP_END_FACTORY_REG(AdjustSaturation)
  30. REG_OP(AdjustContrast)
  31. .INPUT(images, TensorType({DT_FLOAT}))
  32. .INPUT(contrast_factor, TensorType({DT_FLOAT}))
  33. .OUTPUT(y, TensorType({DT_FLOAT}))
  34. .OP_END_FACTORY_REG(AdjustContrast)
  35. REG_OP(CropAndResize)
  36. .INPUT(images, TensorType({DT_UINT8, DT_UINT16, DT_INT8, \
  37. DT_INT16, DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  38. .INPUT(boxes, TensorType({DT_FLOAT}))
  39. .INPUT(box_index, TensorType({DT_INT32}))
  40. .INPUT(crop_size, TensorType({DT_INT32}))
  41. .OUTPUT(y, TensorType({DT_FLOAT}))
  42. .ATTR(extrapolation_value, Float, 0)
  43. .ATTR(method, String, "bilinear")
  44. .OP_END_FACTORY_REG(CropAndResize)
  45. REG_OP(CropAndResizeGradBoxes)
  46. .INPUT(grads, TensorType({DT_FLOAT}))
  47. .INPUT(images, TensorType({DT_UINT8, DT_UINT16, DT_INT8, DT_INT16, \
  48. DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  49. .INPUT(boxes, TensorType({DT_FLOAT}))
  50. .INPUT(box_index, TensorType({DT_INT32}))
  51. .OUTPUT(y, TensorType({DT_FLOAT}))
  52. .ATTR(method, String, "bilinear")
  53. .OP_END_FACTORY_REG(CropAndResizeGradBoxes)
  54. REG_OP(CropAndResizeGradImage)
  55. .INPUT(grads, TensorType({DT_FLOAT}))
  56. .INPUT(boxes, TensorType({DT_FLOAT}))
  57. .INPUT(box_index, TensorType({DT_INT32}))
  58. .INPUT(image_size, TensorType({DT_INT32}))
  59. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  60. .ATTR(method, String, "bilinear")
  61. .REQUIRED_ATTR(T, Type)
  62. .OP_END_FACTORY_REG(CropAndResizeGradImage)
  63. REG_OP(ExtractGlimpse)
  64. .INPUT(x, TensorType({DT_FLOAT}))
  65. .INPUT(size, TensorType({DT_INT32}))
  66. .INPUT(offsets, TensorType({DT_FLOAT}))
  67. .OUTPUT(y, TensorType({DT_FLOAT}))
  68. .ATTR(centered, Bool, true)
  69. .ATTR(normalized, Bool, true)
  70. .ATTR(uniform_noise, Bool, true)
  71. .ATTR(noise, String, "uniform")
  72. .OP_END_FACTORY_REG(ExtractGlimpse)
  73. REG_OP(HSVToRGB)
  74. .INPUT(images, TensorType({ DT_FLOAT, DT_DOUBLE }))
  75. .OUTPUT(y, TensorType({ DT_FLOAT, DT_DOUBLE }))
  76. .OP_END_FACTORY_REG(HSVToRGB)
  77. REG_OP(QuantizedResizeBilinear)
  78. .INPUT(images, TensorType({ DT_FLOAT }))
  79. .INPUT(size, TensorType({ DT_INT32 }))
  80. .INPUT(min, TensorType({ DT_FLOAT }))
  81. .INPUT(max, TensorType({ DT_FLOAT }))
  82. .OUTPUT(resized_images, TensorType({ DT_FLOAT }))
  83. .OUTPUT(y_min, TensorType({ DT_FLOAT }))
  84. .OUTPUT(y_max, TensorType({ DT_FLOAT }))
  85. .ATTR(align_corners, Bool, false)
  86. .ATTR(half_pixel_centers, Bool, false)
  87. .OP_END_FACTORY_REG(QuantizedResizeBilinear)
  88. REG_OP(ResizeArea)
  89. .INPUT(images, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  90. DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  91. .INPUT(size, TensorType({DT_INT32}))
  92. .OUTPUT(y, TensorType({DT_FLOAT}))
  93. .ATTR(align_corners, Bool, false)
  94. .OP_END_FACTORY_REG(ResizeArea)
  95. REG_OP(ResizeBicubicGrad)
  96. .INPUT(grads, TensorType({DT_FLOAT}))
  97. .INPUT(original_image, TensorType({DT_FLOAT, DT_DOUBLE}))
  98. .OUTPUT(y, TensorType({DT_FLOAT, DT_DOUBLE}))
  99. .ATTR(align_corners, Bool, false)
  100. .ATTR(half_pixel_centers, Bool, false)
  101. .OP_END_FACTORY_REG(ResizeBicubicGrad)
  102. REG_OP(ResizeBicubic)
  103. .INPUT(images, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, \
  104. DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  105. .INPUT(size, TensorType({DT_INT32}))
  106. .OUTPUT(y, TensorType({DT_FLOAT}))
  107. .ATTR(align_corners, Bool, false)
  108. .ATTR(half_pixel_centers, Bool, false)
  109. .OP_END_FACTORY_REG(ResizeBicubic)
  110. /**
  111. *@brief Performs the backpropagation of ResizeNearestNeighbor for training scenarios.
  112. *@par Inputs:
  113. * Two inputs, including:
  114. *@li grads: A 4D Tensor, specifying the backpropagation gradients. Must be one of the following types: int8, uint8, int16, uint16, int32, int64, float16, float32, float64.
  115. *@li size: A 1D Tensor of type int32, specifying the source image size (orig_height, orig_width).
  116. *@par Attributes: \n
  117. *align_corners: An optional bool. If "True", the centers of the corner pixels of the input and gradient tensors are aligned. Defaults to "False".
  118. *@par Outputs: \n
  119. *y: A 4D Tensor, specifying the backpropagation gradient after computation. Has the same type as "grads".
  120. *@attention Constraints:
  121. * When the inputs are of type float32, the execution performance is high.
  122. *@see ResizeNearestNeighbor
  123. */
  124. REG_OP(ResizeNearestNeighborGrad)
  125. .INPUT(grads, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32,
  126. DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  127. .INPUT(size, TensorType({DT_INT32}))
  128. .OUTPUT(y, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32,
  129. DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  130. .ATTR(align_corners, Bool, false)
  131. .ATTR(half_pixel_centers, Bool, false)
  132. .OP_END_FACTORY_REG(ResizeNearestNeighborGrad)
  133. REG_OP(ResizeNearestNeighborGradD)
  134. .INPUT(grads, TensorType({DT_FLOAT}))
  135. .OUTPUT(y, TensorType({DT_FLOAT}))
  136. .REQUIRED_ATTR(size, ListInt)
  137. .ATTR(align_corners, Bool, false)
  138. .OP_END_FACTORY_REG(ResizeNearestNeighborGradD)
  139. /**
  140. *@brief Performs the backpropagation of ResizeBilinear, which is used to resize an image\n to a specified size, while this operator is used to restore the resized image to the original image.
  141. *@par Inputs:
  142. * Two inputs, including:
  143. * @li grads: A float32 input in NC1HWC0 format, describing the image information after resizing,\n including the image height, width, number of channels, and number of images.
  144. * @li original_image: A float32 input in NC1HWC0 format, describing the image information before resizing,\n including the image height, width, number of channels, and number of images.
  145. *@par Attributes:
  146. *align_corners: An optional bool. If "True", the centers of the corner pixels of the input and\n gradient tensors are aligned. Defaults to "False".
  147. *@par Outputs:
  148. *y: A float32 output in NC1HWC0 format, specifying the image information before resizing, including the image height,\n
  149. width, number of channels, and number of images.
  150. */
  151. REG_OP(ResizeBilinearGrad)
  152. .INPUT(grads, TensorType({DT_FLOAT}))
  153. .INPUT(original_image, TensorType::FloatingDataType())
  154. .OUTPUT(y, TensorType({DT_FLOAT}))
  155. .ATTR(align_corners, Bool, false)
  156. .OP_END_FACTORY_REG(ResizeBilinearGrad)
  157. /**
  158. *@brief Resizes "images" to "size" using bilinear interpolation.
  159. *@par Inputs:
  160. * Two inputs, including:
  161. *@li images: An NC1HWC0 Tensor.
  162. * Must be one of the following types: int8, uint8, int16, uint16, int32, int64, float16, float32, double
  163. *@li size: An ND Tensor of type int32.
  164. *@par Attributes:
  165. *align_corners: An optional bool. If "true", the centers of the corner pixels of the input and output tensors are aligned. Defaults to "false".
  166. *@par Outputs:
  167. *y: A Tensor with the same format as input "images".
  168. */
  169. REG_OP(ResizeBilinear)
  170. .INPUT(images, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16,
  171. DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  172. .INPUT(size, TensorType({DT_INT32}))
  173. .OUTPUT(y, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16,
  174. DT_INT32, DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  175. .ATTR(align_corners, Bool, false)
  176. .OP_END_FACTORY_REG(ResizeBilinear)
  177. REG_OP(RGBToHSV)
  178. .INPUT(images, TensorType({ DT_FLOAT, DT_DOUBLE }))
  179. .OUTPUT(y, TensorType({ DT_FLOAT, DT_DOUBLE }))
  180. .OP_END_FACTORY_REG(RGBToHSV)
  181. REG_OP(SampleDistortedBoundingBoxExt2)
  182. .INPUT(image_size, TensorType({ DT_UINT8, DT_INT8, DT_INT16, \
  183. DT_INT32, DT_INT64 }))
  184. .INPUT(bounding_boxes, TensorType({ DT_FLAOT }))
  185. .INPUT(min_object_covered, TensorType({ DT_FLOAT }))
  186. .OUTPUT(begin, TensorType({ DT_UINT8, DT_INT8, DT_INT16, \
  187. DT_INT32, DT_INT64 }))
  188. .OUTPUT(size, TensorType({ DT_UINT8, DT_INT8, DT_INT16, \
  189. DT_INT32, DT_INT64 }))
  190. .OUTPUT(bboxes, TensorType({ DT_FLOAT }))
  191. .ATTR(seed, Int, 0)
  192. .ATTR(seed2, Int, 0)
  193. .ATTR(aspect_ratio_range, ListFloat, { 0.75f, 1.33f })
  194. .ATTR(area_range, ListFloat, { 0.05f, 1.0f })
  195. .ATTR(max_attempts, Int, 100)
  196. .ATTR(use_image_if_no_bounding_boxes, Bool, false)
  197. .OP_END_FACTORY_REG(SampleDistortedBoundingBoxExt2)
  198. /**
  199. *@brief Resizes "images" to "size" using nearest neighbor interpolation.
  200. *@par Inputs:
  201. * Two inputs, including:
  202. *@li images: An NC1HWC0 Tensor.
  203. * Must be one of the following types: int8, uint8, int16, uint16, int32, int64, float16, float32, double
  204. *@li size: An ND Tensor of type int32.
  205. *@par Attributes:
  206. *align_corners: An optional bool. If "true", the centers of the corner pixels of the input and output tensors are aligned. Defaults to "false".
  207. *@par Outputs:
  208. *y: A Tensor with the same type and format as input "images".
  209. */
  210. REG_OP(ResizeNearestNeighbor)
  211. .INPUT(images, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32,
  212. DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  213. .INPUT(size, TensorType({DT_INT32}))
  214. .OUTPUT(y, TensorType({DT_INT8, DT_UINT8, DT_INT16, DT_UINT16, DT_INT32,
  215. DT_INT64, DT_FLOAT16, DT_FLOAT, DT_DOUBLE}))
  216. .ATTR(align_corners, Bool, false)
  217. .OP_END_FACTORY_REG(ResizeNearestNeighbor)
  218. REG_OP(DrawBoundingBoxes)
  219. .INPUT(images, TensorType({DT_FLOAT}))
  220. .INPUT(boxes, TensorType({DT_FLOAT}))
  221. .OUTPUT(y, TensorType({DT_FLOAT}))
  222. .OP_END_FACTORY_REG(DrawBoundingBoxes)
  223. REG_OP(NonMaxSuppression)
  224. .INPUT(boxes, TensorType({DT_FLOAT}))
  225. .INPUT(scores, TensorType({DT_FLOAT}))
  226. .INPUT(max_output_size, TensorType({DT_INT32}))
  227. .OUTPUT(selected_indices, TensorType({DT_INT32}))
  228. .ATTR(iou_threshold, Float, 0.5f)
  229. .OP_END_FACTORY_REG(NonMaxSuppression)
  230. REG_OP(NonMaxSuppressionV2)
  231. .INPUT(boxes, TensorType({DT_FLOAT16, DT_FLOAT}))
  232. .INPUT(scores, TensorType({DT_FLOAT16, DT_FLOAT}))
  233. .INPUT(max_output_size, TensorType({DT_INT32}))
  234. .INPUT(iou_threshold, TensorType({DT_FLOAT}))
  235. .OUTPUT(selected_indices, TensorType({DT_INT32}))
  236. .OP_END_FACTORY_REG(NonMaxSuppressionV2)
  237. REG_OP(NonMaxSuppressionV3)
  238. .INPUT(boxes, TensorType({DT_FLOAT16, DT_FLOAT}))
  239. .INPUT(scores, TensorType({DT_FLOAT16, DT_FLOAT}))
  240. .INPUT(max_output_size, TensorType({DT_INT32}))
  241. .INPUT(iou_threshold, TensorType({DT_FLOAT}))
  242. .INPUT(score_threshold, TensorType({DT_FLOAT}))
  243. .OUTPUT(selected_indices, TensorType({DT_INT32}))
  244. .OP_END_FACTORY_REG(NonMaxSuppressionV3)
  245. REG_OP(NonMaxSuppressionV4)
  246. .INPUT(boxes, TensorType({DT_FLOAT16, DT_FLOAT}))
  247. .INPUT(scores, TensorType({DT_FLOAT16, DT_FLOAT}))
  248. .INPUT(max_output_size, TensorType({DT_INT32}))
  249. .INPUT(iou_threshold, TensorType({DT_FLOAT}))
  250. .INPUT(score_threshold, TensorType({DT_FLOAT}))
  251. .OUTPUT(selected_indices, TensorType({DT_INT32}))
  252. .OUTPUT(valid_outputs, TensorType({DT_INT32}))
  253. .ATTR(pad_to_max_output_size, Bool, false)
  254. .OP_END_FACTORY_REG(NonMaxSuppressionV4)
  255. REG_OP(NonMaxSuppressionWithOverlaps)
  256. .INPUT(overlaps, TensorType({DT_FLOAT}))
  257. .INPUT(scores, TensorType({DT_FLOAT}))
  258. .INPUT(max_output_size, TensorType({DT_INT32}))
  259. .INPUT(overlap_threshold, TensorType({DT_FLOAT}))
  260. .INPUT(score_threshold, TensorType({DT_FLOAT}))
  261. .OUTPUT(selected_indices, TensorType({DT_INT32}))
  262. .OP_END_FACTORY_REG(NonMaxSuppressionWithOverlaps)
  263. REG_OP(EncodeJpeg)
  264. .INPUT(image, TensorType({DT_UINT8}))
  265. .OUTPUT(contents, TensorType({DT_STRING}))
  266. .ATTR(format, String, "")
  267. .ATTR(quality, Int, 95)
  268. .ATTR(progressive, Bool, false)
  269. .ATTR(optimize_size, Bool, false)
  270. .ATTR(chroma_downsampling, Bool, true)
  271. .ATTR(density_unit, String, "in")
  272. .ATTR(x_density, Int, 300)
  273. .ATTR(y_density, Int, 300)
  274. .ATTR(xmp_metadata, String, "")
  275. .OP_END_FACTORY_REG(EncodeJpeg)
  276. REG_OP(EncodePng)
  277. .INPUT(image, TensorType({DT_UINT8, DT_UINT16}))
  278. .OUTPUT(contents, TensorType({DT_STRING}))
  279. .ATTR(compression, Int, -1)
  280. .OP_END_FACTORY_REG(EncodePng)
  281. /**
  282. *@brief Resizes "images" to "size" using bilinear interpolation.
  283. *@par Inputs:
  284. * One input:
  285. *images: An NC1HWC0 Tensor. \n
  286. * Must be one of the following types: float16, float32.
  287. *@par Attributes:
  288. *@li size: A required int32 Tensor specifying the new size for the images. No default value.
  289. *@li align_corners: An optional bool. If "true", the centers of the corner pixels of the input and output tensors are aligned. Defaults to "false".
  290. *@par Outputs:
  291. *y: A Tensor with type float32 and the same format as input "images".
  292. *@attention Constraints:
  293. *@li The input "size" must be a tensor of 2 elements: size[0] <= 2048, size[1] <= 2048.
  294. *@li The input "images" must be a tensor of 5 elements: images[2] <= 2048, images[3] <= 2048.
  295. */
  296. REG_OP(ResizeBilinearD)
  297. .INPUT(images, TensorType({DT_FLOAT16, DT_FLOAT}))
  298. .OUTPUT(y, TensorType({DT_FLOAT16, DT_FLOAT}))
  299. .ATTR(align_corners, Bool, false)
  300. .REQUIRED_ATTR(size, ListInt)
  301. .OP_END_FACTORY_REG(ResizeBilinearD)
  302. /**
  303. *@brief Resizes "images" to "size" using nearest neighbor interpolation.
  304. *@par Inputs:
  305. * One input:
  306. *images: An NC1HWC0 Tensor. \n
  307. * Must be one of the following types: float16, float32, int32, int8, uint8
  308. *@par Attributes:
  309. *@li size: A required int32 Tensor specifying the new size for the images. No default value.
  310. *@li align_corners: An optional bool. If "true", the centers of the corner pixels of the input and output tensors are aligned. Defaults to "false".
  311. *@par Outputs:
  312. *y: A Tensor with the same type and format as input "images".
  313. *@attention Constraints:
  314. * The input "size" must be a tensor of 2 elements: size[0] <= 7680, size[1] <= 4320
  315. */
  316. REG_OP(ResizeNearestNeighborD)
  317. .INPUT(images, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  318. .OUTPUT(y, TensorType({DT_FLOAT16,DT_FLOAT,DT_INT32,DT_INT8,DT_UINT8}))
  319. .REQUIRED_ATTR(size, ListInt)
  320. .ATTR(align_corners, Bool, false)
  321. .OP_END_FACTORY_REG(ResizeNearestNeighborD)
  322. REG_OP(ExtractJpegShape)
  323. .INPUT(contents, TensorType({DT_STRING}))
  324. .OUTPUT(image_shape, TensorType({DT_INT32, DT_INT64}))
  325. .REQUIRED_ATTR(output_type, Type)
  326. .OP_END_FACTORY_REG(ExtractJpegShape)
  327. } // namespace ge
  328. #endif // GE_OP_MAGE_OPS_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示