open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.21k stars 9.4k forks source link

Model initialization crash when visualizer is not defined in config file #11792

Open DamianBisewski opened 3 months ago

DamianBisewski commented 3 months ago

Hello, I would like to report an error in the DetInferencer._init_visualizer method (. When the visualizer is not defined in the configuration file, the function fails with the following error: File ~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:99, in DetInferencer.init(self, model, weights, device, scope, palette, show_progress) 97 self.palette = palette 98 init_default_scope(scope) ---> 99 super().init( 100 model=model, weights=weights, device=device, scope=scope) 101 self.model = revert_sync_batchnorm(self.model) 102 self.show_progress = show_progress

File ~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:183, in BaseInferencer.init(self, model, weights, device, scope, show_progress) 181 self.pipeline = self._init_pipeline(cfg) 182 self.collate_fn = self._init_collate(cfg) --> 183 self.visualizer = self._init_visualizer(cfg) 184 self.cfg = cfg 185 self.show_progress = show_progress

File ~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:198, in DetInferencer._init_visualizer(self, cfg) 189 """Initialize visualizers. 190 191 Args: (...) 195 Visualizer or None: Visualizer initialized with config. 196 """ 197 visualizer = super()._init_visualizer(cfg) --> 198 visualizer.dataset_meta = self.model.dataset_meta 199 return visualizer

AttributeError: 'NoneType' object has no attribute 'dataset_meta'

The error arises because the result of the line super()._init_visualizer() is not checked, which can return None when the visualizer is not defined in the configuration file. In such a situation, the next instruction tries to assign a value to a None object, which causes a crash. To fix this issue, the code should look like this:

In mmdet/apis/det_inferencer.py

def _init_visualizer(self, cfg): """Initialize visualizers.

Args: cfg (Config): Configuration dictionary.

Returns: Visualizer or None: Visualizer initialized with config. """ visualizer = super()._init_visualizer(cfg) if visualizer is not None: visualizer.dataset_meta = self.model.dataset_meta return visualizer

With this change, the code checks if the visualizer is None before attempting to assign dataset_meta, preventing the crash.

Environment sys.platform: win32 Python: 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)] CUDA available: False MUSA available: False numpy_random_seed: 2147483648 MSVC: Microsoft (R) C/C++ wersja kompilatora optymalizującego 19.40.33811 dla x64 GCC: n/a PyTorch: 2.1.2+cpu PyTorch compiling details: PyTorch built with:

TorchVision: 0.16.2+cpu OpenCV: 4.8.1 MMEngine: 0.10.4 MMDetection: 3.3.0+ Error traceback If applicable, paste the error trackback here.

File ~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:99, in DetInferencer.__init__(self, model, weights, device, scope, palette, show_progress)
     [97](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:97) self.palette = palette
     [98](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:98) init_default_scope(scope)
---> [99](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:99) super().__init__(
    [100](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:100)     model=model, weights=weights, device=device, scope=scope)
    [101](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:101) self.model = revert_sync_batchnorm(self.model)
    [102](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:102) self.show_progress = show_progress

File ~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:183, in BaseInferencer.__init__(self, model, weights, device, scope, show_progress)
    [181](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:181) self.pipeline = self._init_pipeline(cfg)
    [182](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:182) self.collate_fn = self._init_collate(cfg)
--> [183](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:183) self.visualizer = self._init_visualizer(cfg)
    [184](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:184) self.cfg = cfg
    [185](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmengine/infer/infer.py:185) self.show_progress = show_progress

File ~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:198, in DetInferencer._init_visualizer(self, cfg)
    [189](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:189) """Initialize visualizers.
    [190](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:190) 
    [191](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:191) Args:
   (...)
    [195](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:195)     Visualizer or None: Visualizer initialized with config.
    [196](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:196) """
    [197](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:197) visualizer = super()._init_visualizer(cfg)
--> [198](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:198) visualizer.dataset_meta = self.model.dataset_meta
    [199](https://file+.vscode-resource.vscode-cdn.net/home/jozef/hub-1st/~/anaconda3/lib/python3.11/site-packages/mmdet/apis/det_inferencer.py:199) return visualizer

AttributeError: 'NoneType' object has no attribute 'dataset_meta'

Bug fix The error arises because the result of the line super()._init_visualizer() is not checked, which can return None when the visualizer is not defined in the configuration file. In such a situation, the next instruction tries to assign a value to a None object, which causes a crash. To fix this issue, the code should look like this:

In mmdet/apis/det_inferencer.py

def _init_visualizer(self, cfg): """Initialize visualizers.

Args: cfg (Config): Configuration dictionary.

Returns: Visualizer or None: Visualizer initialized with config. """ visualizer = super()._init_visualizer(cfg) if visualizer is not None: visualizer.dataset_meta = self.model.dataset_meta return visualizer