$npx -y skills add Aperivue/medsci-skills --skill model-scaffoldGenerate a reproducible, runnable PyTorch training repo for a medical-imaging task — segmentation, classification, detection, image-to-image synthesis, self-supervised pretraining, or fine-tuning a pretrained backbone (transfer learning) — the missing middle link between choosing
| 1 | # Model-Scaffold Skill |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | This skill stamps out a **runnable PyTorch training repo** for a medical-imaging task — `--task` |
| 6 | **segmentation** (U-Net), **classification** (CNN / `timm` backbone), **detection** (torchvision Faster |
| 7 | R-CNN / FPN), **synthesis** (Pix2Pix generator + PatchGAN), **ssl** (SimCLR encoder), or **finetune** |
| 8 | (transfer-learning a pretrained backbone with a frozen→unfrozen schedule + a provenance record) — |
| 9 | with the reproducibility guarantees **baked in by construction** — so the build is leakage-safe and |
| 10 | reproducible before a single epoch runs. It is the imaging analogue of how `/analyze-stats` generates |
| 11 | runnable statistical code: the generator produces the repo, you run the training on your GPU / Colab, |
| 12 | and the lane's deterministic gates verify the network-free parts. |
| 13 | |
| 14 | It is the **missing middle link** in the lane: `/architecture-zoo` (choose) → **model-scaffold (build)** |
| 15 | → `/model-validation` (validate the split / design) → `/model-evaluation` + `/analyze-stats` (metrics) |
| 16 | → `/write-paper` + `/check-reporting` (publish). It **integrates** MONAI / nnU-Net / TorchIO (referenced |
| 17 | in the generated `requirements.txt`); it does not reimplement them. |
| 18 | |
| 19 | ## When to use |
| 20 | - You have a data manifest (one row per image, with a patient/subject ID) and want a reproducible, |
| 21 | leakage-safe starting repo for a segmentation model. |
| 22 | - You want to **fine-tune a pretrained backbone** (transfer learning — the common clinician workflow: |
| 23 | a `timm` / MONAI / MedSAM checkpoint adapted to your collected clinical data) with the freeze schedule, |
| 24 | discriminative learning rates, and pretrained-weight provenance recorded (`--task finetune`). |
| 25 | |
| 26 | ## When NOT to use |
| 27 | - Auditing an already-trained model's validation design → `/model-validation`. |
| 28 | - Held-out metrics / calibration / bootstrap CIs → `/model-evaluation` then `/analyze-stats`. |
| 29 | - Choosing the architecture for the research question → `/architecture-zoo` (when available). |
| 30 | - Reimplementing MONAI / nnU-Net → out of scope (the scaffold integrates them). |
| 31 | - LLM / MLLM evaluation → `/mllm-eval`. |
| 32 | |
| 33 | ## Workflow |
| 34 | |
| 35 | ### Phase 1 — Prepare the manifest |
| 36 | A CSV with **one row per image** and a **patient/subject ID** column (`patient_id` / `subject_id` / |
| 37 | `case_id`), plus image and label path columns. The ID column is load-bearing: the split is done at the |
| 38 | patient level off this column. |
| 39 | |
| 40 | ### Phase 2 — Generate the repo |
| 41 | ```bash |
| 42 | python3 ${CLAUDE_SKILL_DIR}/scripts/scaffold.py \ |
| 43 | --manifest <manifest.csv> --task segmentation --out model_repo --seed 42 \ |
| 44 | --in-channels 1 --out-channels 1 |
| 45 | # --task = segmentation | classification | detection | synthesis | ssl | finetune |
| 46 | # (out-channels = num classes for classification/finetune, target channels for synthesis) |
| 47 | # fine-tuning a pretrained backbone (transfer learning) on collected clinical data: |
| 48 | python3 ${CLAUDE_SKILL_DIR}/scripts/scaffold.py \ |
| 49 | --manifest <manifest.csv> --task finetune --out model_repo --seed 42 \ |
| 50 | --out-channels <num_classes> --from-pretrained timm:resnet50.a1_in1k |
| 51 | # emits PRETRAINED.md (provenance) + a frozen→unfrozen train.py with discriminative LRs; |
| 52 | # record the exact pretrained source so the fine-tune is reproducible. |
| 53 | ``` |
| 54 | This writes `model_repo/` with `config.yaml`, `model.py` (the task's model — U-Net / CNN / Faster R-CNN |
| 55 | / Pix2Pix / SimCLR encoder), `dataset.py` (reads the frozen split), `losses.py` (task-appropriate), |
| 56 | `train.py`, `evaluate.py`, `requirements.txt`, |
| 57 | `REPRODUCIBILITY.md`, `methods_st |