MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / run_test

Function run_test

tests/integration/test_integration_sliding_window.py:33–67  ·  view source on GitHub ↗
(batch_size, img_name, seg_name, output_dir, device="cuda:0")

Source from the content-addressed store, hash-verified

31
32
33def run_test(batch_size, img_name, seg_name, output_dir, device="cuda:0"):
34 ds = ImageDataset(
35 [img_name],
36 [seg_name],
37 transform=EnsureChannelFirst(channel_dim="no_channel"),
38 seg_transform=EnsureChannelFirst(channel_dim="no_channel"),
39 image_only=True,
40 )
41 loader = DataLoader(ds, batch_size=1, pin_memory=torch.cuda.is_available())
42
43 net = UNet(
44 spatial_dims=3, in_channels=1, out_channels=1, channels=(4, 8, 16, 32), strides=(2, 2, 2), num_res_units=2
45 ).to(device)
46 roi_size = (16, 32, 48)
47 sw_batch_size = batch_size
48
49 saver = SaveImage(output_dir=output_dir, output_ext=".nii.gz", output_postfix="seg")
50
51 def _sliding_window_processor(_engine, batch):
52 img = batch[0] # first item from ImageDataset is the input image
53 with eval_mode(net):
54 seg_probs = sliding_window_inference(img.to(device), roi_size, sw_batch_size, net, device=device)
55 return predict_segmentation(seg_probs)
56
57 def save_func(engine):
58 for m in engine.state.output:
59 saver(m)
60
61 infer_engine = Engine(_sliding_window_processor)
62 infer_engine.add_event_handler(Events.ITERATION_COMPLETED, save_func)
63 infer_engine.run(loader)
64
65 basename = os.path.basename(img_name)[: -len(".nii.gz")]
66 saved_name = os.path.join(output_dir, basename, f"{basename}_seg.nii.gz")
67 return saved_name
68
69
70@skip_if_quick

Callers 1

test_trainingMethod · 0.70

Calls 6

ImageDatasetClass · 0.90
EnsureChannelFirstClass · 0.90
DataLoaderClass · 0.90
UNetClass · 0.90
SaveImageClass · 0.90
runMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…