micro_sam.v2.datasets.util
1def check_loader(loader, n_samples: int = 50, n_target_channels: int = 4): 2 """Check that raw inputs have 3 channels and targets have the expected number of channels. 3 4 Args: 5 loader: A dataloader yielding (raw, label) batches of shape (B, C, Z, H, W). 6 n_samples: Number of batches to check. 7 The default is set to 50. 8 n_target_channels: Expected number of target channels. 9 The current default is set to 4, i.e. foreground, and the three directed distances. 10 """ 11 raw_channel_errors, label_channel_errors = [], [] 12 13 for i, (x, y) in enumerate(loader): 14 if i >= n_samples: 15 break 16 17 if x.shape[1] != 3: 18 raw_channel_errors.append( 19 f" batch {i}: raw has {x.shape[1]} channels, expected 3 - shape {tuple(x.shape)}" 20 ) 21 22 if y.shape[1] != n_target_channels: 23 label_channel_errors.append( 24 f" batch {i}: target has {y.shape[1]} channels, expected {n_target_channels} - shape {tuple(y.shape)}" 25 ) 26 27 print(f"Checked {min(n_samples, i + 1)} batches.") 28 if raw_channel_errors: 29 print(f"Raw channel errors ({len(raw_channel_errors)}):\n" + "\n".join(raw_channel_errors)) 30 else: 31 print("Raw channels: OK (all have 3 channels)") 32 33 if label_channel_errors: 34 print(f"Target channel errors ({len(label_channel_errors)}):\n" + "\n".join(label_channel_errors)) 35 else: 36 print(f"Target channels: OK (all have {n_target_channels} channels)")
def
check_loader(loader, n_samples: int = 50, n_target_channels: int = 4):
2def check_loader(loader, n_samples: int = 50, n_target_channels: int = 4): 3 """Check that raw inputs have 3 channels and targets have the expected number of channels. 4 5 Args: 6 loader: A dataloader yielding (raw, label) batches of shape (B, C, Z, H, W). 7 n_samples: Number of batches to check. 8 The default is set to 50. 9 n_target_channels: Expected number of target channels. 10 The current default is set to 4, i.e. foreground, and the three directed distances. 11 """ 12 raw_channel_errors, label_channel_errors = [], [] 13 14 for i, (x, y) in enumerate(loader): 15 if i >= n_samples: 16 break 17 18 if x.shape[1] != 3: 19 raw_channel_errors.append( 20 f" batch {i}: raw has {x.shape[1]} channels, expected 3 - shape {tuple(x.shape)}" 21 ) 22 23 if y.shape[1] != n_target_channels: 24 label_channel_errors.append( 25 f" batch {i}: target has {y.shape[1]} channels, expected {n_target_channels} - shape {tuple(y.shape)}" 26 ) 27 28 print(f"Checked {min(n_samples, i + 1)} batches.") 29 if raw_channel_errors: 30 print(f"Raw channel errors ({len(raw_channel_errors)}):\n" + "\n".join(raw_channel_errors)) 31 else: 32 print("Raw channels: OK (all have 3 channels)") 33 34 if label_channel_errors: 35 print(f"Target channel errors ({len(label_channel_errors)}):\n" + "\n".join(label_channel_errors)) 36 else: 37 print(f"Target channels: OK (all have {n_target_channels} channels)")
Check that raw inputs have 3 channels and targets have the expected number of channels.
Arguments:
- loader: A dataloader yielding (raw, label) batches of shape (B, C, Z, H, W).
- n_samples: Number of batches to check. The default is set to 50.
- n_target_channels: Expected number of target channels. The current default is set to 4, i.e. foreground, and the three directed distances.