def finalize_data(layers): layers = np.array(layers) # Get in right order (x,y,z) and if HP.SLICE_DIRECTION == "x": layers = layers.transpose(0, 1, 2, 3) elif HP.SLICE_DIRECTION == "y": layers = layers.transpose(1, 0, 2, 3) elif HP.SLICE_DIRECTION == "z": layers = layers.transpose(1, 2, 0, 3) if scale_to_world_shape: layers = DatasetUtils.scale_input_to_world_shape(layers, HP.DATASET, HP.RESOLUTION) return layers.astype(np.float32)
def get_seg_single_img(self, HP, probs=False, scale_to_world_shape=True): ''' Returns layers for one image (batch manager is only allowed to return batches for one image) :param HP: :return: ([146, 174, 146, nrClasses], [146, 174, 146, nrClasses]) (Prediction, Groundtruth) ''' #Test Time DAug for i in range(1): # segs = [] # ys = [] layers_seg = [] layers_y = [] batch_generator = self.dataManager.get_batches(batch_size=1) batch_generator = list(batch_generator) for j in tqdm(list(range(len(batch_generator)))): batch = batch_generator[j] x = batch["data"] # (bs, nr_of_channels, x, y) y = batch["seg"] # (bs, x, y, nr_of_classes) y = y.astype(HP.LABELS_TYPE) y = np.squeeze( y ) # remove bs dimension which is only 1 -> (x, y, nrClasses) #For normal prediction layer_probs = self.model.get_probs(x) # (bs, x, y, nrClasses) layer_probs = np.squeeze( layer_probs ) # remove bs dimension which is only 1 -> (x, y, nrClasses) #For Dropout Sampling (must set Deterministic=False in model) # NR_SAMPLING = 30 # samples = [] # for i in range(NR_SAMPLING): # layer_probs = self.model.get_probs(x) # (bs, x, y, nrClasses) # samples.append(layer_probs) # # samples = np.array(samples) # (NR_SAMPLING, bs, x, y, nrClasses) # samples = np.squeeze(samples) # (NR_SAMPLING, x, y, nrClasses) # layer_probs = np.mean(samples, axis=0) # #layer_probs = np.std(samples, axis=0) #use std if probs: seg = layer_probs # (x, y, nrClasses) else: seg = layer_probs seg[seg >= HP.THRESHOLD] = 1 seg[seg < HP.THRESHOLD] = 0 seg = seg.astype(np.int16) layers_seg.append(seg) layers_y.append(y) layers_seg = np.array(layers_seg) layers_y = np.array(layers_y) #Get in right order (x,y,z) and if HP.SLICE_DIRECTION == "x": layers_seg = layers_seg.transpose(0, 1, 2, 3) layers_y = layers_y.transpose(0, 1, 2, 3) elif HP.SLICE_DIRECTION == "y": layers_seg = layers_seg.transpose(1, 0, 2, 3) layers_y = layers_y.transpose(1, 0, 2, 3) elif HP.SLICE_DIRECTION == "z": layers_seg = layers_seg.transpose(1, 2, 0, 3) layers_y = layers_y.transpose(1, 2, 0, 3) if scale_to_world_shape: layers_seg = DatasetUtils.scale_input_to_world_shape( layers_seg, HP.DATASET, HP.RESOLUTION) layers_y = DatasetUtils.scale_input_to_world_shape( layers_y, HP.DATASET, HP.RESOLUTION) layers_seg = layers_seg.astype(np.float32) layers_y = layers_y.astype(np.float32) return layers_seg, layers_y # (Prediction, Groundtruth)