Exemplo n.º 1
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)
Exemplo n.º 2
0
def test_gram_matrix_normalize2():
    torch.manual_seed(0)
    tensor_constructors = (torch.ones, torch.rand, torch.randn)

    for constructor in tensor_constructors:
        x = pystiche.gram_matrix(constructor((1, 3, 128, 128)), normalize=True)
        y = pystiche.gram_matrix(constructor((1, 3, 256, 256)), normalize=True)

        ptu.assert_allclose(x, y, atol=2e-2)
Exemplo n.º 3
0
    def test_call(self, encoder):
        torch.manual_seed(0)
        target_image = torch.rand(1, 3, 128, 128)
        input_image = torch.rand(1, 3, 128, 128)

        loss = loss_.GramLoss(encoder)
        loss.set_target_image(target_image)

        actual = loss(input_image)
        desired = F.mse_loss(
            pystiche.gram_matrix(encoder(input_image), normalize=loss.normalize),
            pystiche.gram_matrix(encoder(target_image), normalize=loss.normalize),
        )
        ptu.assert_allclose(actual, desired)
Exemplo n.º 4
0
def test_GramOperator_call():
    torch.manual_seed(0)
    target_image = torch.rand(1, 3, 128, 128)
    input_image = torch.rand(1, 3, 128, 128)
    encoder = enc.SequentialEncoder((nn.Conv2d(3, 3, 1), ))

    op = ops.GramOperator(encoder)
    op.set_target_image(target_image)

    actual = op(input_image)
    desired = mse_loss(
        pystiche.gram_matrix(encoder(input_image), normalize=True),
        pystiche.gram_matrix(encoder(target_image), normalize=True),
    )
    ptu.assert_allclose(actual, desired)
Exemplo n.º 5
0
def test_gram_matrix_normalize1():
    num_channels = 3

    x = torch.ones((1, num_channels, 128, 128))
    y = pystiche.gram_matrix(x, normalize=True)

    actual = y.flatten()
    desired = torch.ones((num_channels**2, ))
    ptu.assert_allclose(actual, desired)
Exemplo n.º 6
0
def test_gram_matrix():
    size = 100

    for dim in (1, 2, 3):
        x = torch.ones((1, 1, *[size] * dim))
        y = pystiche.gram_matrix(x)

        actual = y.item()
        desired = float(size**dim)
        assert actual == desired
Exemplo n.º 7
0
def test_gram_matrix_size():
    batch_size = 1
    num_channels = 3

    torch.manual_seed(0)
    for dim in (1, 2, 3):
        size = (batch_size, num_channels, *torch.randint(256, (dim,)).tolist())
        x = torch.empty(size)
        y = pystiche.gram_matrix(x)

        actual = y.size()
        desired = (batch_size, num_channels, num_channels)
        assert actual == desired
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
 def enc_to_repr(self, enc: torch.Tensor) -> torch.Tensor:
     return pystiche.gram_matrix(enc, normalize=self.normalize)