Пример #1
0
 def _add_comparison_summary(gan_model, reconstructions):
   image_list = (array_ops.unstack(gan_model.generator_inputs[:1]) +
                 array_ops.unstack(gan_model.generated_data[:1]) +
                 array_ops.unstack(reconstructions[:1]))
   summary.image(
       'image_comparison', eval_utils.image_reshaper(
           image_list, num_cols=len(image_list)), max_outputs=1)
Пример #2
0
 def _add_comparison_summary(gan_model, reconstructions):
   image_list = (array_ops.unstack(gan_model.generator_inputs[:1]) +
                 array_ops.unstack(gan_model.generated_data[:1]) +
                 array_ops.unstack(reconstructions[:1]))
   summary.image(
       'image_comparison', eval_utils.image_reshaper(
           image_list, num_cols=len(image_list)), max_outputs=1)
Пример #3
0
def add_image_comparison_summaries(gan_model, num_comparisons=2,
                                   display_diffs=False):
  """Adds image summaries to compare triplets of images.

  The first image is the generator input, the second is the generator output,
  and the third is the real data. This style of comparison is useful for
  image translation problems, where the generator input is a corrupted image,
  the generator output is the reconstruction, and the real data is the target.

  Args:
    gan_model: A GANModel tuple.
    num_comparisons: The number of image triplets to display.
    display_diffs: Also display the difference between generated and target.

  Raises:
    ValueError: If real data, generated data, and generator inputs aren't
      images.
    ValueError: If the generator input, real, and generated data aren't all the
      same size.
  """
  if isinstance(gan_model, namedtuples.CycleGANModel):
    saved_params = locals()
    saved_params.pop('gan_model', None)
    with ops.name_scope('cyclegan_x2y_image_comparison_summaries'):
      add_image_comparison_summaries(gan_model.model_x2y, **saved_params)
    with ops.name_scope('cyclegan_y2x_image_comparison_summaries'):
      add_image_comparison_summaries(gan_model.model_y2x, **saved_params)
    return

  _assert_is_image(gan_model.generator_inputs)
  _assert_is_image(gan_model.generated_data)
  _assert_is_image(gan_model.real_data)

  gan_model.generated_data.shape.assert_is_compatible_with(
      gan_model.generator_inputs.shape)
  gan_model.real_data.shape.assert_is_compatible_with(
      gan_model.generated_data.shape)

  image_list = []
  image_list.extend(
      array_ops.unstack(gan_model.generator_inputs[:num_comparisons]))
  image_list.extend(
      array_ops.unstack(gan_model.generated_data[:num_comparisons]))
  image_list.extend(array_ops.unstack(gan_model.real_data[:num_comparisons]))
  if display_diffs:
    generated_list = array_ops.unstack(
        gan_model.generated_data[:num_comparisons])
    real_list = array_ops.unstack(gan_model.real_data[:num_comparisons])
    diffs = [
        math_ops.abs(math_ops.to_float(generated) - math_ops.to_float(real)) for
        generated, real in zip(generated_list, real_list)]
    image_list.extend(diffs)

  # Reshape image and display.
  summary.image(
      'image_comparison',
      eval_utils.image_reshaper(image_list, num_cols=num_comparisons),
      max_outputs=1)
Пример #4
0
def add_image_comparison_summaries(gan_model,
                                   num_comparisons=2,
                                   display_diffs=False):
    """Adds image summaries to compare triplets of images.

  The first image is the generator input, the second is the generator output,
  and the third is the real data. This style of comparison is useful for
  image translation problems, where the generator input is a corrupted image,
  the generator output is the reconstruction, and the real data is the target.

  Args:
    gan_model: A GANModel tuple.
    num_comparisons: The number of image triplets to display.
    display_diffs: Also display the difference between generated and target.

  Raises:
    ValueError: If real data, generated data, and generator inputs aren't
      images.
    ValueError: If the generator input, real, and generated data aren't all the
      same size.
  """
    _assert_is_image(gan_model.generator_inputs)
    _assert_is_image(gan_model.generated_data)
    _assert_is_image(gan_model.real_data)

    gan_model.generated_data.shape.assert_is_compatible_with(
        gan_model.generator_inputs.shape)
    gan_model.real_data.shape.assert_is_compatible_with(
        gan_model.generated_data.shape)

    image_list = []
    image_list.extend(
        array_ops.unstack(gan_model.generator_inputs[:num_comparisons]))
    image_list.extend(
        array_ops.unstack(gan_model.generated_data[:num_comparisons]))
    image_list.extend(array_ops.unstack(gan_model.real_data[:num_comparisons]))
    if display_diffs:
        generated_list = array_ops.unstack(
            gan_model.generated_data[:num_comparisons])
        real_list = array_ops.unstack(gan_model.real_data[:num_comparisons])
        diffs = [
            math_ops.abs(
                math_ops.cast(generated, dtypes.float32) -
                math_ops.cast(real, dtypes.float32))
            for generated, real in zip(generated_list, real_list)
        ]
        image_list.extend(diffs)

    # Reshape image and display.
    summary.image('image_comparison',
                  eval_utils.image_reshaper(image_list,
                                            num_cols=num_comparisons),
                  max_outputs=1)
Пример #5
0
    def _build_image(image):
        """Helper function to create a result for each image on the fly."""

        # Expand the first dimension as batch_size = 1.
        images = array_ops.expand_dims(image, axis=0)

        # Tile the image num_domains times, so we can get all transformed together.
        images = array_ops.tile(images, [num_domains, 1, 1, 1])

        # Create the targets to 0, 1, 2, ..., num_domains-1.
        targets = array_ops.one_hot(list(range(num_domains)), num_domains)

        with variable_scope.variable_scope(stargan_model.generator_scope,
                                           reuse=True):

            # Add the original image.
            output_images_list = [image]

            # Generate the image and add to the list.
            gen_images = stargan_model.generator_fn(images, targets)
            gen_images_list = array_ops.split(gen_images, num_domains)
            gen_images_list = [
                array_ops.squeeze(img, axis=0) for img in gen_images_list
            ]
            output_images_list.extend(gen_images_list)

            # Display diffs.
            if display_diffs:
                diff_images = gen_images - images
                diff_images_list = array_ops.split(diff_images, num_domains)
                diff_images_list = [
                    array_ops.squeeze(img, axis=0) for img in diff_images_list
                ]
                output_images_list.append(array_ops.zeros_like(image))
                output_images_list.extend(diff_images_list)

            # Create the final image.
            final_image = eval_utils.image_reshaper(output_images_list,
                                                    num_cols=num_domains + 1)

        # Reduce the first rank.
        return array_ops.squeeze(final_image, axis=0)
Пример #6
0
  def _build_image(image):
    """Helper function to create a result for each image on the fly."""

    # Expand the first dimension as batch_size = 1.
    images = array_ops.expand_dims(image, axis=0)

    # Tile the image num_domains times, so we can get all transformed together.
    images = array_ops.tile(images, [num_domains, 1, 1, 1])

    # Create the targets to 0, 1, 2, ..., num_domains-1.
    targets = array_ops.one_hot(list(range(num_domains)), num_domains)

    with variable_scope.variable_scope(
        stargan_model.generator_scope, reuse=True):

      # Add the original image.
      output_images_list = [image]

      # Generate the image and add to the list.
      gen_images = stargan_model.generator_fn(images, targets)
      gen_images_list = array_ops.split(gen_images, num_domains)
      gen_images_list = [
          array_ops.squeeze(img, axis=0) for img in gen_images_list
      ]
      output_images_list.extend(gen_images_list)

      # Display diffs.
      if display_diffs:
        diff_images = gen_images - images
        diff_images_list = array_ops.split(diff_images, num_domains)
        diff_images_list = [
            array_ops.squeeze(img, axis=0) for img in diff_images_list
        ]
        output_images_list.append(array_ops.zeros_like(image))
        output_images_list.extend(diff_images_list)

      # Create the final image.
      final_image = eval_utils.image_reshaper(
          output_images_list, num_cols=num_domains + 1)

    # Reduce the first rank.
    return array_ops.squeeze(final_image, axis=0)
Пример #7
0
    def _add_comparison_summary(gan_model, reconstructions):
        image_list = [
            gan_model.generator_inputs[0, :, :, 0],
            gan_model.generated_data[0, :, :, 0], reconstructions[0, :, :, 0]
        ]
        image_list = [tf.expand_dims(x, axis=-1) for x in image_list]
        image_diff_list = [
            image_list[1] - image_list[0], image_list[2] - image_list[1],
            image_list[2] - image_list[0]
        ]

        print('image', image_list[0].get_shape())
        summary_list = list(image_list)
        summary_list.extend(image_diff_list)
        if include_masks:
            image_list_masks = [
                gan_model.generator_inputs[0, :, :, 1],
                gan_model.generated_data[0, :, :, 1], reconstructions[0, :, :,
                                                                      1]
            ]
            image_list_masks = [
                tf.expand_dims(x, axis=-1) for x in image_list_masks
            ]
            image_mask_diff_list = [
                image_list_masks[1] - image_list_masks[0],
                image_list_masks[2] - image_list_masks[1],
                image_list_masks[2] - image_list_masks[0]
            ]
            print('mask', image_list_masks[0].get_shape())
            summary_list.extend(image_list_masks)
            summary_list.extend(image_mask_diff_list)

        tf.summary.image('image_comparison',
                         eval_utils.image_reshaper(summary_list,
                                                   num_cols=len(image_list)),
                         max_outputs=1)