def data_generator_inference(self, subject_list, normalize_bool=False): it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) if it_subject_batch + 1 == self.batch_size: it_subject_batch = 0 yield [image_batch, mask] else: it_subject_batch += 1
def data_generator_BraTS_mask(self, subject_list, mode='train', normalize_bool=True, sample_weights_bool=False, class_weights=None): if mode == 'train': subject_list = self.data_augmentation(subject_list) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_batch = np.zeros((self.batch_size, ) + self.input_shape) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: np.random.shuffle(subject_list) for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) labels_batch[it_subject_batch, :, :, :] = resize_image( subject.load_tumor_mask(), self.input_shape, pad_value=0) if it_subject_batch + 1 == self.batch_size: it_subject_batch = 0 if sample_weights_bool: labels_batch_resized = np.reshape( labels_batch, (self.batch_size, -1)) sample_weights = np.zeros_like(labels_batch_resized) for k, v in class_weights.items(): sample_weights[np.where( labels_batch_resized == k)] = v yield ([image_batch, mask], one_hot_representation(labels_batch_resized, 2), sample_weights) else: yield ([image_batch, mask], one_hot_representation(labels_batch, 2)) image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_batch = np.zeros((self.batch_size, ) + self.input_shape) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) else: it_subject_batch += 1
def data_generator_one(self, subject): image_batch = np.zeros((1, ) + self.input_shape + (self.num_modalities, )) image = subject.load_channels() for mod in range(image.shape[-1]): image_batch[0, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) labels = resize_image(subject.load_labels(), self.input_shape, pad_value=0)[np.newaxis, :] return (image_batch, one_hot_representation(labels, self.n_classes), subject.id)
def data_generator_full(self, subject_list, mode='train', normalize_bool=True, mask=True): if mode == 'train': subject_list = self.data_augmentation(subject_list) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_batch = np.zeros((self.batch_size, ) + self.input_shape) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) labels_batch[it_subject_batch, :, :, :] = resize_image( subject.load_labels(), self.input_shape, pad_value=0) if it_subject_batch + 1 == self.batch_size: it_subject_batch = 0 if mask: yield ([image_batch, mask], one_hot_representation(labels_batch, self.n_classes)) else: yield (image_batch, one_hot_representation(labels_batch, self.n_classes)) else: it_subject_batch += 1
dice_1[n_sbj] = dice(predictions[:, :, :, 1].flatten(), labels[:, :, :, 1].flatten()) dice_2[n_sbj] = dice(predictions[:, :, :, 2].flatten(), labels[:, :, :, 2].flatten()) dice_3[n_sbj] = dice(predictions[:, :, :, 3].flatten(), labels[:, :, :, 3].flatten()) print("Dice 1: " + str(dice_1[n_sbj])) print("Dice 2: " + str(dice_2[n_sbj])) print("Dice 3: " + str(dice_3[n_sbj])) #Saving images shape = subject.get_subject_shape() predictions_resized = np.zeros(shape + (params[p.N_CLASSES], )) for i in range(params[p.N_CLASSES]): predictions_resized[:, :, :, i] = preprocessing.resize_image( predictions[:, :, :, i], shape) predictions_resize_argmax = np.argmax(predictions_resized, axis=3) predictions_argmax = np.argmax(predictions, axis=3) img = nib.Nifti1Image(tf_labels(predictions_resize_argmax), subject.get_affine()) nib.save(img, join(dir_path, 'results', subject.id + '_predictions.nii.gz')) img = nib.Nifti1Image( preprocessing.resize_image(inputs[1][0, :, :, :, 0], shape), subject.get_affine()) nib.save(img, join(dir_path, 'results', subject.id + '_mask.nii.gz')) print('Subject ' + str(subject.id) + ' has finished')
for inputs, outputs, subject in generator_test: if params[p.MODEL_TYPE] == 'mask': labels = outputs[0] predictions = model.predict_on_batch(inputs)[0] predictions = np.floor( predictions / np.max(predictions, axis=3, keepdims=True)).astype('int') shape = subject.get_subject_shape() predictions_resized = np.zeros(shape + (2, )) for i in range(2): predictions_resized[:, :, :, i] = preprocessing.resize_image( predictions[:, :, :, i], shape) #Saving images predictions_resize_argmax = np.argmax(predictions_resized, axis=3) predictions_argmax = np.argmax(predictions, axis=3) img = nib.Nifti1Image(predictions_resize_argmax, subject.get_affine()) nib.save( img, join(dir_path, 'results', subject.id + '_predictions.nii.gz')) #Metrics: dice_mask[n_sbj] = dice(predictions[:, :, :, 1].flatten(), labels[:, :, :, 1].flatten())
def data_generator_BraTS_mask_seg(self, subject_list, mode='train', normalize_bool=True, sample_weights_bool=False, class_weights=None, n_iterations=None): if mode == 'train': subject_list = self.data_augmentation(subject_list) n_iterations = n_iterations if n_iterations is not None else len( subject_list) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_mask_batch = np.zeros((self.batch_size, ) + self.input_shape) labels_seg_batch = np.zeros((self.batch_size, ) + self.input_shape) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: np.random.shuffle(subject_list) n_sbj = 0 for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) tumor_mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_tumor_mask(), self.input_shape, pad_value=0) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) labels_mask_batch[it_subject_batch, :, :, :] = resize_image( subject.load_tumor_mask(), self.input_shape, pad_value=0) labels_seg_batch[it_subject_batch, :, :, :] = resize_image( subject.load_labels(), self.input_shape, pad_value=0) if it_subject_batch + 1 == self.batch_size: if mode == 'test': yield ([image_batch, mask, mask], [ one_hot_representation(labels_mask_batch, 2), one_hot_representation(labels_seg_batch, 4), ], subject) else: if sample_weights_bool: # sample_weights = np.zeros(labels_seg_batch) # for k, v in class_weights.items(): # sample_weights[np.where(labels_batch_resized == k)] = v yield ([image_batch, mask, tumor_mask], [ one_hot_representation(labels_mask_batch, 2), np.concatenate(( one_hot_representation( labels_seg_batch, 4, class_weights=class_weights) [:, :, :, :, 1:4], one_hot_representation( labels_seg_batch, 4, class_weights=class_weights) [:, :, :, :, 5:], ), axis=4), ]) else: yield ([image_batch, mask, tumor_mask], [ one_hot_representation(labels_mask_batch, 2), one_hot_representation(labels_seg_batch, 4)[:, :, :, :, 1:], ]) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_mask_batch = np.zeros((self.batch_size, ) + self.input_shape) labels_seg_batch = np.zeros((self.batch_size, ) + self.input_shape) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) else: it_subject_batch += 1 n_sbj += 1 if n_sbj >= n_iterations: break
def data_generator_BraTS_seg(self, subject_list, mode='train', normalize_bool=True, sample_weights_bool=False, class_weights=None): if mode == 'train': subject_list = self.data_augmentation(subject_list) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_seg_batch = np.zeros((self.batch_size, ) + self.input_shape) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) tumor_mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_tumor_mask(), self.input_shape, pad_value=0) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) labels_seg_batch[it_subject_batch, :, :, :] = resize_image( subject.load_labels(), self.input_shape, pad_value=0) if it_subject_batch + 1 == self.batch_size: it_subject_batch = 0 if sample_weights_bool: yield ([image_batch, mask, tumor_mask], np.concatenate(( one_hot_representation( labels_seg_batch, 4, class_weights=class_weights)[:, :, :, :, 1:4], one_hot_representation( labels_seg_batch, 4, class_weights=class_weights)[:, :, :, :, 5:], ), axis=4)) else: yield ([image_batch, mask, tumor_mask], one_hot_representation(labels_seg_batch, 4)[:, :, :, :, 1:]) image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_seg_batch = np.zeros((self.batch_size, ) + self.input_shape) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) else: it_subject_batch += 1
def data_generator_BraTS_survival(self, subject_list, mode='train', normalize_bool=True, sample_weights_bool=False): if mode == 'train': subject_list = self.data_augmentation(subject_list) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_survival_batch = np.zeros((self.batch_size, )) boolean_lables_survival = np.zeros((self.batch_size, )) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) while True: for subject in subject_list: image = subject.load_channels(normalize=normalize_bool) for mod in range(image.shape[-1]): image_batch[it_subject_batch, :, :, :, mod] = resize_image(image[:, :, :, mod], self.input_shape, pad_value=image[0, 0, 0, mod]) mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_ROI_mask(), self.input_shape, pad_value=0) tumor_mask[it_subject_batch, :, :, :, 0] = resize_image(subject.load_tumor_mask(), self.input_shape, pad_value=0) survival = subject.load_survival() if survival is None: labels_survival_batch[it_subject_batch] = 0 boolean_lables_survival[it_subject_batch] = 0 else: labels_survival_batch[it_subject_batch] = survival boolean_lables_survival[it_subject_batch] = 1 if it_subject_batch + 1 == self.batch_size: if sample_weights_bool: yield ([image_batch, mask, boolean_lables_survival], labels_survival_batch) else: yield ([image_batch, mask, boolean_lables_survival], labels_survival_batch) it_subject_batch = 0 image_batch = np.zeros((self.batch_size, ) + self.input_shape + (self.num_modalities, )) labels_survival_batch = np.zeros((self.batch_size, )) boolean_lables_survival = np.zeros((self.batch_size, )) mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) tumor_mask = np.zeros((self.batch_size, ) + self.input_shape + (1, )) else: it_subject_batch += 1