def test_TotalVariationLoss(input_image):
    loss = paper.TotalVariationLoss()
    actual = loss(input_image)

    desired = F.total_variation_loss(input_image, reduction="sum")

    assert actual == ptu.approx(desired)
Example #2
0
def test_MultiLayerEncodingLoss(subtests, multi_layer_encoder_with_layer,
                                target_image, input_image):
    multi_layer_encoder, layer = multi_layer_encoder_with_layer
    encoder = multi_layer_encoder.extract_encoder(layer)
    target_repr = pystiche.gram_matrix(encoder(target_image), normalize=True)
    input_repr = pystiche.gram_matrix(encoder(input_image), normalize=True)

    configs = ((True, 1.0), (False, 1.0 / 4.0))
    for impl_params, score_correction_factor in configs:
        with subtests.test(impl_params=impl_params):
            loss = paper.MultiLayerEncodingLoss(
                multi_layer_encoder,
                (layer, ),
                lambda encoder, layer_weight: pystiche.loss.GramLoss(
                    encoder, score_weight=layer_weight),
                impl_params=impl_params,
            )
            loss.set_target_image(target_image)
            actual = loss(input_image)

            score = mse_loss(
                input_repr,
                target_repr,
            )
            desired = score * score_correction_factor

            assert actual == ptu.approx(desired)
def test_MRFLoss(subtests, multi_layer_encoder_with_layer, target_image,
                 input_image):
    multi_layer_encoder, layer = multi_layer_encoder_with_layer
    encoder = multi_layer_encoder.extract_encoder(layer)
    target_enc = encoder(target_image)
    input_enc = encoder(input_image)

    patch_size = 3
    stride = 1
    configs = ((True, 1.0 / 2.0), (False, 1.0))
    for (
            impl_params,
            score_correction_factor,
    ) in configs:
        with subtests.test(impl_params=impl_params):

            loss = paper.MRFLoss(encoder,
                                 patch_size,
                                 impl_params=impl_params,
                                 stride=stride)
            loss.set_target_image(target_image)
            actual = loss(input_image)

            extract_patches2d = (paper.extract_normalized_patches2d if
                                 impl_params else pystiche.extract_patches2d)
            target_repr = extract_patches2d(target_enc, patch_size, stride)
            input_repr = extract_patches2d(input_enc, patch_size, stride)

            score = F.mrf_loss(input_repr,
                               target_repr,
                               reduction="sum",
                               batched_input=True)
            desired = score * score_correction_factor

            assert actual == ptu.approx(desired)
def test_TotalVariationLoss(subtests, input_image):
    configs = ((True, 1.0 / 2.0), (False, 1.0))
    for impl_params, score_correction_factor in configs:
        with subtests.test(impl_params=impl_params):
            loss = paper.TotalVariationLoss(impl_params=impl_params, )
            actual = loss(input_image)

            score = F.total_variation_loss(input_image,
                                           exponent=loss.exponent,
                                           reduction="sum")

            desired = score * score_correction_factor

            assert actual == ptu.approx(desired)
def test_FeatureReconstructionLoss(subtests, multi_layer_encoder_with_layer,
                                   target_image, input_image):
    multi_layer_encoder, layer = multi_layer_encoder_with_layer
    encoder = multi_layer_encoder.extract_encoder(layer)
    target_enc = encoder(target_image)
    input_enc = encoder(input_image)

    configs = ((True, "mean"), (False, "sum"))
    for impl_params, loss_reduction in configs:
        with subtests.test(impl_params=impl_params):
            loss = paper.FeatureReconstructionLoss(
                encoder,
                impl_params=impl_params,
            )
            loss.set_target_image(target_image)
            actual = loss(input_image)

            desired = mse_loss(input_enc, target_enc, reduction=loss_reduction)

            assert actual == ptu.approx(desired)
Example #6
0
    def test_main(self, subtests, multi_layer_encoder_with_layer, target_image,
                  input_image):
        multi_layer_encoder, layer = multi_layer_encoder_with_layer
        encoder = multi_layer_encoder.extract_encoder(layer)
        target_enc = encoder(target_image)
        input_enc = encoder(input_image)

        configs = ((True, "mean", 1.0), (False, "sum", 1.0 / 2.0))
        for impl_params, loss_reduction, score_correction_factor in configs:
            with subtests.test(impl_params=impl_params):
                loss = paper.FeatureReconstructionLoss(encoder,
                                                       impl_params=impl_params)
                loss.set_target_image(target_image)
                actual = loss(input_image)

                score = mse_loss(input_enc,
                                 target_enc,
                                 reduction=loss_reduction)
                desired = score * score_correction_factor

                assert actual == ptu.approx(desired)
def test_GramLoss(subtests, multi_layer_encoder_with_layer, target_image,
                  input_image):
    multi_layer_encoder, layer = multi_layer_encoder_with_layer
    encoder = multi_layer_encoder.extract_encoder(layer)

    configs = ((True, True), (False, False))
    for (impl_params, normalize_by_num_channels) in configs:
        with subtests.test(impl_params=impl_params):
            target_repr = pystiche.gram_matrix(encoder(target_image),
                                               normalize=True)
            input_repr = pystiche.gram_matrix(encoder(input_image),
                                              normalize=True)
            intern_target_repr = (target_repr / target_repr.size()[-1] if
                                  normalize_by_num_channels else target_repr)
            intern_input_repr = (input_repr / input_repr.size()[-1]
                                 if normalize_by_num_channels else input_repr)
            loss = paper.GramLoss(encoder, impl_params=impl_params)
            loss.set_target_image(target_image)
            actual = loss(input_image)

            desired = mse_loss(intern_input_repr, intern_target_repr)

            assert actual == ptu.approx(desired, rel=1e-3)
def test_GramLoss(subtests, multi_layer_encoder_with_layer, target_image,
                  input_image):
    multi_layer_encoder, layer = multi_layer_encoder_with_layer
    encoder = multi_layer_encoder.extract_encoder(layer)
    target_repr = pystiche.gram_matrix(encoder(target_image), normalize=True)
    input_repr = pystiche.gram_matrix(encoder(input_image), normalize=True)

    configs = ((True, target_repr.size()[1]), (False, 1.0))
    for impl_params, extra_num_channels_normalization in configs:
        with subtests.test(impl_params=impl_params):
            loss = paper.GramLoss(
                encoder,
                impl_params=impl_params,
            )
            loss.set_target_image(target_image)
            actual = loss(input_image)

            score = mse_loss(
                input_repr,
                target_repr,
            )
            desired = score / extra_num_channels_normalization**2

            assert actual == ptu.approx(desired, rel=1e-3)