def get_landmark_transform(config): return transforms.Compose([ # WrapImageLandmarksSOLT(), slc.Stream([ slt.RandomFlip(p=0.5, axis=1), slt.RandomScale(range_x=(0.8, 1.2), p=1), slt.RandomRotate(rotation_range=(-180, 180), p=0.2), slt.RandomProjection(affine_transforms=slc.Stream([ slt.RandomScale(range_x=(0.8, 1.3), p=1), slt.RandomRotate(rotation_range=(-180, 180), p=1), slt.RandomShear(range_x=(-0.1, 0.1), range_y=(0, 0), p=0.5), slt.RandomShear(range_y=(-0.1, 0.1), range_x=(0, 0), p=0.5), ]), v_range=(1e-5, 2e-3), p=0.8), slt.PadTransform(int(config.dataset.crop_size * 1.4), padding='z'), slt.CropTransform(config.dataset.crop_size, crop_mode='r'), slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)), slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), slc.Stream([ slt.ImageSaltAndPepper(p=1, gain_range=0.05), slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), ]), slc.Stream([ slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), slt.ImageSaltAndPepper(p=1, gain_range=0.01), ]), slc.Stream() ]), slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 1.5)) ]), SOLTtoHourGlassGSinput(downsample=4, sigma=3), ApplyTransformByIndex(transform=dwutils.npg2tens, ids=[0, 1]), ])
def get_landmark_transform_kneel(config): cutout = slt.ImageCutOut( cutout_size=(int(config.dataset.cutout * config.dataset.augs.crop.crop_x), int(config.dataset.cutout * config.dataset.augs.crop.crop_y)), p=0.5) ppl = transforms.Compose([ slc.Stream(), slc.SelectiveStream( [ slc.Stream([ slt.RandomFlip(p=0.5, axis=1), slt.RandomProjection(affine_transforms=slc.Stream([ slt.RandomScale(range_x=(0.9, 1.1), p=1), slt.RandomRotate(rotation_range=(-90, 90), p=1), slt.RandomShear( range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5), slt.RandomShear( range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5), ]), v_range=(1e-5, 2e-3), p=0.5), # slt.RandomScale(range_x=(0.5, 2.5), p=0.5), ]), slc.Stream() ], probs=[0.7, 0.3]), slc.Stream([ slt.PadTransform( (config.dataset.augs.pad.pad_x, config.dataset.augs.pad.pad_y), padding='z'), slt.CropTransform((config.dataset.augs.crop.crop_x, config.dataset.augs.crop.crop_y), crop_mode='r'), ]), slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)), slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), slc.Stream([ slt.ImageSaltAndPepper(p=1, gain_range=0.05), slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), ]), slc.Stream([ slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), slt.ImageSaltAndPepper(p=1, gain_range=0.01), ]), slc.Stream() ], n=1), slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)), cutout if config.dataset.use_cutout else slc.Stream(), DataToFunction(solt_to_img_target), ApplyByIndex(transforms.ToTensor(), 0) ]) return ppl
def init_augs(): kvs = GlobalKVS() args = kvs['args'] cutout = slt.ImageCutOut(cutout_size=(int(args.cutout * args.crop_x), int(args.cutout * args.crop_y)), p=0.5) # plus-minus 1.3 pixels jitter = slt.KeypointsJitter(dx_range=(-0.003, 0.003), dy_range=(-0.003, 0.003)) ppl = tvt.Compose([ jitter if args.use_target_jitter else slc.Stream(), slc.SelectiveStream([ slc.Stream([ slt.RandomFlip(p=0.5, axis=1), slt.RandomProjection(affine_transforms=slc.Stream([ slt.RandomScale(range_x=(0.8, 1.3), p=1), slt.RandomRotate(rotation_range=(-90, 90), p=1), slt.RandomShear( range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5), ]), v_range=(1e-5, 2e-3), p=0.5), slt.RandomScale(range_x=(0.5, 2.5), p=0.5), ]), slc.Stream() ], probs=[0.7, 0.3]), slc.Stream([ slt.PadTransform((args.pad_x, args.pad_y), padding='z'), slt.CropTransform((args.crop_x, args.crop_y), crop_mode='r'), ]), slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)), slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), slc.Stream([ slt.ImageSaltAndPepper(p=1, gain_range=0.05), slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), ]), slc.Stream([ slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), slt.ImageSaltAndPepper(p=1, gain_range=0.01), ]), slc.Stream() ], n=1), slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)), cutout if args.use_cutout else slc.Stream(), partial(solt2torchhm, downsample=None, sigma=None), ]) kvs.update('train_trf', ppl)
def get_landmark_transform_kneel(config): cutout = slt.ImageCutOut(cutout_size=(int(config.dataset.cutout * config.dataset.augs.crop.crop_x), int(config.dataset.cutout * config.dataset.augs.crop.crop_y)), p=0.5) # plus-minus 1.3 pixels jitter = slt.KeypointsJitter(dx_range=(-0.003, 0.003), dy_range=(-0.003, 0.003)) ppl = transforms.Compose([ ColorPaddingWithSide(p=0.05, pad_size=10, side=SIDES.RANDOM, color=(50,100)), TriangularMask(p=0.025, arm_lengths=(100, 50), side=SIDES.RANDOM, color=(50,100)), TriangularMask(p=0.025, arm_lengths=(50, 100), side=SIDES.RANDOM, color=(50,100)), LowVisibilityTransform(p=0.05, alpha=0.15, bgcolor=(50,100)), SubSampleUpScale(p=0.01), jitter if config.dataset.augs.use_target_jitter else slc.Stream(), slc.SelectiveStream([ slc.Stream([ slt.RandomFlip(p=0.5, axis=1), slt.RandomProjection(affine_transforms=slc.Stream([ slt.RandomScale(range_x=(0.9, 1.1), p=1), slt.RandomRotate(rotation_range=(-90, 90), p=1), slt.RandomShear(range_x=(-0.1, 0.1), range_y=(-0.1, 0.1), p=0.5), ]), v_range=(1e-5, 2e-3), p=0.5), # slt.RandomScale(range_x=(0.5, 2.5), p=0.5), ]), slc.Stream() ], probs=[0.7, 0.3]), slc.Stream([ slt.PadTransform((config.dataset.augs.pad.pad_x, config.dataset.augs.pad.pad_y), padding='z'), slt.CropTransform((config.dataset.augs.crop.crop_x, config.dataset.augs.crop.crop_y), crop_mode='r'), ]), slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=1, blur_type='g', k_size=(3, 5)), slt.ImageBlur(p=1, blur_type='m', k_size=(3, 5)), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), slc.Stream([ slt.ImageSaltAndPepper(p=1, gain_range=0.05), slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), ]), slc.Stream([ slt.ImageBlur(p=0.5, blur_type='m', k_size=(3, 5)), slt.ImageSaltAndPepper(p=1, gain_range=0.01), ]), slc.Stream() ], n=1), slt.ImageGammaCorrection(p=0.5, gamma_range=(0.5, 1.5)), cutout if config.dataset.use_cutout else slc.Stream(), partial(solt2torchhm, downsample=None, sigma=None), ]) return ppl
def get_wrist_fracture_transformation(crop_size): return transforms.Compose([ SplitDataToFunction(wrap_img_target_solt), slc.Stream([ slt.RandomFlip(p=1, axis=1), slt.RandomProjection(affine_transforms=slc.Stream([ slt.RandomScale(range_x=(0.8, 1.2), p=1), slt.RandomShear(range_x=(-0.1, 0.1), p=0.5), slt.RandomShear(range_y=(-0.1, 0.1), p=0.5), slt.RandomRotate(rotation_range=(-10, 10), p=1), ]), v_range=(1e-5, 5e-4), p=0.8), slt.PadTransform(pad_to=(256, 256), padding='z'), slt.CropTransform(crop_size, crop_mode='r'), slc.SelectiveStream([ slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=0.5, blur_type='m', k_size=(11, )), ]), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), ]), slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 1.5)), ]), DataToFunction(solt_to_img_target), ApplyByIndex(transforms.ToTensor(), 0) ])
def test_blur_samples_correctly(blur_t, k_size, sigma): trf = slt.ImageBlur(blur_type=blur_t, k_size=k_size, gaussian_sigma=sigma) trf.sample_transform() if isinstance(k_size, int): k_size = (k_size, ) if sigma is None: sigma = 1 assert trf.state_dict['k_size'] in k_size assert trf.state_dict['sigma'] == sigma
def test_blur_arguments(blur_t, k_size, sigma, to_catch): with pytest.raises(to_catch): slt.ImageBlur(blur_type=blur_t, k_size=k_size, gaussian_sigma=sigma)
def train_test_transforms(conf, mean=None, std=None): trf = conf['training'] prob = trf['transform_probability'] crop_size = tuple(trf['crop_size']) # Training transforms if trf['uCT']: train_transforms = [ slt.RandomProjection(slc.Stream([ slt.RandomRotate(rotation_range=tuple(trf['rotation_range']), p=prob), slt.RandomScale(range_x=tuple(trf['scale_range']), range_y=tuple(trf['scale_range']), same=False, p=prob), slt.RandomShear(range_x=tuple(trf['shear_range']), range_y=tuple(trf['shear_range']), p=prob), slt.RandomTranslate(range_x=trf['translation_range'], range_y=trf['translation_range'], p=prob) ]), v_range=tuple(trf['v_range'])), # Spatial slt.RandomFlip(p=prob), slt.PadTransform(pad_to=crop_size), slt.CropTransform(crop_mode='r', crop_size=crop_size), # Intensity #slt.ImageGammaCorrection(gamma_range=tuple(trf['gamma_range']), p=prob), #slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']), # s_range=tuple(trf['hsv_range']), # v_range=tuple(trf['hsv_range']), p=prob), # Brightness/contrast slc.SelectiveStream([ slt.ImageRandomBrightness(brightness_range=tuple( trf['brightness_range']), p=prob), slt.ImageRandomContrast(contrast_range=trf['contrast_range'], p=prob) ]), # Noise slc.SelectiveStream([ slt.ImageSaltAndPepper(p=prob, gain_range=trf['gain_range_sp']), slt.ImageAdditiveGaussianNoise( p=prob, gain_range=trf['gain_range_gn']), slc.SelectiveStream([ slt.ImageBlur(p=prob, blur_type='g', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])), slt.ImageBlur(p=prob, blur_type='m', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])) ]) ]) ] else: train_transforms = [ # Projection slt.RandomProjection( slc.Stream([ slt.RandomRotate(rotation_range=tuple( trf['rotation_range']), p=prob), slt.RandomScale(range_x=tuple(trf['scale_range']), range_y=tuple(trf['scale_range']), same=False, p=prob), #slt.RandomShear(range_x=tuple(trf['shear_range']), # range_y=tuple(trf['shear_range']), p=prob), #slt.RandomTranslate(range_x=trf['translation_range'], range_y=trf['translation_range'], p=prob) ]), v_range=tuple(trf['v_range'])), # Spatial slt.RandomFlip(p=prob), slt.PadTransform(pad_to=crop_size[1]), slt.CropTransform(crop_mode='r', crop_size=crop_size), # Intensity # Add an empty stream #slc.SelectiveStream([]), slc.SelectiveStream([ slt.ImageGammaCorrection(gamma_range=tuple(trf['gamma_range']), p=prob), slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']), s_range=tuple(trf['hsv_range']), v_range=tuple(trf['hsv_range']), p=prob) ]), slc.SelectiveStream([ slt.ImageRandomBrightness(brightness_range=tuple( trf['brightness_range']), p=prob), slt.ImageRandomContrast(contrast_range=trf['contrast_range'], p=prob) ]), slc.SelectiveStream([ slt.ImageSaltAndPepper(p=prob, gain_range=trf['gain_range_sp']), slt.ImageAdditiveGaussianNoise( p=prob, gain_range=trf['gain_range_gn']), slc.SelectiveStream([ slt.ImageBlur(p=prob, blur_type='g', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])), slt.ImageBlur(p=prob, blur_type='m', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])) ]) ]) ] train_trf = [ wrap_solt, #slc.Stream(train_transforms), slc.Stream([ slt.PadTransform(pad_to=crop_size[1]), slt.CropTransform(crop_mode='r', crop_size=crop_size) ]), unwrap_solt, ApplyTransform(numpy2tens, (0, 1, 2)) ] # Validation transforms val_trf = [ wrap_solt, slc.Stream([ slt.PadTransform(pad_to=crop_size[1]), slt.CropTransform(crop_mode='r', crop_size=crop_size) ]), unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2)) ] # Test transforms test_trf = [unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2))] # Use normalize_channel_wise if mean and std not calculated if mean is not None and std is not None: train_trf.append( ApplyTransform(partial(normalize_channel_wise, mean=mean, std=std))) if mean is not None and std is not None: val_trf.append( ApplyTransform(partial(normalize_channel_wise, mean=mean, std=std))) # Compose transforms train_trf_cmp = Compose(train_trf) val_trf_cmp = Compose(val_trf) test_trf_cmp = Compose(test_trf) return { 'train': train_trf_cmp, 'val': val_trf_cmp, 'test': test_trf_cmp, 'train_list': train_trf, 'val_list': val_trf, 'test_list': test_trf }
def train_test_transforms(conf, mean=None, std=None, crop_size=(512, 1024)): """ Compiles the different image augmentations that are used for input images. :param conf: Transformation parameters :param mean: Dataset image mean :param std: Dataset image std :param crop_size: Image size for the segmentation model :return: Compiled transformation objects, and lists of the used transforms """ trf = conf['training'] prob = trf['transform_probability'] # Training transforms # 3D transforms if trf['experiment'] == '3D': train_transforms = [ slc.SelectiveStream([ slc.Stream([ slt.RandomProjection( slc.Stream([ slt.RandomRotate(rotation_range=tuple( trf['rotation_range']), p=prob), slt.RandomScale(range_x=tuple(trf['scale_range']), range_y=tuple(trf['scale_range']), same=False, p=prob), #slt.RandomShear(range_x=tuple(trf['shear_range']), # range_y=tuple(trf['shear_range']), p=prob), slt.RandomTranslate( range_x=trf['translation_range'], range_y=trf['translation_range'], p=prob) ]), v_range=None #tuple(trf['v_range']) ), # Spatial slt.RandomFlip(p=prob), slt.PadTransform(pad_to=crop_size), slt.CropTransform(crop_mode='r', crop_size=crop_size), # Intensity # Brightness/contrast slc.SelectiveStream([ slt.ImageRandomBrightness(brightness_range=tuple( trf['brightness_range']), p=prob), slt.ImageRandomContrast( contrast_range=trf['contrast_range'], p=prob) ]), # Noise slc.SelectiveStream([ slt.ImageSaltAndPepper( p=prob, gain_range=trf['gain_range_sp']), slt.ImageAdditiveGaussianNoise( p=prob, gain_range=trf['gain_range_gn']), slc.SelectiveStream([ slt.ImageBlur(p=prob, blur_type='g', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])), slt.ImageBlur(p=prob, blur_type='m', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])) ]) ]) ]), # Empty stream slc.Stream([ slt.PadTransform(pad_to=crop_size), slt.CropTransform(crop_mode='r', crop_size=crop_size) ]) ]) ] # 2D transforms else: train_transforms = [ slc.SelectiveStream([ slc.Stream([ # Projection slt.RandomProjection( slc.Stream([ slt.RandomRotate(rotation_range=tuple( trf['rotation_range']), p=prob), slt.RandomScale(range_x=tuple(trf['scale_range']), range_y=tuple(trf['scale_range']), same=False, p=prob), # slt.RandomShear(range_x=tuple(trf['shear_range']), # range_y=tuple(trf['shear_range']), p=prob), slt.RandomTranslate( range_x=trf['translation_range'], range_y=trf['translation_range'], p=prob) ]), v_range=tuple(trf['v_range'])), # Spatial slt.RandomFlip(p=prob), slt.PadTransform(pad_to=crop_size), slt.CropTransform(crop_mode='r', crop_size=crop_size), # Intensity slc.SelectiveStream([ slt.ImageGammaCorrection(gamma_range=tuple( trf['gamma_range']), p=prob), slt.ImageRandomHSV(h_range=tuple(trf['hsv_range']), s_range=tuple(trf['hsv_range']), v_range=tuple(trf['hsv_range']), p=prob) ]), slc.SelectiveStream([ slt.ImageRandomBrightness(brightness_range=tuple( trf['brightness_range']), p=prob), slt.ImageRandomContrast( contrast_range=trf['contrast_range'], p=prob) ]), slc.SelectiveStream([ slt.ImageSaltAndPepper( p=prob, gain_range=trf['gain_range_sp']), slt.ImageAdditiveGaussianNoise( p=prob, gain_range=trf['gain_range_gn']), slc.SelectiveStream([ slt.ImageBlur(p=prob, blur_type='g', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])), slt.ImageBlur(p=prob, blur_type='m', k_size=(3, 7, 11), gaussian_sigma=tuple(trf['sigma'])) ]) ]) ]), # Empty stream slc.Stream([ slt.PadTransform(pad_to=crop_size), slt.CropTransform(crop_mode='r', crop_size=crop_size) ]) ]) ] # Compile training transforms train_trf = [ # Move to SOLT format wrap_solt, # Transforms slc.Stream(train_transforms), # Extract image unwrap_solt, # Move to tensor ApplyTransform(numpy2tens, (0, 1, 2)) ] # Validation transforms val_trf = [ wrap_solt, slc.Stream([ slt.PadTransform(pad_to=crop_size[1]), slt.CropTransform(crop_mode='r', crop_size=crop_size) ]), unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2)) ] # Test transforms test_trf = [unwrap_solt, ApplyTransform(numpy2tens, idx=(0, 1, 2))] # Normalize train and val images if mean and std are given if mean is not None and std is not None: train_trf.append( ApplyTransform(partial(normalize_channel_wise, mean=mean, std=std))) if mean is not None and std is not None: val_trf.append( ApplyTransform(partial(normalize_channel_wise, mean=mean, std=std))) # Compose transforms train_trf_cmp = Compose(train_trf) val_trf_cmp = Compose(val_trf) test_trf_cmp = Compose(test_trf) return { 'train': train_trf_cmp, 'val': val_trf_cmp, 'test': test_trf_cmp, 'train_list': train_trf, 'val_list': val_trf, 'test_list': test_trf }
slt.RandomRotate(rotation_range=(-90, 90), p=1), slt.RandomShear(range_x=(-0.2, 0.2), range_y=None, p=0.7), ]), v_range=(1e-6, 3e-4), p=1), # Various cropping and padding tricks slt.PadTransform(1000, 'z'), slt.CropTransform(1000, crop_mode='c'), slt.CropTransform(950, crop_mode='r'), slt.PadTransform(1000, 'z'), # Intensity augmentations slt.ImageGammaCorrection(p=1, gamma_range=(0.5, 3)), slc.SelectiveStream([ slc.SelectiveStream([ slt.ImageSaltAndPepper(p=1, gain_range=0.01), slt.ImageBlur(p=0.5, blur_type='m', k_size=(11, )), ]), slt.ImageAdditiveGaussianNoise(p=1, gain_range=0.5), ]), ]) for i in range(10): res = stream(dc) img_res, kp_c, lbl_c = res.data fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(1, 1, 1) ax.imshow(img_res, cmap=plt.cm.Greys_r) for pts, cls in zip([kp_c.data], ['Cardiomegaly']): text_bbox = dict(facecolor='red', alpha=0.7, lw=0)