Esempio n. 1
0
# By default, the output label maps contain all the labels used for generation. We can also chose to keep only a subset
# of those, by specifying them in output_labels. This should only contain label already present in the label maps (or in
# generation_labels if it is provided).
output_labels = './data_example/segmentation_labels.npy'

# By default, each label will be associated to a Gaussian distribution when sampling a new image. We can also group
# labels in classes, to force them to share the same Gaussian. This can be done by providing generation classes, which
# should be a sequence, a 1d numpy array, or the path to such an array, with the *same length* as generation_labels.
# Values in generation_classes should be between 0 and K-1, where K is the total number of classes.
generation_classes = './data_example/generation_classes.npy'

########################################################################################################

# instantiate BrainGenerator object
brain_generator = ImageGenerator(labels_dir=path_label_map,
                                 generation_labels=generation_labels,
                                 output_labels=output_labels,
                                 generation_classes=generation_classes)

# create result dir
if not os.path.exists(os.path.join(result_dir)):
    os.mkdir(result_dir)

for n in range(n_examples):

    # generate new image and corresponding labels
    im, lab = brain_generator.generate_image()

    # save output image and label map
    save_volume(im, brain_generator.aff, brain_generator.header,
                os.path.join(result_dir, 'brain_%s.nii.gz' % n))
    save_volume(lab, brain_generator.aff, brain_generator.header,
Esempio n. 2
0
# We specify here the hyperparameters of the prior distributions to sample the means of the GMM.
# As these prior distributions are Gaussians, they are each controlled by a mean and a standard deviation.
# Therefore, the numpy array pointed by prior_means is of size (2, K), where K is the nummber of classes specified in
# generation_classes. The first row of prior_means correspond to the means of the Gaussian priors, and the second row
# correspond to standard deviations.
prior_means = './data_example/prior_means.npy'
# same as for prior_means, but for the standard deviations of the GMM.
prior_stds = './data_example/prior_stds.npy'

########################################################################################################

# instantiate BrainGenerator object
brain_generator = ImageGenerator(labels_dir=path_label_map,
                                 generation_labels=generation_labels,
                                 output_labels=output_labels,
                                 generation_classes=generation_classes,
                                 prior_distributions=prior_distribution,
                                 prior_means=prior_means,
                                 prior_stds=prior_stds,
                                 output_shape=output_shape)

# create result dir
if not os.path.exists(os.path.join(result_dir)):
    os.mkdir(result_dir)

for n in range(n_examples):

    # generate new image and corresponding labels
    im, lab = brain_generator.generate_image()

    # save output image and label map
    save_volume(im, brain_generator.aff, brain_generator.header,
Esempio n. 3
0
# Very simple script showing how to generate new images with random contrast

import os
from lab2im import utils
from lab2im.image_generator import ImageGenerator

# path of the input label map
path_label_map = './data_example/brain_label_map.nii.gz'
# path where to save the generated image
result_dir = './generated_images'

# generate an image from the label map.
# Because the image is spatially deformed, we also output the corresponding deformed label map.
brain_generator = ImageGenerator(path_label_map)
im, lab = brain_generator.generate_image()

# save output image and label map
if not os.path.exists(os.path.join(result_dir)):
    os.mkdir(result_dir)
utils.save_volume(im, brain_generator.aff, brain_generator.header,
                  os.path.join(result_dir, 'brain.nii.gz'))
utils.save_volume(lab, brain_generator.aff, brain_generator.header,
                  os.path.join(result_dir, 'labels.nii.gz'))
Esempio n. 4
0
                       np.array([0, 0, 0, 0, 0, 0]).astype(float), # Rho_f values
                       np.array([0, 0, 0, 0, 0, 0]).astype(float),
                       np.array([0, 0, 0, 0, 0, 0]).astype(float) * (10**-3), # T1 Values
                       np.array([0, 0, 0, 0, 0, 0]).astype(float) * (10**-3),
                       np.array([0, 0, 0, 0, 0, 0]).astype(float) * (10**-3), # T2 Values
                       np.array([0, 0, 0, 0, 0, 0]).astype(float) * (10**-3),
                       ])
np.save('./base_images_phantom4/prior_stds.npy', stds_list)
prior_stds = './base_images_phantom4/prior_stds.npy'

########################################################################################################
# instantiate BrainGenerator object
brain_generator = ImageGenerator(labels_dir=path_label_map,
                                 generation_labels=generation_labels,
                                 prior_distributions=prior_distribution,
                                 prior_means=prior_means,
                                 n_channels=4,
                                 prior_stds=prior_stds,
                                 output_shape=output_shape,
                                 use_specific_stats_for_channel=True)

# create result dir
utils.mkdir(result_dir)

for n in range(n_examples):

    # generate new image and corresponding labels
    start = time.time()
    im, lab = brain_generator.generate_image()
    end = time.time()
    print('generation {0:d} took {1:.01f}s'.format(n, end - start))