saim1309/Cell_Segmentation
0
1from CellAware import BoundaryExclusion, IntensityDiversification2from LoadImage import CustomLoadImaged,CustomLoadImageD,CustomLoadImageDict,CustomLoadImage3from NormalizeImage import CustomNormalizeImage,CustomNormalizeImageD,CustomNormalizeImageDict ,CustomNormalizeImaged4 5from monai.transforms import *6 7__all__ = [8 "train_transforms",9 "public_transforms",10 "valid_transforms",11 "tuning_transforms",12 "unlabeled_transforms",13]14 15train_transforms = Compose(16 [17 # >>> Load and refine data --- img: (H, W, 3); label: (H, W)18 CustomLoadImaged(keys=["img", "label"], image_only=True),19 CustomNormalizeImaged(20 keys=["img"],21 allow_missing_keys=True,22 channel_wise=False,23 percentiles=[0.0, 99.5],24 ),25 EnsureChannelFirstd(keys=["img", "label"], channel_dim=-1),26 RemoveRepeatedChanneld(keys=["label"], repeats=3), # label: (H, W)27 ScaleIntensityd(keys=["img"], allow_missing_keys=True), # Do not scale label28 # >>> Spatial transforms29 RandZoomd(30 keys=["img", "label"],31 prob=0.5,32 min_zoom=0.25,33 max_zoom=1.5,34 mode=["area", "nearest"],35 keep_size=False,36 ),37 SpatialPadd(keys=["img", "label"], spatial_size=512),38 RandSpatialCropd(keys=["img", "label"], roi_size=512, random_size=False),39 RandAxisFlipd(keys=["img", "label"], prob=0.5),40 RandRotate90d(keys=["img", "label"], prob=0.5, spatial_axes=[0, 1]),41 IntensityDiversification(keys=["img", "label"], allow_missing_keys=True),42 # # >>> Intensity transforms43 RandGaussianNoised(keys=["img"], prob=0.25, mean=0, std=0.1),44 RandAdjustContrastd(keys=["img"], prob=0.25, gamma=(1, 2)),45 RandGaussianSmoothd(keys=["img"], prob=0.25, sigma_x=(1, 2)),46 RandHistogramShiftd(keys=["img"], prob=0.25, num_control_points=3),47 RandGaussianSharpend(keys=["img"], prob=0.25),48 EnsureTyped(keys=["img", "label"]),49 ]50)51 52 53public_transforms = Compose(54 [55 CustomLoadImaged(keys=["img", "label"], image_only=True),56 BoundaryExclusion(keys=["label"]),57 CustomNormalizeImaged(58 keys=["img"],59 allow_missing_keys=True,60 channel_wise=False,61 percentiles=[0.0, 99.5],62 ),63 EnsureChannelFirstd(keys=["img", "label"], channel_dim=-1),64 RemoveRepeatedChanneld(keys=["label"], repeats=3), # label: (H, W)65 ScaleIntensityd(keys=["img"], allow_missing_keys=True), # Do not scale label66 # >>> Spatial transforms67 SpatialPadd(keys=["img", "label"], spatial_size=512),68 RandSpatialCropd(keys=["img", "label"], roi_size=512, random_size=False),69 RandAxisFlipd(keys=["img", "label"], prob=0.5),70 RandRotate90d(keys=["img", "label"], prob=0.5, spatial_axes=[0, 1]),71 Rotate90d(k=1, keys=["label"], spatial_axes=(0, 1)),72 Flipd(keys=["label"], spatial_axis=0),73 EnsureTyped(keys=["img", "label"]),74 ]75)76 77 78valid_transforms = Compose(79 [80 CustomLoadImaged(keys=["img", "label"], allow_missing_keys=True, image_only=True),81 CustomNormalizeImaged(82 keys=["img"],83 allow_missing_keys=True,84 channel_wise=False,85 percentiles=[0.0, 99.5],86 ),87 EnsureChannelFirstd(keys=["img", "label"], allow_missing_keys=True, channel_dim=-1),88 RemoveRepeatedChanneld(keys=["label"], repeats=3),89 ScaleIntensityd(keys=["img"], allow_missing_keys=True),90 EnsureTyped(keys=["img", "label"], allow_missing_keys=True),91 ]92)93 94tuning_transforms = Compose(95 [96 CustomLoadImaged(keys=["img"], image_only=True),97 CustomNormalizeImaged(98 keys=["img"],99 allow_missing_keys=True,100 channel_wise=False,101 percentiles=[0.0, 99.5],102 ),103 EnsureChannelFirstd(keys=["img"], channel_dim=-1),104 ScaleIntensityd(keys=["img"]),105 EnsureTyped(keys=["img"]),106 ]107)108 109unlabeled_transforms = Compose(110 [111 # >>> Load and refine data --- img: (H, W, 3); label: (H, W)112 CustomLoadImaged(keys=["img"], image_only=True),113 CustomNormalizeImaged(114 keys=["img"],115 allow_missing_keys=True,116 channel_wise=False,117 percentiles=[0.0, 99.5],118 ),119 EnsureChannelFirstd(keys=["img"], channel_dim=-1),120 RandZoomd(121 keys=["img"],122 prob=0.5,123 min_zoom=0.25,124 max_zoom=1.25,125 mode=["area"],126 keep_size=False,127 ),128 ScaleIntensityd(keys=["img"], allow_missing_keys=True), # Do not scale label129 # >>> Spatial transforms130 SpatialPadd(keys=["img"], spatial_size=512),131 RandSpatialCropd(keys=["img"], roi_size=512, random_size=False),132 EnsureTyped(keys=["img"]),133 ]134)135 136 137def get_pred_transforms():138 """Prediction preprocessing"""139 pred_transforms = Compose(140 [141 # >>> Load and refine data142 CustomLoadImage(image_only=True),143 CustomNormalizeImage(channel_wise=False, percentiles=[0.0, 99.5]),144 EnsureChannelFirst(channel_dim=-1), # image: (3, H, W)145 ScaleIntensity(),146 EnsureType(data_type="tensor"),147 ]148 )149 150 return pred_transforms151 