Exemple #1
0
    def test_ImagePyramid_iter_resize(self):
        class TestOperator(PixelComparisonOperator):
            def target_image_to_repr(self, image):
                return image, None

            def input_image_to_repr(self, image, ctx):
                pass

            def calculate_score(self, input_repr, target_repr, ctx):
                pass

        initial_image_size = (5, 4)
        edge_sizes = (2, 4)

        torch.manual_seed(0)
        target_guide = torch.rand((1, 3, *initial_image_size))
        target_image = torch.rand((1, 3, *initial_image_size))
        input_guide = torch.rand((1, 3, *initial_image_size))

        aspect_ratio = calculate_aspect_ratio(initial_image_size)
        image_sizes = [
            edge_to_image_size(edge_size, aspect_ratio) for edge_size in edge_sizes
        ]

        op = TestOperator()
        op.set_target_guide(target_guide)
        op.set_target_image(target_image)
        op.set_input_guide(input_guide)

        image_pyramid = pyramid.ImagePyramid(edge_sizes, 1, resize_targets=(op,))
        for pyramid_level, image_size in zip(image_pyramid, image_sizes):
            for attr in ("target_guide", "target_image", "input_guide"):
                with self.subTest(attr, pyramid_level=pyramid_level):
                    actual = extract_image_size(getattr(op, attr))
                    desired = image_size
                    self.assertTupleEqual(actual, desired)
def image_pyramid(
    hyper_parameters: Optional[HyperParameters] = None,
    **image_pyramid_kwargs: Any,
) -> pyramid.ImagePyramid:
    r"""Image pyramid from :cite:`GEB+2017`.

    Args:
        hyper_parameters: If omitted,
            :func:`~pystiche_papers.gatys_et_al_2017.hyper_parameters` is used.
        **image_pyramid_kwargs: Additional parameters of a
            :class:`pystiche.pyramid.ImagePyramid`.

    .. seealso::

        - :class:`pystiche.pyramid.ImagePyramid`
    """
    if hyper_parameters is None:
        hyper_parameters = _hyper_parameters()

    return pyramid.ImagePyramid(
        hyper_parameters.image_pyramid.edge_sizes,
        hyper_parameters.image_pyramid.num_steps,
        **image_pyramid_kwargs,
    )
Exemple #3
0
#
#   By default the ``edge_sizes`` correspond to the shorter ``edge`` of the images. To
#   change that you can pass ``edge="long"``. For fine-grained control you can also
#   pass a sequence comprising ``"short"`` and ``"long"`` to select the ``edge`` for
#   each level separately. Its length has to match the length of ``edge_sizes``.
#
# .. note::
#
#   For a fine-grained control over the number of steps on each level you can pass a
#   sequence to select the ``num_steps`` for each level separately. Its length has to
#   match the length of ``edge_sizes``.

edge_sizes = (250, 500)
num_steps = 200
image_pyramid = pyramid.ImagePyramid(edge_sizes,
                                     num_steps,
                                     resize_targets=(criterion, ))
print(image_pyramid)

########################################################################################
# With a pyramid the NST is performed by
# :func:`~pystiche.optim.pyramid_image_optimization`. We time the execution and show
# the result afterwards.
#
# .. note::
#
#   We regenerate the ``input_image`` since it was changed inplace during the first
#   optimization.

input_image = get_input_image(starting_point, content_image=content_image)
#
#   By default the ``edge_sizes`` correspond to the shorter ``edge`` of the images. To
#   change that you can pass ``edge="long"``. For fine-grained control you can also
#   pass a sequence comprising ``"short"`` and ``"long"`` to select the ``edge`` for
#   each level separately. Its length has to match the length of ``edge_sizes``.
#
# .. note::
#
#   For a fine-grained control over the number of steps on each level you can pass a
#   sequence to select the ``num_steps`` for each level separately. Its length has to
#   match the length of ``edge_sizes``.

edge_sizes = (250, 500)
num_steps = 200
image_pyramid = pyramid.ImagePyramid(edge_sizes,
                                     num_steps,
                                     resize_targets=(perceptual_loss, ))
print(image_pyramid)

########################################################################################
# With a pyramid the NST is performed by
# :func:`~pystiche.optim.pyramid_image_optimization`. We time the execution and show
# the result afterwards.
#
# .. note::
#
#   We regenerate the ``input_image`` since it was changed inplace during the first
#   optimization.

input_image = get_input_image(starting_point, content_image=content_image)