File size: 8,113 Bytes
15301a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | """Training script for the Week 04 Kvasir-SEG U-Net assignment.
Run example:
python train.py --output-dir ./kvasir-unet --num-train-epochs 10
To publish after training, pass ``--push-to-hub``.
"""
from __future__ import annotations
import argparse
import os
from pathlib import Path
import numpy as np
import torch
from datasets import load_dataset
from huggingface_hub import upload_folder
from safetensors.torch import save_file
from torch import nn
from torchvision.transforms import Compose, Resize, ToTensor
from torchvision.transforms import InterpolationMode
from transformers import Trainer, TrainingArguments
from model import UNet
IMAGE_SIZE = 256
DEFAULT_HUB_MODEL_ID = "FolashadeO/week4"
DEFAULT_DATASET_CACHE_DIR = Path("hf_cache")
def dataset_cache_dir() -> str | None:
"""Use the workspace dataset cache when present, otherwise use HF defaults."""
configured_cache = os.environ.get("HF_DATASETS_CACHE")
if configured_cache:
return configured_cache
if DEFAULT_DATASET_CACHE_DIR.exists():
return str(DEFAULT_DATASET_CACHE_DIR)
return None
def process_dataset_sample(example: dict) -> dict[str, torch.Tensor]:
"""Convert one Kvasir-SEG sample into tensors expected by the model."""
image_transform = Compose([Resize((IMAGE_SIZE, IMAGE_SIZE)), ToTensor()])
mask_transform = Compose(
[Resize((IMAGE_SIZE, IMAGE_SIZE), interpolation=InterpolationMode.NEAREST), ToTensor()]
)
image = image_transform(example["image"].convert("RGB"))
mask = mask_transform(example["mask"].convert("L"))
# The masks are binary segmentation targets. Thresholding removes any
# interpolation values introduced by resizing.
mask = (mask > 0.5).float()
return {"pixel_values": image, "labels": mask}
class SegmentationCollator:
"""Stack already-processed samples into a mini-batch."""
def __call__(self, features: list[dict[str, torch.Tensor]]) -> dict[str, torch.Tensor]:
return {
"pixel_values": torch.stack([feature["pixel_values"] for feature in features]),
"labels": torch.stack([feature["labels"] for feature in features]),
}
class DiceBCELoss(nn.Module):
"""Binary cross entropy plus Dice loss for imbalanced segmentation masks."""
def __init__(self, smooth: float = 1.0) -> None:
super().__init__()
self.bce = nn.BCEWithLogitsLoss()
self.smooth = smooth
def forward(self, logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
bce_loss = self.bce(logits, labels)
probabilities = torch.sigmoid(logits)
probabilities = probabilities.flatten(start_dim=1)
labels = labels.flatten(start_dim=1)
intersection = (probabilities * labels).sum(dim=1)
denominator = probabilities.sum(dim=1) + labels.sum(dim=1)
dice_loss = 1.0 - ((2.0 * intersection + self.smooth) / (denominator + self.smooth))
return bce_loss + dice_loss.mean()
class UNetTrainer(Trainer):
"""Trainer subclass that applies segmentation loss to raw model logits."""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.loss_fn = DiceBCELoss()
def compute_loss(
self,
model: nn.Module,
inputs: dict[str, torch.Tensor],
return_outputs: bool = False,
**_: object,
):
labels = inputs["labels"]
logits = model(inputs["pixel_values"])
loss = self.loss_fn(logits, labels)
outputs = {"logits": logits}
return (loss, outputs) if return_outputs else loss
def compute_metrics(eval_prediction) -> dict[str, float]:
"""Compute common binary segmentation metrics from Trainer predictions."""
logits, labels = eval_prediction
if isinstance(logits, (tuple, list)):
logits = logits[0]
probabilities = 1.0 / (1.0 + np.exp(-logits))
predictions = probabilities > 0.5
labels = labels > 0.5
intersection = np.logical_and(predictions, labels).sum()
union = np.logical_or(predictions, labels).sum()
prediction_sum = predictions.sum()
label_sum = labels.sum()
dice = (2 * intersection + 1.0) / (prediction_sum + label_sum + 1.0)
iou = (intersection + 1.0) / (union + 1.0)
return {"dice": float(dice), "iou": float(iou)}
def write_model_card(output_dir: str, model_name: str) -> None:
"""Write a lightweight model card for the trained U-Net."""
model_card = f"""---
language: en
license: mit
tags:
- image-segmentation
- medical-imaging
- pytorch
- unet
datasets:
- Angelou0516/kvasir-seg
---
# {model_name}
This model is a compact PyTorch U-Net for binary gastrointestinal polyp
segmentation on the Kvasir-SEG dataset.
## Architecture
The network uses a four-level U-Net with convolutional encoder blocks,
a bottleneck, transposed-convolution decoder blocks, skip connections,
and a final `1 x 1` convolution that produces one mask logit channel.
## Loss
Training uses a combined Dice and binary cross-entropy loss. BCE handles
per-pixel classification, while Dice loss directly optimizes mask overlap
and helps with foreground/background class imbalance.
"""
Path(output_dir).mkdir(parents=True, exist_ok=True)
Path(output_dir, "README.md").write_text(model_card, encoding="utf-8")
def build_datasets():
"""Load and preprocess the required Hugging Face Kvasir-SEG dataset."""
dataset = load_dataset("Angelou0516/kvasir-seg", cache_dir=dataset_cache_dir())
train_dataset = dataset["train"].map(
process_dataset_sample,
remove_columns=dataset["train"].column_names,
)
validation_split = "validation" if "validation" in dataset else "test"
eval_dataset = dataset[validation_split].map(
process_dataset_sample,
remove_columns=dataset[validation_split].column_names,
)
train_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
eval_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
return train_dataset, eval_dataset
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Train a U-Net on Kvasir-SEG.")
parser.add_argument("--output-dir", default="./kvasir-unet", help="Training output directory.")
parser.add_argument("--hub-model-id", default=DEFAULT_HUB_MODEL_ID, help="Hugging Face Hub repo id.")
parser.add_argument("--push-to-hub", action="store_true", help="Push weights and model card.")
parser.add_argument("--num-train-epochs", type=float, default=10.0)
parser.add_argument("--learning-rate", type=float, default=1e-4)
parser.add_argument("--batch-size", type=int, default=4)
parser.add_argument("--eval-batch-size", type=int, default=4)
return parser.parse_args()
def main() -> None:
args = parse_args()
train_dataset, eval_dataset = build_datasets()
model = UNet()
training_args = TrainingArguments(
output_dir=args.output_dir,
num_train_epochs=args.num_train_epochs,
learning_rate=args.learning_rate,
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.eval_batch_size,
eval_strategy="epoch",
save_strategy="epoch",
logging_steps=25,
load_best_model_at_end=True,
metric_for_best_model="dice",
greater_is_better=True,
remove_unused_columns=False,
push_to_hub=False,
hub_model_id=args.hub_model_id,
)
trainer = UNetTrainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
data_collator=SegmentationCollator(),
compute_metrics=compute_metrics,
)
trainer.train()
trainer.evaluate()
save_file(model.state_dict(), f"{args.output_dir}/model.safetensors")
write_model_card(args.output_dir, args.hub_model_id)
if args.push_to_hub:
upload_folder(
repo_id=args.hub_model_id,
folder_path=args.output_dir,
repo_type="model",
)
if __name__ == "__main__":
main()
|