Ejemplo n.º 1
0
def test_hyper_parameters_style_loss(subtests, impl_params):
    hyper_parameters = paper.hyper_parameters(impl_params=impl_params)

    sub_params = "style_loss"
    assert sub_params in hyper_parameters
    hyper_parameters = getattr(hyper_parameters, sub_params)

    if impl_params:
        layers, num_channels = zip(
            ("relu1_1", 64),
            ("relu2_1", 128),
            ("relu3_1", 256),
            ("relu4_1", 512),
            ("relu5_1", 512),
        )
        layer_weights = pytest.approx([1 / n**2 for n in num_channels])
    else:
        layers = ("conv1_1", "conv2_1", "conv3_1", "conv4_1", "conv5_1")
        layer_weights = "mean"

    with subtests.test("layer"):
        assert hyper_parameters.layers == layers

    with subtests.test("layer_weights"):
        assert hyper_parameters.layer_weights == layer_weights

    with subtests.test("score_weight"):
        assert hyper_parameters.score_weight == pytest.approx(1e3)
Ejemplo n.º 2
0
def test_nst_smoke(subtests, mocker, content_image, style_image):
    mock = mocker.patch("pystiche.optim.image_optimization")

    paper.nst(content_image, style_image)

    args, kwargs = mock.call_args
    input_image, criterion = args
    optimizer = kwargs["optimizer"]
    num_steps = kwargs["num_steps"]
    preprocessor = kwargs["preprocessor"]
    postprocessor = kwargs["postprocessor"]

    hyper_parameters = paper.hyper_parameters().nst

    with subtests.test("input_image"):
        ptu.assert_allclose(input_image, content_image)

    with subtests.test("criterion"):
        assert isinstance(criterion, type(paper.perceptual_loss()))

    with subtests.test("optimizer"):
        assert optimizer is paper.optimizer

    with subtests.test("num_steps"):
        assert num_steps == hyper_parameters.num_steps

    with subtests.test("preprocessor"):
        assert isinstance(preprocessor, type(paper.preprocessor()))

    with subtests.test("postprocessor"):
        assert isinstance(postprocessor, type(paper.postprocessor()))
Ejemplo n.º 3
0
def test_content_loss(subtests):
    content_loss = paper.content_loss()
    assert isinstance(content_loss, paper.FeatureReconstructionLoss)

    hyper_parameters = paper.hyper_parameters().content_loss

    with subtests.test("layer"):
        assert content_loss.encoder.layer == hyper_parameters.layer

    with subtests.test("score_weight"):
        assert content_loss.score_weight == pytest.approx(
            hyper_parameters.score_weight)
Ejemplo n.º 4
0
def test_hyper_parameters_content_loss(subtests, impl_params):
    hyper_parameters = paper.hyper_parameters(impl_params=impl_params)

    sub_params = "content_loss"
    assert sub_params in hyper_parameters
    hyper_parameters = getattr(hyper_parameters, sub_params)

    with subtests.test("layer"):
        assert hyper_parameters.layer == "relu4_2" if impl_params else "conv4_2"

    with subtests.test("score_weight"):
        assert hyper_parameters.score_weight == pytest.approx(1e0)
Ejemplo n.º 5
0
def figure_3(args):
    images = paper.images()
    images.download(args.image_source_dir)

    hyper_parameters = paper.hyper_parameters(args.impl_params)

    content_image = images["neckarfront"].read(
        size=hyper_parameters.nst.image_size, device=args.device)
    style_image = images["composition_vii"].read(
        size=hyper_parameters.nst.image_size, device=args.device)

    style_layers = hyper_parameters.style_loss.layers
    layer_configs = [
        style_layers[:idx + 1] for idx in range(len(style_layers))
    ]

    score_weights = (1e5, 1e4, 1e3, 1e2)

    params = "implementation" if args.impl_params else "paper"
    for layers, score_weight in itertools.product(layer_configs,
                                                  score_weights):
        row_label = layers[-1]
        column_label = f"{1.0 / score_weight:.0e}"
        print(
            f"Replicating Figure 3 image in row {row_label} and column {column_label} "
            f"with {params} parameters.")
        hyper_parameters.style_loss.layers = layers
        if args.impl_params:
            hyper_parameters.style_loss.layer_weights = paper.compute_layer_weights(
                layers)
        hyper_parameters.style_loss.score_weight = score_weight

        output_image = paper.nst(
            content_image,
            style_image,
            impl_params=args.impl_params,
            hyper_parameters=hyper_parameters,
        )

        filename = utils.make_output_filename(
            "gatys_ecker_betghe_2016",
            "fig_3",
            row_label,
            column_label,
            impl_params=args.impl_params,
        )
        save_result(
            output_image,
            path.join(args.image_results_dir, filename),
        )
Ejemplo n.º 6
0
def test_hyper_parameters_nst(subtests, impl_params):
    hyper_parameters = paper.hyper_parameters(impl_params=impl_params)

    sub_params = "nst"
    assert sub_params in hyper_parameters
    hyper_parameters = getattr(hyper_parameters, sub_params)

    with subtests.test("num_steps"):
        assert hyper_parameters.num_steps == 500

    with subtests.test("starting_point"):
        assert hyper_parameters.starting_point == "content" if impl_params else "random"

    with subtests.test("image_size"):
        assert hyper_parameters.image_size == 512
Ejemplo n.º 7
0
def figure_2(args):
    images = paper.images()
    images.download(args.image_source_dir)

    hyper_parameters = paper.hyper_parameters()

    content_image = images["neckarfront"].read(
        size=hyper_parameters.nst.image_size, device=args.device)

    class StyleImage:
        def __init__(self, label, image, score_weight):
            self.label = label
            self.image = image.read(size=hyper_parameters.nst.image_size,
                                    device=args.device)
            self.score_weight = score_weight

    style_images = (
        StyleImage("B", images["shipwreck"], 1e3),
        StyleImage("C", images["starry_night"], 1e3),
        StyleImage("D", images["the_scream"], 1e3),
        StyleImage("E", images["femme_nue_assise"], 1e4),
        StyleImage("F", images["composition_vii"], 1e4),
    )

    for style_image in style_images:
        print(f"Replicating Figure 2 {style_image.label}")
        hyper_parameters.style_loss.score_weight = style_image.score_weight

        output_image = paper.nst(
            content_image,
            style_image.image,
            impl_params=args.impl_params,
            hyper_parameters=hyper_parameters,
        )
        filename = utils.make_output_filename(
            "gatys_ecker_betghe_2016",
            "fig_2",
            style_image.label,
            impl_params=args.impl_params,
        )
        save_result(
            output_image,
            path.join(args.image_results_dir, filename),
        )
Ejemplo n.º 8
0
def test_style_loss(subtests):
    style_loss = paper.style_loss()
    assert isinstance(style_loss, pystiche.loss.MultiLayerEncodingLoss)

    hyper_parameters = paper.hyper_parameters().style_loss

    with subtests.test("encoding_losses"):
        assert all(
            isinstance(loss, pystiche.loss.GramLoss)
            for loss in style_loss.children())

    layers, layer_weights = zip(*[(loss.encoder.layer, loss.score_weight)
                                  for loss in style_loss.children()])
    with subtests.test("layers"):
        assert layers == hyper_parameters.layers

    with subtests.test("layer_weights"):
        assert layer_weights == pytest.approx(hyper_parameters.layer_weights)

    with subtests.test("score_weight"):
        assert style_loss.score_weight == pytest.approx(
            hyper_parameters.score_weight)
Ejemplo n.º 9
0
def test_hyper_parameters(impl_params):
    hyper_parameters = paper.hyper_parameters(impl_params=impl_params)
    assert isinstance(hyper_parameters, HyperParameters)