Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- model.py +119 -0
- pyproject.toml +17 -0
- submission.py +175 -0
- train.py +253 -0
- week4_caa.pdf +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
week4_caa.pdf filter=lfs diff=lfs merge=lfs -text
|
model.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""U-Net model for binary medical image segmentation.
|
| 2 |
+
|
| 3 |
+
The assignment requires an output mask of shape ``1 x H x W``. This module
|
| 4 |
+
therefore returns one logit channel per pixel; sigmoid activation is applied
|
| 5 |
+
inside the loss/metric code or during inference, not inside the model.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
from torch import nn
|
| 12 |
+
from torch.nn import functional as F
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class DoubleConv(nn.Module):
|
| 16 |
+
"""Two convolution blocks used throughout the U-Net encoder and decoder."""
|
| 17 |
+
|
| 18 |
+
def __init__(self, in_channels: int, out_channels: int) -> None:
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.block = nn.Sequential(
|
| 21 |
+
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False),
|
| 22 |
+
nn.BatchNorm2d(out_channels),
|
| 23 |
+
nn.ReLU(inplace=True),
|
| 24 |
+
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False),
|
| 25 |
+
nn.BatchNorm2d(out_channels),
|
| 26 |
+
nn.ReLU(inplace=True),
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 30 |
+
return self.block(x)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class DownBlock(nn.Module):
|
| 34 |
+
"""Encoder block: reduce spatial size, then extract richer features."""
|
| 35 |
+
|
| 36 |
+
def __init__(self, in_channels: int, out_channels: int) -> None:
|
| 37 |
+
super().__init__()
|
| 38 |
+
self.block = nn.Sequential(
|
| 39 |
+
nn.MaxPool2d(kernel_size=2, stride=2),
|
| 40 |
+
DoubleConv(in_channels, out_channels),
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 44 |
+
return self.block(x)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class UpBlock(nn.Module):
|
| 48 |
+
"""Decoder block with learned upsampling and a U-Net skip connection."""
|
| 49 |
+
|
| 50 |
+
def __init__(self, in_channels: int, skip_channels: int, out_channels: int) -> None:
|
| 51 |
+
super().__init__()
|
| 52 |
+
self.up = nn.ConvTranspose2d(
|
| 53 |
+
in_channels,
|
| 54 |
+
out_channels,
|
| 55 |
+
kernel_size=2,
|
| 56 |
+
stride=2,
|
| 57 |
+
)
|
| 58 |
+
self.conv = DoubleConv(out_channels + skip_channels, out_channels)
|
| 59 |
+
|
| 60 |
+
def forward(self, x: torch.Tensor, skip: torch.Tensor) -> torch.Tensor:
|
| 61 |
+
x = self.up(x)
|
| 62 |
+
|
| 63 |
+
# Resize defensively in case an input dimension is not perfectly divisible
|
| 64 |
+
# by 16. The default 256 x 256 preprocessing does not need this path.
|
| 65 |
+
if x.shape[-2:] != skip.shape[-2:]:
|
| 66 |
+
x = F.interpolate(
|
| 67 |
+
x,
|
| 68 |
+
size=skip.shape[-2:],
|
| 69 |
+
mode="bilinear",
|
| 70 |
+
align_corners=False,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return self.conv(torch.cat([skip, x], dim=1))
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class UNet(nn.Module):
|
| 77 |
+
"""A compact four-level U-Net for binary Kvasir-SEG polyp segmentation."""
|
| 78 |
+
|
| 79 |
+
def __init__(
|
| 80 |
+
self,
|
| 81 |
+
in_channels: int = 3,
|
| 82 |
+
out_channels: int = 1,
|
| 83 |
+
features: tuple[int, int, int, int] = (64, 128, 256, 512),
|
| 84 |
+
) -> None:
|
| 85 |
+
super().__init__()
|
| 86 |
+
f1, f2, f3, f4 = features
|
| 87 |
+
|
| 88 |
+
self.encoder1 = DoubleConv(in_channels, f1)
|
| 89 |
+
self.encoder2 = DownBlock(f1, f2)
|
| 90 |
+
self.encoder3 = DownBlock(f2, f3)
|
| 91 |
+
self.encoder4 = DownBlock(f3, f4)
|
| 92 |
+
|
| 93 |
+
self.bottleneck = DownBlock(f4, f4 * 2)
|
| 94 |
+
|
| 95 |
+
self.decoder4 = UpBlock(f4 * 2, f4, f4)
|
| 96 |
+
self.decoder3 = UpBlock(f4, f3, f3)
|
| 97 |
+
self.decoder2 = UpBlock(f3, f2, f2)
|
| 98 |
+
self.decoder1 = UpBlock(f2, f1, f1)
|
| 99 |
+
|
| 100 |
+
self.output_conv = nn.Conv2d(f1, out_channels, kernel_size=1)
|
| 101 |
+
|
| 102 |
+
def forward(
|
| 103 |
+
self,
|
| 104 |
+
pixel_values: torch.Tensor,
|
| 105 |
+
labels: torch.Tensor | None = None,
|
| 106 |
+
) -> torch.Tensor:
|
| 107 |
+
enc1 = self.encoder1(pixel_values)
|
| 108 |
+
enc2 = self.encoder2(enc1)
|
| 109 |
+
enc3 = self.encoder3(enc2)
|
| 110 |
+
enc4 = self.encoder4(enc3)
|
| 111 |
+
|
| 112 |
+
bottleneck = self.bottleneck(enc4)
|
| 113 |
+
|
| 114 |
+
dec4 = self.decoder4(bottleneck, enc4)
|
| 115 |
+
dec3 = self.decoder3(dec4, enc3)
|
| 116 |
+
dec2 = self.decoder2(dec3, enc2)
|
| 117 |
+
dec1 = self.decoder1(dec2, enc1)
|
| 118 |
+
|
| 119 |
+
return self.output_conv(dec1)
|
pyproject.toml
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "week-04"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Week 04 Lab Assignment"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.13"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"numpy>=2.4.2",
|
| 9 |
+
"safetensors>=0.5.2",
|
| 10 |
+
"torch>=2.10.0",
|
| 11 |
+
"torchvision>=0.25.0",
|
| 12 |
+
"transformers>=4.48.2",
|
| 13 |
+
"datasets>=4.6.0",
|
| 14 |
+
"huggingface-hub>=0.36.0",
|
| 15 |
+
"accelerate>=1.1.0",
|
| 16 |
+
]
|
| 17 |
+
|
submission.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Submission sanity-check script for the Week 04 U-Net lab.
|
| 2 |
+
|
| 3 |
+
The evaluator imports ``load_model`` and ``process_dataset_sample``. Keep both
|
| 4 |
+
functions lightweight and deterministic so they work in the benchmark runner.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from typing import cast
|
| 12 |
+
|
| 13 |
+
import torch
|
| 14 |
+
from datasets import load_dataset
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
from safetensors.torch import load_file
|
| 17 |
+
from torch.utils.data import Dataset as TorchDataset
|
| 18 |
+
from torchvision.transforms import Compose, Resize, ToTensor
|
| 19 |
+
from torchvision.transforms import InterpolationMode
|
| 20 |
+
|
| 21 |
+
from model import UNet
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
IMAGE_SIZE = 256
|
| 25 |
+
DEFAULT_HUB_MODEL_ID = "FolashadeO/week4"
|
| 26 |
+
DEFAULT_LOCAL_WEIGHT_PATHS = (
|
| 27 |
+
Path("model.safetensors"),
|
| 28 |
+
Path("kvasir-unet/model.safetensors"),
|
| 29 |
+
)
|
| 30 |
+
DEFAULT_DATASET_CACHE_DIR = Path("hf_cache")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def dataset_cache_dir() -> str | None:
|
| 34 |
+
"""Use the workspace dataset cache when present, otherwise use HF defaults."""
|
| 35 |
+
|
| 36 |
+
configured_cache = os.environ.get("HF_DATASETS_CACHE")
|
| 37 |
+
if configured_cache:
|
| 38 |
+
return configured_cache
|
| 39 |
+
|
| 40 |
+
if DEFAULT_DATASET_CACHE_DIR.exists():
|
| 41 |
+
return str(DEFAULT_DATASET_CACHE_DIR)
|
| 42 |
+
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def load_model() -> torch.nn.Module:
|
| 47 |
+
"""Download trained weights from Hugging Face Hub and return the U-Net.
|
| 48 |
+
|
| 49 |
+
Set ``HF_MODEL_REPO_ID`` to the repository that contains ``model.safetensors``.
|
| 50 |
+
For local checks before publishing, set ``MODEL_WEIGHTS_PATH`` or place the
|
| 51 |
+
trained weights at ``./model.safetensors`` or ``./kvasir-unet/model.safetensors``.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
model = UNet(in_channels=3, out_channels=1)
|
| 55 |
+
local_weights = os.environ.get("MODEL_WEIGHTS_PATH")
|
| 56 |
+
|
| 57 |
+
if local_weights:
|
| 58 |
+
weights_path = Path(local_weights)
|
| 59 |
+
else:
|
| 60 |
+
weights_path = next(
|
| 61 |
+
(path for path in DEFAULT_LOCAL_WEIGHT_PATHS if path.exists()),
|
| 62 |
+
None,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if weights_path is not None:
|
| 66 |
+
state_dict = load_file(str(weights_path))
|
| 67 |
+
model.load_state_dict(state_dict, strict=True)
|
| 68 |
+
model.eval()
|
| 69 |
+
return model
|
| 70 |
+
|
| 71 |
+
repo_id = os.environ.get("HF_MODEL_REPO_ID", DEFAULT_HUB_MODEL_ID)
|
| 72 |
+
if not repo_id:
|
| 73 |
+
raise ValueError(
|
| 74 |
+
"No trained weights were found. Set MODEL_WEIGHTS_PATH for a local "
|
| 75 |
+
"model.safetensors file or set HF_MODEL_REPO_ID to your Hugging Face "
|
| 76 |
+
"model repository."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
hub_weights_path = hf_hub_download(repo_id=repo_id, filename="model.safetensors")
|
| 80 |
+
state_dict = load_file(hub_weights_path)
|
| 81 |
+
model.load_state_dict(state_dict, strict=True)
|
| 82 |
+
model.eval()
|
| 83 |
+
return model
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def load_model_from_local_weights(weights_path: str | Path) -> torch.nn.Module:
|
| 87 |
+
"""Convenience helper for local testing before the model is pushed to Hub."""
|
| 88 |
+
|
| 89 |
+
model = UNet(in_channels=3, out_channels=1)
|
| 90 |
+
model.load_state_dict(load_file(str(weights_path)), strict=True)
|
| 91 |
+
model.eval()
|
| 92 |
+
return model
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def process_dataset_sample(example: dict) -> dict[str, torch.Tensor]:
|
| 96 |
+
"""Convert a raw Kvasir-SEG example into model-ready tensors.
|
| 97 |
+
|
| 98 |
+
Input keys:
|
| 99 |
+
image: RGB endoscopy image as a PIL image.
|
| 100 |
+
mask: binary polyp mask as a PIL image.
|
| 101 |
+
|
| 102 |
+
Output keys:
|
| 103 |
+
pixel_values: float tensor shaped ``3 x 256 x 256`` in ``[0, 1]``.
|
| 104 |
+
labels: binary float tensor shaped ``1 x 256 x 256``.
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
image_transform = Compose([Resize((IMAGE_SIZE, IMAGE_SIZE)), ToTensor()])
|
| 108 |
+
mask_transform = Compose(
|
| 109 |
+
[Resize((IMAGE_SIZE, IMAGE_SIZE), interpolation=InterpolationMode.NEAREST), ToTensor()]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
image = image_transform(example["image"].convert("RGB"))
|
| 113 |
+
mask = mask_transform(example["mask"].convert("L"))
|
| 114 |
+
mask = (mask > 0.5).float()
|
| 115 |
+
|
| 116 |
+
return {"pixel_values": image, "labels": mask}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def main() -> None:
|
| 120 |
+
"""Run a quick shape and DataLoader integration check."""
|
| 121 |
+
|
| 122 |
+
print("Loading model...")
|
| 123 |
+
model = load_model()
|
| 124 |
+
|
| 125 |
+
print("Loading test dataset...")
|
| 126 |
+
dataset = load_dataset(
|
| 127 |
+
"Angelou0516/kvasir-seg",
|
| 128 |
+
split="test",
|
| 129 |
+
cache_dir=dataset_cache_dir(),
|
| 130 |
+
)
|
| 131 |
+
processed_dataset = dataset.map(
|
| 132 |
+
process_dataset_sample,
|
| 133 |
+
remove_columns=dataset.column_names,
|
| 134 |
+
)
|
| 135 |
+
processed_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
|
| 136 |
+
|
| 137 |
+
eval_dataloader = torch.utils.data.DataLoader(
|
| 138 |
+
cast(TorchDataset, processed_dataset),
|
| 139 |
+
batch_size=2,
|
| 140 |
+
shuffle=False,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 144 |
+
model.to(device)
|
| 145 |
+
|
| 146 |
+
print("Running a quick sanity check on a few batches...\n")
|
| 147 |
+
|
| 148 |
+
for batch_index, batch in enumerate(eval_dataloader):
|
| 149 |
+
if batch_index >= 3:
|
| 150 |
+
break
|
| 151 |
+
|
| 152 |
+
pixel_values = batch["pixel_values"].to(device)
|
| 153 |
+
labels = batch["labels"].to(device)
|
| 154 |
+
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
logits = model(pixel_values)
|
| 157 |
+
|
| 158 |
+
print(f"--- Batch {batch_index + 1} ---")
|
| 159 |
+
print(f"Input shape: {pixel_values.shape}")
|
| 160 |
+
print(f"Label shape: {labels.shape}")
|
| 161 |
+
print(f"Output shape: {logits.shape}\n")
|
| 162 |
+
|
| 163 |
+
assert logits.ndim == 4, (
|
| 164 |
+
f"Model output should be 4D (batch, channels, height, width), got {logits.ndim}D."
|
| 165 |
+
)
|
| 166 |
+
assert logits.shape[2:] == pixel_values.shape[2:], (
|
| 167 |
+
"Output spatial dimensions must match the input."
|
| 168 |
+
)
|
| 169 |
+
assert logits.shape[1] == 1, "Model output should have one mask channel."
|
| 170 |
+
|
| 171 |
+
print("Model inference test passed.")
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
if __name__ == "__main__":
|
| 175 |
+
main()
|
train.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Training script for the Week 04 Kvasir-SEG U-Net assignment.
|
| 2 |
+
|
| 3 |
+
Run example:
|
| 4 |
+
python train.py --output-dir ./kvasir-unet --num-train-epochs 10
|
| 5 |
+
|
| 6 |
+
To publish after training, pass ``--push-to-hub``.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import argparse
|
| 12 |
+
import os
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
import torch
|
| 17 |
+
from datasets import load_dataset
|
| 18 |
+
from huggingface_hub import upload_folder
|
| 19 |
+
from safetensors.torch import save_file
|
| 20 |
+
from torch import nn
|
| 21 |
+
from torchvision.transforms import Compose, Resize, ToTensor
|
| 22 |
+
from torchvision.transforms import InterpolationMode
|
| 23 |
+
from transformers import Trainer, TrainingArguments
|
| 24 |
+
|
| 25 |
+
from model import UNet
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
IMAGE_SIZE = 256
|
| 29 |
+
DEFAULT_HUB_MODEL_ID = "FolashadeO/week4"
|
| 30 |
+
DEFAULT_DATASET_CACHE_DIR = Path("hf_cache")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def dataset_cache_dir() -> str | None:
|
| 34 |
+
"""Use the workspace dataset cache when present, otherwise use HF defaults."""
|
| 35 |
+
|
| 36 |
+
configured_cache = os.environ.get("HF_DATASETS_CACHE")
|
| 37 |
+
if configured_cache:
|
| 38 |
+
return configured_cache
|
| 39 |
+
|
| 40 |
+
if DEFAULT_DATASET_CACHE_DIR.exists():
|
| 41 |
+
return str(DEFAULT_DATASET_CACHE_DIR)
|
| 42 |
+
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def process_dataset_sample(example: dict) -> dict[str, torch.Tensor]:
|
| 47 |
+
"""Convert one Kvasir-SEG sample into tensors expected by the model."""
|
| 48 |
+
|
| 49 |
+
image_transform = Compose([Resize((IMAGE_SIZE, IMAGE_SIZE)), ToTensor()])
|
| 50 |
+
mask_transform = Compose(
|
| 51 |
+
[Resize((IMAGE_SIZE, IMAGE_SIZE), interpolation=InterpolationMode.NEAREST), ToTensor()]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
image = image_transform(example["image"].convert("RGB"))
|
| 55 |
+
mask = mask_transform(example["mask"].convert("L"))
|
| 56 |
+
|
| 57 |
+
# The masks are binary segmentation targets. Thresholding removes any
|
| 58 |
+
# interpolation values introduced by resizing.
|
| 59 |
+
mask = (mask > 0.5).float()
|
| 60 |
+
return {"pixel_values": image, "labels": mask}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class SegmentationCollator:
|
| 64 |
+
"""Stack already-processed samples into a mini-batch."""
|
| 65 |
+
|
| 66 |
+
def __call__(self, features: list[dict[str, torch.Tensor]]) -> dict[str, torch.Tensor]:
|
| 67 |
+
return {
|
| 68 |
+
"pixel_values": torch.stack([feature["pixel_values"] for feature in features]),
|
| 69 |
+
"labels": torch.stack([feature["labels"] for feature in features]),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class DiceBCELoss(nn.Module):
|
| 74 |
+
"""Binary cross entropy plus Dice loss for imbalanced segmentation masks."""
|
| 75 |
+
|
| 76 |
+
def __init__(self, smooth: float = 1.0) -> None:
|
| 77 |
+
super().__init__()
|
| 78 |
+
self.bce = nn.BCEWithLogitsLoss()
|
| 79 |
+
self.smooth = smooth
|
| 80 |
+
|
| 81 |
+
def forward(self, logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor:
|
| 82 |
+
bce_loss = self.bce(logits, labels)
|
| 83 |
+
probabilities = torch.sigmoid(logits)
|
| 84 |
+
|
| 85 |
+
probabilities = probabilities.flatten(start_dim=1)
|
| 86 |
+
labels = labels.flatten(start_dim=1)
|
| 87 |
+
|
| 88 |
+
intersection = (probabilities * labels).sum(dim=1)
|
| 89 |
+
denominator = probabilities.sum(dim=1) + labels.sum(dim=1)
|
| 90 |
+
dice_loss = 1.0 - ((2.0 * intersection + self.smooth) / (denominator + self.smooth))
|
| 91 |
+
|
| 92 |
+
return bce_loss + dice_loss.mean()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class UNetTrainer(Trainer):
|
| 96 |
+
"""Trainer subclass that applies segmentation loss to raw model logits."""
|
| 97 |
+
|
| 98 |
+
def __init__(self, *args, **kwargs) -> None:
|
| 99 |
+
super().__init__(*args, **kwargs)
|
| 100 |
+
self.loss_fn = DiceBCELoss()
|
| 101 |
+
|
| 102 |
+
def compute_loss(
|
| 103 |
+
self,
|
| 104 |
+
model: nn.Module,
|
| 105 |
+
inputs: dict[str, torch.Tensor],
|
| 106 |
+
return_outputs: bool = False,
|
| 107 |
+
**_: object,
|
| 108 |
+
):
|
| 109 |
+
labels = inputs["labels"]
|
| 110 |
+
logits = model(inputs["pixel_values"])
|
| 111 |
+
loss = self.loss_fn(logits, labels)
|
| 112 |
+
outputs = {"logits": logits}
|
| 113 |
+
return (loss, outputs) if return_outputs else loss
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def compute_metrics(eval_prediction) -> dict[str, float]:
|
| 117 |
+
"""Compute common binary segmentation metrics from Trainer predictions."""
|
| 118 |
+
|
| 119 |
+
logits, labels = eval_prediction
|
| 120 |
+
if isinstance(logits, (tuple, list)):
|
| 121 |
+
logits = logits[0]
|
| 122 |
+
|
| 123 |
+
probabilities = 1.0 / (1.0 + np.exp(-logits))
|
| 124 |
+
predictions = probabilities > 0.5
|
| 125 |
+
labels = labels > 0.5
|
| 126 |
+
|
| 127 |
+
intersection = np.logical_and(predictions, labels).sum()
|
| 128 |
+
union = np.logical_or(predictions, labels).sum()
|
| 129 |
+
prediction_sum = predictions.sum()
|
| 130 |
+
label_sum = labels.sum()
|
| 131 |
+
|
| 132 |
+
dice = (2 * intersection + 1.0) / (prediction_sum + label_sum + 1.0)
|
| 133 |
+
iou = (intersection + 1.0) / (union + 1.0)
|
| 134 |
+
|
| 135 |
+
return {"dice": float(dice), "iou": float(iou)}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def write_model_card(output_dir: str, model_name: str) -> None:
|
| 139 |
+
"""Write a lightweight model card for the trained U-Net."""
|
| 140 |
+
|
| 141 |
+
model_card = f"""---
|
| 142 |
+
language: en
|
| 143 |
+
license: mit
|
| 144 |
+
tags:
|
| 145 |
+
- image-segmentation
|
| 146 |
+
- medical-imaging
|
| 147 |
+
- pytorch
|
| 148 |
+
- unet
|
| 149 |
+
datasets:
|
| 150 |
+
- Angelou0516/kvasir-seg
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
# {model_name}
|
| 154 |
+
|
| 155 |
+
This model is a compact PyTorch U-Net for binary gastrointestinal polyp
|
| 156 |
+
segmentation on the Kvasir-SEG dataset.
|
| 157 |
+
|
| 158 |
+
## Architecture
|
| 159 |
+
|
| 160 |
+
The network uses a four-level U-Net with convolutional encoder blocks,
|
| 161 |
+
a bottleneck, transposed-convolution decoder blocks, skip connections,
|
| 162 |
+
and a final `1 x 1` convolution that produces one mask logit channel.
|
| 163 |
+
|
| 164 |
+
## Loss
|
| 165 |
+
|
| 166 |
+
Training uses a combined Dice and binary cross-entropy loss. BCE handles
|
| 167 |
+
per-pixel classification, while Dice loss directly optimizes mask overlap
|
| 168 |
+
and helps with foreground/background class imbalance.
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
Path(output_dir).mkdir(parents=True, exist_ok=True)
|
| 172 |
+
Path(output_dir, "README.md").write_text(model_card, encoding="utf-8")
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def build_datasets():
|
| 176 |
+
"""Load and preprocess the required Hugging Face Kvasir-SEG dataset."""
|
| 177 |
+
|
| 178 |
+
dataset = load_dataset("Angelou0516/kvasir-seg", cache_dir=dataset_cache_dir())
|
| 179 |
+
train_dataset = dataset["train"].map(
|
| 180 |
+
process_dataset_sample,
|
| 181 |
+
remove_columns=dataset["train"].column_names,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
validation_split = "validation" if "validation" in dataset else "test"
|
| 185 |
+
eval_dataset = dataset[validation_split].map(
|
| 186 |
+
process_dataset_sample,
|
| 187 |
+
remove_columns=dataset[validation_split].column_names,
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
train_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
|
| 191 |
+
eval_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
|
| 192 |
+
return train_dataset, eval_dataset
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
def parse_args() -> argparse.Namespace:
|
| 196 |
+
parser = argparse.ArgumentParser(description="Train a U-Net on Kvasir-SEG.")
|
| 197 |
+
parser.add_argument("--output-dir", default="./kvasir-unet", help="Training output directory.")
|
| 198 |
+
parser.add_argument("--hub-model-id", default=DEFAULT_HUB_MODEL_ID, help="Hugging Face Hub repo id.")
|
| 199 |
+
parser.add_argument("--push-to-hub", action="store_true", help="Push weights and model card.")
|
| 200 |
+
parser.add_argument("--num-train-epochs", type=float, default=10.0)
|
| 201 |
+
parser.add_argument("--learning-rate", type=float, default=1e-4)
|
| 202 |
+
parser.add_argument("--batch-size", type=int, default=4)
|
| 203 |
+
parser.add_argument("--eval-batch-size", type=int, default=4)
|
| 204 |
+
return parser.parse_args()
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def main() -> None:
|
| 208 |
+
args = parse_args()
|
| 209 |
+
train_dataset, eval_dataset = build_datasets()
|
| 210 |
+
|
| 211 |
+
model = UNet()
|
| 212 |
+
training_args = TrainingArguments(
|
| 213 |
+
output_dir=args.output_dir,
|
| 214 |
+
num_train_epochs=args.num_train_epochs,
|
| 215 |
+
learning_rate=args.learning_rate,
|
| 216 |
+
per_device_train_batch_size=args.batch_size,
|
| 217 |
+
per_device_eval_batch_size=args.eval_batch_size,
|
| 218 |
+
eval_strategy="epoch",
|
| 219 |
+
save_strategy="epoch",
|
| 220 |
+
logging_steps=25,
|
| 221 |
+
load_best_model_at_end=True,
|
| 222 |
+
metric_for_best_model="dice",
|
| 223 |
+
greater_is_better=True,
|
| 224 |
+
remove_unused_columns=False,
|
| 225 |
+
push_to_hub=False,
|
| 226 |
+
hub_model_id=args.hub_model_id,
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
trainer = UNetTrainer(
|
| 230 |
+
model=model,
|
| 231 |
+
args=training_args,
|
| 232 |
+
train_dataset=train_dataset,
|
| 233 |
+
eval_dataset=eval_dataset,
|
| 234 |
+
data_collator=SegmentationCollator(),
|
| 235 |
+
compute_metrics=compute_metrics,
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
trainer.train()
|
| 239 |
+
trainer.evaluate()
|
| 240 |
+
|
| 241 |
+
save_file(model.state_dict(), f"{args.output_dir}/model.safetensors")
|
| 242 |
+
write_model_card(args.output_dir, args.hub_model_id)
|
| 243 |
+
|
| 244 |
+
if args.push_to_hub:
|
| 245 |
+
upload_folder(
|
| 246 |
+
repo_id=args.hub_model_id,
|
| 247 |
+
folder_path=args.output_dir,
|
| 248 |
+
repo_type="model",
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
if __name__ == "__main__":
|
| 253 |
+
main()
|
week4_caa.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d4c4fabe2aa12c6c0db5054f6f6adce878c9ac3feb5eb696419f85f636989dd0
|
| 3 |
+
size 128707
|