Repository: open-mmlab/mmdetection

Issue:
Issue about tools\analysis_tools\get_flops.py
When I used the tools/analysis_tools/get_flops.py to compute the Flops and Parameters of YOLACT , I got an error "NotImplementedError".

Traceback (most recent call last):
  File "tools/analysis_tools/get_flops.py", line 81, in <module>
    main()
  File "tools/analysis_tools/get_flops.py", line 71, in main
    flops, params = get_model_complexity_info(model, input_shape)
  File "/home/scsc01/anaconda3/envs/mmlab/lib/python3.8/site-packages/mmcv/cnn/utils/flops_counter.py", line 104, in get_model_complexity_info
    _ = flops_model(batch)
  File "/home/scsc01/anaconda3/envs/mmlab/lib/python3.8/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/scsc01/lxx/mmlab/mmdet/models/detectors/yolact.py", line 32, in forward_dummy
    raise NotImplementedError
NotImplementedError
Does the get_flops.py not support the complexity calculation of YOLACT? thank you~~


Discussion:
In **yolact.py**, `forward_dummy` is not implemented
https://github.com/open-mmlab/mmdetection/blob/6882fa0a220880e6e2e7e0536037f043b9031185/mmdet/models/detectors/yolact.py#L27-L32
But in **get_flops.py**
https://github.com/open-mmlab/mmdetection/blob/6882fa0a220880e6e2e7e0536037f043b9031185/tools/analysis_tools/get_flops.py#L64-L69
I have checked `forward_dummy` of other models, and they are all implemented. I assume that the expected behavior is `forward_dummy` should not be declared in **yolact.py**, so the error information can be easier to understand?
For now we have not supported it. We will support it in the future.

Resolving patch:
diff --git a/mmdet/models/dense_heads/yolact_head.py b/mmdet/models/dense_heads/yolact_head.py
--- a/mmdet/models/dense_heads/yolact_head.py
+++ b/mmdet/models/dense_heads/yolact_head.py
@@ -663,6 +663,10 @@ def _init_layers(self):
             protonets = protonets[:-1]
         return nn.Sequential(*protonets)
 
+    def forward_dummy(self, x):
+        prototypes = self.protonet(x)
+        return prototypes
+
     def forward(self, x, coeff_pred, bboxes, img_meta, sampling_results=None):
         """Forward feature from the upstream network to get prototypes and
         linearly combine the prototypes, using masks coefficients, into
diff --git a/mmdet/models/detectors/yolact.py b/mmdet/models/detectors/yolact.py
--- a/mmdet/models/detectors/yolact.py
+++ b/mmdet/models/detectors/yolact.py
@@ -30,7 +30,10 @@ def forward_dummy(self, img):
 
         See `mmdetection/tools/analysis_tools/get_flops.py`
         """
-        raise NotImplementedError
+        feat = self.extract_feat(img)
+        bbox_outs = self.bbox_head(feat)
+        prototypes = self.mask_head.forward_dummy(feat[0])
+        return (bbox_outs, prototypes)
 
     def forward_train(self,
                       img,


Test patch:
diff --git a/tests/test_models/test_forward.py b/tests/test_models/test_forward.py
--- a/tests/test_models/test_forward.py
+++ b/tests/test_models/test_forward.py
@@ -520,6 +520,9 @@ def test_yolact_forward():
         return_loss=True)
     assert isinstance(losses, dict)
 
+    # Test forward dummy for get_flops
+    detector.forward_dummy(imgs)
+
     # Test forward test
     detector.eval()
     with torch.no_grad():