week4 / submission.py
FolashadeO's picture
Upload folder using huggingface_hub
15301a5 verified
Raw History Blame Contribute Delete
5.5 kB
"""Submission sanity-check script for the Week 04 U-Net lab.
The evaluator imports ``load_model`` and ``process_dataset_sample``. Keep both
functions lightweight and deterministic so they work in the benchmark runner.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import cast
import torch
from datasets import load_dataset
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
from torch.utils.data import Dataset as TorchDataset
from torchvision.transforms import Compose, Resize, ToTensor
from torchvision.transforms import InterpolationMode
from model import UNet
IMAGE_SIZE = 256
DEFAULT_HUB_MODEL_ID = "FolashadeO/week4"
DEFAULT_LOCAL_WEIGHT_PATHS = (
Path("model.safetensors"),
Path("kvasir-unet/model.safetensors"),
)
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 load_model() -> torch.nn.Module:
"""Download trained weights from Hugging Face Hub and return the U-Net.
Set ``HF_MODEL_REPO_ID`` to the repository that contains ``model.safetensors``.
For local checks before publishing, set ``MODEL_WEIGHTS_PATH`` or place the
trained weights at ``./model.safetensors`` or ``./kvasir-unet/model.safetensors``.
"""
model = UNet(in_channels=3, out_channels=1)
local_weights = os.environ.get("MODEL_WEIGHTS_PATH")
if local_weights:
weights_path = Path(local_weights)
else:
weights_path = next(
(path for path in DEFAULT_LOCAL_WEIGHT_PATHS if path.exists()),
None,
)
if weights_path is not None:
state_dict = load_file(str(weights_path))
model.load_state_dict(state_dict, strict=True)
model.eval()
return model
repo_id = os.environ.get("HF_MODEL_REPO_ID", DEFAULT_HUB_MODEL_ID)
if not repo_id:
raise ValueError(
"No trained weights were found. Set MODEL_WEIGHTS_PATH for a local "
"model.safetensors file or set HF_MODEL_REPO_ID to your Hugging Face "
"model repository."
)
hub_weights_path = hf_hub_download(repo_id=repo_id, filename="model.safetensors")
state_dict = load_file(hub_weights_path)
model.load_state_dict(state_dict, strict=True)
model.eval()
return model
def load_model_from_local_weights(weights_path: str | Path) -> torch.nn.Module:
"""Convenience helper for local testing before the model is pushed to Hub."""
model = UNet(in_channels=3, out_channels=1)
model.load_state_dict(load_file(str(weights_path)), strict=True)
model.eval()
return model
def process_dataset_sample(example: dict) -> dict[str, torch.Tensor]:
"""Convert a raw Kvasir-SEG example into model-ready tensors.
Input keys:
image: RGB endoscopy image as a PIL image.
mask: binary polyp mask as a PIL image.
Output keys:
pixel_values: float tensor shaped ``3 x 256 x 256`` in ``[0, 1]``.
labels: binary float tensor shaped ``1 x 256 x 256``.
"""
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"))
mask = (mask > 0.5).float()
return {"pixel_values": image, "labels": mask}
def main() -> None:
"""Run a quick shape and DataLoader integration check."""
print("Loading model...")
model = load_model()
print("Loading test dataset...")
dataset = load_dataset(
"Angelou0516/kvasir-seg",
split="test",
cache_dir=dataset_cache_dir(),
)
processed_dataset = dataset.map(
process_dataset_sample,
remove_columns=dataset.column_names,
)
processed_dataset.set_format(type="torch", columns=["pixel_values", "labels"])
eval_dataloader = torch.utils.data.DataLoader(
cast(TorchDataset, processed_dataset),
batch_size=2,
shuffle=False,
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
print("Running a quick sanity check on a few batches...\n")
for batch_index, batch in enumerate(eval_dataloader):
if batch_index >= 3:
break
pixel_values = batch["pixel_values"].to(device)
labels = batch["labels"].to(device)
with torch.no_grad():
logits = model(pixel_values)
print(f"--- Batch {batch_index + 1} ---")
print(f"Input shape: {pixel_values.shape}")
print(f"Label shape: {labels.shape}")
print(f"Output shape: {logits.shape}\n")
assert logits.ndim == 4, (
f"Model output should be 4D (batch, channels, height, width), got {logits.ndim}D."
)
assert logits.shape[2:] == pixel_values.shape[2:], (
"Output spatial dimensions must match the input."
)
assert logits.shape[1] == 1, "Model output should have one mask channel."
print("Model inference test passed.")
if __name__ == "__main__":
main()