You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

yolact.py 4.7 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import torch
  3. from mmdet.core import bbox2result
  4. from ..builder import DETECTORS, build_head
  5. from .single_stage import SingleStageDetector
  6. @DETECTORS.register_module()
  7. class YOLACT(SingleStageDetector):
  8. """Implementation of `YOLACT <https://arxiv.org/abs/1904.02689>`_"""
  9. def __init__(self,
  10. backbone,
  11. neck,
  12. bbox_head,
  13. segm_head,
  14. mask_head,
  15. train_cfg=None,
  16. test_cfg=None,
  17. pretrained=None,
  18. init_cfg=None):
  19. super(YOLACT, self).__init__(backbone, neck, bbox_head, train_cfg,
  20. test_cfg, pretrained, init_cfg)
  21. self.segm_head = build_head(segm_head)
  22. self.mask_head = build_head(mask_head)
  23. def forward_dummy(self, img):
  24. """Used for computing network flops.
  25. See `mmdetection/tools/analysis_tools/get_flops.py`
  26. """
  27. feat = self.extract_feat(img)
  28. bbox_outs = self.bbox_head(feat)
  29. prototypes = self.mask_head.forward_dummy(feat[0])
  30. return (bbox_outs, prototypes)
  31. def forward_train(self,
  32. img,
  33. img_metas,
  34. gt_bboxes,
  35. gt_labels,
  36. gt_bboxes_ignore=None,
  37. gt_masks=None):
  38. """
  39. Args:
  40. img (Tensor): of shape (N, C, H, W) encoding input images.
  41. Typically these should be mean centered and std scaled.
  42. img_metas (list[dict]): list of image info dict where each dict
  43. has: 'img_shape', 'scale_factor', 'flip', and may also contain
  44. 'filename', 'ori_shape', 'pad_shape', and 'img_norm_cfg'.
  45. For details on the values of these keys see
  46. `mmdet/datasets/pipelines/formatting.py:Collect`.
  47. gt_bboxes (list[Tensor]): Ground truth bboxes for each image with
  48. shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
  49. gt_labels (list[Tensor]): class indices corresponding to each box
  50. gt_bboxes_ignore (None | list[Tensor]): specify which bounding
  51. boxes can be ignored when computing the loss.
  52. gt_masks (None | Tensor) : true segmentation masks for each box
  53. used if the architecture supports a segmentation task.
  54. Returns:
  55. dict[str, Tensor]: a dictionary of loss components
  56. """
  57. # convert Bitmap mask or Polygon Mask to Tensor here
  58. gt_masks = [
  59. gt_mask.to_tensor(dtype=torch.uint8, device=img.device)
  60. for gt_mask in gt_masks
  61. ]
  62. x = self.extract_feat(img)
  63. cls_score, bbox_pred, coeff_pred = self.bbox_head(x)
  64. bbox_head_loss_inputs = (cls_score, bbox_pred) + (gt_bboxes, gt_labels,
  65. img_metas)
  66. losses, sampling_results = self.bbox_head.loss(
  67. *bbox_head_loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore)
  68. segm_head_outs = self.segm_head(x[0])
  69. loss_segm = self.segm_head.loss(segm_head_outs, gt_masks, gt_labels)
  70. losses.update(loss_segm)
  71. mask_pred = self.mask_head(x[0], coeff_pred, gt_bboxes, img_metas,
  72. sampling_results)
  73. loss_mask = self.mask_head.loss(mask_pred, gt_masks, gt_bboxes,
  74. img_metas, sampling_results)
  75. losses.update(loss_mask)
  76. # check NaN and Inf
  77. for loss_name in losses.keys():
  78. assert torch.isfinite(torch.stack(losses[loss_name]))\
  79. .all().item(), '{} becomes infinite or NaN!'\
  80. .format(loss_name)
  81. return losses
  82. def simple_test(self, img, img_metas, rescale=False):
  83. """Test function without test-time augmentation."""
  84. feat = self.extract_feat(img)
  85. det_bboxes, det_labels, det_coeffs = self.bbox_head.simple_test(
  86. feat, img_metas, rescale=rescale)
  87. bbox_results = [
  88. bbox2result(det_bbox, det_label, self.bbox_head.num_classes)
  89. for det_bbox, det_label in zip(det_bboxes, det_labels)
  90. ]
  91. segm_results = self.mask_head.simple_test(
  92. feat,
  93. det_bboxes,
  94. det_labels,
  95. det_coeffs,
  96. img_metas,
  97. rescale=rescale)
  98. return list(zip(bbox_results, segm_results))
  99. def aug_test(self, imgs, img_metas, rescale=False):
  100. """Test with augmentations."""
  101. raise NotImplementedError(
  102. 'YOLACT does not support test-time augmentation')

No Description

Contributors (3)