|
| 1 | +import pickle |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | +from lightning import Trainer |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def pretrain_files(tmp_path): |
| 9 | + """Three .pt files of shape [1, 32, 32, 32] for pretraining (raw image, no label). |
| 10 | + 32^3 ensures the UNet bottleneck (4 max-pool stages) stays at 2x2x2, avoiding |
| 11 | + single-element BatchNorm errors with batch_size=1. |
| 12 | + """ |
| 13 | + files = [] |
| 14 | + for i in range(3): |
| 15 | + path = tmp_path / f"pre_{i:03d}.pt" |
| 16 | + torch.save(torch.randn(1, 32, 32, 32), path) |
| 17 | + files.append(str(path)) |
| 18 | + return {"train": files[:2], "val": [files[2]]} |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def seg_files(tmp_path): |
| 23 | + """Three .pt + .pkl file pairs for segmentation. Shape [2, 32, 32, 32] = [image, label]. |
| 24 | + 32^3 ensures the UNet bottleneck (4 max-pool stages) stays at 2x2x2. |
| 25 | + """ |
| 26 | + files = [] |
| 27 | + for i in range(3): |
| 28 | + pt = tmp_path / f"seg_{i:03d}.pt" |
| 29 | + pkl = tmp_path / f"seg_{i:03d}.pkl" |
| 30 | + data = torch.zeros(2, 32, 32, 32) |
| 31 | + data[0] = torch.randn(32, 32, 32) |
| 32 | + data[1] = torch.randint(0, 2, (32, 32, 32)).float() |
| 33 | + torch.save(data, pt) |
| 34 | + with open(pkl, "wb") as f: |
| 35 | + pickle.dump({"foreground_locations": []}, f) |
| 36 | + files.append(str(pt)) |
| 37 | + return {"train": files[:2], "val": [files[2]]} |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def clsreg_files(tmp_path): |
| 42 | + """Three .pt files containing (image[1,32,32,32], label_scalar) tuples. |
| 43 | + 32^3 prevents single-element BatchNorm errors in the 4-stage UNet encoder. |
| 44 | + Labels are 0-dim int tensors; ClassificationModule.on_before_batch_transfer |
| 45 | + squeezes and converts to long before the training step. |
| 46 | + """ |
| 47 | + files = [] |
| 48 | + for i in range(3): |
| 49 | + path = tmp_path / f"cls_{i:03d}.pt" |
| 50 | + torch.save((torch.randn(1, 32, 32, 32), torch.tensor(i % 2)), path) |
| 51 | + files.append(str(path)) |
| 52 | + return {"train": files[:2], "val": [files[2]], "test": [files[2]]} |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def reg_files(tmp_path): |
| 57 | + """Three .pt files containing (image[1,32,32,32], label[1]) tuples. |
| 58 | + Labels are 1D float tensors so they collate to [B, 1], matching the |
| 59 | + unet_clsreg_tiny output shape [B, 1] expected by MeanSquaredError. |
| 60 | + """ |
| 61 | + files = [] |
| 62 | + for i in range(3): |
| 63 | + path = tmp_path / f"reg_{i:03d}.pt" |
| 64 | + torch.save((torch.randn(1, 32, 32, 32), torch.tensor([float(i % 2)])), path) |
| 65 | + files.append(str(path)) |
| 66 | + return {"train": files[:2], "val": [files[2]], "test": [files[2]]} |
| 67 | + |
| 68 | + |
| 69 | +@pytest.fixture |
| 70 | +def cls_probe_files(tmp_path): |
| 71 | + """Five .pt files for classification / linear-probe tests. 0-dim integer labels. |
| 72 | + 2 train + 2 val gives full batches when batch_size=2, avoiding the squeeze()-to-scalar |
| 73 | + edge case in ClassificationModule.on_before_batch_transfer with batch_size=1. |
| 74 | + 2 test files (labels 1, 0) ensure both classes are present for AUROC computation. |
| 75 | + """ |
| 76 | + labels = [0, 1, 0, 1, 0, 1] |
| 77 | + files = [] |
| 78 | + for i, lbl in enumerate(labels): |
| 79 | + path = tmp_path / f"clsp_{i:03d}.pt" |
| 80 | + torch.save((torch.randn(1, 32, 32, 32), torch.tensor(lbl)), path) |
| 81 | + files.append(str(path)) |
| 82 | + return {"train": files[:2], "val": files[2:4], "test": files[4:6]} |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def make_trainer(tmp_path): |
| 87 | + """Factory fixture that builds a minimal CPU Trainer for smoke tests.""" |
| 88 | + |
| 89 | + def _make(**kwargs): |
| 90 | + defaults = dict( |
| 91 | + accelerator="cpu", |
| 92 | + max_epochs=1, |
| 93 | + limit_train_batches=2, |
| 94 | + limit_val_batches=2, |
| 95 | + logger=False, |
| 96 | + enable_checkpointing=False, |
| 97 | + enable_progress_bar=False, |
| 98 | + num_sanity_val_steps=0, |
| 99 | + ) |
| 100 | + defaults.update(kwargs) |
| 101 | + return Trainer(default_root_dir=str(tmp_path), **defaults) |
| 102 | + |
| 103 | + return _make |
0 commit comments