Repository: open-mmlab/mmdetection

Issue:
Attribute error with RetinaNet and with_mask, with_bbox, with_neck, with_shared_head

**Describe the bug**

The `with_mask`, `with_bbox`, `with_neck`, and `with_shared_head` attributes no longer seem to work. 


**Reproduction**

```python
    import sys, ubelt
    sys.path.append(ubelt.expandpath('~/code/mmdetection/tests/test_models'))
    from test_forward import *  # NOQA
    from test_forward import _get_config_directory, _get_config_module, _get_detector_cfg, _demo_mm_inputs
    model, train_cfg, test_cfg = _get_detector_cfg(
        'rpn/rpn_r50_fpn_1x_coco.py')
    model['pretrained'] = None

    from mmdet.models import build_detector
    detector = build_detector(model, train_cfg=train_cfg, test_cfg=test_cfg)

    detector.with_bbox
    detector.with_mask
    detector.with_neck
    detector.with_shared_head

```

Accessing the attributes raise attribute errors even though all are defined as properties in the BaseDetector 

**Environment**

```
(py38) joncrall@Ooo:~/code/mmdetection$ python mmdet/utils/collect_env.py
sys.platform: linux
Python: 3.8.3 (default, Jul  2 2020, 16:21:59) [GCC 7.3.0]
CUDA available: True
CUDA_HOME: /home/joncrall/.local/cuda
NVCC: Cuda compilation tools, release 10.1, V10.1.243
GPU 0: GeForce GTX 1080 Ti
GCC: gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
PyTorch: 1.5.1
PyTorch compiling details: PyTorch built with:
  - GCC 7.3
  - C++ Version: 201402
  - Intel(R) Math Kernel Library Version 2019.0.5 Product Build 20190808 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CPU capability usage: AVX
  - CUDA Runtime 10.2
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  - CuDNN 7.6.5
  - Magma 2.5.2
  - Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DNDEBUG -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DUSE_INTERNAL_THREADPOOL_IMPL -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Wno-stringop-overflow, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF, 

TorchVision: 0.6.1
OpenCV: 4.3.0
MMCV: 1.0.4
MMDetection: 2.3.0rc0+595bf86
MMDetection Compiler: GCC 7.3
MMDetection CUDA Compiler: 10.2
```

**Bug fix**

The following code that checks for the existence of 'roi_head' seems to fix the issue: 

```python
    @property
    def with_shared_head(self):
        """bool: whether the detector has a shared head in the RoI Head"""
        return (hasattr(self, 'roi_head') and
                hasattr(self.roi_head, 'shared_head') and
                self.roi_head.shared_head is not None)

    @property
    def with_bbox(self):
        """bool: whether the detector has a bbox head"""
        return ((hasattr(self, 'roi_head') and hasattr(self.roi_head, 'bbox_head')
                 and self.roi_head.bbox_head is not None)
                or (hasattr(self, 'bbox_head') and self.bbox_head is not None))

    @property
    def with_mask(self):
        """bool: whether the detector has a mask head"""
        return ((hasattr(self, 'roi_head') and hasattr(self.roi_head, 'mask_head')
                 and self.roi_head.mask_head is not None)
                or (hasattr(self, 'mask_head') and self.mask_head is not None))
```



Discussion:
Thanks for your bug report. Would you like to create a PR to fix it?
I would prefer if someone else could take care of the PR. The above code should fix the issue, so making it should be trivial.

Resolving patch:
diff --git a/mmdet/models/detectors/base.py b/mmdet/models/detectors/base.py
--- a/mmdet/models/detectors/base.py
+++ b/mmdet/models/detectors/base.py
@@ -29,21 +29,18 @@ def with_neck(self):
     @property
     def with_shared_head(self):
         """bool: whether the detector has a shared head in the RoI Head"""
-        return hasattr(self.roi_head,
-                       'shared_head') and self.roi_head.shared_head is not None
+        return hasattr(self, 'roi_head') and self.roi_head.with_shared_head
 
     @property
     def with_bbox(self):
         """bool: whether the detector has a bbox head"""
-        return ((hasattr(self.roi_head, 'bbox_head')
-                 and self.roi_head.bbox_head is not None)
+        return ((hasattr(self, 'roi_head') and self.roi_head.with_bbox)
                 or (hasattr(self, 'bbox_head') and self.bbox_head is not None))
 
     @property
     def with_mask(self):
         """bool: whether the detector has a mask head"""
-        return ((hasattr(self.roi_head, 'mask_head')
-                 and self.roi_head.mask_head is not None)
+        return ((hasattr(self, 'roi_head') and self.roi_head.with_mask)
                 or (hasattr(self, 'mask_head') and self.mask_head is not None))
 
     @abstractmethod


Test patch: