Exemple #1
0
    conditioning_label = unique_brain_data_tags[step %
                                                len(unique_brain_data_tags)]
    conditioning_label = torch.Tensor(np.expand_dims(conditioning_label, 0))
    if CUDA:
        conditioning_label = conditioning_label.cuda()
    conditioning_label = Variable(conditioning_label)

    # Generate synthetic brain image data with the same label as the real data
    noise_sample = Variable(
        utils.utils.noise(size=(1, NOISE_SAMPLE_LENGTH), cuda=CUDA))
    sythetic_brain_img_data = generator(noise_sample, conditioning_label)

    # Upsample synthetic brain image data
    synthetic_sample_data = sythetic_brain_img_data[0].cpu().data.numpy(
    ).squeeze()
    upsampled_synthetic_brain_img = invert_preprocessor_scaling(
        synthetic_sample_data, brainpedia.preprocessor)

    # Save upsampled synthetic brain image data
    synthetic_sample_output_path = "{0}image_{1}.nii.gz".format(
        args.output_dir, step)
    nibabel.save(upsampled_synthetic_brain_img, synthetic_sample_output_path)

    # Save synthetic brain image metadata
    with open("{0}image_{1}_metadata.json".format(args.output_dir, step),
              'w') as metadata_f:
        tags = ""
        for sample_label in brainpedia.preprocessor.decode_label(
                conditioning_label.data[0]):
            tags += sample_label + ','

        json.dump({'tags': tags}, metadata_f)
        syn_dataset_1.append(syn_data)
    else:
        syn_dataset_2.append(syn_data)

# Trim real datasets to the same length
real_dataset_length = min(len(real_dataset_1), len(real_dataset_2))
real_dataset_1 = np.array(real_dataset_1[:real_dataset_length])
real_dataset_2 = np.array(real_dataset_2[:real_dataset_length])

# Trim synthetic datasets to the same length
syn_dataset_length = min(len(syn_dataset_1), len(syn_dataset_2))
syn_dataset_1 = np.array(syn_dataset_1[:syn_dataset_length])
syn_dataset_2 = np.array(syn_dataset_2[:syn_dataset_length])

# Plot examples from datasets
real_dataset_1_img = invert_preprocessor_scaling(
    real_dataset_1[0].squeeze(), real_dataset_brainpedia.preprocessor)
real_dataset_2_img = invert_preprocessor_scaling(
    real_dataset_2[0].squeeze(), real_dataset_brainpedia.preprocessor)
syn_dataset_1_img = invert_preprocessor_scaling(
    syn_dataset_1[0].squeeze(), syn_dataset_brainpedia.preprocessor)
syn_dataset_2_img = invert_preprocessor_scaling(
    syn_dataset_2[2].squeeze(), syn_dataset_brainpedia.preprocessor)

figure, axes = plt.subplots(nrows=6, ncols=1, figsize=(15, 40))
plotting.plot_glass_brain(real_dataset_1_img, threshold='auto',
                          title="[REAL {0}]".format(args.tag), axes=axes[0])
plotting.plot_glass_brain(syn_dataset_1_img, threshold='auto',
                          title="[SYN {0}]".format(args.tag), axes=axes[1])
plotting.plot_glass_brain(real_dataset_2_img, threshold='auto',
                          title="[REAL {0}]".format('NON-' + args.tag), axes=axes[2])
plotting.plot_glass_brain(syn_dataset_2_img, threshold='auto',