def test_values(self): # check system default flags set_determinism(None) self.assertTrue(not torch.backends.cudnn.deterministic) self.assertTrue(get_seed() is None) # set default seed set_determinism() self.assertTrue(get_seed() is not None) self.assertTrue(torch.backends.cudnn.deterministic) self.assertTrue(not torch.backends.cudnn.benchmark) # resume default set_determinism(None) self.assertTrue(not torch.backends.cudnn.deterministic) self.assertTrue(not torch.backends.cudnn.benchmark) self.assertTrue(get_seed() is None) # test seeds seed = 255 set_determinism(seed=seed) self.assertEqual(seed, get_seed()) a = np.random.randint(seed) b = torch.randint(seed, (1,)) set_determinism(seed=seed) c = np.random.randint(seed) d = torch.randint(seed, (1,)) self.assertEqual(a, c) self.assertEqual(b, d) self.assertTrue(torch.backends.cudnn.deterministic) self.assertTrue(not torch.backends.cudnn.benchmark) set_determinism(seed=None)
def __init__( self, img: Sequence, img_transform: Optional[Callable] = None, seg: Optional[Sequence] = None, seg_transform: Optional[Callable] = None, labels: Optional[Sequence] = None, label_transform: Optional[Callable] = None, ) -> None: """ Initializes the dataset with the filename lists. The transform `img_transform` is applied to the images and `seg_transform` to the segmentations. Args: img: sequence of images. img_transform: transform to apply to each element in `img`. seg: sequence of segmentations. seg_transform: transform to apply to each element in `seg`. labels: sequence of labels. label_transform: transform to apply to each element in `labels`. """ items = [(img, img_transform), (seg, seg_transform), (labels, label_transform)] self.set_random_state(seed=get_seed()) datasets = [Dataset(x[0], x[1]) for x in items if x[0] is not None] self.dataset = datasets[0] if len(datasets) == 1 else ZipDataset( datasets) self._seed = 0 # transform synchronization seed
def __init__( self, img_files, img_transform: Optional[Callable] = None, seg_files=None, seg_transform: Optional[Callable] = None, labels=None, label_transform: Optional[Callable] = None, ): """ Initializes the dataset with the filename lists. The transform `img_transform` is applied to the images and `seg_transform` to the segmentations. Args: img_files (iterable, list of str): list of image filenames img_transform (Callable, optional): transform to apply to image arrays seg_files (iterable, list of str): if in segmentation task, list of segmentation filenames seg_transform (Callable, optional): transform to apply to segmentation arrays labels (iterable, list or array): if in classification task, list of classification labels label_transform (Callable, optional): transform to apply to label arrays """ items = [(img_files, img_transform), (seg_files, seg_transform), (labels, label_transform)] self.set_random_state(seed=get_seed()) super().__init__( [Dataset(x[0], x[1]) for x in items if x[0] is not None])
def test_inverse(self, _, data_name, acceptable_diff, *transforms): name = _ data = self.all_data[data_name] forwards = [data.copy()] # Apply forwards for t in transforms: if isinstance(t, Randomizable): t.set_random_state(seed=get_seed()) forwards.append(t(forwards[-1])) # Check that error is thrown when inverse are used out of order. t = SpatialPadd("image", [10, 5]) with self.assertRaises(RuntimeError): t.inverse(forwards[-1]) # Apply inverses fwd_bck = forwards[-1].copy() for i, t in enumerate(reversed(transforms)): if isinstance(t, InvertibleTransform): fwd_bck = t.inverse(fwd_bck) self.check_inverse(name, data.keys(), forwards[-i - 2], fwd_bck, forwards[-1], acceptable_diff)
def test_inverse(self, _, data_name, acceptable_diff, is_meta, *transforms): name = _ data = self.all_data[data_name] if is_meta: data = ToMetaTensord(KEYS)(data) forwards = [data.copy()] # Apply forwards for t in transforms: if isinstance(t, Randomizable): t.set_random_state(seed=get_seed()) forwards.append(t(forwards[-1])) # Apply inverses fwd_bck = forwards[-1].copy() for i, t in enumerate(reversed(transforms)): if isinstance(t, InvertibleTransform): if isinstance(fwd_bck, list): for j, _fwd_bck in enumerate(fwd_bck): fwd_bck = t.inverse(_fwd_bck) self.check_inverse(name, data.keys(), forwards[-i - 2], fwd_bck, forwards[-1][j], acceptable_diff) else: fwd_bck = t.inverse(fwd_bck) self.check_inverse(name, data.keys(), forwards[-i - 2], fwd_bck, forwards[-1], acceptable_diff)
def __init__( self, image_files: Sequence[str], seg_files: Optional[Sequence[str]] = None, labels: Optional[Sequence[float]] = None, transform: Optional[Callable] = None, seg_transform: Optional[Callable] = None, label_transform: Optional[Callable] = None, image_only: bool = True, transform_with_metadata: bool = False, dtype: DtypeLike = np.float32, reader: Optional[Union[ImageReader, str]] = None, *args, **kwargs, ) -> None: """ Initializes the dataset with the image and segmentation filename lists. The transform `transform` is applied to the images and `seg_transform` to the segmentations. Args: image_files: list of image filenames. seg_files: if in segmentation task, list of segmentation filenames. labels: if in classification task, list of classification labels. transform: transform to apply to image arrays. seg_transform: transform to apply to segmentation arrays. label_transform: transform to apply to the label data. image_only: if True return only the image volume, otherwise, return image volume and the metadata. transform_with_metadata: if True, the metadata will be passed to the transforms whenever possible. dtype: if not None convert the loaded image to this data type. reader: register reader to load image file and meta data, if None, will use the default readers. If a string of reader name provided, will construct a reader object with the `*args` and `**kwargs` parameters, supported reader name: "NibabelReader", "PILReader", "ITKReader", "NumpyReader" args: additional parameters for reader if providing a reader name. kwargs: additional parameters for reader if providing a reader name. Raises: ValueError: When ``seg_files`` length differs from ``image_files`` """ if seg_files is not None and len(image_files) != len(seg_files): raise ValueError( "Must have same the number of segmentation as image files: " f"images={len(image_files)}, segmentations={len(seg_files)}.") self.image_files = image_files self.seg_files = seg_files self.labels = labels self.transform = transform self.seg_transform = seg_transform self.label_transform = label_transform if image_only and transform_with_metadata: raise ValueError( "transform_with_metadata=True requires image_only=False.") self.image_only = image_only self.transform_with_metadata = transform_with_metadata self.loader = LoadImage(reader, image_only, dtype, *args, **kwargs) self.set_random_state(seed=get_seed()) self._seed = 0 # transform synchronization seed
def __init__( self, transforms: Optional[Union[Sequence[Callable], Callable]] = None) -> None: if transforms is None: transforms = [] self.transforms = ensure_tuple(transforms) self.set_random_state(seed=get_seed())
def __init__( self, inputZ01: Sequence, inputZ02: Sequence, inputZ03: Sequence, inputZ04: Sequence, inputZ05: Sequence, inputZ06: Sequence, inputZ07: Sequence, targetC01: Sequence, targetC02: Sequence, targetC03: Sequence, roi_size: int, n_samples: int, inputZ01_transform: Optional[Callable] = None, inputZ02_transform: Optional[Callable] = None, inputZ03_transform: Optional[Callable] = None, inputZ04_transform: Optional[Callable] = None, inputZ05_transform: Optional[Callable] = None, inputZ06_transform: Optional[Callable] = None, inputZ07_transform: Optional[Callable] = None, targetC01_transform: Optional[Callable] = None, targetC02_transform: Optional[Callable] = None, targetC03_transform: Optional[Callable] = None, ) -> None: """ Initializes the dataset with the filename lists. The transform `img_transform` is applied to the images and `seg_transform` to the segmentations. """ items = [(inputZ01, inputZ01_transform), (inputZ02, inputZ02_transform), (inputZ03, inputZ03_transform), (inputZ04, inputZ04_transform), (inputZ05, inputZ05_transform), (inputZ06, inputZ06_transform), (inputZ07, inputZ07_transform), (targetC01, targetC01_transform), (targetC02, targetC02_transform), (targetC03, targetC03_transform)] self.roi_size = roi_size self.n_samples = n_samples self.set_random_state(seed=get_seed()) sampler = RandSpatialCropSamples(roi_size=(self.roi_size, self.roi_size), num_samples=self.n_samples, random_center=True, random_size=False) datasets = [PatchDataset(dataset=x[0], patch_func=sampler, samples_per_image=self.n_samples, transform=x[1]) for x in items if x[0] is not None] self.dataset = datasets[0] if len(datasets) == 1 else ZipDataset(datasets) self._seed = 0 # transform synchronization seed
def __init__( self, transforms: Optional[Union[Sequence[Callable], Callable]] = None, map_items: bool = True, unpack_items: bool = False, log_stats: bool = False, ) -> None: if transforms is None: transforms = [] self.transforms = ensure_tuple(transforms) self.map_items = map_items self.unpack_items = unpack_items self.log_stats = log_stats self.set_random_state(seed=get_seed())
def __init__( self, image_files: Sequence[str], seg_files: Optional[Sequence[str]] = None, labels: Optional[Sequence[float]] = None, as_closest_canonical: bool = False, transform: Optional[Callable] = None, seg_transform: Optional[Callable] = None, image_only: bool = True, dtype: Optional[np.dtype] = np.float32, ) -> None: """ Initializes the dataset with the image and segmentation filename lists. The transform `transform` is applied to the images and `seg_transform` to the segmentations. Args: image_files: list of image filenames seg_files: if in segmentation task, list of segmentation filenames labels: if in classification task, list of classification labels as_closest_canonical: if True, load the image as closest to canonical orientation transform: transform to apply to image arrays seg_transform: transform to apply to segmentation arrays image_only: if True return only the image volume, other return image volume and header dict dtype: if not None convert the loaded image to this data type Raises: ValueError: When ``seg_files`` length differs from ``image_files``. """ if seg_files is not None and len(image_files) != len(seg_files): raise ValueError( "Must have same the number of segmentation as image files: " f"images={len(image_files)}, segmentations={len(seg_files)}.") self.image_files = image_files self.seg_files = seg_files self.labels = labels self.as_closest_canonical = as_closest_canonical self.transform = transform self.seg_transform = seg_transform self.image_only = image_only self.dtype = dtype self.set_random_state(seed=get_seed()) self._seed = 0 # transform synchronization seed
def __init__(self, inputZ01: Sequence, inputZ02: Sequence, inputZ03: Sequence, inputZ04: Sequence, inputZ05: Sequence, inputZ06: Sequence, inputZ07: Sequence, targetC01: Sequence, targetC02: Sequence, targetC03: Sequence, inputZ01_transform: Optional[Callable] = None, inputZ02_transform: Optional[Callable] = None, inputZ03_transform: Optional[Callable] = None, inputZ04_transform: Optional[Callable] = None, inputZ05_transform: Optional[Callable] = None, inputZ06_transform: Optional[Callable] = None, inputZ07_transform: Optional[Callable] = None, targetC01_transform: Optional[Callable] = None, targetC02_transform: Optional[Callable] = None, targetC03_transform: Optional[Callable] = None) -> None: """ Initializes the dataset with the filename lists. The transform `img_transform` is applied to the images and `seg_transform` to the segmentations. """ items = [ (inputZ01, inputZ01_transform), (inputZ02, inputZ02_transform), (inputZ03, inputZ03_transform), (inputZ04, inputZ04_transform), (inputZ05, inputZ05_transform), (inputZ06, inputZ06_transform), (inputZ07, inputZ07_transform), (targetC01, targetC01_transform), (targetC02, targetC02_transform), (targetC03, targetC03_transform) ] self.set_random_state(seed=get_seed()) datasets = [Dataset(x[0], x[1]) for x in items if x[0] is not None] self.dataset = datasets[0] if len(datasets) == 1 else ZipDataset( datasets) self._seed = 0 # transform synchronization seed
def __init__(self, transforms=None) -> None: if transforms is None: transforms = [] self.transforms = ensure_tuple(transforms) self.set_random_state(seed=get_seed())