def _generator_fn(idx): identifier = sampler[idx] if idx == len(sampler) - 1: sampler.randomize() outputs = [ output for output in Creator(creator.outputs).eval(identifier) ] if len(outputs) > 0: outputs = [[ Sample( np.concatenate([output[j][i] for output in outputs]), np.concatenate( [output[j][i].affine for output in outputs])) for i in range(len(outputs[0][j])) ] for j in range(len(outputs[0]))] else: outputs = [[ np.full((1, output_shape[1] or 1, output_shape[2] or 1, output_shape[3] or 1, output_shape[4] or 1), 1234567890, dtype=np.float32) for output_shape in output_shapes ] for output_shapes in creator.output_shapes] gc.collect() return [output_ for output in outputs for output_ in output]
def load(self): img = Image.open(self.file_path) if self.kwargs: img = img.convert(**self.kwargs) img = np.asarray(img) assert img.ndim in [ 2, 3 ], "A 2D image file can only have a maximum of three dimensions: x, y, color." return Sample(img if img.ndim == 2 else img[:, :, None, :])
def predict(self, split, model_name, key, fold_i=None, round_i=None, name_tag=None, **kwargs): prev_fold_i = self.fold_i if fold_i is None: fold_i = self.fold_i prev_round_i = self.round_i if round_i is None: round_i = self.round_i if fold_i is not None and round_i is not None: if not isinstance(fold_i, Iterable): fold_i = [fold_i] dvn_models = [] for fold_i_ in fold_i: self.update(fold_i=fold_i_, round_i=round_i) dvn_models.append( DvnModel.load_model( os.path.join(self.models_dir, model_name))) self.update(fold_i="-".join([str(fold_i_) for fold_i_ in fold_i]), round_i=round_i) output_dirs = self.get_split_dirs(split) identifiers = self.get_split_identifiers(split) assert output_dirs is not None and identifiers is not None, "For this split there were no identifiers or there was no Mirc object specified." self.create() for identifier, output_dir in zip(identifiers, output_dirs): samples = [ dvn_model.predict(key, [identifier])[0] for dvn_model in dvn_models ] sample = [[ Sample( np.mean([sample[i][j] for sample in samples], axis=0), np.mean([sample[i][j].affine for sample in samples], axis=0)) for j in range(len(samples[0][i])) ] for i in range(len(samples[0]))] DvnModel.save_sample(key, sample, output_dir, name_tag=name_tag, **kwargs) self.update(fold_i=prev_fold_i, round_i=prev_round_i)
def load(self): niis = [nib.load(file_path) for file_path in self.file_paths] assert all([ np.allclose(niis[0].affine, nii.affine) for nii in niis[1:] ]), "Not all affines are equal!" if self.mode == "concat": array = np.concatenate( [nii.get_fdata(caching="unchanged") for nii in niis], axis=self.axis) else: array = np.stack( [nii.get_fdata(caching="unchanged") for nii in niis], axis=self.axis) return Sample(array, niis[0].affine)
def predict(self, key, sampler, mode="last", output_dirs=None, name_tag=None, save_x=True, save_y=False, save_sample_weight=False): assert mode in [ "all", "last" ], "Will we only keep the last generated output (i.e. last) or everything (i.e. all)?" assert key in self.outputs, "There are no outputs available for this key." assert len( self.outputs[key] ) >= 1, "Outputs must be in the format [x, y, sample_weight] and to use predict at least [x] must be available." predictions = [] for identifier_i, identifier in enumerate(sampler): start_time = time.time() samples = [ sample for sample in Creator(self.outputs[key]).eval(identifier) ][0 if mode == "all" else -1:] samples = [[ Sample( np.concatenate([output[j][i] for output in samples]), np.concatenate([output[j][i].affine for output in samples])) for i in range(len(samples[0][j])) ] for j in range(len(samples[0]))] if output_dirs is not None: self.save_sample(key, samples, output_dirs[identifier_i], name_tag=name_tag, save_x=save_x, save_y=save_y, save_sample_weight=save_sample_weight) predictions.append(samples) print("Predicted {} with {} in {:.0f} s.".format( sampler[identifier_i](), key, time.time() - start_time)) return predictions
def load(self): nii = nib.load(self.file_path) return Sample(nii.get_fdata(caching="unchanged"), nii.affine)
def load(self): return Sample(self.nifty.get_fdata(caching="unchanged"), self.nifty.affine)
def load(self): return Sample(self.array, self.affine)
def create_samples(): # Let's define a toy example x = np.zeros((100, 100)) x[25:50, 25:50] = 1 x[25:50, 50:75] = 2 y = np.zeros((100, 100)) y[25:50, 25:75] = 1 fig, axs = plt.subplots(1, 2) axs[0].imshow(x, vmin=0, vmax=2) axs[1].imshow(y, vmin=0, vmax=2) plt.show() # inputs x_input = x_path = SampleInput( Sample(x) ) # Sample converts x into a 5D array (batch, x, y, z, features) with an affine attribute (here just np.eye) y_input = y_path = SampleInput(Sample(y), n=None) # processing x_path, y_path = AffineDeformation(x_path, translation_window_width=(10, 10, 0), rotation_window_width=(3.14 / 10, 0, 0))(x_path, y_path) x_path_intermediate = x_path x_path, y_path = RandomCrop(x_path, (27, 27, 1), n=16, nonzero=True)(x_path, y_path) x_path, y_path = Flip((0.5, 0, 0))(x_path, y_path) y_path = Crop(y_path, (21, 21, 1))(y_path) # outputs x_output = Put(y_input, caching=False)(x_path) y_output = y_input # Let's visualize some x samples at the end of the processing pathway: fig, axs = plt.subplots(3, 3) for i in range(3): x_sample = x_path.eval()[0] x_sample_intermediate = x_path_intermediate[0] axs[i, 0].imshow(x, vmin=0, vmax=2) axs[i, 1].imshow(x_sample_intermediate[0, :, :, 0, 0], vmin=0, vmax=2) axs[i, 2].imshow(x_sample[0, :, :, 0, 0], vmin=0, vmax=2) plt.show() # It was clear that the original x is affine transformed and then multiple patches are taken # Suppose we also want to put the drawn sample back onto the original image space (to see what happens if we would instantiate the Put transformer with caching=True, see end of this demo ;-)) fig, axs = plt.subplots(1, 4) axs[0].imshow(x, vmin=0, vmax=2) for i in range(3): x_sample = x_output.eval()[0] axs[i + 1].imshow(x_sample[0, :, :, 0, 0], vmin=0, vmax=2) plt.show() # Suppose we want to produce x and y samples, we must make sure that all transformers are updated (and not only the ones "below" x_path # There are two options: # Option 1: Just make sure they are grouped in one path and evaluate that path samples_path = Group()([x_path, y_path]) fig, axs = plt.subplots(2, 4) axs[0, 0].imshow(x, vmin=0, vmax=2) axs[1, 0].imshow(y, vmin=0, vmax=2) for i in range(3): x_sample, y_sample = samples_path.eval() axs[0, i + 1].imshow(x_sample[0, :, :, 0, 0], vmin=0, vmax=2) axs[1, i + 1].imshow(y_sample[0, :, :, 0, 0], vmin=0, vmax=2) plt.show() # Option 2: Make a higher abstract Creator network and evaluate that network (this has the advantage that it only evaluates the nodes that are relevant and thus is more efficient) creator = Creator([x_path, y_path]) creator.summary() fig, axs = plt.subplots(2, 4) axs[0, 0].imshow(x, vmin=0, vmax=2) axs[1, 0].imshow(y, vmin=0, vmax=2) for i, output in enumerate(creator.eval()): x_sample, y_sample = output axs[0, i + 1].imshow(x_sample[0][0, :, :, 0, 0], vmin=0, vmax=2) axs[1, i + 1].imshow(y_sample[0][0, :, :, 0, 0], vmin=0, vmax=2) if i == 2: break plt.show() # Finally, a Creator has notion of when your network has ran out of samples. As you can see, we ask 16 samples from the RandomCrop transformer (all the rest generate a default of 1 sample). # This means the creator should create us 16 samples creator = Creator([x_output, y_output]) creator.summary() fig, axs = plt.subplots(4, 4) count = 0 for i, output in enumerate(creator.eval()): x_sample, y_sample = output axs[i // 4, i % 4].imshow(x_sample[0][0, :, :, 0, 0], vmin=0, vmax=2) count += 1 plt.show() assert count == 16 # Suppose we use a GridCrop instead of a RandomCrop with no n specified --> The GridCrop will create a number of samples as to complete the entire grid. # inputs x_input = x_path = SampleInput(Sample(x)) y_input = y_path = SampleInput(Sample(y), n=None) # processing x_path, y_path = AffineDeformation(x_path, translation_window_width=(10, 10, 0), rotation_window_width=(0, 0, 0))(x_path, y_path) x_path, y_path = GridCrop(x_path, (27, 27, 1), strides=(21, 21, 1), nonzero=True)(x_path, y_path) x_path, y_path = Flip((0.5, 0, 0))(x_path, y_path) y_path = Crop(y_path, (21, 21, 1))(x_path) # outputs # Try to understand completely what the Buffer does! Experiment a bit with different buffer_sizes (also try None) and drop_remainder=True/False. It basically batches the samples that arrive here. # Just like with any other transformer you can hang multiple paths to the Buffer if they have to be buffered in the same way x_path, y_path = Buffer(buffer_size=3, drop_remainder=False)(x_path, y_path) x_output = Put(y_input)(x_path) y_output = y_input creator = Creator([x_output, y_output]) creator.summary() fig, axs = plt.subplots(4, 4) count = 0 for i, output in enumerate(creator.eval()): if i == 0: creator.write_transformer_outputs( "/Users/jberte3/Desktop/deepvoxnet2") x_sample, y_sample = output axs[i // 4, i % 4].imshow(x_sample[0][0, :, :, 0, 0], vmin=0, vmax=2) count += 1 plt.show() print("There were {} samples from the grid cropper.".format(count))