界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
设计与创意 · SKILL.md

patch-workflows

patch-workflows

【设计与创意】Design TorchIO patch-based training queues and dense patch inference loops with samplers, SubjectsLoader batches, PatchLocation metadata, and PatchAggregator.

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: patch-workflows description: "Design TorchIO patch-based training queues and dense patch inference loops with samplers, SubjectsLoader batches, PatchLocation metadata, and PatchAggregator." disable-model-invocation: true metadata: disco-role: operating license: Apache 2.0

Patch Workflows

Use this sub-skill when an agent needs to train or infer on large 3D images by sampling patches instead of processing whole volumes. It covers GridSampler, UniformSampler, WeightedSampler, LabelSampler, Queue, SubjectsLoader, ImagesLoader, PatchLocation, and PatchAggregator.

Route by Goal

  • Need dense whole-volume inference: use GridSampler plus PatchAggregator; see dense inference.
  • Need stochastic training patches: use UniformSampler, WeightedSampler, or LabelSampler, optionally buffered by Queue; see training queues.
  • Need to choose sampler or batch access patterns: see API reference.
  • Hit shape, overlap, probability map, or metadata errors: see troubleshooting.
  • Want a runnable synthetic sanity check: run python scripts/patch_workflow_smoke.py --help, then python scripts/patch_workflow_smoke.py from this sub-skill directory.

Current API Reminders

  • Construct in-memory images as tio.ScalarImage(tensor) and labels as tio.LabelMap(tensor); do not use source=tensor.
  • Image tensors are 4D (C, I, J, K) and loader batches are 5D (B, C, I, J, K).
  • SubjectsLoader returns a SubjectsBatch: named images are accessed as batch.t1.data or batch["t1"].data.
  • Patch locations are stored on individual patch subjects as patch.patch_location; after batching, use batch.metadata["patch_location"].
  • PatchAggregator.add_batch() expects model outputs shaped (B, C, I, J, K) and a list of one PatchLocation per batch item.

Minimal Dense Inference Pattern

sampler = tio.GridSampler(subject, patch_size=(64, 64, 64), patch_overlap=16)
loader = tio.SubjectsLoader(sampler, batch_size=4)
aggregator = tio.PatchAggregator(
    spatial_shape=subject.spatial_shape,
    overlap_mode="hann",
    patch_overlap=16,
)

for batch in loader:
    logits = model(batch.t1.data)
    locations = batch.metadata["patch_location"]
    aggregator.add_batch(logits, locations)

volume_logits = aggregator.get_output()

Minimal Training Queue Pattern

sampler = tio.UniformSampler(subjects[0], patch_size=(64, 64, 64))
queue = tio.Queue(
    subjects,
    patch_sampler=sampler,
    max_length=300,
    patches_per_volume=10,
    num_workers=4,
    transform=train_transform,
)
loader = tio.SubjectsLoader(queue, batch_size=16)

for batch in loader:
    inputs = batch.t1.data
    targets = batch.seg.data
    loss = criterion(model(inputs), targets)
    loss.backward()

Boundaries

  • For creating Subject, ScalarImage, and LabelMap objects, use the parent data-model guidance.
  • For choosing augmentations or preprocessing transforms, use the transforms sub-skill.
  • For command-line operations, use the CLI and I/O sub-skill.
我的收藏