Instance Segmentation of Colonic Polyps using Detectron2 and Mask R-CNN (ResNet-50 FPN) trained on the CVC-ClinicDB dataset.
This repository implements a complete deep learning pipeline:
- Dataset download from Kaggle
- Mask → COCO annotation conversion
- Detectron2 dataset registration
- Model training (Mask R-CNN R50-FPN)
- Inference & visualization
- Model export and configuration saving
Without segmentation (bgr image)
Model predicting polyps on the image
Early detection of colonic polyps is essential for preventing colorectal cancer.
This project builds a full instance segmentation pipeline using Detectron2 to automatically detect and segment polyps from colonoscopy images.
- Download dataset from Kaggle
- Convert ground truth masks to COCO format
- Register dataset in Detectron2
- Train Mask R-CNN model
- Perform inference on unseen images
- Visualize predictions
Dataset: CVC-ClinicDB
Source: Kaggle
https://www.kaggle.com/datasets/balraj98/cvcclinicdb
PNG/
├── Original/
└── Ground Truth/
- Total images: 612
- Training split: 80%
- Validation split: 20%
- Classes: 1 (
Polyp)
Ground truth masks are converted into COCO polygon segmentation format before training.
- Framework: Detectron2 (v0.6)
- Deep Learning: PyTorch (2.5)
- Backbone: Mask R-CNN ResNet-50 FPN
- CUDA: 12.x
- Image Processing: OpenCV
- Visualization: Matplotlib
- Environment: Google Colab / Local GPU setup
git clone https://github.com/your-username/polyp-detection-detectron2.git
cd polyp-detection-detectron2pip install pyyaml==5.1
pip install opencv-python
pip install matplotlib
pip install scikit-learn
pip install kaggle
pip install 'git+https://github.com/facebookresearch/detectron2.git'Requirements:
- Python ≥ 3.8
- CUDA-enabled GPU recommended
kaggle datasets download balraj98/cvcclinicdb
unzip cvcclinicdb.zipThe notebook performs:
- Contour extraction using
cv2.findContours - Polygon conversion
- Bounding box calculation
- COCO JSON generation
- Train/Validation split (80/20)
Output structure:
COCO_annotations/
├── train/
│ └── annotations.json
└── val/
└── annotations.json
Model configuration:
COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml
Key training parameters:
cfg.SOLVER.BASE_LR = 0.00025
cfg.SOLVER.MAX_ITER = 1000
cfg.SOLVER.IMS_PER_BATCH = 2
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 256
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1trainer = DefaultTrainer(cfg)
trainer.resume_or_load(resume=False)
trainer.train()Outputs:
models/
├── model_1000_iter.pth
└── config-1k_iter.yaml
TensorBoard monitoring supported:
tensorboard --logdir models/cfg.MODEL.WEIGHTS = "model_1000_iter.pth"
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.45
predictor = DefaultPredictor(cfg)
outputs = predictor(image)Predictions include:
- Segmentation masks
- Bounding boxes
- Confidence scores
Visualization performed using Detectron2 Visualizer.
- Training Images: 489
- Validation Images: 123
- Single-class instance segmentation
- Accurate mask overlays on colonoscopy frames
(Extend this section with mAP / IoU metrics if available.)
- AI-assisted colonoscopy
- Automated colorectal cancer screening
- Clinical decision support systems
- Medical imaging research
- Real-time endoscopic detection systems
polyp-detection-detectron2/
│
├── dataset/
├── COCO_annotations/
│ ├── train/
│ └── val/
├── models/
├── notebook/
│ └── polyp_detection_using_detectron2.ipynb
└── README.md
- Add COCOEvaluator metrics (mAP)
- Cross-dataset validation (e.g., Kvasir-SEG)
- Hyperparameter optimization
- Data augmentation
- Real-time video inference
- Deploy via Streamlit or FastAPI
- Docker containerization
Anirudha Kumar Sahu
Ph.D in Cancer Biology, BITS Pilani
- Detectron2 – Facebook AI Research
- Mask R-CNN (He et al., 2017)
- CVC-ClinicDB Dataset
Dataset license: As specified on Kaggle
⭐ If you find this project useful, consider starring the repository.

