Esempio n. 1
0
def johnson_alahi_li_2016_style_loss(
    impl_params: bool = True,
    instance_norm: bool = True,
    style: Optional[str] = None,
    multi_layer_encoder: Optional[MultiLayerEncoder] = None,
    layers: Optional[Sequence[str]] = None,
    layer_weights: Union[str, Sequence[float]] = "sum",
    score_weight: float = 5e0,
    **gram_op_kwargs,
):
    if multi_layer_encoder is None:
        multi_layer_encoder = johnson_alahi_li_2016_multi_layer_encoder(
            impl_params=impl_params)

    if layers is None:
        layers = ("relu1_2", "relu2_2", "relu3_3", "relu4_3")

    if score_weight is None:
        score_weight = get_style_score_weight(impl_params,
                                              instance_norm,
                                              style=style)

    def get_encoding_op(encoder, layer_weight):
        return JohnsonAlahiLi2016GramOperator(encoder,
                                              score_weight=layer_weight,
                                              **gram_op_kwargs)

    return MultiLayerEncodingOperator(
        multi_layer_encoder,
        layers,
        get_encoding_op,
        layer_weights=layer_weights,
        score_weight=score_weight,
    )
Esempio n. 2
0
def get_region_op(region, region_weight):
    return MultiLayerEncodingOperator(
        multi_layer_encoder,
        style_layers,
        get_style_op,
        score_weight=region_weight,
    )
Esempio n. 3
0
def li_wand_2016_style_loss(
    impl_params: bool = True,
    multi_layer_encoder: Optional[MultiLayerEncoder] = None,
    layers: Optional[Sequence[str]] = None,
    layer_weights: Union[str, Sequence[float]] = "sum",
    patch_size: Union[int, Tuple[int, int]] = 3,
    stride: Optional[Union[int, Tuple[int, int]]] = None,
    num_scale_steps: Optional[int] = None,
    scale_step_width: float = 5e-2,
    num_rotation_steps: Optional[int] = None,
    rotation_step_width: float = 7.5,
    score_weight: Optional[float] = None,
):
    if multi_layer_encoder is None:
        multi_layer_encoder = li_wand_2016_multi_layer_encoder()

    if layers is None:
        layers = ("relu3_1", "relu4_1")

    if stride is None:
        stride = 2 if impl_params else 1

    if num_scale_steps is None:
        num_scale_steps = 1 if impl_params else 3

    if num_rotation_steps is None:
        num_rotation_steps = 1 if impl_params else 2

    def get_encoding_op(encoder, layer_weight):
        return LiWand2016MRFOperator(
            encoder,
            patch_size,
            impl_params=impl_params,
            stride=stride,
            num_scale_steps=num_scale_steps,
            scale_step_width=scale_step_width,
            num_rotation_steps=num_rotation_steps,
            rotation_step_width=rotation_step_width,
            score_weight=layer_weight,
        )

    if score_weight is None:
        score_weight = 1e-4 if impl_params else 1e0

    return MultiLayerEncodingOperator(
        multi_layer_encoder,
        layers,
        get_encoding_op,
        layer_weights=layer_weights,
        score_weight=score_weight,
    )
Esempio n. 4
0
def ulyanov_et_al_2016_style_loss(
    impl_params: bool = True,
    instance_norm: bool = True,
    stylization: bool = True,
    multi_layer_encoder: Optional[MultiLayerEncoder] = None,
    layers: Optional[Sequence[str]] = None,
    layer_weights: Union[str, Sequence[float]] = "sum",
    score_weight: float = None,
    **gram_op_kwargs,
):
    if score_weight is None:
        if impl_params:
            if instance_norm:
                score_weight = 1e0
            else:
                score_weight = 1e3 if stylization else 1e0
        else:
            score_weight = 1e0

    if multi_layer_encoder is None:
        multi_layer_encoder = ulyanov_et_al_2016_multi_layer_encoder()

    if layers is None:
        if impl_params and instance_norm:
            layers = ("relu_1_1", "relu_2_1", "relu_3_1", "relu_4_1")
        else:
            layers = ("relu_1_1", "relu_2_1", "relu_3_1", "relu_4_1",
                      "relu_5_1")

    def get_encoding_op(encoder, layer_weight):
        return UlyanovEtAl2016GramOperator(encoder,
                                           score_weight=layer_weight,
                                           **gram_op_kwargs)

    return MultiLayerEncodingOperator(
        multi_layer_encoder,
        layers,
        get_encoding_op,
        layer_weights=layer_weights,
        score_weight=score_weight,
    )
style_layers = ("relu3_1", "relu4_1")
style_weight = 2e0


def get_style_op(encoder, layer_weight):
    patch_size = 3
    return MRFOperator(encoder,
                       patch_size,
                       stride=2,
                       score_weight=layer_weight)


style_loss = MultiLayerEncodingOperator(
    multi_layer_encoder,
    style_layers,
    get_style_op,
    score_weight=style_weight,
)

criterion = PerceptualLoss(content_loss, style_loss).to(device)
print(criterion)

########################################################################################
# Opposed to the prior examples we want to perform an NST on multiple resolutions. In
# ``pystiche`` this handled by an :class:`~pystiche.pyramid.ImagePyramid` . The
# resolutions are selected by specifying the ``edge_sizes`` of the images on each level
# . The optimization is performed for ``num_steps`` on the different levels.
#
# The resizing of all images, i.e. ``input_image`` and target images (``content_image``
# and ``style_image``) is handled by the ``pyramid``. For that we need to register the
# perceptual loss (``criterion``) as ``resize_targets``.