def __init__(self, mode, dataset_path='.././datasets', split_idx=150, crop_dim=(512, 512), samples=100, classes=7, save=True): """ :param mode: 'train','val' :param image_paths: image dataset paths :param label_paths: label dataset paths :param crop_dim: 2 element tuple to decide crop values :param samples: number of sub-grids to create(patches of the input img) """ image_paths = sorted( glob.glob( dataset_path + "/MICCAI_2019_pathology_challenge/Train Imgs/Train Imgs/*.jpg") ) label_paths = sorted( glob.glob(dataset_path + "/MICCAI_2019_pathology_challenge/Labels/*.png")) # TODO problem with random shuffle in val and train # TODo cope with 3d affine and full volume for better generalization image_paths, label_paths = utils.shuffle_lists(image_paths, label_paths) self.full_volume = None self.affine = None self.slices = 244 # dataset instances self.mode = mode self.crop_dim = crop_dim self.sample_list = [] self.samples = samples self.save = save self.root = dataset_path self.per_image_sample = int(self.samples / self.slices) if self.per_image_sample < 1: self.per_image_sample = 1 print("per image sampleeeeee", self.per_image_sample) sub_grid = '_2dgrid_' + str(crop_dim[0]) + 'x' + str(crop_dim[1]) if self.save: self.sub_vol_path = self.root + '/MICCAI_2019_pathology_challenge/generated/' + mode + sub_grid + '/' utils.make_dirs(self.sub_vol_path) if self.mode == 'train': self.list_imgs = image_paths[0:split_idx] self.list_labels = label_paths[0:split_idx] elif self.mode == 'val': self.list_imgs = image_paths[split_idx:] self.list_labels = label_paths[split_idx:] self.generate_samples()
def __init__(self, mode, dataset_path='./datasets', classes=5, crop_dim=(200, 200, 150), split_idx=260, samples=10, load=False): """ :param mode: 'train','val','test' :param dataset_path: root dataset folder :param crop_dim: subvolume tuple :param split_idx: 1 to 10 values :param samples: number of sub-volumes that you want to create """ self.mode = mode self.root = str(dataset_path) self.training_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Training/' self.testing_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Validation/' self.full_vol_dim = (240, 240, 155) # slice, width, height self.crop_size = crop_dim self.list = [] self.samples = samples self.full_volume = None self.classes = classes self.save_name = self.root + '/brats2019/brats2019-list-' + mode + '-samples-' + str( samples) + '.txt' if load: self.list = utils.load_list(self.save_name) list_IDsT1 = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*t1.nii.gz'))) self.affine = img_loader.load_affine_matrix(list_IDsT1[0]) return subvol = '_vol_' + str(crop_dim[0]) + 'x' + str( crop_dim[1]) + 'x' + str(crop_dim[2]) self.sub_vol_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Training/generated/' + mode + subvol + '/' utils.make_dirs(self.sub_vol_path) list_IDsT1 = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*t1.nii.gz'))) list_IDsT1ce = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*t1ce.nii.gz'))) list_IDsT2 = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*t2.nii.gz'))) list_IDsFlair = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*_flair.nii.gz'))) labels = sorted( glob.glob(os.path.join(self.training_path, '*GG/*/*_seg.nii.gz'))) list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels = utils.shuffle_lists( list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, seed=17) self.affine = img_loader.load_affine_matrix(list_IDsT1[0]) if self.mode == 'train': print('Brats2019, Total data:', len(list_IDsT1)) list_IDsT1 = list_IDsT1[:split_idx] list_IDsT1ce = list_IDsT1ce[:split_idx] list_IDsT2 = list_IDsT2[:split_idx] list_IDsFlair = list_IDsFlair[:split_idx] labels = labels[:split_idx] self.list = create_sub_volumes(list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, dataset_name="brats2019", mode=mode, samples=samples, full_vol_dim=self.full_vol_dim, crop_size=self.crop_size, sub_vol_path=self.sub_vol_path) elif self.mode == 'val': list_IDsT1 = list_IDsT1[split_idx:] list_IDsT1ce = list_IDsT1ce[split_idx:] list_IDsT2 = list_IDsT2[split_idx:] list_IDsFlair = list_IDsFlair[split_idx:] labels = labels[split_idx:] self.list = create_sub_volumes(list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, dataset_name="brats2019", mode=mode, samples=samples, full_vol_dim=self.full_vol_dim, crop_size=self.crop_size, sub_vol_path=self.sub_vol_path) elif self.mode == 'test': self.list_IDsT1 = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t1.nii.gz'))) self.list_IDsT1ce = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t1ce.nii.gz'))) self.list_IDsT2 = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t2.nii.gz'))) self.list_IDsFlair = sorted( glob.glob( os.path.join(self.testing_path, '*GG/*/*_flair.nii.gz'))) self.labels = None # Todo inference code here utils.save_list(self.save_name, self.list)
def __init__(self, args, mode, dataset_path='./datasets', classes=4, crop_dim=(100, 100, 100), split_idx=204, samples=10, load=False): """ :param mode: 'train','val','test' :param dataset_path: root dataset folder :param crop_dim: subvolume tuple :param split_idx: 1 to 10 values :param samples: number of sub-volumes that you want to create """ self.mode = mode self.root = str(dataset_path) self.training_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Training/train_generated' self.testing_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Validation/' self.full_vol_dim = (128, 128, 128) # slice, width, height self.crop_size = crop_dim self.threshold = args.threshold self.normalization = args.normalization self.augmentation = args.augmentation self.list = [] self.samples = samples self.full_volume = None self.classes = classes if self.augmentation: self.transform = augment3D.RandomChoice(transforms=[ augment3D.GaussianNoise(mean=0, std=0.01), augment3D.RandomFlip(), augment3D.ElasticTransform() ], p=0.5) self.save_name = self.root + '/brats2019/brats2019-list-' + mode + '-samples-' + str( samples) + '.txt' if load: ## load pre-generated data self.list = utils.load_list(self.save_name) list_IDsT1 = sorted( glob.glob( os.path.join(self.training_path, '***_imgs/***_imgs_t1.npy'))) self.affine = img_loader.load_affine_matrix(list_IDsT1[0]) return subvol = '_vol_' + str(crop_dim[0]) + 'x' + str( crop_dim[1]) + 'x' + str(crop_dim[2]) self.sub_vol_path = self.root + '/brats2019/MICCAI_BraTS_2019_Data_Training/generated/' + mode + subvol + '/' utils.make_dirs(self.sub_vol_path) list_IDsT1 = sorted( glob.glob( os.path.join(self.training_path, '***_imgs/***_imgs_t1.npy'))) list_IDsT1ce = sorted( glob.glob( os.path.join(self.training_path, '***_imgs/***_imgs_t1gd.npy'))) list_IDsT2 = sorted( glob.glob( os.path.join(self.training_path, '***_imgs/***_imgs_t2.npy'))) list_IDsFlair = sorted( glob.glob( os.path.join(self.training_path, '***_imgs/***_imgs_flair.npy'))) labels = sorted( glob.glob( os.path.join(self.training_path, '***_seg/***_seg_label.npy'))) list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels = utils.shuffle_lists( list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, seed=17) self.affine = img_loader.load_affine_matrix(list_IDsT1[0]) if self.mode == 'train': print('Brats2019, Total data:', len(list_IDsT1)) list_IDsT1 = list_IDsT1[:split_idx] list_IDsT1ce = list_IDsT1ce[:split_idx] list_IDsT2 = list_IDsT2[:split_idx] list_IDsFlair = list_IDsFlair[:split_idx] labels = labels[:split_idx] self.list = create_sub_volumes(list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, dataset_name="brats2019", mode=mode, samples=samples, full_vol_dim=self.full_vol_dim, crop_size=self.crop_size, sub_vol_path=self.sub_vol_path, th_percent=self.threshold) elif self.mode == 'val': list_IDsT1 = list_IDsT1[split_idx:] list_IDsT1ce = list_IDsT1ce[split_idx:] list_IDsT2 = list_IDsT2[split_idx:] list_IDsFlair = list_IDsFlair[split_idx:] labels = labels[split_idx:] self.list = create_sub_volumes(list_IDsT1, list_IDsT1ce, list_IDsT2, list_IDsFlair, labels, dataset_name="brats2019", mode=mode, samples=samples, full_vol_dim=self.full_vol_dim, crop_size=self.crop_size, sub_vol_path=self.sub_vol_path, th_percent=self.threshold) elif self.mode == 'test': self.list_IDsT1 = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t1.nii.gz'))) self.list_IDsT1ce = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t1ce.nii.gz'))) self.list_IDsT2 = sorted( glob.glob(os.path.join(self.testing_path, '*GG/*/*t2.nii.gz'))) self.list_IDsFlair = sorted( glob.glob( os.path.join(self.testing_path, '*GG/*/*_flair.nii.gz'))) self.labels = None # Todo inference code here utils.save_list(self.save_name, self.list)