Ejemplo n.º 1
0
def nst(
    content_image: torch.Tensor,
    style_image: torch.Tensor,
    impl_params: bool = True,
    hyper_parameters: Optional[HyperParameters] = None,
    quiet: bool = False,
) -> torch.Tensor:
    r"""NST from :cite:`LW2016`.

    Args:
        content_image: Content image for the NST.
        style_image: Style image for the NST.
        impl_params: Switch the behavior and hyper-parameters between the reference
            implementation of the original authors and what is described in the paper.
            For details see :ref:`here <li_wand_2016-impl_params>`.
        hyper_parameters: If omitted,
            :func:`~pystiche_papers.li_wand_2016.hyper_parameters` is used.
        quiet: If ``True``, not information is logged during the optimization. Defaults
            to ``False``.
    """
    if hyper_parameters is None:
        hyper_parameters = _hyper_parameters(impl_params=impl_params)

    device = content_image.device

    criterion = perceptual_loss(impl_params=impl_params,
                                hyper_parameters=hyper_parameters)
    criterion = criterion.to(device)

    image_pyramid = _image_pyramid(hyper_parameters=hyper_parameters,
                                   resize_targets=(criterion, ))

    initial_resize = image_pyramid[-1].resize_image
    content_image = initial_resize(content_image)
    style_image = initial_resize(style_image)

    input_image = misc.get_input_image(
        starting_point=hyper_parameters.nst.starting_point,
        content_image=content_image)

    preprocessor = _preprocessor().to(device)
    postprocessor = _postprocessor().to(device)

    criterion.set_content_image(preprocessor(content_image))
    criterion.set_style_image(preprocessor(style_image))

    return optim.pyramid_image_optimization(
        input_image,
        criterion,
        image_pyramid,
        get_optimizer=optimizer,
        preprocessor=preprocessor,
        postprocessor=postprocessor,
        quiet=quiet,
    )
Ejemplo n.º 2
0
def test_default_image_pyramid_optim_loop(optim_asset_loader):
    asset = optim_asset_loader("default_image_pyramid_optim_loop")

    actual = optim.pyramid_image_optimization(
        asset.input.image,
        asset.input.perceptual_loss,
        asset.input.pyramid,
        get_optimizer=asset.params.get_optimizer,
        quiet=True,
    )
    desired = asset.output.image
    ptu.assert_allclose(actual, desired, rtol=1e-4)
Ejemplo n.º 3
0
def test_default_image_pyramid_optim_loop_processing(optim_asset_loader):
    asset = optim_asset_loader("default_image_pyramid_optim_loop")

    actual = optim.pyramid_image_optimization(
        asset.input.image,
        asset.input.criterion,
        asset.input.pyramid,
        get_optimizer=asset.params.get_optimizer,
        preprocessor=asset.params.preprocessor,
        postprocessor=asset.params.postprocessor,
        quiet=True,
    )
    desired = asset.output.image
    ptu.assert_allclose(actual, desired, rtol=1e-4)
Ejemplo n.º 4
0
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)

start_with_pyramid = time.time()
output_image = optim.pyramid_image_optimization(input_image, criterion,
                                                image_pyramid)
stop_with_pyramid = time.time()

# sphinx_gallery_thumbnail_number = 5
show_image(output_image, title="Output image with pyramid")

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

elapsed_time_with_pyramid = stop_with_pyramid - start_with_pyramid
relative_decrease = 1.0 - elapsed_time_with_pyramid / elapsed_time_without_pyramid
print(
    f"With pyramid the optimization took {elapsed_time_with_pyramid:.0f} seconds. "
    f"This is a {relative_decrease:.0%} decrease.")

########################################################################################
# With the coarse-to-fine architecture of the image pyramid, the stylization of the
Ejemplo n.º 5
0
def guided_nst(
    content_image: torch.Tensor,
    content_guides: Dict[str, torch.Tensor],
    style_images_and_guides: Dict[str, Tuple[torch.Tensor, torch.Tensor]],
    impl_params: bool = True,
    hyper_parameters: Optional[HyperParameters] = None,
    quiet: bool = False,
) -> torch.Tensor:
    r"""Guided NST from :cite:`GEB+2017`.

    Args:
        content_image: Content image for the guided NST.
        content_guides: Content image guides for the guided NST.
        style_images_and_guides: Dictionary with the style images and the corresponding
            guides for each region.
        impl_params: Switch the behavior and hyper-parameters between the reference
            implementation of the original authors and what is described in the paper.
            For details see :ref:`here <gatys_et_al_2017-impl_params>`.
        hyper_parameters: If omitted,
            :func:`~pystiche_papers.gatys_et_al_2017.hyper_parameters` is used.
        quiet: If ``True``, not information is logged during the optimization. Defaults
            to ``False``.
    """
    regions = set(content_guides.keys())
    if regions != set(style_images_and_guides.keys()):
        # FIXME
        raise RuntimeError
    regions = sorted(regions)

    if hyper_parameters is None:
        hyper_parameters = _hyper_parameters()

    device = content_image.device

    criterion = guided_perceptual_loss(regions,
                                       impl_params=impl_params,
                                       hyper_parameters=hyper_parameters)
    criterion = criterion.to(device)

    image_pyramid = _image_pyramid(hyper_parameters=hyper_parameters,
                                   resize_targets=(criterion, ))
    initial_image_resize = image_pyramid[-1].resize_image
    initial_guide_resize = image_pyramid[-1].resize_guide

    content_image = initial_image_resize(content_image)
    content_guides = {
        region: initial_guide_resize(guide)
        for region, guide in content_guides.items()
    }
    style_images_and_guides = {
        region: (initial_image_resize(image), initial_guide_resize(guide))
        for region, (image, guide) in style_images_and_guides.items()
    }
    input_image = misc.get_input_image(starting_point="content",
                                       content_image=content_image)

    preprocessor = _preprocessor().to(device)
    postprocessor = _postprocessor().to(device)

    criterion.set_content_image(preprocessor(content_image))
    for region, guide in content_guides.items():
        criterion.set_content_guide(guide, region=region)

    for region, (image, guide) in style_images_and_guides.items():
        criterion.set_style_image(preprocessor(image),
                                  guide=guide,
                                  region=region)

    return optim.pyramid_image_optimization(
        input_image,
        criterion,
        image_pyramid,
        get_optimizer=optimizer,
        preprocessor=preprocessor,
        postprocessor=postprocessor,
        quiet=quiet,
    )
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)

start_with_pyramid = time.time()
output_image = optim.pyramid_image_optimization(input_image, perceptual_loss,
                                                image_pyramid)
stop_with_pyramid = time.time()

# sphinx_gallery_thumbnail_number = 5
show_image(output_image, title="Output image with pyramid")

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

elapsed_time_with_pyramid = stop_with_pyramid - start_with_pyramid
relative_decrease = 1.0 - elapsed_time_with_pyramid / elapsed_time_without_pyramid
print(
    f"With pyramid the optimization took {elapsed_time_with_pyramid:.0f} seconds. "
    f"This is a {relative_decrease:.0%} decrease.")

########################################################################################
# With the coarse-to-fine architecture of the image pyramid, the stylization of the