def main(start_time):
    tf.enable_eager_execution()

    # handle arguments and config
    args = parse_arguments()
    args.start_time = start_time

    tf.logging.info("Args: {}".format(args))
    args.second_data_dir = args.second_data_dir or args.data_dir

    args.input_type = "image"
    args.second_input_type = "patho"
    args.has_colored_input = True
    args.has_colored_second_input = False
    args.has_colored_target = True
    args.has_noise_input = False
    args.discriminator_classes = 1
    if os.path.exists(os.path.join("output", args.eval_dir, "checkpoints")):
        args.output_dir = os.path.join("output", args.eval_dir)
    else:
        args.output_dir = os.path.join("old-output", args.eval_dir)
    args.checkpoint_dir = os.path.join(args.output_dir, "checkpoints")
    model = load_model(args)
    generator = model.get_generator()
    discriminator = model.get_discriminator()
    load_checkpoint(args,
                    checkpoint_number=args.epoch // 25,
                    generator=generator,
                    discriminator=discriminator)

    images_per_type = 2
    training_input_image_names = np.array(load_image_names(args.data_dir))
    training_second_input_image_names = np.array(
        load_image_names(args.second_data_dir))
    args.test_data_dir = args.data_dir + "-TEST"
    args.test_second_data_dir = args.second_data_dir + "-TEST"
    test_input_image_names = np.array(load_image_names(args.test_data_dir))
    test_second_input_image_names = np.array(
        load_image_names(args.test_second_data_dir))
    input_indexes = np.random.choice(min(len(training_input_image_names),
                                         len(test_input_image_names)),
                                     args.image_count * images_per_type,
                                     replace=False)

    for image_number in range(args.image_count):
        indexes = input_indexes[images_per_type *
                                image_number:images_per_type *
                                (image_number + 1)]
        generate_samples(args, generator, discriminator,
                         training_input_image_names[indexes],
                         training_second_input_image_names[indexes],
                         test_input_image_names[indexes],
                         test_second_input_image_names[indexes], image_number)

    tf.logging.info("Finished evaluation")
    def load_data(self):
        image_names = load_image_names(self._config.data_dir,
                                       self._config.match_pattern)
        target_images = load_images(image_names,
                                    self._config.data_dir,
                                    self._config.target_type,
                                    flip_lr=self._config.augmentation_flip_lr,
                                    flip_ud=self._config.augmentation_flip_ud)

        # load/prepare test data
        test_target_images = None
        if self._config.test_data_dir:
            tf.logging.info("Loading test data")
            test_image_names = load_image_names(self._config.test_data_dir,
                                                self._config.match_pattern)
            test_target_images = load_images(test_image_names,
                                             self._config.test_data_dir,
                                             self._config.target_type,
                                             flip_lr=False,
                                             flip_ud=False)
        if self._config.test_data_percentage:
            tf.logging.warning(
                "Using the first {}% of the training data for testing".format(
                    100 * self._config.test_data_percentage))
            split = int(len(target_images) * self._config.test_data_percentage)
            test_target_images = target_images[:split]
            target_images = target_images[split:]
        data_set_size = len(target_images)
        test_set_size = len(
            [] if test_target_images is None else test_target_images)

        # build data sets
        self._data_set = tf.data.Dataset.from_tensor_slices(target_images)\
            .map(lambda x: (tf.random_normal([self._config.noise_dimensions]), x))\
            .shuffle(data_set_size).batch(self._config.batch_size)
        del target_images
        if test_target_images is not None:
            self._test_data_set = tf.data.Dataset.from_tensor_slices(test_target_images)\
                .map(lambda x: (tf.random_normal([self._config.noise_dimensions]), x))\
                .shuffle(test_set_size).batch(self._config.batch_size)
            del test_target_images
        else:
            tf.logging.warning("Running evaluation without test data!")

        return data_set_size, test_set_size
Beispiel #3
0
    def initialize(self, override_data_dir=None):
        assert self._real_activations is None

        data_dir = override_data_dir if override_data_dir else \
            (self._config.target_data_dir if self._config.target_data_dir else self._config.data_dir)
        activations_file = os.path.join(
            "data", data_dir,
            "activations_{}.npz".format(self._config.extractor_name))
        if os.path.exists(activations_file):
            tf.logging.info(
                "Loading activations from {}".format(activations_file))
            with np.load(activations_file) as activations:
                self._real_activations = [
                    tf.convert_to_tensor(activations[f])
                    for f in sorted(activations.files)
                ]
        else:
            tf.logging.warning(
                "Computing activations for real images in '{}'".format(
                    data_dir))
            self._real_activations = self._get_activations_from_images(
                load_image_names(data_dir))
            tf.logging.info(
                "Saving activations to {}".format(activations_file))
            np.savez(
                activations_file, **{
                    "block_{}".format(i): act.numpy()
                    for i, act in enumerate(self._real_activations)
                })

        tf.logging.debug("Fitting PCA")
        self._pca = PCA(n_components=2)
        low_dimensional_real_activations = self._pca.fit_transform(
            self._real_activations[-1])
        tf.logging.debug("Explained variance: {} ({:.5f})".format(
            self._pca.explained_variance_ratio_,
            np.sum(self._pca.explained_variance_ratio_)))

        high_dimensional_clusters = 7
        tf.logging.debug(
            "Clustering high-dimensional activations with {} clusters".format(
                high_dimensional_clusters))
        self._high_dimensional_kmeans = KMeans(
            n_clusters=high_dimensional_clusters)
        self._high_dimensional_kmeans.fit(self._real_activations[-1])
        tf.logging.debug("Inertia: {:.1f}".format(
            self._high_dimensional_kmeans.inertia_))

        low_dimensional_clusters = 4
        tf.logging.debug(
            "Clustering low-dimensional activations with {} clusters".format(
                low_dimensional_clusters))
        self._low_dimensional_kmeans = KMeans(
            n_clusters=low_dimensional_clusters)
        self._low_dimensional_kmeans.fit(low_dimensional_real_activations)
        tf.logging.debug("Inertia: {:.1f}".format(
            self._low_dimensional_kmeans.inertia_))
def compute_perceptual_scores(generator, discriminator, args):
    tf.logging.fatal("Computing perceptual scores")
    assert args.data_dir
    generate_samples(generator, discriminator, args,
                     len(load_image_names(args.data_dir)))

    extractor = PerceptualScores(args)
    extractor.initialize()

    fid, mmd, high_dimensional, low_dimensional, _, _ = extractor.compute_scores_from_samples(
    )
    tf.logging.warning(
        "Clustering scores, high-dimensional: {:.3f}; low-dimensional: {:.3f}".
        format(high_dimensional, low_dimensional))
    tf.logging.warning("FID: {:.3f}, k-MMD: {:.3f}".format(fid, mmd))
    tf.logging.info(
        "Perceptual scores (FID,MMD,high,low): {:.5f}, {:.5f}, {:.5f}, {:.5f}".
        format(fid, mmd, high_dimensional, low_dimensional))
    def load_data(self):
        # load and prepare names
        input_image_names = load_image_names(self._config.data_dir,
                                             self._config.match_pattern)
        second_input_image_names = input_image_names if not self._config.second_data_dir else \
            load_image_names(self._config.second_data_dir, self._config.match_pattern)
        assert not self._config.use_extra_first_inputs, "not implemented"
        if len(second_input_image_names) < len(input_image_names):
            tf.logging.warning(
                "There are fewer second input images; shuffling and reducing input images"
            )
            np.random.shuffle(input_image_names)
            input_image_names = input_image_names[:len(second_input_image_names
                                                       )]
        target_image_names = input_image_names if not self._config.target_data_dir else \
            second_input_image_names if self._config.target_data_dir == self._config.second_data_dir else \
            load_image_names(self._config.target_data_dir, self._config.match_pattern)
        assert len(target_image_names) >= len(input_image_names)
        name_size = min(
            min(len(input_image_names), len(second_input_image_names)),
            len(target_image_names))
        if len(input_image_names) != len(target_image_names) or len(
                second_input_image_names) != len(target_image_names):
            tf.logging.warning(
                "Reducing data set to {} (input: {}, second input: {}, target: {})"
                .format(name_size, len(input_image_names),
                        len(second_input_image_names),
                        len(target_image_names)))
            input_image_names = input_image_names[:name_size]
            second_input_image_names = second_input_image_names[:name_size]
            tf.logging.info(
                "Input and target data are different; shuffling targets before reducing"
            )
            np.random.shuffle(target_image_names)
            if self._config.train_disc_on_extra_targets:
                extra_target_image_names = target_image_names[name_size:]
                extra_target_image_names = extra_target_image_names[:len(
                    input_image_names)]  # select the first X extra images
            else:
                extra_target_image_names = None
            target_image_names = target_image_names[:name_size]
        else:
            assert not self._config.train_disc_on_extra_targets, "there are no extra targets to train on"
            extra_target_image_names = None

        # load images
        input_images = load_images(input_image_names,
                                   self._config.data_dir,
                                   self._config.input_type,
                                   flip_lr=self._config.augmentation_flip_lr,
                                   flip_ud=self._config.augmentation_flip_ud)
        second_input_images = load_images(
            second_input_image_names,
            self._config.second_data_dir or self._config.data_dir,
            self._config.second_input_type,
            flip_lr=self._config.augmentation_flip_lr,
            flip_ud=self._config.augmentation_flip_ud)
        combined_input_images = np.concatenate(
            [input_images, second_input_images], axis=-1)
        del input_images
        target_images = load_images(target_image_names,
                                    self._config.target_data_dir
                                    or self._config.data_dir,
                                    self._config.target_type,
                                    flip_lr=self._config.augmentation_flip_lr,
                                    flip_ud=self._config.augmentation_flip_ud)
        if extra_target_image_names:
            extra_target_images = load_images(
                extra_target_image_names,
                self._config.target_data_dir or self._config.data_dir,
                self._config.target_type,
                flip_lr=self._config.augmentation_flip_lr,
                flip_ud=self._config.augmentation_flip_ud)
            tf.logging.warning(
                "Adding {} extra targets for the discriminator!".format(
                    len(extra_target_images)))
            self._extra_discriminator_data_set = tf.data.Dataset.from_tensor_slices(extra_target_images)\
                .shuffle(len(extra_target_images)).batch(self._config.batch_size)
            del extra_target_images
        assert len(combined_input_images) == len(target_images)
        combined_target_images = np.concatenate(
            [target_images, second_input_images], axis=-1)
        data_set_size = len(target_images)
        del second_input_images
        del target_images

        # load/prepare test data
        test_input_images = None
        test_target_images = None
        if self._config.test_data_dir:
            tf.logging.info("Loading test data")
            test_input_image_names = load_image_names(
                self._config.test_data_dir, self._config.match_pattern)
            test_second_input_image_names = test_input_image_names if not self._config.test_second_data_dir else \
                load_image_names(self._config.test_second_data_dir, self._config.match_pattern)
            if len(test_second_input_image_names) < len(
                    test_input_image_names):
                tf.logging.warning(
                    "TEST: There are fewer second input images; shuffling and reducing input images"
                )
                np.random.shuffle(test_input_image_names)
                test_input_image_names = test_input_image_names[:len(
                    test_second_input_image_names)]
            test_target_image_names = test_input_image_names if not self._config.test_target_data_dir else \
                test_second_input_image_names if self._config.test_target_data_dir == self._config.test_second_data_dir else \
                load_image_names(self._config.test_target_data_dir, self._config.match_pattern)
            assert len(test_target_image_names) >= len(test_input_image_names)
            name_size = min(
                min(len(test_input_image_names),
                    len(test_second_input_image_names)),
                len(test_target_image_names))
            if len(test_input_image_names) != len(
                    test_target_image_names) or len(
                        test_second_input_image_names) != len(
                            test_target_image_names):
                tf.logging.warning(
                    "Reducing data set to {} (input: {}, second input: {}, target: {})"
                    .format(name_size, len(test_input_image_names),
                            len(test_second_input_image_names),
                            len(test_target_image_names)))
                test_input_image_names = test_input_image_names[:name_size]
                test_second_input_image_names = test_second_input_image_names[:
                                                                              name_size]
                tf.logging.info(
                    "Input and target data are different; shuffling targets before reducing"
                )
                np.random.shuffle(test_target_image_names)
                test_target_image_names = test_target_image_names[:name_size]

            test_input_images = load_images(test_input_image_names,
                                            self._config.test_data_dir,
                                            self._config.input_type,
                                            flip_lr=False,
                                            flip_ud=False)
            test_second_input_images = load_images(
                test_second_input_image_names,
                self._config.test_second_data_dir
                or self._config.test_data_dir,
                self._config.second_input_type,
                flip_lr=False,
                flip_ud=False)
            test_combined_input_images = np.concatenate(
                [test_input_images, test_second_input_images], axis=-1)
            del test_input_images
            test_target_images = load_images(test_target_image_names,
                                             self._config.test_target_data_dir
                                             or self._config.test_data_dir,
                                             self._config.target_type,
                                             flip_lr=False,
                                             flip_ud=False)
            test_combined_target_images = np.concatenate(
                [test_target_images, test_second_input_images], axis=-1)
            assert len(test_combined_input_images) == len(test_target_images)
            del test_second_input_images

        assert not self._config.test_data_percentage, "not implemented"
        test_set_size = len(
            [] if test_target_images is None else test_target_images)
        del test_target_images

        # set up epoch samples
        sample_indexes = np.random.choice(data_set_size,
                                          np.prod(self._epoch_images_shape),
                                          replace=False)
        self._epoch_sample_input = tf.convert_to_tensor(
            combined_input_images[sample_indexes])
        self._epoch_sample_targets = tf.convert_to_tensor(
            combined_target_images[sample_indexes])

        # build data sets
        self._data_set = tf.data.Dataset.from_tensor_slices((combined_input_images, tf.random_shuffle(combined_target_images)))\
            .shuffle(data_set_size).batch(self._config.batch_size)
        del combined_input_images
        del combined_target_images
        if test_set_size:
            self._test_data_set = tf.data.Dataset.from_tensor_slices((test_combined_input_images, tf.random_shuffle(test_combined_target_images)))\
                .shuffle(test_set_size).batch(self._config.batch_size)
            del test_combined_input_images
            del test_combined_target_images
        else:
            tf.logging.warning("Running evaluation without test data!")

        return data_set_size, test_set_size
def main(start_time):
    tf.enable_eager_execution()
    configure_logging()

    # handle arguments and config
    args = parse_arguments()
    args.start_time = start_time

    tf.logging.info("Args: {}".format(args))
    args.second_data_dir = args.second_data_dir or args.data_dir
    args.target_data_dir = args.target_data_dir or args.data_dir

    args.match_pattern = None
    args.augmentation_flip_lr = False
    args.augmentation_flip_ud = False
    args.has_noise_input = args.input_type == "noise"
    args.has_colored_input = args.input_type == "image"
    args.has_colored_second_input = args.second_input_type == "image"
    args.has_colored_target = args.target_type == "image"
    args.discriminator_classes = 1
    if os.path.exists(os.path.join("output", args.eval_dir, "checkpoints")):
        args.checkpoint_dir = os.path.join("output", args.eval_dir,
                                           "checkpoints")
    else:
        args.checkpoint_dir = os.path.join("old-output", args.eval_dir,
                                           "checkpoints")
    model = load_model(args)
    discriminator = model.get_discriminator()
    if args.evaluate_generator:
        generator = model.get_generator()
        load_checkpoint(args,
                        checkpoint_number=(args.epoch + 24) //
                        25 if args.epoch else args.epoch,
                        generator=generator,
                        discriminator=discriminator)
    else:
        generator = None
        load_checkpoint(args,
                        checkpoint_number=(args.epoch + 24) //
                        25 if args.epoch else args.epoch,
                        discriminator=discriminator)

    # assert not (bool(args.test_data_dir) and bool(args.test_data_samples)), \
    #     "either use a part of the training data for test *OR* some actual test data"

    training_input_image_names = load_image_names(args.data_dir)
    training_second_input_image_names = load_image_names(
        args.second_data_dir) if args.second_input_type else None
    training_target_image_names = load_image_names(args.target_data_dir)
    if training_second_input_image_names is not None:
        if len(training_input_image_names) > len(
                training_second_input_image_names):
            tf.logging.info(
                "Input and second input data are different; shuffling inputs before reducing"
            )
            np.random.shuffle(training_input_image_names)
            training_input_image_names = training_input_image_names[:len(
                training_second_input_image_names)]
        assert len(training_input_image_names) == len(
            training_second_input_image_names)
    assert len(training_target_image_names) == len(training_input_image_names)
    if args.data_dir != args.target_data_dir or args.second_data_dir != args.target_data_dir:
        tf.logging.info(
            "Input and target data are different; shuffling targets before reducing"
        )
        np.random.shuffle(training_target_image_names)
        training_target_image_names = training_target_image_names[:len(
            training_input_image_names)]
    if args.compute_test_scores:
        if args.test_data_samples:  # remove data that's not actually in the training set from the training data
            if args.split_data:
                raise NotImplementedError()
            tf.logging.warning(
                "Using the first {} unaugmented samples of the training data for testing"
                .format(args.test_data_samples))
            test_input_image_names = training_input_image_names[:args.
                                                                test_data_samples]
            training_input_image_names = training_input_image_names[
                args.test_data_samples:]
            if args.second_input_type:
                test_second_input_image_names = training_second_input_image_names[:
                                                                                  args
                                                                                  .
                                                                                  test_data_samples]
                training_second_input_image_names = training_second_input_image_names[
                    args.test_data_samples:]
            else:
                test_second_input_image_names = None
            test_target_image_names = training_target_image_names[:args.
                                                                  test_data_samples]
            training_target_image_names = training_target_image_names[
                args.test_data_samples:]
            args.test_data_dir = args.data_dir
            args.test_second_data_dir = args.second_data_dir
            args.test_target_data_dir = args.target_data_dir
        else:
            args.test_data_dir = args.data_dir + "-TEST"
            args.test_second_data_dir = args.second_data_dir + "-TEST"
            args.test_target_data_dir = args.target_data_dir + "-TEST"
            test_input_image_names = load_image_names(args.test_data_dir)
            test_second_input_image_names = load_image_names(
                args.test_second_data_dir) if args.second_input_type else None
            test_target_image_names = load_image_names(
                args.test_target_data_dir)
            if test_second_input_image_names is not None:
                if len(test_input_image_names) > len(
                        test_second_input_image_names):
                    tf.logging.info(
                        "TEST input and second input data are different; shuffling inputs before reducing"
                    )
                    np.random.shuffle(test_input_image_names)
                    test_input_image_names = test_input_image_names[:len(
                        test_second_input_image_names)]
                assert len(test_input_image_names) == len(
                    test_second_input_image_names)
            assert len(test_target_image_names) >= len(test_input_image_names)
            if args.test_data_dir != args.test_target_data_dir or args.test_second_data_dir != args.test_target_data_dir:
                tf.logging.info(
                    "TEST input and target data are different; shuffling targets before reducing"
                )
                np.random.shuffle(test_target_image_names)
                test_target_image_names = test_target_image_names[:len(
                    test_input_image_names)]
    else:
        test_input_image_names = test_second_input_image_names = test_target_image_names = None

    if args.evaluate_discriminator:
        # evaluate D on real images
        tf.logging.warning("Evaluating D")
        evaluate_discriminations(
            args, None, discriminator, training_input_image_names,
            training_second_input_image_names, training_target_image_names,
            test_input_image_names, test_second_input_image_names,
            test_target_image_names)

    if generator:
        # if there's also a generator, evalaute D on generated images
        tf.logging.warning("Evaluating G (evaluating D on generated images)")
        evaluate_discriminations(
            args, generator, discriminator, training_input_image_names,
            training_second_input_image_names, training_target_image_names,
            test_input_image_names, test_second_input_image_names,
            test_target_image_names)

    tf.logging.info("Finished evaluation")
Beispiel #7
0
# parse descriptions
descriptions = load_flicker8k_token(filename)

# clean descriptions
cleaned_descriptions = clean_descriptions(descriptions)

# summarize vocabulary
vocabulary = get_all_vocab(descriptions)

save_descriptions(descriptions, 'descriptions.txt')
##################################################################

# load training dataset (6K)
filename = os.path.join(text_file_base_dir, 'Flickr_8k.trainImages.txt')
train = load_image_names(filename)
print('Num of data samples: %d' % len(train))

# Below path contains all the images
images_dir = os.path.join(base_dir, 'data/captioning/Flicker8k_Dataset/')
# Below file conatains the names of images to be used in train data
train_image_txt_file = os.path.join(text_file_base_dir,
                                    'Flickr_8k.trainImages.txt')

train_image_paths = get_image_paths(images_dir, train_image_txt_file)

test_image_txt_file = os.path.join(text_file_base_dir,
                                   'Flickr_8k.testImages.txt')

test_image_paths = get_image_paths(images_dir, test_image_txt_file)
Beispiel #8
0
def main(start_time):
    tf.enable_eager_execution()

    # handle arguments and config
    args = parse_arguments()
    args.start_time = start_time

    args.match_pattern = None
    args.batch_size = 4
    args.eval_dir = os.path.join("output", args.eid)
    if not os.path.exists(args.eval_dir):
        args.eval_dir = os.path.join("old-output", args.eid)
    args.checkpoint_dir = os.path.join(args.eval_dir, "checkpoints")

    tf.logging.info("Args: {}".format(args))

    if not args.noise:
        tf.logging.warning("Needs to be cleaned/fixed/re-implemented first")
        legacy(args)
    else:
        args.noise_dimensions = 100
        args.has_colored_target = True

        image_names = load_image_names(args.data_dir)
        images = load_images(image_names, args.data_dir, image_subdir)
        targets = (tf.convert_to_tensor(images) + 1) * 127.5

        model = load_model(args)
        generator = model.get_generator()
        # discriminator = model.get_discriminator()
        load_checkpoint(args,
                        checkpoint_number=args.epoch // 25,
                        generator=generator)  #, discriminator=discriminator)

        args.rows = 4
        args.columns = 2

        for i in trange(args.image_count):
            plt.figure(figsize=(32, 32))
            plt.axis("off")
            for j, noise in enumerate(
                    tf.data.Dataset.from_tensor_slices(
                        tf.random_normal(
                            [args.rows * args.columns,
                             args.noise_dimensions])).batch(args.batch_size)):
                samples = generator(noise, training=False)
                for k, sample in enumerate(samples):
                    plt.subplot(args.rows, 2 * args.columns,
                                2 * j * args.batch_size + 2 * k + 1)
                    Evaluation.plot_image(sample)
                    plt.subplot(args.rows, 2 * args.columns,
                                2 * j * args.batch_size + 2 * k + 2)
                    similar_sample, similarity = find_nearest_sample(
                        (sample + 1) * 127.5, targets, args.batch_size)
                    Evaluation.plot_image(similar_sample,
                                          similarity,
                                          denormalize=False)
            plt.tight_layout()
            figure_file = os.path.join(
                args.eval_dir, "nearest_samples_ssim_{:03d}.png".format(i))
            plt.savefig(figure_file)
            plt.close()

    tf.logging.info("Finished generating {} images".format(args.image_count))
def main(start_time):
  tf.enable_eager_execution()

  # handle arguments and config
  args = parse_arguments()
  args.start_time = start_time
  args.noise_dimensions = None
  args.has_colored_target = False
  args.output_dir = os.path.join("output", args.eval_dir)
  if not os.path.exists(args.output_dir):
    args.output_dir = os.path.join("old-output", args.eval_dir)
  args.checkpoint_dir = os.path.join(args.output_dir, "checkpoints")
  tf.logging.info("Args: {}".format(args))

  model = load_model(args)
  generator = model.get_generator()
  if args.cycle:
    tf.logging.warning("Loading second generator of CycleGAN")
    load_checkpoint(args, checkpoint_number=(args.epoch+24)//25, second_generator=generator)
  else:
    load_checkpoint(args, checkpoint_number=(args.epoch+24)//25, generator=generator)

  image_names = load_image_names(args.test_data, args.match_pattern)
  input_images = load_images(image_names, args.test_data, "image")
  target_images = load_images(image_names, args.test_data, "patho")

  target_data_set = tf.data.Dataset.from_tensor_slices((input_images, target_images)).batch(args.batch_size)

  tf.logging.info("Computing segmentation score over {} images in batches of {}".format(len(target_images), args.batch_size))
  scores = list()
  all_tp = 0
  all_fp = 0
  all_fn = 0
  with tqdm(total=len(target_images)) as pbar:
    for batch in target_data_set:
      inputs, targets = batch
      pbar.update(inputs.shape[0].value)
      generated_images = generator(inputs, training=False)

      predicted = tf.cast(generated_images >= 0, tf.uint8)
      actual = tf.cast(targets >= 0, tf.uint8)

      tp = tf.count_nonzero(predicted * actual)
      fp = tf.count_nonzero(predicted * (actual - 1))
      fn = tf.count_nonzero((predicted - 1) * actual)

      all_tp += tp
      all_fp += fp
      all_fn += fn

      precision = tp / (tp + fp)
      recall = tp / (tp + fn)
      f1 = (2 * precision * recall / (precision + recall)).numpy()

      if not np.isnan(f1):
        scores.append(f1)

  precision = all_tp / (all_tp + all_fp)
  recall = all_tp / (all_tp + all_fn)
  f1 = (2 * precision * recall / (precision + recall)).numpy()

  tf.logging.info("Segmentation score: {:.3f} ({:.3}+-{:.3})".format(f1, np.mean(scores), np.std(scores)))
  tf.logging.info("TP {}, FP {}, FN {}".format(all_tp, all_fp, all_fn))
  if args.score_name:
    with open(os.path.join(args.output_dir, args.score_name), "w") as fh:
      fh.write("{}\n".format(",".join([str(s) for s in scores])))
def compute_reconstruction_scores(generator, args):
    tf.logging.fatal("Computing reconstruction scores")
    image_names = load_image_names(args.data_dir)
    np.random.shuffle(image_names)
    images = load_images(image_names[:args.batch_size], args.data_dir,
                         image_subdir)
    targets = tf.convert_to_tensor(images)

    rows = 4
    columns = 3
    images, titles, all_similarity_losses = ReconstructionScores(
        generator, targets, args.noise_dimensions).compute_scores(True)

    # assert len(images) == len(titles) and len(images[0]) == len(titles[0])
    # for j, _ in enumerate(images):
    #   plt.figure(figsize=(32, 32))
    #   subplot = 1
    #   for i in range(0, len(images[0]), 10):
    #     if i == len(images[0])-1:
    #       break # the last image is printed afterwards
    #     plt.subplot(rows, columns, subplot)
    #     Evaluation.plot_image(images[j][i], titles[j][i])
    #     subplot += 1
    #   plt.subplot(rows, columns, subplot)
    #   Evaluation.plot_image(images[j][-1], "Final " + titles[j][-1])
    #   plt.subplot(rows, columns, subplot+1)
    #   Evaluation.plot_image(targets[j], "Target")
    #   plt.savefig(os.path.join(args.output_dir, "{}reconstruction_{:03d}.png".format(
    #     "{}_".format(args.description) if args.description else "", j)))

    # plot for report
    plt.figure(figsize=(30, 4 * args.batch_size))
    for j, _ in enumerate(images):
        epochs_to_plot = [0, 5, 10, 50, 100]
        for subplot, i in enumerate(epochs_to_plot):
            plt.subplot(args.batch_size,
                        len(epochs_to_plot) + 1,
                        subplot + 1 + j * (len(epochs_to_plot) + 1))
            Evaluation.plot_image(images[j][i], titles[j][i])
        plt.subplot(args.batch_size,
                    len(epochs_to_plot) + 1,
                    subplot + 2 + j * (len(epochs_to_plot) + 1))
        Evaluation.plot_image(targets[j], "Target")
    plt.tight_layout()
    plt.savefig(
        os.path.join(
            args.output_dir, "{}reconstruction_report.png".format(
                "{}_".format(args.description) if args.description else "")))

    plt.figure()
    last_losses = all_similarity_losses[-1].numpy()
    plt.title("Similarity losses, final ({} epochs): {:.5f}+-{:.5f}".format(
        len(all_similarity_losses), last_losses.mean(), last_losses.std()))
    plt.xlabel("Epochs")
    plt.ylabel("Similarity loss")
    for i in range(all_similarity_losses[0].shape[0]):
        plt.plot(range(1,
                       len(all_similarity_losses) + 1),
                 [similarity[i] for similarity in all_similarity_losses],
                 label="Reconstruction {}".format(i + 1))
    plt.legend()
    plt.savefig(
        os.path.join(
            args.output_dir, "{}reconstruction_loss-curves.png".format(
                "{}_".format(args.description) if args.description else "")))
    plt.close()
Beispiel #11
0
def main(start_time):
    tf.enable_eager_execution()

    # handle arguments and config
    args = parse_arguments()
    args.start_time = start_time

    tf.logging.info("Args: {}".format(args))

    assert args.input_type == "patho"
    assert args.target_type == "image"

    args.has_colored_input = args.input_type == "image"
    args.has_colored_target = args.target_type == "image"
    args.discriminator_classes = 1
    args.checkpoint_dir = os.path.join("output", args.eval_dir, "checkpoints")
    model = load_model(args)
    generator = model.get_generator()
    discriminator = model.get_discriminator()
    load_checkpoint(args,
                    checkpoint_number=args.epoch // 25,
                    generator=generator,
                    discriminator=discriminator)

    input_image_names = np.array(load_image_names(args.data_dir))
    # target_image_names = input_image_names if not args.target_data_dir else \
    #     np.array(load_image_names(args.target_data_dir))
    sample_indexes = np.random.choice(len(input_image_names),
                                      args.interpolation_pairs * 2 *
                                      args.image_count,
                                      replace=False)
    input_images = load_images(input_image_names[sample_indexes],
                               args.data_dir, args.input_type)
    # target_images = load_images(target_image_names[sample_indexes], args.target_data_dir or args.data_dir, args.target_type)

    for image_number in range(args.image_count):
        tf.logging.info("Generating image {}/{}".format(
            image_number + 1, args.image_count))
        plt.figure(figsize=(int(args.width / 100), int(args.height / 100)))

        with tqdm(total=args.interpolation_pairs *
                  args.interpolation_steps) as pbar:
            for i in range(args.interpolation_pairs):
                index = 2 * i + 2 * image_number * args.interpolation_pairs
                for j in range(args.interpolation_steps):
                    pbar.update(1)
                    input_sample = interp_shape(
                        input_images[index, :, :, 0],
                        input_images[index + 1, :, :, 0],
                        float(j) / (args.interpolation_steps - 1))
                    gen_input = tf.expand_dims(tf.expand_dims(input_sample, 0),
                                               -1)
                    predictions = generator(gen_input, training=False)
                    disc_input = tf.concat(
                        [gen_input, predictions], axis=-1
                    ) if args.conditioned_discriminator else predictions
                    classifications = discriminator(disc_input, training=False)

                    plt.subplot(args.interpolation_pairs * 2,
                                args.interpolation_steps,
                                2 * i * args.interpolation_steps + j + 1)
                    plt.axis("off")
                    if args.has_colored_input:
                        plt.imshow(
                            np.array((input_sample + 1) * 127.5,
                                     dtype=np.uint8))
                    else:
                        plt.imshow(np.array((input_sample + 1) * 127.5,
                                            dtype=np.uint8),
                                   cmap="gray",
                                   vmin=0,
                                   vmax=255)

                    plt.subplot(
                        args.interpolation_pairs * 2, args.interpolation_steps,
                        2 * i * args.interpolation_steps + j + 1 +
                        args.interpolation_steps)
                    plt.axis("off")
                    plt.title(np.round(
                        logistic(classifications[0]).numpy(), 5),
                              fontsize=20)
                    if args.has_colored_target:
                        plt.imshow(
                            np.array((predictions[0] + 1) * 127.5,
                                     dtype=np.uint8))
                    else:
                        plt.imshow(np.array(
                            (predictions[0, :, :, 0] + 1) * 127.5,
                            dtype=np.uint8),
                                   cmap="gray",
                                   vmin=0,
                                   vmax=255)

        plt.tight_layout()
        figure_file = os.path.join(
            "output", args.eval_dir, "noise_interpolation{}_{:03d}.png".format(
                "_{}".format(args.description) if args.description else "",
                image_number + 1))
        plt.savefig(figure_file)
        plt.close()

    tf.logging.info("Finished generating {} images".format(args.image_count))
    def load_data(self):
        # load and prepare names
        input_image_names = load_image_names(self._config.data_dir,
                                             self._config.match_pattern)
        target_image_names = input_image_names if not self._config.target_data_dir else \
            load_image_names(self._config.target_data_dir, self._config.match_pattern)
        name_size = min(len(input_image_names), len(target_image_names))
        if len(input_image_names) != len(target_image_names):
            tf.logging.warning(
                "Reducing data set to {} (input: {}, target: {})".format(
                    name_size, len(input_image_names),
                    len(target_image_names)))
            input_image_names = input_image_names[:name_size]
            assert self._config.data_dir == self._config.target_data_dir, "should shuffle target images before reducing"
            target_image_names = target_image_names[:name_size]
        assert not self._config.train_disc_on_extra_targets, "not implemented"

        # load images
        input_images = load_images(input_image_names,
                                   self._config.data_dir,
                                   self._config.input_type,
                                   flip_lr=self._config.augmentation_flip_lr,
                                   flip_ud=self._config.augmentation_flip_ud)
        target_images = load_images(target_image_names,
                                    self._config.target_data_dir
                                    or self._config.data_dir,
                                    self._config.target_type,
                                    flip_lr=self._config.augmentation_flip_lr,
                                    flip_ud=self._config.augmentation_flip_ud)
        assert len(input_images) == len(target_images)

        # load/prepare test data
        test_input_images = None
        test_target_images = None
        if self._config.test_data_dir:
            assert not self._config.target_data_dir, "alternative test target data currently isn't supported"
            tf.logging.info("Loading test data")
            test_input_image_names = load_image_names(
                self._config.test_data_dir, self._config.match_pattern)
            test_input_images = load_images(test_input_image_names,
                                            self._config.test_data_dir,
                                            self._config.input_type,
                                            flip_lr=False,
                                            flip_ud=False)
            test_target_images = load_images(test_input_image_names,
                                             self._config.test_data_dir,
                                             self._config.target_type,
                                             flip_lr=False,
                                             flip_ud=False)
            assert len(test_input_images) == len(test_target_images)
        if self._config.test_data_percentage:
            tf.logging.warning(
                "Using the first {}% of the training data for testing".format(
                    100 * self._config.test_data_percentage))
            split = int(len(target_images) * self._config.test_data_percentage)
            test_input_images = input_images[:split]
            input_images = input_images[split:]
            test_target_images = target_images[:split]
            target_images = target_images[split:]
        data_set_size = len(target_images)
        test_set_size = len(
            [] if test_target_images is None else test_target_images)

        # set up epoch samples
        sample_indexes = np.random.choice(data_set_size,
                                          np.prod(self._epoch_images_shape),
                                          replace=False)
        self._epoch_sample_input = tf.convert_to_tensor(
            input_images[sample_indexes])
        self._epoch_sample_targets = target_images[sample_indexes]

        # build data sets
        self._data_set = tf.data.Dataset.from_tensor_slices((input_images, target_images))\
            .shuffle(data_set_size).batch(self._config.batch_size)
        del input_images
        del target_images
        if test_target_images is not None:
            self._test_data_set = tf.data.Dataset.from_tensor_slices((test_input_images, test_target_images))\
                .shuffle(test_set_size).batch(self._config.batch_size)
            del test_input_images
            del test_target_images
        else:
            tf.logging.warning("Running evaluation without test data!")

        return data_set_size, test_set_size