This repository implements a U-Net-based segmentation pipeline to detect polyps in colonoscopy images using the Kvasir-SEG dataset.
- U-Net model architecture
- BCE + Dice loss
- W&B logging and hyperparameter sweeps
- Augmentations using Albumentations
- Validation with IoU and Dice metrics
-
Clone the repository
git clone https://github.com/your-username/Polyp_Segmentation.git cd Polyp_Segmentation -
Install dependencies
pip install -r requirements.txt -
Setup Weights & Biases
-
Create a free W&B account: https://wandb.ai
-
Go to your W&B settings and copy your API Key
-
Create a
.envfile in the project root:WANDB_API_KEY=your_api_key_here
-
python3 train.py
You can configure training by editing the config dictionary in train.py:
config = {
"use_pretrained": True,
"encoder": "efficientnet-b4",
"encoder_weights": "imagenet",
"use_amp": True,
"epochs": 20,
"batch_size": 8,
...
}
-
If
use_pretrainedisTrue, the model will use a pretrained EfficientNet encoder viasegmentation_models.pytorch. -
If
use_pretrainedisFalse, it will fallback to the custom U-Net defined inmodels/unet.py.
python3 sweep.py
This script will:
- Initialize a W&B sweep
- Launch training jobs with different hyperparameter combination.
You can modify sweep configurations in sweep.py:
sweep_config = {
"method": "bayes",
"metric": {"name": "val/dice_coeff", "goal": "maximize"},
"parameters": {
"batch_size": {"values": [8, 16]},
"encoder": {"values": ["resnet34", "efficientnet-b0", "efficientnet-b4"]},
...
}
}
Polyp_Segmentation/
│
├── README.md
├── requirements.txt
│
├── train.py
├── sweep.py
├── inference.py
│
├── models/
│ └── unet.py
│
├── utils/
│ ├── losses.py
│ ├── metrics.py
│ └── augmentations.py
│
├── data/
├── images/
└── masks/