MMDetection

Google Colaboratory - Image and Video with Faster R-CNN on Version 3.3.0
https://colab.research.google.com/drive/11pbaSUnA8eWopz2-OHh9XkCiTQdbMl8t

Google Colaboratory - MS-COCO Training with Faster R-CNN on Version 3.3.0
https://colab.research.google.com/drive/1tYssvTbCa0zuICyeVShUx42PGMeJxWlW

Google Colaboratory - Image and Video with Faster R-CNN/YOLOX/DETR/Mask R-CNN/Panoptic FPN/Mask2Former on Version 2.28.2
https://colab.research.google.com/drive/1hbOLBwOBeCVOELl3-I2yXD0QTmWKVylZ

The official approach is using OpenMIM and the latest repositories on GitHub but the following steps simply work on Google Colab:

!pip install mmengine
!pip install mmcv==2.1.0
!pip install mmdet
## 01. MMEngine is recommended to be installed
mim install mmengine
## 02. show_result_pyplot and model.show_result() have been removed (see below for alternative approaches)
#from mmdet.apis import show_result_pyplot
## 03. Register all modules
from mmdet.utils import register_all_modules
register_all_modules()
## 04. Some configuration file names have been changed
#mim download mmdet --config faster_rcnn_r50_fpn_1x_coco --dest $config_dir
mim download mmdet --config faster-rcnn_r50_fpn_1x_coco --dest $config_dir

Information - Useful Tools

Troubleshooting - MMCV Compatibility on Google Colab

https://github.com/open-mmlab/mmcv/issues/3059

## Downgrading PyTorch on Google Colab (2.3.0+cu121) for MMCV Compatibility (Confirmed 2024/07/04)
!pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu121
!pip install --upgrade openmim
!mim install 'mmcv==2.1.0'

Troubleshooting - Conflict with Jedi (on Google Colab)

## Resolving the dependency conflicts for openmim: ipython 7.9.0 requires jedi>=0.10
!pip install --upgrade jedi

Troubleshooting - Visualizer Warning

https://mmengine.readthedocs.io/en/v0.10.4/_modules/mmengine/visualization/visualizer.html

## visualizer.py:196: UserWarning:
## Failed to add <class 'mmengine.visualization.vis_backend.LocalVisBackend'>, please provide the `save_dir` argument.
model.cfg.visualizer.vis_backends[0]['save_dir'] = output_dir

Troubleshooting - Visualizer Warning

## mmengine/utils/manager.py:113: UserWarning:
## <class 'mmdet.visualization.local_visualizer.DetLocalVisualizer'> instance named of visualizer has been created, the method `get_instance` should not accept any other arguments

To be investigated; no actual problem was recognized.

Troubleshooting - model.show_result() was removed after Version 2

https://github.com/open-mmlab/mmdetection/issues/10380

  • MMEngine Visualizer is recommended to be used instead of show_result()
  • Option A is the official approach, but font sizes might be too small if on a large image
  • Option B is a tailor-made approach for visualization with annotations
## Loading Image
import mmcv
image = mmcv.imread(img, channel_order='rgb')
#image = mmcv.imconvert(image, 'bgr', 'rgb')
## Model and Result
model = init_detector(config_file, checkpoint_file, device=device)
result = inference_detector(model, img)
## Option A
from mmdet.registry import VISUALIZERS

visualizer = VISUALIZERS.build(model.cfg.visualizer)
visualizer.dataset_meta = model.dataset_meta
visualizer.add_datasample('result', image, data_sample=result, draw_gt=False, show=True, wait_time=0, pred_score_thr=0.70, out_file='result.jpg')
visualizer.get_image()
visualizer.show()
## Option B
import torch
import cv2
from mmengine.visualization import Visualizer

pred_score_thr = 0.70
scores = result.pred_instances.scores[torch.where(result.pred_instances.scores>=pred_score_thr)]
labels = result.pred_instances.labels[torch.where(result.pred_instances.scores>=pred_score_thr)]
bboxes = result.pred_instances.bboxes[torch.where(result.pred_instances.scores>=pred_score_thr)]
labels_classes = list(map(lambda x: model.dataset_meta['classes'][x], labels.tolist()))
labels_palette = list(map(lambda x: model.dataset_meta['palette'][x], labels.tolist()))
labels_print = list(map(lambda x, y: f'{x}: {(y * 100.0):.1f}', labels_classes, scores.tolist()))
bboxes_origins = list(map(lambda x: [x[0], x[1]], bboxes.tolist()))
visualizer = Visualizer(image=image)
visualizer.draw_bboxes(bboxes, edge_colors=labels_palette, line_widths=3)
visualizer.draw_texts(labels_print, torch.tensor(bboxes_origins), font_sizes=30, colors='white', bboxes=dict(facecolor='red', edgecolor='black', linewidth=0, alpha=0.8))
visualizer.get_image()
visualizer.show()
cv2.imwrite('result.jpg', cv2.cvtColor(visualizer.get_image(), cv2.COLOR_RGB2BGR))

Information - Loss Functions

Information - Optimizers

https://pytorch.org/docs/stable/optim.html

optimizer=dict(_delete_=True) removes originally-defined parameters in dict()

Information - Freeze Parameters

https://github.com/open-mmlab/mmdetection/blob/main/mmdet/models/backbones/resnet.py

  • frozen_stages = -1 : not freezing any parameters
  • frozen_stages = 0 : freezing the stem part only
  • frozen_stages = n [n >= 1] : freezing the stem part and first n stages

Troubleshooting - loss=0 and acc=100

https://github.com/open-mmlab/mmdetection/issues/2744
https://mmdetection.readthedocs.io/en/3.x/user_guides/useful_tools.html

  • Annotations likely have not been loaded correctly in this case: loss_bbox = 0.0000
  • Triple check dataset format makes sense at first
  • Using browse_dataset.py is helpful to see if annotations align with the data
  • Check whether the image{“width”: <param>, “height”: <param>} fields in the COCO annotations match the actual image width and height
mmengine - INFO - Epoch(train)  [1][ 50/300]  lr: 1.9820e-03  loss: 0.1679  loss_rpn_cls: 0.0009  loss_rpn_bbox: 0.0000  loss_cls: 0.1669  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Epoch(train)  [1][100/300]  lr: 3.9840e-03  loss: 0.0000  loss_rpn_cls: 0.0000  loss_rpn_bbox: 0.0000  loss_cls: 0.0000  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Epoch(train)  [1][150/300]  lr: 5.9860e-03  loss: 0.0000  loss_rpn_cls: 0.0000  loss_rpn_bbox: 0.0000  loss_cls: 0.0000  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Epoch(train)  [1][200/300]  lr: 7.9880e-03  loss: 0.0000  loss_rpn_cls: 0.0000  loss_rpn_bbox: 0.0000  loss_cls: 0.0000  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Epoch(train)  [1][250/300]  lr: 9.9900e-03  loss: 0.0000  loss_rpn_cls: 0.0000  loss_rpn_bbox: 0.0000  loss_cls: 0.0000  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Epoch(train)  [1][300/300]  lr: 1.1992e-02  loss: 0.0000  loss_rpn_cls: 0.0000  loss_rpn_bbox: 0.0000  loss_cls: 0.0000  acc: 100.0000  loss_bbox: 0.0000
mmengine - INFO - Exp name: faster-rcnn_r50_fpn_1x_custom
mmengine - INFO - Saving checkpoint at 1 epochs
mmengine - INFO - Epoch(val)  [1][ 50/100] ...
mmengine - INFO - Epoch(val)  [1][100/100] ...
mmengine - INFO - Evaluating bbox...
Loading and preparing results...
mmengine - ERROR - coco_metric.py - compute_metrics - 465 - The testing results of the whole dataset is empty.
mmengine - INFO - Epoch(val) [1][100/100] ...
mmengine - WARNING - Since `metrics` is an empty dict, the behavior to save the best checkpoint will be skipped in this evaluation.

Daiphys is a professional services company in research and development of leading-edge technologies in science and engineering.
Get started accelerating your business through our deep expertise in R&D with AI, quantum computing, and space development; please get in touch with Daiphys today!

Name*


Email*


Subject


Message*




* Indicates required field

Daiphys Technologies LLC - https://www.daiphys.com/

  • Last modified: 2025/08/07 05:34
  • by Daiphys