ops.py 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. import numpy as np
  15. import paddle
  16. from paddle import _C_ops
  17. from paddle.tensor.math import _add_with_axis
  18. from paddle.utils import convert_to_list
  19. from ..base import core
  20. from ..base.data_feeder import check_type, check_variable_and_dtype
  21. from ..base.framework import (
  22. Variable,
  23. convert_np_dtype_to_dtype_,
  24. in_dygraph_mode,
  25. in_dynamic_or_pir_mode,
  26. in_pir_mode,
  27. )
  28. from ..base.layer_helper import LayerHelper
  29. from ..framework import _current_expected_place
  30. from ..nn import BatchNorm2D, Conv2D, Layer, ReLU, Sequential
  31. from ..nn.initializer import Normal
  32. __all__ = [
  33. 'yolo_loss',
  34. 'yolo_box',
  35. 'prior_box',
  36. 'box_coder',
  37. 'deform_conv2d',
  38. 'DeformConv2D',
  39. 'distribute_fpn_proposals',
  40. 'generate_proposals',
  41. 'read_file',
  42. 'decode_jpeg',
  43. 'roi_pool',
  44. 'RoIPool',
  45. 'psroi_pool',
  46. 'PSRoIPool',
  47. 'roi_align',
  48. 'RoIAlign',
  49. 'nms',
  50. 'matrix_nms',
  51. ]
  52. def yolo_loss(
  53. x,
  54. gt_box,
  55. gt_label,
  56. anchors,
  57. anchor_mask,
  58. class_num,
  59. ignore_thresh,
  60. downsample_ratio,
  61. gt_score=None,
  62. use_label_smooth=True,
  63. name=None,
  64. scale_x_y=1.0,
  65. ):
  66. r"""
  67. This operator generates YOLOv3 loss based on given predict result and ground
  68. truth boxes.
  69. The output of previous network is in shape [N, C, H, W], while H and W
  70. should be the same, H and W specify the grid size, each grid point predict
  71. given number bounding boxes, this given number, which following will be represented as S,
  72. is specified by the number of anchor clusters in each scale. In the second dimension(the channel
  73. dimension), C should be equal to S * (class_num + 5), class_num is the object
  74. category number of source dataset(such as 80 in coco dataset), so in the
  75. second(channel) dimension, apart from 4 box location coordinates x, y, w, h,
  76. also includes confidence score of the box and class one-hot key of each anchor box.
  77. Assume the 4 location coordinates are :math:`t_x, t_y, t_w, t_h`, the box predictions
  78. should be as follows:
  79. $$
  80. b_x = \\sigma(t_x) + c_x
  81. $$
  82. $$
  83. b_y = \\sigma(t_y) + c_y
  84. $$
  85. $$
  86. b_w = p_w e^{t_w}
  87. $$
  88. $$
  89. b_h = p_h e^{t_h}
  90. $$
  91. In the equation above, :math:`c_x, c_y` is the left top corner of current grid
  92. and :math:`p_w, p_h` is specified by anchors.
  93. As for confidence score, it is the logistic regression value of IoU between
  94. anchor boxes and ground truth boxes, the score of the anchor box which has
  95. the max IoU should be 1, and if the anchor box has IoU bigger than ignore
  96. thresh, the confidence score loss of this anchor box will be ignored.
  97. Therefore, the YOLOv3 loss consists of three major parts: box location loss,
  98. objectness loss and classification loss. The L1 loss is used for
  99. box coordinates (w, h), sigmoid cross entropy loss is used for box
  100. coordinates (x, y), objectness loss and classification loss.
  101. Each ground truth box finds a best matching anchor box in all anchors.
  102. Prediction of this anchor box will incur all three parts of losses, and
  103. prediction of anchor boxes with no GT box matched will only incur objectness
  104. loss.
  105. In order to trade off box coordinate losses between big boxes and small
  106. boxes, box coordinate losses will be multiplied by scale weight, which is
  107. calculated as follows.
  108. $$
  109. weight_{box} = 2.0 - t_w * t_h
  110. $$
  111. Final loss will be represented as follows.
  112. $$
  113. loss = (loss_{xy} + loss_{wh}) * weight_{box} + loss_{conf} + loss_{class}
  114. $$
  115. While :attr:`use_label_smooth` is set to be :attr:`True`, the classification
  116. target will be smoothed when calculating classification loss, target of
  117. positive samples will be smoothed to :math:`1.0 - 1.0 / class\_num` and target of
  118. negative samples will be smoothed to :math:`1.0 / class\_num`.
  119. While :attr:`gt_score` is given, which means the mixup score of ground truth
  120. boxes, all losses incurred by a ground truth box will be multiplied by its
  121. mixup score.
  122. Args:
  123. x (Tensor): The input tensor of YOLOv3 loss operator, This is a 4-D
  124. tensor with shape of [N, C, H, W]. H and W should be same,
  125. and the second dimension(C) stores box locations, confidence
  126. score and classification one-hot keys of each anchor box.
  127. The data type is float32 or float64.
  128. gt_box (Tensor): ground truth boxes, should be in shape of [N, B, 4],
  129. in the third dimension, x, y, w, h should be stored.
  130. x,y is the center coordinate of boxes, w, h are the
  131. width and height, x, y, w, h should be divided by
  132. input image height to scale to [0, 1].
  133. N is the batch number and B is the max box number in
  134. an image.The data type is float32 or float64.
  135. gt_label (Tensor): class id of ground truth boxes, should be in shape
  136. of [N, B].The data type is int32.
  137. anchors (list|tuple): The anchor width and height, it will be parsed
  138. pair by pair.
  139. anchor_mask (list|tuple): The mask index of anchors used in current
  140. YOLOv3 loss calculation.
  141. class_num (int): The number of classes.
  142. ignore_thresh (float): The ignore threshold to ignore confidence loss.
  143. downsample_ratio (int): The downsample ratio from network input to YOLOv3
  144. loss input, so 32, 16, 8 should be set for the
  145. first, second, and third YOLOv3 loss operators.
  146. gt_score (Tensor, optional): mixup score of ground truth boxes, should be in shape
  147. of [N, B]. Default None.
  148. use_label_smooth (bool, optional): Whether to use label smooth. Default True.
  149. name (str, optional): The default value is None. Normally there is no need
  150. for user to set this property. For more information,
  151. please refer to :ref:`api_guide_Name`
  152. scale_x_y (float, optional): Scale the center point of decoded bounding box.
  153. Default 1.0.
  154. Returns:
  155. Tensor: A 1-D tensor with shape [N], the value of yolov3 loss
  156. Examples:
  157. .. code-block:: python
  158. >>> import paddle
  159. >>> x = paddle.rand([2, 14, 8, 8]).astype('float32')
  160. >>> gt_box = paddle.rand([2, 10, 4]).astype('float32')
  161. >>> gt_label = paddle.rand([2, 10]).astype('int32')
  162. >>> loss = paddle.vision.ops.yolo_loss(x,
  163. ... gt_box=gt_box,
  164. ... gt_label=gt_label,
  165. ... anchors=[10, 13, 16, 30],
  166. ... anchor_mask=[0, 1],
  167. ... class_num=2,
  168. ... ignore_thresh=0.7,
  169. ... downsample_ratio=8,
  170. ... use_label_smooth=True,
  171. ... scale_x_y=1.)
  172. """
  173. if in_dynamic_or_pir_mode():
  174. loss = _C_ops.yolo_loss(
  175. x,
  176. gt_box,
  177. gt_label,
  178. gt_score,
  179. anchors,
  180. anchor_mask,
  181. class_num,
  182. ignore_thresh,
  183. downsample_ratio,
  184. use_label_smooth,
  185. scale_x_y,
  186. )
  187. return loss
  188. else:
  189. helper = LayerHelper('yolov3_loss', **locals())
  190. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'yolo_loss')
  191. check_variable_and_dtype(
  192. gt_box, 'gt_box', ['float32', 'float64'], 'yolo_loss'
  193. )
  194. check_variable_and_dtype(gt_label, 'gt_label', 'int32', 'yolo_loss')
  195. check_type(anchors, 'anchors', (list, tuple), 'yolo_loss')
  196. check_type(anchor_mask, 'anchor_mask', (list, tuple), 'yolo_loss')
  197. check_type(class_num, 'class_num', int, 'yolo_loss')
  198. check_type(ignore_thresh, 'ignore_thresh', float, 'yolo_loss')
  199. check_type(use_label_smooth, 'use_label_smooth', bool, 'yolo_loss')
  200. loss = helper.create_variable_for_type_inference(dtype=x.dtype)
  201. objectness_mask = helper.create_variable_for_type_inference(
  202. dtype='int32'
  203. )
  204. gt_match_mask = helper.create_variable_for_type_inference(dtype='int32')
  205. inputs = {
  206. "X": x,
  207. "GTBox": gt_box,
  208. "GTLabel": gt_label,
  209. }
  210. if gt_score is not None:
  211. inputs["GTScore"] = gt_score
  212. attrs = {
  213. "anchors": anchors,
  214. "anchor_mask": anchor_mask,
  215. "class_num": class_num,
  216. "ignore_thresh": ignore_thresh,
  217. "downsample_ratio": downsample_ratio,
  218. "use_label_smooth": use_label_smooth,
  219. "scale_x_y": scale_x_y,
  220. }
  221. helper.append_op(
  222. type='yolov3_loss',
  223. inputs=inputs,
  224. outputs={
  225. 'Loss': loss,
  226. 'ObjectnessMask': objectness_mask,
  227. 'GTMatchMask': gt_match_mask,
  228. },
  229. attrs=attrs,
  230. )
  231. return loss
  232. def yolo_box(
  233. x,
  234. img_size,
  235. anchors,
  236. class_num,
  237. conf_thresh,
  238. downsample_ratio,
  239. clip_bbox=True,
  240. name=None,
  241. scale_x_y=1.0,
  242. iou_aware=False,
  243. iou_aware_factor=0.5,
  244. ):
  245. r"""
  246. This operator generates YOLO detection boxes from output of YOLOv3 network.
  247. The output of previous network is in shape [N, C, H, W], while H and W
  248. should be the same, H and W specify the grid size, each grid point predict
  249. given number boxes, this given number, which following will be represented as S,
  250. is specified by the number of anchors. In the second dimension(the channel
  251. dimension), C should be equal to S * (5 + class_num) if :attr:`iou_aware` is false,
  252. otherwise C should be equal to S * (6 + class_num). class_num is the object
  253. category number of source dataset(such as 80 in coco dataset), so the
  254. second(channel) dimension, apart from 4 box location coordinates x, y, w, h,
  255. also includes confidence score of the box and class one-hot key of each anchor
  256. box.
  257. Assume the 4 location coordinates are :math:`t_x, t_y, t_w, t_h`, the box
  258. predictions should be as follows:
  259. $$
  260. b_x = \\sigma(t_x) + c_x
  261. $$
  262. $$
  263. b_y = \\sigma(t_y) + c_y
  264. $$
  265. $$
  266. b_w = p_w e^{t_w}
  267. $$
  268. $$
  269. b_h = p_h e^{t_h}
  270. $$
  271. in the equation above, :math:`c_x, c_y` is the left top corner of current grid
  272. and :math:`p_w, p_h` is specified by anchors.
  273. The logistic regression value of the 5th channel of each anchor prediction boxes
  274. represents the confidence score of each prediction box, and the logistic
  275. regression value of the last :attr:`class_num` channels of each anchor prediction
  276. boxes represents the classification scores. Boxes with confidence scores less than
  277. :attr:`conf_thresh` should be ignored, and box final scores is the product of
  278. confidence scores and classification scores.
  279. $$
  280. score_{pred} = score_{conf} * score_{class}
  281. $$
  282. Args:
  283. x (Tensor): The input tensor of YoloBox operator is a 4-D tensor with
  284. shape of [N, C, H, W]. The second dimension(C) stores box
  285. locations, confidence score and classification one-hot keys
  286. of each anchor box. Generally, X should be the output of
  287. YOLOv3 network. The data type is float32 or float64.
  288. img_size (Tensor): The image size tensor of YoloBox operator, This is a
  289. 2-D tensor with shape of [N, 2]. This tensor holds
  290. height and width of each input image used for resizing
  291. output box in input image scale. The data type is int32.
  292. anchors (list|tuple): The anchor width and height, it will be parsed pair
  293. by pair.
  294. class_num (int): The number of classes.
  295. conf_thresh (float): The confidence scores threshold of detection boxes.
  296. Boxes with confidence scores under threshold should
  297. be ignored.
  298. downsample_ratio (int): The downsample ratio from network input to
  299. :attr:`yolo_box` operator input, so 32, 16, 8
  300. should be set for the first, second, and third
  301. :attr:`yolo_box` layer.
  302. clip_bbox (bool, optional): Whether clip output bonding box in :attr:`img_size`
  303. boundary. Default true.
  304. name (str, optional): The default value is None. Normally there is no need
  305. for user to set this property. For more information,
  306. please refer to :ref:`api_guide_Name`.
  307. scale_x_y (float, optional): Scale the center point of decoded bounding box. Default 1.0
  308. iou_aware (bool, optional): Whether use iou aware. Default false.
  309. iou_aware_factor (float, optional): iou aware factor. Default 0.5.
  310. Returns:
  311. Tensor: A 3-D tensor with shape [N, M, 4], the coordinates of boxes,
  312. and a 3-D tensor with shape [N, M, :attr:`class_num`], the classification
  313. scores of boxes.
  314. Examples:
  315. .. code-block:: python
  316. >>> import paddle
  317. >>> x = paddle.rand([2, 14, 8, 8]).astype('float32')
  318. >>> img_size = paddle.ones((2, 2)).astype('int32')
  319. >>> boxes, scores = paddle.vision.ops.yolo_box(x,
  320. ... img_size=img_size,
  321. ... anchors=[10, 13, 16, 30],
  322. ... class_num=2,
  323. ... conf_thresh=0.01,
  324. ... downsample_ratio=8,
  325. ... clip_bbox=True,
  326. ... scale_x_y=1.)
  327. """
  328. if in_dynamic_or_pir_mode():
  329. boxes, scores = _C_ops.yolo_box(
  330. x,
  331. img_size,
  332. anchors,
  333. class_num,
  334. conf_thresh,
  335. downsample_ratio,
  336. clip_bbox,
  337. scale_x_y,
  338. iou_aware,
  339. iou_aware_factor,
  340. )
  341. return boxes, scores
  342. else:
  343. helper = LayerHelper('yolo_box', **locals())
  344. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'yolo_box')
  345. check_variable_and_dtype(img_size, 'img_size', 'int32', 'yolo_box')
  346. check_type(anchors, 'anchors', (list, tuple), 'yolo_box')
  347. check_type(conf_thresh, 'conf_thresh', float, 'yolo_box')
  348. boxes = helper.create_variable_for_type_inference(dtype=x.dtype)
  349. scores = helper.create_variable_for_type_inference(dtype=x.dtype)
  350. attrs = {
  351. "anchors": anchors,
  352. "class_num": class_num,
  353. "conf_thresh": conf_thresh,
  354. "downsample_ratio": downsample_ratio,
  355. "clip_bbox": clip_bbox,
  356. "scale_x_y": scale_x_y,
  357. "iou_aware": iou_aware,
  358. "iou_aware_factor": iou_aware_factor,
  359. }
  360. helper.append_op(
  361. type='yolo_box',
  362. inputs={
  363. "X": x,
  364. "ImgSize": img_size,
  365. },
  366. outputs={
  367. 'Boxes': boxes,
  368. 'Scores': scores,
  369. },
  370. attrs=attrs,
  371. )
  372. return boxes, scores
  373. def prior_box(
  374. input,
  375. image,
  376. min_sizes,
  377. max_sizes=None,
  378. aspect_ratios=[1.0],
  379. variance=[0.1, 0.1, 0.2, 0.2],
  380. flip=False,
  381. clip=False,
  382. steps=[0.0, 0.0],
  383. offset=0.5,
  384. min_max_aspect_ratios_order=False,
  385. name=None,
  386. ):
  387. r"""
  388. This op generates prior boxes for SSD(Single Shot MultiBox Detector) algorithm.
  389. Each position of the input produce N prior boxes, N is determined by
  390. the count of min_sizes, max_sizes and aspect_ratios, The size of the
  391. box is in range(min_size, max_size) interval, which is generated in
  392. sequence according to the aspect_ratios.
  393. Args:
  394. input (Tensor): 4-D tensor(NCHW), the data type should be float32 or float64.
  395. image (Tensor): 4-D tensor(NCHW), the input image data of PriorBoxOp,
  396. the data type should be float32 or float64.
  397. min_sizes (list|tuple|float): the min sizes of generated prior boxes.
  398. max_sizes (list|tuple|None, optional): the max sizes of generated prior boxes.
  399. Default: None, means [] and will not be used.
  400. aspect_ratios (list|tuple|float, optional): the aspect ratios of generated
  401. prior boxes. Default: [1.0].
  402. variance (list|tuple, optional): the variances to be encoded in prior boxes.
  403. Default:[0.1, 0.1, 0.2, 0.2].
  404. flip (bool): Whether to flip aspect ratios. Default:False.
  405. clip (bool): Whether to clip out-of-boundary boxes. Default: False.
  406. steps (list|tuple, optional): Prior boxes steps across width and height, If
  407. steps[0] equals to 0.0 or steps[1] equals to 0.0, the prior boxes steps across
  408. height or weight of the input will be automatically calculated.
  409. Default: [0., 0.]
  410. offset (float, optional)): Prior boxes center offset. Default: 0.5
  411. min_max_aspect_ratios_order (bool, optional): If set True, the output prior box is
  412. in order of [min, max, aspect_ratios], which is consistent with
  413. Caffe. Please note, this order affects the weights order of
  414. convolution layer followed by and does not affect the final
  415. detection results. Default: False.
  416. name (str, optional): The default value is None. Normally there is no need for
  417. user to set this property. For more information, please refer to :ref:`api_guide_Name`
  418. Returns:
  419. Tensor: the output prior boxes and the expanded variances of PriorBox.
  420. The prior boxes is a 4-D tensor, the layout is [H, W, num_priors, 4],
  421. num_priors is the total box count of each position of input.
  422. The expanded variances is a 4-D tensor, same shape as the prior boxes.
  423. Examples:
  424. .. code-block:: python
  425. >>> import paddle
  426. >>> input = paddle.rand((1, 3, 6, 9), dtype=paddle.float32)
  427. >>> image = paddle.rand((1, 3, 9, 12), dtype=paddle.float32)
  428. >>> box, var = paddle.vision.ops.prior_box(
  429. ... input=input,
  430. ... image=image,
  431. ... min_sizes=[2.0, 4.0],
  432. ... clip=True,
  433. ... flip=True)
  434. ...
  435. """
  436. def _is_list_or_tuple_(data):
  437. return isinstance(data, (list, tuple))
  438. if not _is_list_or_tuple_(min_sizes):
  439. min_sizes = [min_sizes]
  440. if not _is_list_or_tuple_(aspect_ratios):
  441. aspect_ratios = [aspect_ratios]
  442. if not _is_list_or_tuple_(steps):
  443. steps = [steps]
  444. if not len(steps) == 2:
  445. raise ValueError('steps should be (step_w, step_h)')
  446. min_sizes = list(map(float, min_sizes))
  447. aspect_ratios = list(map(float, aspect_ratios))
  448. steps = list(map(float, steps))
  449. cur_max_sizes = None
  450. if max_sizes is not None and len(max_sizes) > 0 and max_sizes[0] > 0:
  451. if not _is_list_or_tuple_(max_sizes):
  452. max_sizes = [max_sizes]
  453. cur_max_sizes = max_sizes
  454. if in_dynamic_or_pir_mode():
  455. step_w, step_h = steps
  456. if max_sizes is None:
  457. max_sizes = []
  458. box, var = _C_ops.prior_box(
  459. input,
  460. image,
  461. min_sizes,
  462. max_sizes,
  463. aspect_ratios,
  464. variance,
  465. flip,
  466. clip,
  467. step_w,
  468. step_h,
  469. offset,
  470. min_max_aspect_ratios_order,
  471. )
  472. return box, var
  473. else:
  474. helper = LayerHelper("prior_box", **locals())
  475. dtype = helper.input_dtype()
  476. check_variable_and_dtype(
  477. input, 'input', ['uint8', 'int8', 'float32', 'float64'], 'prior_box'
  478. )
  479. attrs = {
  480. 'min_sizes': min_sizes,
  481. 'aspect_ratios': aspect_ratios,
  482. 'variances': variance,
  483. 'flip': flip,
  484. 'clip': clip,
  485. 'step_w': steps[0],
  486. 'step_h': steps[1],
  487. 'offset': offset,
  488. 'min_max_aspect_ratios_order': min_max_aspect_ratios_order,
  489. }
  490. if cur_max_sizes is not None:
  491. attrs['max_sizes'] = cur_max_sizes
  492. box = helper.create_variable_for_type_inference(dtype)
  493. var = helper.create_variable_for_type_inference(dtype)
  494. helper.append_op(
  495. type="prior_box",
  496. inputs={"Input": input, "Image": image},
  497. outputs={"Boxes": box, "Variances": var},
  498. attrs=attrs,
  499. )
  500. box.stop_gradient = True
  501. var.stop_gradient = True
  502. return box, var
  503. def box_coder(
  504. prior_box,
  505. prior_box_var,
  506. target_box,
  507. code_type="encode_center_size",
  508. box_normalized=True,
  509. axis=0,
  510. name=None,
  511. ):
  512. r"""
  513. Encode/Decode the target bounding box with the priorbox information.
  514. The Encoding schema described below:
  515. .. math::
  516. ox &= (tx - px) / pw / pxv
  517. oy &= (ty - py) / ph / pyv
  518. ow &= log(abs(tw / pw)) / pwv
  519. oh &= log(abs(th / ph)) / phv
  520. The Decoding schema described below:
  521. .. math::
  522. ox &= (pw * pxv * tx * + px) - tw / 2
  523. oy &= (ph * pyv * ty * + py) - th / 2
  524. ow &= exp(pwv * tw) * pw + tw / 2
  525. oh &= exp(phv * th) * ph + th / 2
  526. where `tx`, `ty`, `tw`, `th` denote the target box's center coordinates,
  527. width and height respectively. Similarly, `px`, `py`, `pw`, `ph` denote
  528. the priorbox's (anchor) center coordinates, width and height. `pxv`,
  529. `pyv`, `pwv`, `phv` denote the variance of the priorbox and `ox`, `oy`,
  530. `ow`, `oh` denote the encoded/decoded coordinates, width and height.
  531. During Box Decoding, two modes for broadcast are supported. Say target
  532. box has shape [N, M, 4], and the shape of prior box can be [N, 4] or
  533. [M, 4]. Then prior box will broadcast to target box along the
  534. assigned axis.
  535. Args:
  536. prior_box (Tensor): Box list prior_box is a 2-D Tensor with shape
  537. [M, 4] holds M boxes and data type is float32 or float64. Each box
  538. is represented as [xmin, ymin, xmax, ymax], [xmin, ymin] is the
  539. left top coordinate of the anchor box, if the input is image feature
  540. map, they are close to the origin of the coordinate system.
  541. [xmax, ymax] is the right bottom coordinate of the anchor box.
  542. prior_box_var (Tensor|List|tuple|None): prior_box_var supports four types
  543. of input. One is Tensor with shape [M, 4] which holds M group and
  544. data type is float32 or float64. The second is list or tuple consist
  545. of 4 elements shared by all boxes and data type is float32 or float64.
  546. Other is None and not involved in calculation.
  547. target_box (Tensor): This input can be a 2-D LoDTensor with shape
  548. [N, 4] when code_type is 'encode_center_size'. This input also can
  549. be a 3-D Tensor with shape [N, M, 4] when code_type is
  550. 'decode_center_size'. Each box is represented as
  551. [xmin, ymin, xmax, ymax]. The data type is float32 or float64.
  552. code_type (str, optional): The code type used with the target box. It can be
  553. `encode_center_size` or `decode_center_size`. `encode_center_size`
  554. by default.
  555. box_normalized (bool, optional): Whether treat the priorbox as a normalized box.
  556. Set true by default.
  557. axis (int, optional): Which axis in PriorBox to broadcast for box decode,
  558. for example, if axis is 0 and TargetBox has shape [N, M, 4] and
  559. PriorBox has shape [M, 4], then PriorBox will broadcast to [N, M, 4]
  560. for decoding. It is only valid when code type is
  561. `decode_center_size`. Set 0 by default.
  562. name (str, optional): For detailed information, please refer
  563. to :ref:`api_guide_Name`. Usually name is no need to set and
  564. None by default.
  565. Returns:
  566. Tensor: output boxes, when code_type is 'encode_center_size', the
  567. output tensor of box_coder_op with shape [N, M, 4] representing the
  568. result of N target boxes encoded with M Prior boxes and variances.
  569. When code_type is 'decode_center_size', N represents the batch size
  570. and M represents the number of decoded boxes.
  571. Examples:
  572. .. code-block:: python
  573. >>> import paddle
  574. >>> # For encode
  575. >>> prior_box_encode = paddle.rand((80, 4), dtype=paddle.float32)
  576. >>> prior_box_var_encode = paddle.rand((80, 4), dtype=paddle.float32)
  577. >>> target_box_encode = paddle.rand((20, 4), dtype=paddle.float32)
  578. >>> output_encode = paddle.vision.ops.box_coder(
  579. ... prior_box=prior_box_encode,
  580. ... prior_box_var=prior_box_var_encode,
  581. ... target_box=target_box_encode,
  582. ... code_type="encode_center_size")
  583. ...
  584. >>> # For decode
  585. >>> prior_box_decode = paddle.rand((80, 4), dtype=paddle.float32)
  586. >>> prior_box_var_decode = paddle.rand((80, 4), dtype=paddle.float32)
  587. >>> target_box_decode = paddle.rand((20, 80, 4), dtype=paddle.float32)
  588. >>> output_decode = paddle.vision.ops.box_coder(
  589. ... prior_box=prior_box_decode,
  590. ... prior_box_var=prior_box_var_decode,
  591. ... target_box=target_box_decode,
  592. ... code_type="decode_center_size",
  593. ... box_normalized=False)
  594. ...
  595. """
  596. if in_dynamic_or_pir_mode():
  597. if isinstance(prior_box_var, (core.eager.Tensor, paddle.pir.Value)):
  598. output_box = _C_ops.box_coder(
  599. prior_box,
  600. prior_box_var,
  601. target_box,
  602. code_type,
  603. box_normalized,
  604. axis,
  605. [],
  606. )
  607. elif isinstance(prior_box_var, (list, tuple)):
  608. prior_box_var = list(prior_box_var)
  609. assert (
  610. len(prior_box_var) == 4
  611. ), "Input prior_box_var must be Variable or list|tuple with 4 elements."
  612. output_box = _C_ops.box_coder(
  613. prior_box,
  614. None,
  615. target_box,
  616. code_type,
  617. box_normalized,
  618. axis,
  619. prior_box_var,
  620. )
  621. else:
  622. raise TypeError(
  623. "Input prior_box_var must be Variable or list|tuple"
  624. )
  625. return output_box
  626. else:
  627. check_variable_and_dtype(
  628. prior_box, 'prior_box', ['float32', 'float64'], 'box_coder'
  629. )
  630. check_variable_and_dtype(
  631. target_box, 'target_box', ['float32', 'float64'], 'box_coder'
  632. )
  633. helper = LayerHelper("box_coder", **locals())
  634. output_box = helper.create_variable_for_type_inference(
  635. dtype=prior_box.dtype
  636. )
  637. inputs = {"PriorBox": prior_box, "TargetBox": target_box}
  638. attrs = {
  639. "code_type": code_type,
  640. "box_normalized": box_normalized,
  641. "axis": axis,
  642. }
  643. if isinstance(prior_box_var, Variable):
  644. inputs['PriorBoxVar'] = prior_box_var
  645. elif isinstance(prior_box_var, (list, tuple)):
  646. attrs['variance'] = prior_box_var
  647. assert (
  648. len(attrs['variance']) == 4
  649. ), "Input prior_box_var must be Variable or list|tuple with 4 elements."
  650. else:
  651. raise TypeError(
  652. "Input prior_box_var must be Variable or list|tuple"
  653. )
  654. helper.append_op(
  655. type="box_coder",
  656. inputs=inputs,
  657. attrs=attrs,
  658. outputs={"OutputBox": output_box},
  659. )
  660. return output_box
  661. def deform_conv2d(
  662. x,
  663. offset,
  664. weight,
  665. bias=None,
  666. stride=1,
  667. padding=0,
  668. dilation=1,
  669. deformable_groups=1,
  670. groups=1,
  671. mask=None,
  672. name=None,
  673. ):
  674. r"""
  675. Compute 2-D deformable convolution on 4-D input.
  676. Given input image x, output feature map y, the deformable convolution operation can be expressed as follow:
  677. Deformable Convolution v2:
  678. .. math::
  679. y(p) = \sum_{k=1}^{K}{w_k * x(p + p_k + \Delta p_k) * \Delta m_k}
  680. Deformable Convolution v1:
  681. .. math::
  682. y(p) = \sum_{k=1}^{K}{w_k * x(p + p_k + \Delta p_k)}
  683. Where :math:`\Delta p_k` and :math:`\Delta m_k` are the learnable offset and modulation scalar for the k-th location,
  684. Which :math:`\Delta m_k` is one in deformable convolution v1. Please refer to `Deformable ConvNets v2: More Deformable, Better Results
  685. <https://arxiv.org/abs/1811.11168v2>`_ and `Deformable Convolutional Networks <https://arxiv.org/abs/1703.06211>`_.
  686. Example:
  687. - Input:
  688. x shape: :math:`(N, C_{in}, H_{in}, W_{in})`
  689. weight shape: :math:`(C_{out}, C_{in}, H_f, W_f)`
  690. offset shape: :math:`(N, 2 * H_f * W_f, H_{out}, W_{out})`
  691. mask shape: :math:`(N, H_f * W_f, H_{out}, W_{out})`
  692. - Output:
  693. Output shape: :math:`(N, C_{out}, H_{out}, W_{out})`
  694. Where
  695. .. math::
  696. H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \\
  697. W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1
  698. Args:
  699. x (Tensor): The input image with [N, C, H, W] format. A Tensor with type
  700. float32, float64.
  701. offset (Tensor): The input coordinate offset of deformable convolution layer.
  702. A Tensor with type float32, float64.
  703. weight (Tensor): The convolution kernel with shape [M, C/g, kH, kW], where M is
  704. the number of output channels, g is the number of groups, kH is the filter's
  705. height, kW is the filter's width.
  706. bias (Tensor, optional): The bias with shape [M,]. Default: None.
  707. stride (int|list|tuple, optional): The stride size. If stride is a list/tuple, it must
  708. contain two integers, (stride_H, stride_W). Otherwise, the
  709. stride_H = stride_W = stride. Default: 1.
  710. padding (int|list|tuple, optional): The padding size. If padding is a list/tuple, it must
  711. contain two integers, (padding_H, padding_W). Otherwise, the
  712. padding_H = padding_W = padding. Default: 0.
  713. dilation (int|list|tuple, optional): The dilation size. If dilation is a list/tuple, it must
  714. contain two integers, (dilation_H, dilation_W). Otherwise, the
  715. dilation_H = dilation_W = dilation. Default: 1.
  716. deformable_groups (int): The number of deformable group partitions.
  717. Default: 1.
  718. groups (int, optional): The groups number of the deformable conv layer. According to
  719. grouped convolution in Alex Krizhevsky's Deep CNN paper: when group=2,
  720. the first half of the filters is only connected to the first half
  721. of the input channels, while the second half of the filters is only
  722. connected to the second half of the input channels. Default: 1.
  723. mask (Tensor, optional): The input mask of deformable convolution layer.
  724. A Tensor with type float32, float64. It should be None when you use
  725. deformable convolution v1. Default: None.
  726. name(str, optional): For details, please refer to :ref:`api_guide_Name`.
  727. Generally, no setting is required. Default: None.
  728. Returns:
  729. Tensor: 4-D Tensor storing the deformable convolution result.\
  730. A Tensor with type float32, float64.
  731. Examples:
  732. .. code-block:: python
  733. >>> #deformable conv v2:
  734. >>> import paddle
  735. >>> input = paddle.rand((8, 1, 28, 28))
  736. >>> kh, kw = 3, 3
  737. >>> weight = paddle.rand((16, 1, kh, kw))
  738. >>> # offset shape should be [bs, 2 * kh * kw, out_h, out_w]
  739. >>> # mask shape should be [bs, hw * hw, out_h, out_w]
  740. >>> # In this case, for an input of 28, stride of 1
  741. >>> # and kernel size of 3, without padding, the output size is 26
  742. >>> offset = paddle.rand((8, 2 * kh * kw, 26, 26))
  743. >>> mask = paddle.rand((8, kh * kw, 26, 26))
  744. >>> out = paddle.vision.ops.deform_conv2d(input, offset, weight, mask=mask)
  745. >>> print(out.shape)
  746. [8, 16, 26, 26]
  747. >>> #deformable conv v1:
  748. >>> import paddle
  749. >>> input = paddle.rand((8, 1, 28, 28))
  750. >>> kh, kw = 3, 3
  751. >>> weight = paddle.rand((16, 1, kh, kw))
  752. >>> # offset shape should be [bs, 2 * kh * kw, out_h, out_w]
  753. >>> # In this case, for an input of 28, stride of 1
  754. >>> # and kernel size of 3, without padding, the output size is 26
  755. >>> offset = paddle.rand((8, 2 * kh * kw, 26, 26))
  756. >>> out = paddle.vision.ops.deform_conv2d(input, offset, weight)
  757. >>> print(out.shape)
  758. [8, 16, 26, 26]
  759. """
  760. stride = convert_to_list(stride, 2, 'stride')
  761. padding = convert_to_list(padding, 2, 'padding')
  762. dilation = convert_to_list(dilation, 2, 'dilation')
  763. use_deform_conv2d_v1 = True if mask is None else False
  764. if in_dynamic_or_pir_mode():
  765. pre_bias = _C_ops.deformable_conv(
  766. x,
  767. offset,
  768. weight,
  769. mask,
  770. stride,
  771. padding,
  772. dilation,
  773. deformable_groups,
  774. groups,
  775. 1,
  776. )
  777. if bias is not None:
  778. out = _add_with_axis(pre_bias, bias, axis=1)
  779. else:
  780. out = pre_bias
  781. else:
  782. check_variable_and_dtype(
  783. x, "x", ['float32', 'float64'], 'deform_conv2d'
  784. )
  785. check_variable_and_dtype(
  786. offset, "offset", ['float32', 'float64'], 'deform_conv2d'
  787. )
  788. num_channels = x.shape[1]
  789. helper = LayerHelper('deformable_conv', **locals())
  790. dtype = helper.input_dtype()
  791. stride = convert_to_list(stride, 2, 'stride')
  792. padding = convert_to_list(padding, 2, 'padding')
  793. dilation = convert_to_list(dilation, 2, 'dilation')
  794. pre_bias = helper.create_variable_for_type_inference(dtype)
  795. if use_deform_conv2d_v1:
  796. op_type = 'deformable_conv_v1'
  797. inputs = {
  798. 'Input': x,
  799. 'Filter': weight,
  800. 'Offset': offset,
  801. }
  802. else:
  803. op_type = 'deformable_conv'
  804. inputs = {
  805. 'Input': x,
  806. 'Filter': weight,
  807. 'Offset': offset,
  808. 'Mask': mask,
  809. }
  810. outputs = {"Output": pre_bias}
  811. attrs = {
  812. 'strides': stride,
  813. 'paddings': padding,
  814. 'dilations': dilation,
  815. 'groups': groups,
  816. 'deformable_groups': deformable_groups,
  817. 'im2col_step': 1,
  818. }
  819. helper.append_op(
  820. type=op_type, inputs=inputs, outputs=outputs, attrs=attrs
  821. )
  822. if bias is not None:
  823. out = helper.create_variable_for_type_inference(dtype)
  824. helper.append_op(
  825. type='elementwise_add',
  826. inputs={'X': [pre_bias], 'Y': [bias]},
  827. outputs={'Out': [out]},
  828. attrs={'axis': 1},
  829. )
  830. else:
  831. out = pre_bias
  832. return out
  833. class DeformConv2D(Layer):
  834. r"""
  835. Compute 2-D deformable convolution on 4-D input.
  836. Given input image x, output feature map y, the deformable convolution operation can be expressed as follow:
  837. Deformable Convolution v2:
  838. .. math::
  839. y(p) = \sum_{k=1}^{K}{w_k * x(p + p_k + \Delta p_k) * \Delta m_k}
  840. Deformable Convolution v1:
  841. .. math::
  842. y(p) = \sum_{k=1}^{K}{w_k * x(p + p_k + \Delta p_k)}
  843. Where :math:`\Delta p_k` and :math:`\Delta m_k` are the learnable offset and modulation scalar for the k-th location,
  844. Which :math:`\Delta m_k` is one in deformable convolution v1. Please refer to `Deformable ConvNets v2: More Deformable, Better Results
  845. <https://arxiv.org/abs/1811.11168v2>`_ and `Deformable Convolutional Networks <https://arxiv.org/abs/1703.06211>`_.
  846. Example:
  847. - Input:
  848. x shape: :math:`(N, C_{in}, H_{in}, W_{in})`
  849. weight shape: :math:`(C_{out}, C_{in}, H_f, W_f)`
  850. offset shape: :math:`(N, 2 * H_f * W_f, H_{out}, W_{out})`
  851. mask shape: :math:`(N, H_f * W_f, H_{out}, W_{out})`
  852. - Output:
  853. Output shape: :math:`(N, C_{out}, H_{out}, W_{out})`
  854. Where
  855. .. math::
  856. H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \\
  857. W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1
  858. Parameters:
  859. in_channels(int): The number of input channels in the input image.
  860. out_channels(int): The number of output channels produced by the convolution.
  861. kernel_size(int|list|tuple): The size of the convolving kernel.
  862. stride(int|list|tuple, optional): The stride size. If stride is a list/tuple, it must
  863. contain three integers, (stride_H, stride_W). Otherwise, the
  864. stride_H = stride_W = stride. The default value is 1.
  865. padding (int|list|tuple, optional): The padding size. If padding is a list/tuple, it must
  866. contain two integers, (padding_H, padding_W). Otherwise, the
  867. padding_H = padding_W = padding. Default: padding = 0.
  868. dilation(int|list|tuple, optional): The dilation size. If dilation is a list/tuple, it must
  869. contain three integers, (dilation_D, dilation_H, dilation_W). Otherwise, the
  870. dilation_D = dilation_H = dilation_W = dilation. The default value is 1.
  871. deformable_groups (int, optional): The number of deformable group partitions.
  872. Default: deformable_groups = 1.
  873. groups(int, optional): The groups number of the Conv3D Layer. According to grouped
  874. convolution in Alex Krizhevsky's Deep CNN paper: when group=2,
  875. the first half of the filters is only connected to the first half
  876. of the input channels, while the second half of the filters is only
  877. connected to the second half of the input channels. The default value is 1.
  878. weight_attr(ParamAttr, optional): The parameter attribute for learnable parameters/weights
  879. of conv2d. If it is set to None or one attribute of ParamAttr, conv2d
  880. will create ParamAttr as param_attr. If it is set to None, the parameter
  881. is initialized with :math:`Normal(0.0, std)`, and the :math:`std` is
  882. :math:`(\frac{2.0 }{filter\_elem\_num})^{0.5}`. The default value is None.
  883. bias_attr(ParamAttr|bool, optional): The parameter attribute for the bias of conv2d.
  884. If it is set to False, no bias will be added to the output units.
  885. If it is set to None or one attribute of ParamAttr, conv2d
  886. will create ParamAttr as bias_attr. If the Initializer of the bias_attr
  887. is not set, the bias is initialized zero. The default value is None.
  888. Attribute:
  889. **weight** (Parameter): the learnable weights of filter of this layer.
  890. **bias** (Parameter or None): the learnable bias of this layer.
  891. Shape:
  892. - x: :math:`(N, C_{in}, H_{in}, W_{in})`
  893. - offset: :math:`(N, 2 * H_f * W_f, H_{out}, W_{out})`
  894. - mask: :math:`(N, H_f * W_f, H_{out}, W_{out})`
  895. - output: :math:`(N, C_{out}, H_{out}, W_{out})`
  896. Where
  897. .. math::
  898. H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (kernel\_size[0] - 1) + 1))}{strides[0]} + 1 \\
  899. W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (kernel\_size[1] - 1) + 1))}{strides[1]} + 1
  900. Examples:
  901. .. code-block:: python
  902. >>> #deformable conv v2:
  903. >>> import paddle
  904. >>> input = paddle.rand((8, 1, 28, 28))
  905. >>> kh, kw = 3, 3
  906. >>> # offset shape should be [bs, 2 * kh * kw, out_h, out_w]
  907. >>> # mask shape should be [bs, hw * hw, out_h, out_w]
  908. >>> # In this case, for an input of 28, stride of 1
  909. >>> # and kernel size of 3, without padding, the output size is 26
  910. >>> offset = paddle.rand((8, 2 * kh * kw, 26, 26))
  911. >>> mask = paddle.rand((8, kh * kw, 26, 26))
  912. >>> deform_conv = paddle.vision.ops.DeformConv2D(
  913. ... in_channels=1,
  914. ... out_channels=16,
  915. ... kernel_size=[kh, kw])
  916. >>> out = deform_conv(input, offset, mask)
  917. >>> print(out.shape)
  918. [8, 16, 26, 26]
  919. >>> #deformable conv v1:
  920. >>> import paddle
  921. >>> input = paddle.rand((8, 1, 28, 28))
  922. >>> kh, kw = 3, 3
  923. >>> # offset shape should be [bs, 2 * kh * kw, out_h, out_w]
  924. >>> # mask shape should be [bs, hw * hw, out_h, out_w]
  925. >>> # In this case, for an input of 28, stride of 1
  926. >>> # and kernel size of 3, without padding, the output size is 26
  927. >>> offset = paddle.rand((8, 2 * kh * kw, 26, 26))
  928. >>> deform_conv = paddle.vision.ops.DeformConv2D(
  929. ... in_channels=1,
  930. ... out_channels=16,
  931. ... kernel_size=[kh, kw])
  932. >>> out = deform_conv(input, offset)
  933. >>> print(out.shape)
  934. [8, 16, 26, 26]
  935. """
  936. def __init__(
  937. self,
  938. in_channels,
  939. out_channels,
  940. kernel_size,
  941. stride=1,
  942. padding=0,
  943. dilation=1,
  944. deformable_groups=1,
  945. groups=1,
  946. weight_attr=None,
  947. bias_attr=None,
  948. ):
  949. super().__init__()
  950. assert (
  951. weight_attr is not False
  952. ), "weight_attr should not be False in Conv."
  953. self._weight_attr = weight_attr
  954. self._bias_attr = bias_attr
  955. self._deformable_groups = deformable_groups
  956. self._groups = groups
  957. self._in_channels = in_channels
  958. self._out_channels = out_channels
  959. self._channel_dim = 1
  960. self._stride = convert_to_list(stride, 2, 'stride')
  961. self._dilation = convert_to_list(dilation, 2, 'dilation')
  962. self._kernel_size = convert_to_list(kernel_size, 2, 'kernel_size')
  963. if in_channels % groups != 0:
  964. raise ValueError("in_channels must be divisible by groups.")
  965. self._padding = convert_to_list(padding, 2, 'padding')
  966. filter_shape = [out_channels, in_channels // groups] + self._kernel_size
  967. def _get_default_param_initializer():
  968. filter_elem_num = np.prod(self._kernel_size) * self._in_channels
  969. std = (2.0 / filter_elem_num) ** 0.5
  970. return Normal(0.0, std)
  971. self.weight = self.create_parameter(
  972. shape=filter_shape,
  973. attr=self._weight_attr,
  974. default_initializer=_get_default_param_initializer(),
  975. )
  976. self.bias = self.create_parameter(
  977. attr=self._bias_attr, shape=[self._out_channels], is_bias=True
  978. )
  979. def forward(self, x, offset, mask=None):
  980. out = deform_conv2d(
  981. x=x,
  982. offset=offset,
  983. weight=self.weight,
  984. bias=self.bias,
  985. stride=self._stride,
  986. padding=self._padding,
  987. dilation=self._dilation,
  988. deformable_groups=self._deformable_groups,
  989. groups=self._groups,
  990. mask=mask,
  991. )
  992. return out
  993. def distribute_fpn_proposals(
  994. fpn_rois,
  995. min_level,
  996. max_level,
  997. refer_level,
  998. refer_scale,
  999. pixel_offset=False,
  1000. rois_num=None,
  1001. name=None,
  1002. ):
  1003. r"""
  1004. In Feature Pyramid Networks (FPN) models, it is needed to distribute
  1005. all proposals into different FPN level, with respect to scale of the proposals,
  1006. the referring scale and the referring level. Besides, to restore the order of
  1007. proposals, we return an array which indicates the original index of rois
  1008. in current proposals. To compute FPN level for each roi, the formula is given as follows:
  1009. .. math::
  1010. roi\_scale &= \sqrt{BBoxArea(fpn\_roi)} \\
  1011. level &= floor(\log(\frac{roi\_scale}{refer\_scale}) + refer\_level)
  1012. where BBoxArea is a function to compute the area of each roi.
  1013. Args:
  1014. fpn_rois (Tensor): The input fpn_rois. 2-D Tensor with shape [N, 4] and data type can be
  1015. float32 or float64.
  1016. min_level (int): The lowest level of FPN layer where the proposals come
  1017. from.
  1018. max_level (int): The highest level of FPN layer where the proposals
  1019. come from.
  1020. refer_level (int): The referring level of FPN layer with specified scale.
  1021. refer_scale (int): The referring scale of FPN layer with specified level.
  1022. pixel_offset (bool, optional): Whether there is pixel offset. If True, the offset of
  1023. image shape will be 1. 'False' by default.
  1024. rois_num (Tensor, optional): 1-D Tensor contains the number of RoIs in each image.
  1025. The shape is [B] and data type is int32. B is the number of images.
  1026. If rois_num not None, it will return a list of 1-D Tensor. Each element
  1027. is the output RoIs' number of each image on the corresponding level
  1028. and the shape is [B]. None by default.
  1029. name (str, optional): For detailed information, please refer
  1030. to :ref:`api_guide_Name`. Usually name is no need to set and
  1031. None by default.
  1032. Returns:
  1033. - multi_rois (List), The proposals in each FPN level. It is a list of 2-D Tensor with shape [M, 4], where M is
  1034. and data type is same as `fpn_rois` . The length is max_level-min_level+1.
  1035. - restore_ind (Tensor), The index used to restore the order of fpn_rois. It is a 2-D Tensor with shape [N, 1]
  1036. , where N is the number of total rois. The data type is int32.
  1037. - rois_num_per_level (List), A list of 1-D Tensor and each Tensor is
  1038. the RoIs' number in each image on the corresponding level. The shape
  1039. is [B] and data type of int32, where B is the number of images.
  1040. Examples:
  1041. .. code-block:: python
  1042. >>> import paddle
  1043. >>> fpn_rois = paddle.rand((10, 4))
  1044. >>> rois_num = paddle.to_tensor([3, 1, 4, 2], dtype=paddle.int32)
  1045. >>> multi_rois, restore_ind, rois_num_per_level = paddle.vision.ops.distribute_fpn_proposals(
  1046. ... fpn_rois=fpn_rois,
  1047. ... min_level=2,
  1048. ... max_level=5,
  1049. ... refer_level=4,
  1050. ... refer_scale=224,
  1051. ... rois_num=rois_num)
  1052. ...
  1053. """
  1054. assert (
  1055. max_level > 0 and min_level > 0
  1056. ), "min_level and max_level should be greater than 0"
  1057. num_lvl = max_level - min_level + 1
  1058. assert num_lvl > 1, "max_level should be greater than min_level"
  1059. assert (
  1060. num_lvl < 100
  1061. ), "Only support max to 100 levels, (max_level - min_level + 1 < 100)"
  1062. if in_dynamic_or_pir_mode():
  1063. assert (
  1064. rois_num is not None
  1065. ), "rois_num should not be None in dygraph mode."
  1066. (
  1067. multi_rois,
  1068. rois_num_per_level,
  1069. restore_ind,
  1070. ) = _C_ops.distribute_fpn_proposals(
  1071. fpn_rois,
  1072. rois_num,
  1073. min_level,
  1074. max_level,
  1075. refer_level,
  1076. refer_scale,
  1077. pixel_offset,
  1078. )
  1079. return multi_rois, restore_ind, rois_num_per_level
  1080. else:
  1081. check_variable_and_dtype(
  1082. fpn_rois,
  1083. 'fpn_rois',
  1084. ['float32', 'float64'],
  1085. 'distribute_fpn_proposals',
  1086. )
  1087. helper = LayerHelper('distribute_fpn_proposals', **locals())
  1088. dtype = helper.input_dtype('fpn_rois')
  1089. multi_rois = [
  1090. helper.create_variable_for_type_inference(dtype)
  1091. for i in range(num_lvl)
  1092. ]
  1093. restore_ind = helper.create_variable_for_type_inference(dtype='int32')
  1094. inputs = {'FpnRois': fpn_rois}
  1095. outputs = {
  1096. 'MultiFpnRois': multi_rois,
  1097. 'RestoreIndex': restore_ind,
  1098. }
  1099. if rois_num is not None:
  1100. inputs['RoisNum'] = rois_num
  1101. rois_num_per_level = [
  1102. helper.create_variable_for_type_inference(dtype='int32')
  1103. for i in range(num_lvl)
  1104. ]
  1105. outputs['MultiLevelRoIsNum'] = rois_num_per_level
  1106. else:
  1107. rois_num_per_level = None
  1108. helper.append_op(
  1109. type='distribute_fpn_proposals',
  1110. inputs=inputs,
  1111. outputs=outputs,
  1112. attrs={
  1113. 'min_level': min_level,
  1114. 'max_level': max_level,
  1115. 'refer_level': refer_level,
  1116. 'refer_scale': refer_scale,
  1117. 'pixel_offset': pixel_offset,
  1118. },
  1119. )
  1120. return multi_rois, restore_ind, rois_num_per_level
  1121. def read_file(filename, name=None):
  1122. """
  1123. Reads and outputs the bytes contents of a file as a uint8 Tensor
  1124. with one dimension.
  1125. Args:
  1126. filename (str): Path of the file to be read.
  1127. name (str, optional): The default value is None. Normally there is no
  1128. need for user to set this property. For more information, please
  1129. refer to :ref:`api_guide_Name`.
  1130. Returns:
  1131. A uint8 tensor.
  1132. Examples:
  1133. .. code-block:: python
  1134. >>> import cv2
  1135. >>> import paddle
  1136. >>> paddle.seed(2023)
  1137. >>> fake_img = (paddle.rand((400, 300, 3)).numpy() * 255).astype('uint8')
  1138. >>> cv2.imwrite('fake.jpg', fake_img)
  1139. >>> img_bytes = paddle.vision.ops.read_file('fake.jpg')
  1140. >>> print(img_bytes.shape)
  1141. [142773]
  1142. """
  1143. attr_dtype = convert_np_dtype_to_dtype_('uint8')
  1144. if in_dynamic_or_pir_mode():
  1145. return _C_ops.read_file(filename, attr_dtype, paddle.CPUPlace())
  1146. else:
  1147. inputs = {}
  1148. attrs = {'filename': filename}
  1149. helper = LayerHelper("read_file", **locals())
  1150. out = helper.create_variable_for_type_inference('uint8')
  1151. helper.append_op(
  1152. type="read_file", inputs=inputs, attrs=attrs, outputs={"Out": out}
  1153. )
  1154. return out
  1155. def decode_jpeg(x, mode='unchanged', name=None):
  1156. """
  1157. Decodes a JPEG image into a 3 dimensional RGB Tensor or 1 dimensional Gray Tensor.
  1158. Optionally converts the image to the desired format.
  1159. The values of the output tensor are uint8 between 0 and 255.
  1160. Args:
  1161. x (Tensor): A one dimensional uint8 tensor containing the raw bytes
  1162. of the JPEG image.
  1163. mode (str, optional): The read mode used for optionally converting the image.
  1164. Default: 'unchanged'.
  1165. name (str, optional): The default value is None. Normally there is no
  1166. need for user to set this property. For more information, please
  1167. refer to :ref:`api_guide_Name`.
  1168. Returns:
  1169. Tensor: A decoded image tensor with shape (image_channels, image_height, image_width)
  1170. Examples:
  1171. .. code-block:: python
  1172. >>> # doctest: +REQUIRES(env:GPU)
  1173. >>> import cv2
  1174. >>> import numpy as np
  1175. >>> import paddle
  1176. >>> paddle.device.set_device('gpu')
  1177. >>> fake_img = (np.random.random(
  1178. ... (400, 300, 3)) * 255).astype('uint8')
  1179. >>> cv2.imwrite('fake.jpg', fake_img)
  1180. >>> img_bytes = paddle.vision.ops.read_file('fake.jpg')
  1181. >>> img = paddle.vision.ops.decode_jpeg(img_bytes)
  1182. >>> print(img.shape)
  1183. [3, 400, 300]
  1184. """
  1185. if in_dynamic_or_pir_mode():
  1186. return _C_ops.decode_jpeg(x, mode, _current_expected_place())
  1187. else:
  1188. inputs = {'X': x}
  1189. attrs = {"mode": mode}
  1190. helper = LayerHelper("decode_jpeg", **locals())
  1191. out = helper.create_variable_for_type_inference('uint8')
  1192. helper.append_op(
  1193. type="decode_jpeg", inputs=inputs, attrs=attrs, outputs={"Out": out}
  1194. )
  1195. return out
  1196. def psroi_pool(x, boxes, boxes_num, output_size, spatial_scale=1.0, name=None):
  1197. """
  1198. Position sensitive region of interest pooling (also known as PSROIPooling) is to perform
  1199. position-sensitive average pooling on regions of interest specified by input. It performs
  1200. on inputs of nonuniform sizes to obtain fixed-size feature maps.
  1201. PSROIPooling is proposed by R-FCN. Please refer to https://arxiv.org/abs/1605.06409 for more details.
  1202. Args:
  1203. x (Tensor): Input features with shape (N, C, H, W). The data type can be float32 or float64.
  1204. boxes (Tensor): Box coordinates of ROIs (Regions of Interest) to pool over. It should be
  1205. a 2-D Tensor with shape (num_rois, 4). Given as [[x1, y1, x2, y2], ...],
  1206. (x1, y1) is the top left coordinates, and (x2, y2) is the bottom
  1207. right coordinates.
  1208. boxes_num (Tensor): The number of boxes contained in each picture in the batch.
  1209. output_size (int|Tuple(int, int)) The pooled output size(H, W), data type
  1210. is int32. If int, H and W are both equal to output_size.
  1211. spatial_scale (float, optional): Multiplicative spatial scale factor to translate ROI coords from their
  1212. input scale to the scale used when pooling. Default: 1.0
  1213. name(str, optional): The default value is None.
  1214. Normally there is no need for user to set this property.
  1215. For more information, please refer to :ref:`api_guide_Name`
  1216. Returns:
  1217. 4-D Tensor. The pooled ROIs with shape (num_rois, output_channels, pooled_h, pooled_w).
  1218. The output_channels equal to C / (pooled_h * pooled_w), where C is the channels of input.
  1219. Examples:
  1220. .. code-block:: python
  1221. >>> import paddle
  1222. >>> x = paddle.uniform([2, 490, 28, 28], dtype='float32')
  1223. >>> boxes = paddle.to_tensor([[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], dtype='float32')
  1224. >>> boxes_num = paddle.to_tensor([1, 2], dtype='int32')
  1225. >>> pool_out = paddle.vision.ops.psroi_pool(x, boxes, boxes_num, 7, 1.0)
  1226. >>> print(pool_out.shape)
  1227. [3, 10, 7, 7]
  1228. """
  1229. check_type(output_size, 'output_size', (int, tuple, list), 'psroi_pool')
  1230. if isinstance(output_size, int):
  1231. output_size = (output_size, output_size)
  1232. pooled_height, pooled_width = output_size
  1233. assert len(x.shape) == 4, "Input features with shape should be (N, C, H, W)"
  1234. if pooled_height * pooled_width == 0:
  1235. raise ValueError('output_size should not contain 0.')
  1236. output_channels = int(x.shape[1] / (pooled_height * pooled_width))
  1237. if in_dynamic_or_pir_mode():
  1238. return _C_ops.psroi_pool(
  1239. x,
  1240. boxes,
  1241. boxes_num,
  1242. pooled_height,
  1243. pooled_width,
  1244. output_channels,
  1245. spatial_scale,
  1246. )
  1247. else:
  1248. helper = LayerHelper('psroi_pool', **locals())
  1249. dtype = helper.input_dtype()
  1250. out = helper.create_variable_for_type_inference(dtype)
  1251. helper.append_op(
  1252. type='psroi_pool',
  1253. inputs={'X': x, 'ROIs': boxes},
  1254. outputs={'Out': out},
  1255. attrs={
  1256. 'output_channels': output_channels,
  1257. 'spatial_scale': spatial_scale,
  1258. 'pooled_height': pooled_height,
  1259. 'pooled_width': pooled_width,
  1260. },
  1261. )
  1262. return out
  1263. class PSRoIPool(Layer):
  1264. """
  1265. This interface is used to construct a callable object of the ``PSRoIPool`` class. Please
  1266. refer to :ref:`api_paddle_vision_ops_psroi_pool`.
  1267. Args:
  1268. output_size (int|Tuple(int, int)) The pooled output size(H, W), data type
  1269. is int32. If int, H and W are both equal to output_size.
  1270. spatial_scale (float, optional): Multiplicative spatial scale factor to translate ROI coords from their
  1271. input scale to the scale used when pooling. Default: 1.0.
  1272. Shape:
  1273. - x: 4-D Tensor with shape (N, C, H, W).
  1274. - boxes: 2-D Tensor with shape (num_rois, 4).
  1275. - boxes_num: 1-D Tensor.
  1276. - output: 4-D tensor with shape (num_rois, output_channels, pooled_h, pooled_w).
  1277. The output_channels equal to C / (pooled_h * pooled_w), where C is the channels of input.
  1278. Returns:
  1279. None.
  1280. Examples:
  1281. .. code-block:: python
  1282. >>> import paddle
  1283. >>> psroi_module = paddle.vision.ops.PSRoIPool(7, 1.0)
  1284. >>> x = paddle.uniform([2, 490, 28, 28], dtype='float32')
  1285. >>> boxes = paddle.to_tensor([[1, 5, 8, 10], [4, 2, 6, 7], [12, 12, 19, 21]], dtype='float32')
  1286. >>> boxes_num = paddle.to_tensor([1, 2], dtype='int32')
  1287. >>> pool_out = psroi_module(x, boxes, boxes_num)
  1288. >>> print(pool_out.shape)
  1289. [3, 10, 7, 7]
  1290. """
  1291. def __init__(self, output_size, spatial_scale=1.0):
  1292. super().__init__()
  1293. self.output_size = output_size
  1294. self.spatial_scale = spatial_scale
  1295. def forward(self, x, boxes, boxes_num):
  1296. return psroi_pool(
  1297. x, boxes, boxes_num, self.output_size, self.spatial_scale
  1298. )
  1299. def roi_pool(x, boxes, boxes_num, output_size, spatial_scale=1.0, name=None):
  1300. """
  1301. This operator implements the roi_pooling layer.
  1302. Region of interest pooling (also known as RoI pooling) is to perform max pooling on inputs of nonuniform sizes to obtain fixed-size feature maps (e.g. 7*7).
  1303. The operator has three steps: 1. Dividing each region proposal into equal-sized sections with output_size(h, w) 2. Finding the largest value in each section 3. Copying these max values to the output buffer
  1304. For more information, please refer to https://stackoverflow.com/questions/43430056/what-is-roi-layer-in-fast-rcnn.
  1305. Args:
  1306. x (Tensor): input feature, 4D-Tensor with the shape of [N,C,H,W],
  1307. where N is the batch size, C is the input channel, H is Height, W is weight.
  1308. The data type is float32 or float64.
  1309. boxes (Tensor): boxes (Regions of Interest) to pool over.
  1310. 2D-Tensor with the shape of [num_boxes,4].
  1311. Given as [[x1, y1, x2, y2], ...], (x1, y1) is the top left coordinates,
  1312. and (x2, y2) is the bottom right coordinates.
  1313. boxes_num (Tensor): the number of RoIs in each image, data type is int32.
  1314. output_size (int or tuple[int, int]): the pooled output size(h, w), data type is int32. If int, h and w are both equal to output_size.
  1315. spatial_scale (float, optional): multiplicative spatial scale factor to translate ROI coords from their input scale to the scale used when pooling. Default: 1.0.
  1316. name(str, optional): for detailed information, please refer to :ref:`api_guide_Name`. Usually name is no need to set and None by default. Default: None.
  1317. Returns:
  1318. pool_out (Tensor): the pooled feature, 4D-Tensor with the shape of [num_boxes, C, output_size[0], output_size[1]].
  1319. Examples:
  1320. .. code-block:: python
  1321. >>> import paddle
  1322. >>> from paddle.vision.ops import roi_pool
  1323. >>> data = paddle.rand([1, 256, 32, 32])
  1324. >>> boxes = paddle.rand([3, 4])
  1325. >>> boxes[:, 2] += boxes[:, 0] + 3
  1326. >>> boxes[:, 3] += boxes[:, 1] + 4
  1327. >>> boxes_num = paddle.to_tensor([3]).astype('int32')
  1328. >>> pool_out = roi_pool(data, boxes, boxes_num=boxes_num, output_size=3)
  1329. >>> print(pool_out.shape)
  1330. [3, 256, 3, 3]
  1331. """
  1332. check_type(output_size, 'output_size', (int, tuple), 'roi_pool')
  1333. if isinstance(output_size, int):
  1334. output_size = (output_size, output_size)
  1335. pooled_height, pooled_width = output_size
  1336. if in_dynamic_or_pir_mode():
  1337. assert (
  1338. boxes_num is not None
  1339. ), "boxes_num should not be None in dygraph mode."
  1340. return _C_ops.roi_pool(
  1341. x, boxes, boxes_num, pooled_height, pooled_width, spatial_scale
  1342. )
  1343. else:
  1344. check_variable_and_dtype(x, 'x', ['float32'], 'roi_pool')
  1345. check_variable_and_dtype(boxes, 'boxes', ['float32'], 'roi_pool')
  1346. helper = LayerHelper('roi_pool', **locals())
  1347. dtype = helper.input_dtype()
  1348. pool_out = helper.create_variable_for_type_inference(dtype)
  1349. argmaxes = helper.create_variable_for_type_inference(dtype='int32')
  1350. inputs = {
  1351. "X": x,
  1352. "ROIs": boxes,
  1353. }
  1354. if boxes_num is not None:
  1355. inputs['RoisNum'] = boxes_num
  1356. helper.append_op(
  1357. type="roi_pool",
  1358. inputs=inputs,
  1359. outputs={"Out": pool_out, "Argmax": argmaxes},
  1360. attrs={
  1361. "pooled_height": pooled_height,
  1362. "pooled_width": pooled_width,
  1363. "spatial_scale": spatial_scale,
  1364. },
  1365. )
  1366. return pool_out
  1367. class RoIPool(Layer):
  1368. """
  1369. This interface is used to construct a callable object of the `RoIPool` class. Please
  1370. refer to :ref:`api_paddle_vision_ops_roi_pool`.
  1371. Args:
  1372. output_size (int or tuple[int, int]): the pooled output size(h, w), data type is int32. If int, h and w are both equal to output_size.
  1373. spatial_scale (float, optional): multiplicative spatial scale factor to translate ROI coords from their input scale to the scale used when pooling. Default: 1.0.
  1374. Returns:
  1375. pool_out (Tensor): the pooled feature, 4D-Tensor with the shape of [num_boxes, C, output_size[0], output_size[1]].
  1376. Examples:
  1377. .. code-block:: python
  1378. >>> import paddle
  1379. >>> from paddle.vision.ops import RoIPool
  1380. >>> data = paddle.rand([1, 256, 32, 32])
  1381. >>> boxes = paddle.rand([3, 4])
  1382. >>> boxes[:, 2] += boxes[:, 0] + 3
  1383. >>> boxes[:, 3] += boxes[:, 1] + 4
  1384. >>> boxes_num = paddle.to_tensor([3]).astype('int32')
  1385. >>> roi_pool = RoIPool(output_size=(4, 3))
  1386. >>> pool_out = roi_pool(data, boxes, boxes_num)
  1387. >>> print(pool_out.shape)
  1388. [3, 256, 4, 3]
  1389. """
  1390. def __init__(self, output_size, spatial_scale=1.0):
  1391. super().__init__()
  1392. self._output_size = output_size
  1393. self._spatial_scale = spatial_scale
  1394. def forward(self, x, boxes, boxes_num):
  1395. return roi_pool(
  1396. x=x,
  1397. boxes=boxes,
  1398. boxes_num=boxes_num,
  1399. output_size=self._output_size,
  1400. spatial_scale=self._spatial_scale,
  1401. )
  1402. def extra_repr(self):
  1403. main_str = 'output_size={_output_size}, spatial_scale={_spatial_scale}'
  1404. return main_str.format(**self.__dict__)
  1405. def roi_align(
  1406. x,
  1407. boxes,
  1408. boxes_num,
  1409. output_size,
  1410. spatial_scale=1.0,
  1411. sampling_ratio=-1,
  1412. aligned=True,
  1413. name=None,
  1414. ):
  1415. """
  1416. Implementing the roi_align layer.
  1417. Region of Interest (RoI) Align operator (also known as RoI Align) is to
  1418. perform bilinear interpolation on inputs of nonuniform sizes to obtain
  1419. fixed-size feature maps (e.g. 7*7), as described in Mask R-CNN.
  1420. Dividing each region proposal into equal-sized sections with the pooled_width
  1421. and pooled_height. Location remains the origin result.
  1422. In each ROI bin, the value of the four regularly sampled locations are
  1423. computed directly through bilinear interpolation. The output is the mean of
  1424. four locations. Thus avoid the misaligned problem.
  1425. Args:
  1426. x (Tensor): Input feature, 4D-Tensor with the shape of [N,C,H,W],
  1427. where N is the batch size, C is the input channel, H is Height,
  1428. W is weight. The data type is float32 or float64.
  1429. boxes (Tensor): Boxes (RoIs, Regions of Interest) to pool over. It
  1430. should be a 2-D Tensor of shape (num_boxes, 4). The data type is
  1431. float32 or float64. Given as [[x1, y1, x2, y2], ...], (x1, y1) is
  1432. the top left coordinates, and (x2, y2) is the bottom right coordinates.
  1433. boxes_num (Tensor): The number of boxes contained in each picture in
  1434. the batch, the data type is int32.
  1435. output_size (int or Tuple[int, int]): The pooled output size(h, w), data
  1436. type is int32. If int, h and w are both equal to output_size.
  1437. spatial_scale (float32, optional): Multiplicative spatial scale factor to translate
  1438. ROI coords from their input scale to the scale used when pooling.
  1439. Default: 1.0.
  1440. sampling_ratio (int32, optional): number of sampling points in the interpolation
  1441. grid used to compute the output value of each pooled output bin.
  1442. If > 0, then exactly ``sampling_ratio x sampling_ratio`` sampling
  1443. points per bin are used.
  1444. If <= 0, then an adaptive number of grid points are used (computed
  1445. as ``ceil(roi_width / output_width)``, and likewise for height).
  1446. Default: -1.
  1447. aligned (bool, optional): If False, use the legacy implementation. If True, pixel
  1448. shift the box coordinates it by -0.5 for a better alignment with the
  1449. two neighboring pixel indices. This version is used in Detectron2.
  1450. Default: True.
  1451. name(str, optional): For detailed information, please refer to :
  1452. ref:`api_guide_Name`. Usually name is no need to set and None by
  1453. default. Default: None.
  1454. Returns:
  1455. The output of ROIAlignOp is a 4-D tensor with shape (num_boxes,\
  1456. channels, pooled_h, pooled_w). The data type is float32 or float64.
  1457. Examples:
  1458. .. code-block:: python
  1459. >>> import paddle
  1460. >>> from paddle.vision.ops import roi_align
  1461. >>> data = paddle.rand([1, 256, 32, 32])
  1462. >>> boxes = paddle.rand([3, 4])
  1463. >>> boxes[:, 2] += boxes[:, 0] + 3
  1464. >>> boxes[:, 3] += boxes[:, 1] + 4
  1465. >>> boxes_num = paddle.to_tensor([3]).astype('int32')
  1466. >>> align_out = roi_align(data, boxes, boxes_num, output_size=3)
  1467. >>> print(align_out.shape)
  1468. [3, 256, 3, 3]
  1469. """
  1470. check_type(output_size, 'output_size', (int, tuple), 'roi_align')
  1471. if isinstance(output_size, int):
  1472. output_size = (output_size, output_size)
  1473. pooled_height, pooled_width = output_size
  1474. if in_dynamic_or_pir_mode():
  1475. assert (
  1476. boxes_num is not None
  1477. ), "boxes_num should not be None in dygraph mode."
  1478. return _C_ops.roi_align(
  1479. x,
  1480. boxes,
  1481. boxes_num,
  1482. pooled_height,
  1483. pooled_width,
  1484. spatial_scale,
  1485. sampling_ratio,
  1486. aligned,
  1487. )
  1488. else:
  1489. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'roi_align')
  1490. check_variable_and_dtype(
  1491. boxes, 'boxes', ['float32', 'float64'], 'roi_align'
  1492. )
  1493. helper = LayerHelper('roi_align', **locals())
  1494. dtype = helper.input_dtype()
  1495. align_out = helper.create_variable_for_type_inference(dtype)
  1496. inputs = {
  1497. "X": x,
  1498. "ROIs": boxes,
  1499. }
  1500. if boxes_num is not None:
  1501. inputs['RoisNum'] = boxes_num
  1502. helper.append_op(
  1503. type="roi_align",
  1504. inputs=inputs,
  1505. outputs={"Out": align_out},
  1506. attrs={
  1507. "pooled_height": pooled_height,
  1508. "pooled_width": pooled_width,
  1509. "spatial_scale": spatial_scale,
  1510. "sampling_ratio": sampling_ratio,
  1511. "aligned": aligned,
  1512. },
  1513. )
  1514. return align_out
  1515. class RoIAlign(Layer):
  1516. """
  1517. This interface is used to construct a callable object of the `RoIAlign` class.
  1518. Please refer to :ref:`api_paddle_vision_ops_roi_align`.
  1519. Args:
  1520. output_size (int or tuple[int, int]): The pooled output size(h, w),
  1521. data type is int32. If int, h and w are both equal to output_size.
  1522. spatial_scale (float32, optional): Multiplicative spatial scale factor
  1523. to translate ROI coords from their input scale to the scale used
  1524. when pooling. Default: 1.0.
  1525. Returns:
  1526. The output of ROIAlign operator is a 4-D tensor with \
  1527. shape (num_boxes, channels, pooled_h, pooled_w).
  1528. Examples:
  1529. .. code-block:: python
  1530. >>> import paddle
  1531. >>> from paddle.vision.ops import RoIAlign
  1532. >>> data = paddle.rand([1, 256, 32, 32])
  1533. >>> boxes = paddle.rand([3, 4])
  1534. >>> boxes[:, 2] += boxes[:, 0] + 3
  1535. >>> boxes[:, 3] += boxes[:, 1] + 4
  1536. >>> boxes_num = paddle.to_tensor([3]).astype('int32')
  1537. >>> roi_align = RoIAlign(output_size=(4, 3))
  1538. >>> align_out = roi_align(data, boxes, boxes_num)
  1539. >>> print(align_out.shape)
  1540. [3, 256, 4, 3]
  1541. """
  1542. def __init__(self, output_size, spatial_scale=1.0):
  1543. super().__init__()
  1544. self._output_size = output_size
  1545. self._spatial_scale = spatial_scale
  1546. def forward(self, x, boxes, boxes_num, aligned=True):
  1547. return roi_align(
  1548. x=x,
  1549. boxes=boxes,
  1550. boxes_num=boxes_num,
  1551. output_size=self._output_size,
  1552. spatial_scale=self._spatial_scale,
  1553. aligned=aligned,
  1554. )
  1555. class ConvNormActivation(Sequential):
  1556. """
  1557. Configurable block used for Convolution-Normalization-Activation blocks.
  1558. This code is based on the torchvision code with modifications.
  1559. You can also see at https://github.com/pytorch/vision/blob/main/torchvision/ops/misc.py#L68
  1560. Args:
  1561. in_channels (int): Number of channels in the input image
  1562. out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block
  1563. kernel_size: (int|list|tuple, optional): Size of the convolving kernel. Default: 3
  1564. stride (int|list|tuple, optional): Stride of the convolution. Default: 1
  1565. padding (int|str|tuple|list, optional): Padding added to all four sides of the input. Default: None,
  1566. in which case it will calculated as ``padding = (kernel_size - 1) // 2 * dilation``
  1567. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
  1568. norm_layer (Callable[..., paddle.nn.Layer], optional): Norm layer that will be stacked on top of the convolution layer.
  1569. If ``None`` this layer wont be used. Default: ``paddle.nn.BatchNorm2D``
  1570. activation_layer (Callable[..., paddle.nn.Layer], optional): Activation function which will be stacked on top of the normalization
  1571. layer (if not ``None``), otherwise on top of the conv layer. If ``None`` this layer wont be used. Default: ``paddle.nn.ReLU``
  1572. dilation (int): Spacing between kernel elements. Default: 1
  1573. bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.
  1574. """
  1575. def __init__(
  1576. self,
  1577. in_channels,
  1578. out_channels,
  1579. kernel_size=3,
  1580. stride=1,
  1581. padding=None,
  1582. groups=1,
  1583. norm_layer=BatchNorm2D,
  1584. activation_layer=ReLU,
  1585. dilation=1,
  1586. bias=None,
  1587. ):
  1588. if padding is None:
  1589. padding = (kernel_size - 1) // 2 * dilation
  1590. if bias is None:
  1591. bias = norm_layer is None
  1592. layers = [
  1593. Conv2D(
  1594. in_channels,
  1595. out_channels,
  1596. kernel_size,
  1597. stride,
  1598. padding,
  1599. dilation=dilation,
  1600. groups=groups,
  1601. bias_attr=bias,
  1602. )
  1603. ]
  1604. if norm_layer is not None:
  1605. layers.append(norm_layer(out_channels))
  1606. if activation_layer is not None:
  1607. layers.append(activation_layer())
  1608. super().__init__(*layers)
  1609. def nms(
  1610. boxes,
  1611. iou_threshold=0.3,
  1612. scores=None,
  1613. category_idxs=None,
  1614. categories=None,
  1615. top_k=None,
  1616. ):
  1617. r"""
  1618. This operator implements non-maximum suppression. Non-maximum suppression (NMS)
  1619. is used to select one bounding box out of many overlapping bounding boxes in object detection.
  1620. Boxes with IoU > iou_threshold will be considered as overlapping boxes,
  1621. just one with highest score can be kept. Here IoU is Intersection Over Union,
  1622. which can be computed by:
  1623. .. math::
  1624. IoU = \frac{intersection\_area(box1, box2)}{union\_area(box1, box2)}
  1625. If scores are provided, input boxes will be sorted by their scores firstly.
  1626. If category_idxs and categories are provided, NMS will be performed with a batched style,
  1627. which means NMS will be applied to each category respectively and results of each category
  1628. will be concatenated and sorted by scores.
  1629. If K is provided, only the first k elements will be returned. Otherwise, all box indices sorted by scores will be returned.
  1630. Args:
  1631. boxes(Tensor): The input boxes data to be computed, it's a 2D-Tensor with
  1632. the shape of [num_boxes, 4]. The data type is float32 or float64.
  1633. Given as [[x1, y1, x2, y2], …], (x1, y1) is the top left coordinates,
  1634. and (x2, y2) is the bottom right coordinates.
  1635. Their relation should be ``0 <= x1 < x2 && 0 <= y1 < y2``.
  1636. iou_threshold(float32, optional): IoU threshold for determine overlapping boxes. Default value: 0.3.
  1637. scores(Tensor, optional): Scores corresponding to boxes, it's a 1D-Tensor with
  1638. shape of [num_boxes]. The data type is float32 or float64. Default: None.
  1639. category_idxs(Tensor, optional): Category indices corresponding to boxes.
  1640. it's a 1D-Tensor with shape of [num_boxes]. The data type is int64. Default: None.
  1641. categories(List, optional): A list of unique id of all categories. The data type is int64. Default: None.
  1642. top_k(int64, optional): The top K boxes who has higher score and kept by NMS preds to
  1643. consider. top_k should be smaller equal than num_boxes. Default: None.
  1644. Returns:
  1645. Tensor: 1D-Tensor with the shape of [num_boxes]. Indices of boxes kept by NMS.
  1646. Examples:
  1647. .. code-block:: python
  1648. >>> import paddle
  1649. >>> paddle.seed(2023)
  1650. >>> boxes = paddle.rand([4, 4]).astype('float32')
  1651. >>> boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
  1652. >>> boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
  1653. >>> print(boxes)
  1654. Tensor(shape=[4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1655. [[0.86583614, 0.52014720, 1.12544549, 1.42540050],
  1656. [0.42400089, 0.40641287, 1.39420986, 1.15078652],
  1657. [0.51785129, 0.73292869, 1.49571705, 0.77608776],
  1658. [0.42639419, 0.71958369, 0.63450879, 0.91689879]])
  1659. >>> out = paddle.vision.ops.nms(boxes, 0.1)
  1660. >>> print(out)
  1661. Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
  1662. [0, 2, 3])
  1663. >>> scores = paddle.to_tensor([0.6, 0.7, 0.4, 0.233])
  1664. >>> categories = [0, 1, 2, 3]
  1665. >>> category_idxs = paddle.to_tensor([2, 0, 0, 3], dtype="int64")
  1666. >>> out = paddle.vision.ops.nms(boxes,
  1667. ... 0.1,
  1668. ... paddle.to_tensor(scores),
  1669. ... paddle.to_tensor(category_idxs),
  1670. ... categories,
  1671. ... 4)
  1672. >>> print(out)
  1673. Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
  1674. [1, 0, 2, 3])
  1675. """
  1676. def _nms(boxes, iou_threshold):
  1677. if in_dynamic_or_pir_mode():
  1678. return _C_ops.nms(boxes, iou_threshold)
  1679. else:
  1680. helper = LayerHelper('nms', **locals())
  1681. out = helper.create_variable_for_type_inference('int64')
  1682. helper.append_op(
  1683. type='nms',
  1684. inputs={'Boxes': boxes},
  1685. outputs={'KeepBoxesIdxs': out},
  1686. attrs={'iou_threshold': iou_threshold},
  1687. )
  1688. return out
  1689. if scores is None:
  1690. return _nms(boxes, iou_threshold)
  1691. import paddle
  1692. if category_idxs is None:
  1693. sorted_global_indices = paddle.argsort(scores, descending=True)
  1694. sorted_keep_boxes_indices = _nms(
  1695. boxes[sorted_global_indices], iou_threshold
  1696. )
  1697. return sorted_global_indices[sorted_keep_boxes_indices]
  1698. if top_k is not None:
  1699. assert (
  1700. top_k <= scores.shape[0]
  1701. ), "top_k should be smaller equal than the number of boxes"
  1702. assert (
  1703. categories is not None
  1704. ), "if category_idxs is given, categories which is a list of unique id of all categories is necessary"
  1705. mask = paddle.zeros_like(scores, dtype='int32')
  1706. for category_id in categories:
  1707. cur_category_boxes_idxs = paddle.where(
  1708. paddle.equal(category_idxs, paddle.to_tensor(category_id))
  1709. )[0]
  1710. shape = cur_category_boxes_idxs.shape[0]
  1711. cur_category_boxes_idxs = paddle.reshape(
  1712. cur_category_boxes_idxs, [shape]
  1713. )
  1714. if shape == 0:
  1715. continue
  1716. elif shape == 1:
  1717. mask[cur_category_boxes_idxs] = 1
  1718. continue
  1719. cur_category_boxes = boxes[cur_category_boxes_idxs]
  1720. cur_category_scores = scores[cur_category_boxes_idxs]
  1721. cur_category_sorted_indices = paddle.argsort(
  1722. cur_category_scores, descending=True
  1723. )
  1724. cur_category_sorted_boxes = cur_category_boxes[
  1725. cur_category_sorted_indices
  1726. ]
  1727. cur_category_keep_boxes_sub_idxs = cur_category_sorted_indices[
  1728. _nms(cur_category_sorted_boxes, iou_threshold)
  1729. ]
  1730. updates = paddle.ones_like(
  1731. cur_category_boxes_idxs[cur_category_keep_boxes_sub_idxs],
  1732. dtype='int32',
  1733. )
  1734. mask = paddle.scatter(
  1735. mask,
  1736. cur_category_boxes_idxs[cur_category_keep_boxes_sub_idxs],
  1737. updates,
  1738. overwrite=True,
  1739. )
  1740. keep_boxes_idxs = paddle.where(mask)[0]
  1741. shape = keep_boxes_idxs.shape[0]
  1742. keep_boxes_idxs = paddle.reshape(keep_boxes_idxs, [shape])
  1743. sorted_sub_indices = paddle.argsort(
  1744. scores[keep_boxes_idxs], descending=True
  1745. )
  1746. if top_k is None:
  1747. return keep_boxes_idxs[sorted_sub_indices]
  1748. if in_dygraph_mode():
  1749. top_k = shape if shape < top_k else top_k
  1750. _, topk_sub_indices = paddle.topk(scores[keep_boxes_idxs], top_k)
  1751. return keep_boxes_idxs[topk_sub_indices]
  1752. return keep_boxes_idxs[sorted_sub_indices][:top_k]
  1753. def generate_proposals(
  1754. scores,
  1755. bbox_deltas,
  1756. img_size,
  1757. anchors,
  1758. variances,
  1759. pre_nms_top_n=6000,
  1760. post_nms_top_n=1000,
  1761. nms_thresh=0.5,
  1762. min_size=0.1,
  1763. eta=1.0,
  1764. pixel_offset=False,
  1765. return_rois_num=False,
  1766. name=None,
  1767. ):
  1768. """
  1769. This operation proposes RoIs according to each box with their
  1770. probability to be a foreground object. And
  1771. the proposals of RPN output are calculated by anchors, bbox_deltas and scores. Final proposals
  1772. could be used to train detection net.
  1773. For generating proposals, this operation performs following steps:
  1774. 1. Transpose and resize scores and bbox_deltas in size of
  1775. (H * W * A, 1) and (H * W * A, 4)
  1776. 2. Calculate box locations as proposals candidates.
  1777. 3. Clip boxes to image
  1778. 4. Remove predicted boxes with small area.
  1779. 5. Apply non-maximum suppression (NMS) to get final proposals as output.
  1780. Args:
  1781. scores (Tensor): A 4-D Tensor with shape [N, A, H, W] represents
  1782. the probability for each box to be an object.
  1783. N is batch size, A is number of anchors, H and W are height and
  1784. width of the feature map. The data type must be float32.
  1785. bbox_deltas (Tensor): A 4-D Tensor with shape [N, 4*A, H, W]
  1786. represents the difference between predicted box location and
  1787. anchor location. The data type must be float32.
  1788. img_size (Tensor): A 2-D Tensor with shape [N, 2] represents origin
  1789. image shape information for N batch, including height and width of the input sizes.
  1790. The data type can be float32 or float64.
  1791. anchors (Tensor): A 4-D Tensor represents the anchors with a layout
  1792. of [H, W, A, 4]. H and W are height and width of the feature map,
  1793. num_anchors is the box count of each position. Each anchor is
  1794. in (xmin, ymin, xmax, ymax) format an unnormalized. The data type must be float32.
  1795. variances (Tensor): A 4-D Tensor. The expanded variances of anchors with a layout of
  1796. [H, W, num_priors, 4]. Each variance is in
  1797. (xcenter, ycenter, w, h) format. The data type must be float32.
  1798. pre_nms_top_n (float, optional): Number of total bboxes to be kept per
  1799. image before NMS. `6000` by default.
  1800. post_nms_top_n (float, optional): Number of total bboxes to be kept per
  1801. image after NMS. `1000` by default.
  1802. nms_thresh (float, optional): Threshold in NMS. The data type must be float32. `0.5` by default.
  1803. min_size (float, optional): Remove predicted boxes with either height or
  1804. width less than this value. `0.1` by default.
  1805. eta(float, optional): Apply in adaptive NMS, only works if adaptive `threshold > 0.5`,
  1806. `adaptive_threshold = adaptive_threshold * eta` in each iteration. 1.0 by default.
  1807. pixel_offset (bool, optional): Whether there is pixel offset. If True, the offset of `img_size` will be 1. 'False' by default.
  1808. return_rois_num (bool, optional): Whether to return `rpn_rois_num` . When setting True, it will return a 1D Tensor with shape [N, ] that includes Rois's
  1809. num of each image in one batch. 'False' by default.
  1810. name(str, optional): For detailed information, please refer
  1811. to :ref:`api_guide_Name`. Usually name is no need to set and
  1812. None by default.
  1813. Returns:
  1814. - rpn_rois (Tensor): The generated RoIs. 2-D Tensor with shape ``[N, 4]`` while ``N`` is the number of RoIs. The data type is the same as ``scores``.
  1815. - rpn_roi_probs (Tensor): The scores of generated RoIs. 2-D Tensor with shape ``[N, 1]`` while ``N`` is the number of RoIs. The data type is the same as ``scores``.
  1816. - rpn_rois_num (Tensor): Rois's num of each image in one batch. 1-D Tensor with shape ``[B,]`` while ``B`` is the batch size. And its sum equals to RoIs number ``N`` .
  1817. Examples:
  1818. .. code-block:: python
  1819. >>> import paddle
  1820. >>> paddle.seed(2023)
  1821. >>> scores = paddle.rand((2,4,5,5), dtype=paddle.float32)
  1822. >>> bbox_deltas = paddle.rand((2, 16, 5, 5), dtype=paddle.float32)
  1823. >>> img_size = paddle.to_tensor([[224.0, 224.0], [224.0, 224.0]])
  1824. >>> anchors = paddle.rand((2,5,4,4), dtype=paddle.float32)
  1825. >>> variances = paddle.rand((2,5,10,4), dtype=paddle.float32)
  1826. >>> rois, roi_probs, roi_nums = paddle.vision.ops.generate_proposals(scores, bbox_deltas,
  1827. ... img_size, anchors, variances, return_rois_num=True)
  1828. >>> # doctest: +SKIP('random sample')
  1829. >>> print(rois, roi_probs, roi_nums)
  1830. Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1831. [[0., 0., 0., 0.],
  1832. [0., 0., 0., 0.]])
  1833. Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
  1834. [[0.],
  1835. [0.]])
  1836. Tensor(shape=[2], dtype=int32, place=Place(cpu), stop_gradient=True,
  1837. [1, 1])
  1838. """
  1839. if in_dygraph_mode():
  1840. assert (
  1841. return_rois_num
  1842. ), "return_rois_num should be True in dygraph mode."
  1843. attrs = (
  1844. pre_nms_top_n,
  1845. post_nms_top_n,
  1846. nms_thresh,
  1847. min_size,
  1848. eta,
  1849. pixel_offset,
  1850. )
  1851. rpn_rois, rpn_roi_probs, rpn_rois_num = _C_ops.generate_proposals(
  1852. scores, bbox_deltas, img_size, anchors, variances, *attrs
  1853. )
  1854. return rpn_rois, rpn_roi_probs, rpn_rois_num
  1855. elif in_pir_mode():
  1856. assert (
  1857. return_rois_num
  1858. ), "return_rois_num should be True in PaddlePaddle inner op mode."
  1859. rpn_rois, rpn_roi_probs, rpn_rois_num = _C_ops.generate_proposals(
  1860. scores,
  1861. bbox_deltas,
  1862. img_size,
  1863. anchors,
  1864. variances,
  1865. pre_nms_top_n,
  1866. post_nms_top_n,
  1867. nms_thresh,
  1868. min_size,
  1869. eta,
  1870. pixel_offset,
  1871. )
  1872. rpn_rois.stop_gradient = True
  1873. rpn_roi_probs.stop_gradient = True
  1874. rpn_rois_num.stop_gradient = True
  1875. return rpn_rois, rpn_roi_probs, rpn_rois_num
  1876. else:
  1877. helper = LayerHelper('generate_proposals_v2', **locals())
  1878. check_variable_and_dtype(
  1879. scores, 'scores', ['float32'], 'generate_proposals_v2'
  1880. )
  1881. check_variable_and_dtype(
  1882. bbox_deltas, 'bbox_deltas', ['float32'], 'generate_proposals_v2'
  1883. )
  1884. check_variable_and_dtype(
  1885. img_size,
  1886. 'img_size',
  1887. ['float32', 'float64'],
  1888. 'generate_proposals_v2',
  1889. )
  1890. check_variable_and_dtype(
  1891. anchors, 'anchors', ['float32'], 'generate_proposals_v2'
  1892. )
  1893. check_variable_and_dtype(
  1894. variances, 'variances', ['float32'], 'generate_proposals_v2'
  1895. )
  1896. rpn_rois = helper.create_variable_for_type_inference(
  1897. dtype=bbox_deltas.dtype
  1898. )
  1899. rpn_roi_probs = helper.create_variable_for_type_inference(
  1900. dtype=scores.dtype
  1901. )
  1902. outputs = {
  1903. 'RpnRois': rpn_rois,
  1904. 'RpnRoiProbs': rpn_roi_probs,
  1905. }
  1906. if return_rois_num:
  1907. rpn_rois_num = helper.create_variable_for_type_inference(
  1908. dtype='int32'
  1909. )
  1910. rpn_rois_num.stop_gradient = True
  1911. outputs['RpnRoisNum'] = rpn_rois_num
  1912. helper.append_op(
  1913. type="generate_proposals_v2",
  1914. inputs={
  1915. 'Scores': scores,
  1916. 'BboxDeltas': bbox_deltas,
  1917. 'ImShape': img_size,
  1918. 'Anchors': anchors,
  1919. 'Variances': variances,
  1920. },
  1921. attrs={
  1922. 'pre_nms_topN': pre_nms_top_n,
  1923. 'post_nms_topN': post_nms_top_n,
  1924. 'nms_thresh': nms_thresh,
  1925. 'min_size': min_size,
  1926. 'eta': eta,
  1927. 'pixel_offset': pixel_offset,
  1928. },
  1929. outputs=outputs,
  1930. )
  1931. rpn_rois.stop_gradient = True
  1932. rpn_roi_probs.stop_gradient = True
  1933. if not return_rois_num:
  1934. rpn_rois_num = None
  1935. return rpn_rois, rpn_roi_probs, rpn_rois_num
  1936. def matrix_nms(
  1937. bboxes,
  1938. scores,
  1939. score_threshold,
  1940. post_threshold,
  1941. nms_top_k,
  1942. keep_top_k,
  1943. use_gaussian=False,
  1944. gaussian_sigma=2.0,
  1945. background_label=0,
  1946. normalized=True,
  1947. return_index=False,
  1948. return_rois_num=True,
  1949. name=None,
  1950. ):
  1951. """
  1952. This operator does matrix non maximum suppression (NMS).
  1953. First selects a subset of candidate bounding boxes that have higher scores
  1954. than score_threshold (if provided), then the top k candidate is selected if
  1955. nms_top_k is larger than -1. Score of the remaining candidate are then
  1956. decayed according to the Matrix NMS scheme.
  1957. After NMS step, at most keep_top_k number of total bboxes are to be kept
  1958. per image if keep_top_k is larger than -1.
  1959. Args:
  1960. bboxes (Tensor): A 3-D Tensor with shape [N, M, 4] represents the
  1961. predicted locations of M bounding bboxes,
  1962. N is the batch size. Each bounding box has four
  1963. coordinate values and the layout is
  1964. [xmin, ymin, xmax, ymax], when box size equals to 4.
  1965. The data type is float32 or float64.
  1966. scores (Tensor): A 3-D Tensor with shape [N, C, M]
  1967. represents the predicted confidence predictions.
  1968. N is the batch size, C is the class number, M is
  1969. number of bounding boxes. For each category there
  1970. are total M scores which corresponding M bounding
  1971. boxes. Please note, M is equal to the 2nd dimension
  1972. of BBoxes. The data type is float32 or float64.
  1973. score_threshold (float): Threshold to filter out bounding boxes with
  1974. low confidence score.
  1975. post_threshold (float): Threshold to filter out bounding boxes with
  1976. low confidence score AFTER decaying.
  1977. nms_top_k (int): Maximum number of detections to be kept according to
  1978. the confidences after the filtering detections based
  1979. on score_threshold.
  1980. keep_top_k (int): Number of total bboxes to be kept per image after NMS
  1981. step. -1 means keeping all bboxes after NMS step.
  1982. use_gaussian (bool, optional): Use Gaussian as the decay function. Default: False
  1983. gaussian_sigma (float, optional): Sigma for Gaussian decay function. Default: 2.0
  1984. background_label (int, optional): The index of background label, the background
  1985. label will be ignored. If set to -1, then all
  1986. categories will be considered. Default: 0
  1987. normalized (bool, optional): Whether detections are normalized. Default: True
  1988. return_index(bool, optional): Whether return selected index. Default: False
  1989. return_rois_num(bool, optional): whether return rois_num. Default: True
  1990. name(str, optional): Name of the matrix nms op. Default: None.
  1991. Returns:
  1992. - A tuple with three Tensor, (Out, Index, RoisNum) if return_index is True,
  1993. otherwise, a tuple with two Tensor (Out, RoisNum) is returned.
  1994. - Out (Tensor), A 2-D Tensor with shape [No, 6] containing the
  1995. detection results.
  1996. Each row has 6 values, [label, confidence, xmin, ymin, xmax, ymax]
  1997. - Index (Tensor), A 2-D Tensor with shape [No, 1] containing the
  1998. selected indices, which are absolute values cross batches.
  1999. - rois_num (Tensor), A 1-D Tensor with shape [N] containing
  2000. the number of detected boxes in each image.
  2001. Examples:
  2002. .. code-block:: python
  2003. >>> import paddle
  2004. >>> from paddle.vision.ops import matrix_nms
  2005. >>> boxes = paddle.rand([4, 1, 4])
  2006. >>> boxes[..., 2] = boxes[..., 0] + boxes[..., 2]
  2007. >>> boxes[..., 3] = boxes[..., 1] + boxes[..., 3]
  2008. >>> scores = paddle.rand([4, 80, 1])
  2009. >>> out = matrix_nms(bboxes=boxes, scores=scores, background_label=0,
  2010. ... score_threshold=0.5, post_threshold=0.1,
  2011. ... nms_top_k=400, keep_top_k=200, normalized=False)
  2012. """
  2013. if in_dynamic_or_pir_mode():
  2014. out, index, rois_num = _C_ops.matrix_nms(
  2015. bboxes,
  2016. scores,
  2017. score_threshold,
  2018. nms_top_k,
  2019. keep_top_k,
  2020. post_threshold,
  2021. use_gaussian,
  2022. gaussian_sigma,
  2023. background_label,
  2024. normalized,
  2025. )
  2026. if not return_index:
  2027. index = None
  2028. if not return_rois_num:
  2029. rois_num = None
  2030. return out, rois_num, index
  2031. else:
  2032. check_variable_and_dtype(
  2033. bboxes, 'BBoxes', ['float32', 'float64'], 'matrix_nms'
  2034. )
  2035. check_variable_and_dtype(
  2036. scores, 'Scores', ['float32', 'float64'], 'matrix_nms'
  2037. )
  2038. check_type(score_threshold, 'score_threshold', float, 'matrix_nms')
  2039. check_type(post_threshold, 'post_threshold', float, 'matrix_nms')
  2040. check_type(nms_top_k, 'nums_top_k', int, 'matrix_nms')
  2041. check_type(keep_top_k, 'keep_top_k', int, 'matrix_nms')
  2042. check_type(normalized, 'normalized', bool, 'matrix_nms')
  2043. check_type(use_gaussian, 'use_gaussian', bool, 'matrix_nms')
  2044. check_type(gaussian_sigma, 'gaussian_sigma', float, 'matrix_nms')
  2045. check_type(background_label, 'background_label', int, 'matrix_nms')
  2046. helper = LayerHelper('matrix_nms', **locals())
  2047. output = helper.create_variable_for_type_inference(dtype=bboxes.dtype)
  2048. index = helper.create_variable_for_type_inference(dtype='int32')
  2049. outputs = {'Out': output, 'Index': index}
  2050. if return_rois_num:
  2051. rois_num = helper.create_variable_for_type_inference(dtype='int32')
  2052. outputs['RoisNum'] = rois_num
  2053. helper.append_op(
  2054. type="matrix_nms",
  2055. inputs={'BBoxes': bboxes, 'Scores': scores},
  2056. attrs={
  2057. 'background_label': background_label,
  2058. 'score_threshold': score_threshold,
  2059. 'post_threshold': post_threshold,
  2060. 'nms_top_k': nms_top_k,
  2061. 'gaussian_sigma': gaussian_sigma,
  2062. 'use_gaussian': use_gaussian,
  2063. 'keep_top_k': keep_top_k,
  2064. 'normalized': normalized,
  2065. },
  2066. outputs=outputs,
  2067. )
  2068. output.stop_gradient = True
  2069. if not return_index:
  2070. index = None
  2071. if not return_rois_num:
  2072. rois_num = None
  2073. return output, rois_num, index