det.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 copy
  15. import os.path as osp
  16. import numpy as np
  17. import paddle
  18. from paddleslim import L1NormFilterPruner
  19. from . import cv
  20. from .cv.models.utils.visualize import visualize_detection, draw_pr_curve
  21. from paddlex.cv.transforms import det_transforms
  22. from paddlex.cv.transforms.operators import _NormalizeBox, _PadBox, _BboxXYXY2XYWH
  23. from paddlex.cv.transforms.batch_operators import BatchCompose, BatchRandomResize, BatchRandomResizeByShort, \
  24. _BatchPadding, _Gt2YoloTarget
  25. import paddlex.utils.logging as logging
  26. from paddlex.utils.checkpoint import det_pretrain_weights_dict
  27. from paddlex.cv.models.utils.ema import ExponentialMovingAverage
  28. transforms = det_transforms
  29. visualize = visualize_detection
  30. draw_pr_curve = draw_pr_curve
  31. class FasterRCNN(cv.models.FasterRCNN):
  32. def __init__(self,
  33. num_classes=81,
  34. backbone='ResNet50',
  35. with_fpn=True,
  36. aspect_ratios=[0.5, 1.0, 2.0],
  37. anchor_sizes=[32, 64, 128, 256, 512],
  38. with_dcn=None,
  39. rpn_cls_loss=None,
  40. rpn_focal_loss_alpha=None,
  41. rpn_focal_loss_gamma=None,
  42. rcnn_bbox_loss=None,
  43. rcnn_nms=None,
  44. keep_top_k=100,
  45. nms_threshold=0.5,
  46. score_threshold=0.05,
  47. softnms_sigma=None,
  48. bbox_assigner=None,
  49. fpn_num_channels=256,
  50. input_channel=None,
  51. rpn_batch_size_per_im=256,
  52. rpn_fg_fraction=0.5,
  53. test_pre_nms_top_n=None,
  54. test_post_nms_top_n=1000):
  55. if with_dcn is not None:
  56. logging.warning(
  57. "`with_dcn` is deprecated in PaddleX 2.0 and won't take effect. Defaults to False."
  58. )
  59. if rpn_cls_loss is not None:
  60. logging.warning(
  61. "`rpn_cls_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  62. "Defaults to 'SigmoidCrossEntropy'.")
  63. if rpn_focal_loss_alpha is not None or rpn_focal_loss_gamma is not None:
  64. logging.warning(
  65. "Focal loss is deprecated in PaddleX 2.0."
  66. " `rpn_focal_loss_alpha` and `rpn_focal_loss_gamma` won't take effect."
  67. )
  68. if rcnn_bbox_loss is not None:
  69. logging.warning(
  70. "`rcnn_bbox_loss` is deprecated in PaddleX 2.0 and won't take effect. "
  71. "Defaults to 'SmoothL1Loss'")
  72. if rcnn_nms is not None:
  73. logging.warning(
  74. "MultiClassSoftNMS is deprecated in PaddleX 2.0. "
  75. "`rcnn_nms` and `softnms_sigma` won't take effect. MultiClassNMS will be used by default"
  76. )
  77. if bbox_assigner is not None:
  78. logging.warning(
  79. "`bbox_assigner` is deprecated in PaddleX 2.0 and won't take effect. "
  80. "Defaults to 'BBoxAssigner'")
  81. if input_channel is not None:
  82. logging.warning(
  83. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  84. )
  85. if isinstance(anchor_sizes[0], int):
  86. anchor_sizes = [[size] for size in anchor_sizes]
  87. super(FasterRCNN, self).__init__(
  88. num_classes=num_classes - 1,
  89. backbone=backbone,
  90. with_fpn=with_fpn,
  91. aspect_ratios=aspect_ratios,
  92. anchor_sizes=anchor_sizes,
  93. keep_top_k=keep_top_k,
  94. nms_threshold=nms_threshold,
  95. score_threshold=score_threshold,
  96. fpn_num_channels=fpn_num_channels,
  97. rpn_batch_size_per_im=rpn_batch_size_per_im,
  98. rpn_fg_fraction=rpn_fg_fraction,
  99. test_pre_nms_top_n=test_pre_nms_top_n,
  100. test_post_nms_top_n=test_post_nms_top_n)
  101. def train(self,
  102. num_epochs,
  103. train_dataset,
  104. train_batch_size=2,
  105. eval_dataset=None,
  106. save_interval_epochs=1,
  107. log_interval_steps=2,
  108. save_dir='output',
  109. pretrain_weights='IMAGENET',
  110. optimizer=None,
  111. learning_rate=0.0025,
  112. warmup_steps=500,
  113. warmup_start_lr=1.0 / 1200,
  114. lr_decay_epochs=[8, 11],
  115. lr_decay_gamma=0.1,
  116. metric=None,
  117. use_vdl=False,
  118. early_stop=False,
  119. early_stop_patience=5,
  120. sensitivities_file=None,
  121. pruned_flops=.2):
  122. _legacy_train(
  123. self,
  124. num_epochs=num_epochs,
  125. train_dataset=train_dataset,
  126. train_batch_size=train_batch_size,
  127. eval_dataset=eval_dataset,
  128. save_interval_epochs=save_interval_epochs,
  129. log_interval_steps=log_interval_steps,
  130. save_dir=save_dir,
  131. pretrain_weights=pretrain_weights,
  132. optimizer=optimizer,
  133. learning_rate=learning_rate,
  134. warmup_steps=warmup_steps,
  135. warmup_start_lr=warmup_start_lr,
  136. lr_decay_epochs=lr_decay_epochs,
  137. lr_decay_gamma=lr_decay_gamma,
  138. metric=metric,
  139. use_vdl=use_vdl,
  140. early_stop=early_stop,
  141. early_stop_patience=early_stop_patience,
  142. sensitivities_file=sensitivities_file,
  143. pruned_flops=pruned_flops)
  144. class YOLOv3(cv.models.YOLOv3):
  145. def __init__(self,
  146. num_classes=80,
  147. backbone='MobileNetV1',
  148. anchors=None,
  149. anchor_masks=None,
  150. ignore_threshold=0.7,
  151. nms_score_threshold=0.01,
  152. nms_topk=1000,
  153. nms_keep_topk=100,
  154. nms_iou_threshold=0.45,
  155. label_smooth=False,
  156. train_random_shapes=[
  157. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  158. ],
  159. input_channel=None):
  160. if input_channel is not None:
  161. logging.warning(
  162. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  163. )
  164. if anchors is None:
  165. anchors = [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
  166. [59, 119], [116, 90], [156, 198], [373, 326]]
  167. if anchor_masks is None:
  168. anchor_masks = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
  169. super(YOLOv3, self).__init__(
  170. num_classes=num_classes,
  171. backbone=backbone,
  172. anchors=anchors,
  173. anchor_masks=anchor_masks,
  174. ignore_threshold=ignore_threshold,
  175. nms_score_threshold=nms_score_threshold,
  176. nms_topk=nms_topk,
  177. nms_keep_topk=nms_keep_topk,
  178. nms_iou_threshold=nms_iou_threshold,
  179. label_smooth=label_smooth)
  180. self.train_random_shapes = train_random_shapes
  181. def train(self,
  182. num_epochs,
  183. train_dataset,
  184. train_batch_size=8,
  185. eval_dataset=None,
  186. save_interval_epochs=20,
  187. log_interval_steps=2,
  188. save_dir='output',
  189. pretrain_weights='IMAGENET',
  190. optimizer=None,
  191. learning_rate=1.0 / 8000,
  192. warmup_steps=1000,
  193. warmup_start_lr=0.0,
  194. lr_decay_epochs=[213, 240],
  195. lr_decay_gamma=0.1,
  196. metric=None,
  197. use_vdl=False,
  198. sensitivities_file=None,
  199. pruned_flops=.2,
  200. early_stop=False,
  201. early_stop_patience=5):
  202. _legacy_train(
  203. self,
  204. num_epochs=num_epochs,
  205. train_dataset=train_dataset,
  206. train_batch_size=train_batch_size,
  207. eval_dataset=eval_dataset,
  208. save_interval_epochs=save_interval_epochs,
  209. log_interval_steps=log_interval_steps,
  210. save_dir=save_dir,
  211. pretrain_weights=pretrain_weights,
  212. optimizer=optimizer,
  213. learning_rate=learning_rate,
  214. warmup_steps=warmup_steps,
  215. warmup_start_lr=warmup_start_lr,
  216. lr_decay_epochs=lr_decay_epochs,
  217. lr_decay_gamma=lr_decay_gamma,
  218. metric=metric,
  219. use_vdl=use_vdl,
  220. early_stop=early_stop,
  221. early_stop_patience=early_stop_patience,
  222. sensitivities_file=sensitivities_file,
  223. pruned_flops=pruned_flops)
  224. def _compose_batch_transform(self, transforms, mode='train'):
  225. if mode == 'train':
  226. default_batch_transforms = [
  227. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  228. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  229. _Gt2YoloTarget(
  230. anchor_masks=self.anchor_masks,
  231. anchors=self.anchors,
  232. downsample_ratios=getattr(self, 'downsample_ratios',
  233. [32, 16, 8]),
  234. num_classes=self.num_classes)
  235. ]
  236. else:
  237. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  238. if mode == 'eval' and self.metric == 'voc':
  239. collate_batch = False
  240. else:
  241. collate_batch = True
  242. custom_batch_transforms = []
  243. random_shape_defined = False
  244. for i, op in enumerate(transforms.transforms):
  245. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  246. if mode != 'train':
  247. raise Exception(
  248. "{} cannot be present in the {} transforms. ".format(
  249. op.__class__.__name__, mode) +
  250. "Please check the {} transforms.".format(mode))
  251. custom_batch_transforms.insert(0, copy.deepcopy(op))
  252. random_shape_defined = True
  253. if not random_shape_defined:
  254. default_batch_transforms.insert(
  255. 0,
  256. BatchRandomResize(
  257. target_sizes=self.train_random_shapes, interp='RANDOM'))
  258. batch_transforms = BatchCompose(
  259. custom_batch_transforms + default_batch_transforms,
  260. collate_batch=collate_batch)
  261. return batch_transforms
  262. class PPYOLO(cv.models.PPYOLO):
  263. def __init__(
  264. self,
  265. num_classes=80,
  266. backbone='ResNet50_vd_ssld',
  267. with_dcn_v2=None,
  268. # YOLO Head
  269. anchors=None,
  270. anchor_masks=None,
  271. use_coord_conv=True,
  272. use_iou_aware=True,
  273. use_spp=True,
  274. use_drop_block=True,
  275. scale_x_y=1.05,
  276. # PPYOLO Loss
  277. ignore_threshold=0.7,
  278. label_smooth=False,
  279. use_iou_loss=True,
  280. # NMS
  281. use_matrix_nms=True,
  282. nms_score_threshold=0.01,
  283. nms_topk=1000,
  284. nms_keep_topk=100,
  285. nms_iou_threshold=0.45,
  286. train_random_shapes=[
  287. 320, 352, 384, 416, 448, 480, 512, 544, 576, 608
  288. ],
  289. input_channel=None):
  290. if backbone == 'ResNet50_vd_ssld':
  291. backbone = 'ResNet50_vd_dcn'
  292. if with_dcn_v2 is not None:
  293. logging.warning(
  294. "`with_dcn_v2` is deprecated in PaddleX 2.0 and will not take effect. "
  295. "To use backbone with deformable convolutional networks, "
  296. "please specify in `backbone_name`. "
  297. "Currently the only backbone with dcn is 'ResNet50_vd_dcn'.")
  298. if input_channel is not None:
  299. logging.warning(
  300. "`input_channel` is deprecated in PaddleX 2.0 and won't take effect. Defaults to 3."
  301. )
  302. super(PPYOLO, self).__init__(
  303. num_classes=num_classes,
  304. backbone=backbone,
  305. anchors=anchors,
  306. anchor_masks=anchor_masks,
  307. use_coord_conv=use_coord_conv,
  308. use_iou_aware=use_iou_aware,
  309. use_spp=use_spp,
  310. use_drop_block=use_drop_block,
  311. scale_x_y=scale_x_y,
  312. ignore_threshold=ignore_threshold,
  313. label_smooth=label_smooth,
  314. use_iou_loss=use_iou_loss,
  315. use_matrix_nms=use_matrix_nms,
  316. nms_score_threshold=nms_score_threshold,
  317. nms_topk=nms_topk,
  318. nms_keep_topk=nms_keep_topk,
  319. nms_iou_threshold=nms_iou_threshold)
  320. self.train_random_shapes = train_random_shapes
  321. def train(self,
  322. num_epochs,
  323. train_dataset,
  324. train_batch_size=8,
  325. eval_dataset=None,
  326. save_interval_epochs=20,
  327. log_interval_steps=2,
  328. save_dir='output',
  329. pretrain_weights='IMAGENET',
  330. optimizer=None,
  331. learning_rate=1.0 / 8000,
  332. warmup_steps=1000,
  333. warmup_start_lr=0.0,
  334. lr_decay_epochs=[213, 240],
  335. lr_decay_gamma=0.1,
  336. metric=None,
  337. use_vdl=False,
  338. sensitivities_file=None,
  339. pruned_flops=.2,
  340. early_stop=False,
  341. early_stop_patience=5,
  342. resume_checkpoint=None,
  343. use_ema=True,
  344. ema_decay=0.9998):
  345. _legacy_train(
  346. self,
  347. num_epochs=num_epochs,
  348. train_dataset=train_dataset,
  349. train_batch_size=train_batch_size,
  350. eval_dataset=eval_dataset,
  351. save_interval_epochs=save_interval_epochs,
  352. log_interval_steps=log_interval_steps,
  353. save_dir=save_dir,
  354. pretrain_weights=pretrain_weights,
  355. optimizer=optimizer,
  356. learning_rate=learning_rate,
  357. warmup_steps=warmup_steps,
  358. warmup_start_lr=warmup_start_lr,
  359. lr_decay_epochs=lr_decay_epochs,
  360. lr_decay_gamma=lr_decay_gamma,
  361. metric=metric,
  362. use_vdl=use_vdl,
  363. early_stop=early_stop,
  364. early_stop_patience=early_stop_patience,
  365. sensitivities_file=sensitivities_file,
  366. pruned_flops=pruned_flops,
  367. use_ema=use_ema,
  368. ema_decay=ema_decay)
  369. def _compose_batch_transform(self, transforms, mode='train'):
  370. if mode == 'train':
  371. default_batch_transforms = [
  372. _BatchPadding(pad_to_stride=-1), _NormalizeBox(),
  373. _PadBox(getattr(self, 'num_max_boxes', 50)), _BboxXYXY2XYWH(),
  374. _Gt2YoloTarget(
  375. anchor_masks=self.anchor_masks,
  376. anchors=self.anchors,
  377. downsample_ratios=getattr(self, 'downsample_ratios',
  378. [32, 16, 8]),
  379. num_classes=self.num_classes)
  380. ]
  381. else:
  382. default_batch_transforms = [_BatchPadding(pad_to_stride=-1)]
  383. if mode == 'eval' and self.metric == 'voc':
  384. collate_batch = False
  385. else:
  386. collate_batch = True
  387. custom_batch_transforms = []
  388. random_shape_defined = False
  389. for i, op in enumerate(transforms.transforms):
  390. if isinstance(op, (BatchRandomResize, BatchRandomResizeByShort)):
  391. if mode != 'train':
  392. raise Exception(
  393. "{} cannot be present in the {} transforms. ".format(
  394. op.__class__.__name__, mode) +
  395. "Please check the {} transforms.".format(mode))
  396. custom_batch_transforms.insert(0, copy.deepcopy(op))
  397. random_shape_defined = True
  398. if not random_shape_defined:
  399. default_batch_transforms.insert(
  400. 0,
  401. BatchRandomResize(
  402. target_sizes=self.train_random_shapes, interp='RANDOM'))
  403. batch_transforms = BatchCompose(
  404. custom_batch_transforms + default_batch_transforms,
  405. collate_batch=collate_batch)
  406. return batch_transforms
  407. def _legacy_train(model,
  408. num_epochs,
  409. train_dataset,
  410. train_batch_size,
  411. eval_dataset,
  412. save_interval_epochs,
  413. log_interval_steps,
  414. save_dir,
  415. pretrain_weights,
  416. optimizer,
  417. learning_rate,
  418. warmup_steps,
  419. warmup_start_lr,
  420. lr_decay_epochs,
  421. lr_decay_gamma,
  422. metric,
  423. use_vdl,
  424. early_stop,
  425. early_stop_patience,
  426. sensitivities_file,
  427. pruned_flops,
  428. use_ema=False,
  429. ema_decay=0.9998):
  430. if train_dataset.__class__.__name__ == 'VOCDetection':
  431. train_dataset.data_fields = {
  432. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class', 'difficult'
  433. }
  434. elif train_dataset.__class__.__name__ == 'CocoDetection':
  435. if model.__class__.__name__ == 'MaskRCNN':
  436. train_dataset.data_fields = {
  437. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  438. 'gt_poly', 'is_crowd'
  439. }
  440. else:
  441. train_dataset.data_fields = {
  442. 'im_id', 'image_shape', 'image', 'gt_bbox', 'gt_class',
  443. 'is_crowd'
  444. }
  445. if metric is None:
  446. if eval_dataset.__class__.__name__ == 'VOCDetection':
  447. model.metric = 'voc'
  448. elif eval_dataset.__class__.__name__ == 'CocoDetection':
  449. model.metric = 'coco'
  450. else:
  451. assert metric.lower() in ['coco', 'voc'], \
  452. "Evaluation metric {} is not supported, please choose form 'COCO' and 'VOC'"
  453. model.metric = metric.lower()
  454. model.labels = train_dataset.labels
  455. model.num_max_boxes = train_dataset.num_max_boxes
  456. train_dataset.batch_transforms = model._compose_batch_transform(
  457. train_dataset.transforms, mode='train')
  458. if sensitivities_file is not None:
  459. dataset = eval_dataset or train_dataset
  460. im_shape = dataset[0]['image'].shape[:2]
  461. if getattr(model, 'with_fpn', False):
  462. im_shape[0] = int(np.ceil(im_shape[0] / 32) * 32)
  463. im_shape[1] = int(np.ceil(im_shape[1] / 32) * 32)
  464. inputs = [{
  465. "image": paddle.ones(
  466. shape=[1, 3] + list(im_shape), dtype='float32'),
  467. "im_shape": paddle.full(
  468. [1, 2], 640, dtype='float32'),
  469. "scale_factor": paddle.ones(
  470. shape=[1, 2], dtype='float32')
  471. }]
  472. model.pruner = L1NormFilterPruner(
  473. model.net, inputs=inputs, sen_file=sensitivities_file)
  474. model.pruner.sensitive_prune(pruned_flops=pruned_flops)
  475. # build optimizer if not defined
  476. if optimizer is None:
  477. num_steps_each_epoch = len(train_dataset) // train_batch_size
  478. model.optimizer = model.default_optimizer(
  479. parameters=model.net.parameters(),
  480. learning_rate=learning_rate,
  481. warmup_steps=warmup_steps,
  482. warmup_start_lr=warmup_start_lr,
  483. lr_decay_epochs=lr_decay_epochs,
  484. lr_decay_gamma=lr_decay_gamma,
  485. num_steps_each_epoch=num_steps_each_epoch)
  486. else:
  487. model.optimizer = optimizer
  488. # initiate weights
  489. if pretrain_weights is not None and not osp.exists(pretrain_weights):
  490. if pretrain_weights not in det_pretrain_weights_dict['_'.join(
  491. [model.model_name, model.backbone_name])]:
  492. logging.warning("Path of pretrain_weights('{}') does not exist!".
  493. format(pretrain_weights))
  494. pretrain_weights = det_pretrain_weights_dict['_'.join(
  495. [model.model_name, model.backbone_name])][0]
  496. logging.warning("Pretrain_weights is forcibly set to '{}'. "
  497. "If you don't want to use pretrain weights, "
  498. "set pretrain_weights to be None.".format(
  499. pretrain_weights))
  500. pretrained_dir = osp.join(save_dir, 'pretrain')
  501. model.net_initialize(
  502. pretrain_weights=pretrain_weights, save_dir=pretrained_dir)
  503. if use_ema:
  504. ema = ExponentialMovingAverage(
  505. decay=ema_decay, model=model.net, use_thres_step=True)
  506. else:
  507. ema = None
  508. # start train loop
  509. model.train_loop(
  510. num_epochs=num_epochs,
  511. train_dataset=train_dataset,
  512. train_batch_size=train_batch_size,
  513. eval_dataset=eval_dataset,
  514. save_interval_epochs=save_interval_epochs,
  515. log_interval_steps=log_interval_steps,
  516. save_dir=save_dir,
  517. ema=ema,
  518. early_stop=early_stop,
  519. early_stop_patience=early_stop_patience,
  520. use_vdl=use_vdl)