--- license: other license_name: kvasir-seg-research-and-education license_link: https://datasets.simula.no/kvasir-seg/ library_name: onnx pipeline_tag: image-segmentation tags: [colonoscopy, polyp-segmentation, medical-imaging, healthcare, medical-ai, unet, resnet34, domain-shift, pytorch, onnx] metrics: [dice] --- # PolypShift: colonoscopy polyp segmentation, tested across hospitals A U-Net with an ImageNet-pretrained ResNet34 encoder that outlines **polyps in colonoscopy images**. It was trained on Kvasir-SEG and tested **unchanged on three datasets it never saw**. > ⚠️ **Research model, not a medical device.** Trained and tested on public retrospective data only. Do not use it for diagnosis or treatment decisions. ▶ **Try it in your browser:** https://huggingface.co/spaces/rigel08/polypshift · **Code and results:** https://github.com/rigel08/polyp-segmentation ## Results (mean Dice, 95% CI) | Images from | Images | Dice | |---|---:|---| | Kvasir-SEG held-out test (same source) | 100 | **0.894** (0.862–0.920) | | CVC-ColonDB (unseen) | 380 | 0.754 (0.726–0.781) | | ETIS-Larib (unseen) | 196 | 0.649 (0.598–0.699) | Small polyps drive much of the drop. ImageNet pretraining added +0.069 Dice (95% CI 0.031–0.110). Near-duplicate video frames were grouped (ORB + RANSAC) so no polyp appears in both train and test. ## Files | File | What it is | |---|---| | `model_fp16.onnx` | ONNX, 16-bit weights (49 MB). Input `image` (1, 3, 256, 256); output `logits` (1, 1, 256, 256). Matches full precision to within 0.00054 per pixel | | `model.pt` | Full-precision PyTorch checkpoint for `load_checkpoint()` in the repo | | `config.json` | Preprocessing, mask threshold (0.4), test-time augmentation | | `example_1.jpg` | A Kvasir-SEG image (see `ATTRIBUTION.md`) | | `example.py` | Segments the image and saves `mask.png` | ## Use it ```bash pip install onnxruntime huggingface_hub numpy pillow python example.py # writes mask.png ``` ```python import json, numpy as np, onnxruntime as ort from PIL import Image from huggingface_hub import hf_hub_download repo = "rigel08/polypshift-polyp-segmentation" cfg = json.load(open(hf_hub_download(repo, "config.json"))) sess = ort.InferenceSession(hf_hub_download(repo, "model_fp16.onnx")) img = Image.open("frame.jpg").convert("RGB") x = (np.asarray(img.resize((256, 256)), np.float32) / 255 - cfg["imagenet_mean"]) / cfg["imagenet_std"] x = x.transpose(2, 0, 1)[None].astype(np.float32) prob = 1 / (1 + np.exp(-sess.run(None, {"image": x})[0][0, 0])) flip = 1 / (1 + np.exp(-sess.run(None, {"image": x[..., ::-1].copy()})[0][0, 0]))[:, ::-1] mask = ((prob + flip) / 2) >= cfg["mask_threshold"] # 256 x 256 polyp mask ``` ## Training data and licence Kvasir-SEG (Jha et al., MMM 2020) is licensed for **research and educational use only**, so these weights carry the same restriction. ## Limitations One training run. Every test image contains a polyp. Frames from the same video are not independent. Expect worse results on small polyps and on unfamiliar endoscopes. **Portfolio:** https://rigel08.github.io/portfolio-site/ · **GitHub:** https://github.com/rigel08 · **All my models and demos:** https://huggingface.co/rigel08