det.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Copyright (c) 2021 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 logging
  15. from . import cv
  16. from .cv.models.utils.visualize import visualize_detection, draw_pr_curve
  17. from paddlex.cv.transforms import det_transforms
  18. transforms = det_transforms
  19. visualize = visualize_detection
  20. draw_pr_curve = draw_pr_curve
  21. class FasterRCNN(cv.models.FasterRCNN):
  22. def __init__(self,
  23. num_classes=81,
  24. backbone='ResNet50',
  25. with_fpn=True,
  26. aspect_ratios=[0.5, 1.0, 2.0],
  27. anchor_sizes=[32, 64, 128, 256, 512],
  28. with_dcn=None,
  29. rpn_cls_loss=None,
  30. rpn_focal_loss_alpha=None,
  31. rpn_focal_loss_gamma=None,
  32. rcnn_bbox_loss=None,
  33. rcnn_nms=None,
  34. keep_top_k=100,
  35. nms_threshold=0.5,
  36. score_threshold=0.05,
  37. softnms_sigma=None,
  38. bbox_assigner=None,
  39. fpn_num_channels=256,
  40. input_channel=None,
  41. rpn_batch_size_per_im=256,
  42. rpn_fg_fraction=0.5,
  43. test_pre_nms_top_n=None,
  44. test_post_nms_top_n=1000):
  45. if with_dcn is not None:
  46. logging.warning(
  47. "`with_dcn` is deprecated in PaddleX 2.0 and won't take effect. Defaults to False."
  48. )
  49. if rpn_cls_loss is not None:
  50. logging.warning(
  51. "`rpn_cls_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  52. "Defaults to 'SigmoidCrossEntropy'.")
  53. if rpn_focal_loss_alpha is not None or rpn_focal_loss_gamma is not None:
  54. logging.warning(
  55. "Focal loss is deprecated in PaddleX 2.0."
  56. " `rpn_focal_loss_alpha` and `rpn_focal_loss_gamma` won't take effect."
  57. )
  58. if rcnn_bbox_loss is not None:
  59. logging.warning(
  60. "`rcnn_bbox_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  61. "Defaults to 'SmoothL1Loss'")
  62. if rcnn_nms is not None:
  63. logging.warning(
  64. "MultiClassSoftNMS is deprecated in PaddleX 2.0. "
  65. "`rcnn_nms` and `softnms_sigma` won't take effect. MultiClassNMS will be used by default"
  66. )
  67. if bbox_assigner is not None:
  68. logging.warning(
  69. "`bbox_assigner` is deprecated in PaddleX 2.0 and won't take effect. "
  70. "Defaults to 'BBoxAssigner'")
  71. if input_channel is not None:
  72. logging.warning(
  73. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  74. )
  75. super(FasterRCNN, self).__init__(
  76. num_classes=num_classes - 1,
  77. backbone=backbone,
  78. with_fpn=with_fpn,
  79. aspect_ratios=aspect_ratios,
  80. anchor_sizes=anchor_sizes,
  81. keep_top_k=keep_top_k,
  82. nms_threshold=nms_threshold,
  83. score_threshold=score_threshold,
  84. fpn_num_channels=fpn_num_channels,
  85. rpn_batch_size_per_im=rpn_batch_size_per_im,
  86. rpn_fg_fraction=rpn_fg_fraction,
  87. test_pre_nms_top_n=test_pre_nms_top_n,
  88. test_post_nms_top_n=test_post_nms_top_n)
  89. class YOLOv3(cv.models.YOLOv3):
  90. def __init__(self,
  91. num_classes=80,
  92. backbone='MobileNetV1',
  93. anchors=None,
  94. anchor_masks=None,
  95. ignore_threshold=0.7,
  96. nms_score_threshold=0.01,
  97. nms_topk=1000,
  98. nms_keep_topk=100,
  99. nms_iou_threshold=0.45,
  100. label_smooth=False,
  101. train_random_shapes=None,
  102. input_channel=None):
  103. if train_random_shapes is not None:
  104. logging.warning(
  105. "`train_random_shapes` is deprecated in PaddleX 2.0 and won't take effect. "
  106. "To apply multi_scale training, please refer to paddlex.transforms.BatchRandomResize: "
  107. "'https://github.com/PaddlePaddle/PaddleX/blob/develop/dygraph/paddlex/cv/transforms/batch_operators.py#L53'"
  108. )
  109. if input_channel is not None:
  110. logging.warning(
  111. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  112. )
  113. super(YOLOv3, self).__init__(
  114. num_classes=num_classes,
  115. backbone=backbone,
  116. anchors=anchors,
  117. anchor_masks=anchor_masks,
  118. ignore_threshold=ignore_threshold,
  119. nms_score_threshold=nms_score_threshold,
  120. nms_topk=nms_topk,
  121. nms_keep_topk=nms_keep_topk,
  122. nms_iou_threshold=nms_iou_threshold,
  123. label_smooth=label_smooth)
  124. class PPYOLO(cv.models.PPYOLO):
  125. def __init__(
  126. self,
  127. num_classes=80,
  128. backbone='ResNet50_vd_ssld',
  129. with_dcn_v2=None,
  130. # YOLO Head
  131. anchors=None,
  132. anchor_masks=None,
  133. use_coord_conv=True,
  134. use_iou_aware=True,
  135. use_spp=True,
  136. use_drop_block=True,
  137. scale_x_y=1.05,
  138. # PPYOLO Loss
  139. ignore_threshold=0.7,
  140. label_smooth=False,
  141. use_iou_loss=True,
  142. # NMS
  143. use_matrix_nms=True,
  144. nms_score_threshold=0.01,
  145. nms_topk=1000,
  146. nms_keep_topk=100,
  147. nms_iou_threshold=0.45,
  148. train_random_shapes=None,
  149. input_channel=None):
  150. if with_dcn_v2 is not None:
  151. logging.warning(
  152. "`with_dcn_v2` is deprecated in PaddleX 2.0 and will not take effect. "
  153. "To use backbone with deformable convolutional networks, "
  154. "please specify in `backbone_name`. "
  155. "Currently the only backbone with dcn is 'ResNet50_vd_dcn'.")
  156. if train_random_shapes is not None:
  157. logging.warning(
  158. "`train_random_shapes` is deprecated in PaddleX 2.0 and won't take effect. "
  159. "To apply multi_scale training, please refer to paddlex.transforms.BatchRandomResize: "
  160. "'https://github.com/PaddlePaddle/PaddleX/blob/develop/dygraph/paddlex/cv/transforms/batch_operators.py#L53'"
  161. )
  162. if input_channel is not None:
  163. logging.warning(
  164. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  165. )
  166. super(PPYOLO, self).__init__(
  167. num_classes=num_classes,
  168. backbone=backbone,
  169. anchors=anchors,
  170. anchor_masks=anchor_masks,
  171. use_coord_conv=use_coord_conv,
  172. use_iou_aware=use_iou_aware,
  173. use_spp=use_spp,
  174. use_drop_block=use_drop_block,
  175. scale_x_y=scale_x_y,
  176. ignore_threshold=ignore_threshold,
  177. label_smooth=label_smooth,
  178. use_iou_loss=use_iou_loss,
  179. use_matrix_nms=use_matrix_nms,
  180. nms_score_threshold=nms_score_threshold,
  181. nms_topk=nms_topk,
  182. nms_keep_topk=nms_keep_topk,
  183. nms_iou_threshold=nms_iou_threshold)