示例#1
0
def test_HyperParameters_new_similar(subtests, params):
    x = HyperParameters(**params)
    new = -1
    y = x.new_similar(a=new)

    assert isinstance(y, HyperParameters)

    with subtests.test("old"):
        del params["a"]
        for name, value in params.items():
            assert getattr(y, name) == value

    with subtests.test("new"):
        assert y.a == new
示例#2
0
def hyper_parameters(impl_params: bool = True) -> HyperParameters:
    r"""Hyper parameters from :cite:`GEB+2017`."""
    # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
    # Cell [3] / layers['style']
    style_loss_layers: Tuple[str, ...] = (
        "conv1_1",
        "conv2_1",
        "conv3_1",
        "conv4_1",
        "conv5_1",
    )
    if impl_params:
        style_loss_layers = tuple(
            layer.replace("conv", "relu") for layer in style_loss_layers
        )
    style_loss = HyperParameters(
        layers=style_loss_layers,
        # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
        # Cell [3] / weights['style']
        layer_weights=compute_layer_weights(style_loss_layers),
        # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
        # Cell [3] / sw
        score_weight=1e3,
    )

    return HyperParameters(
        content_loss=HyperParameters(
            # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
            # Cell [3] / layers['content']
            layer="relu4_2" if impl_params else "conv4_2",
            # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
            # Cell [3] / cw
            score_weight=1e0,
        ),
        style_loss=style_loss,
        guided_style_loss=style_loss.new_similar(
            # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/SpatialControl.ipynb
            # TODO: find the cell where this is performed
            region_weights="sum"
        ),
        image_pyramid=HyperParameters(
            # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
            # Cell [3] / img_size, hr_img_size
            edge_sizes=(512 if impl_params else 500, 1024),
            # https://github.com/pmeier/NeuralImageSynthesis/blob/cced0b978fe603569033b2c7f04460839e4d82c4/ExampleNotebooks/BasicStyleTransfer.ipynb
            # Cell [3] / max_iter, hr_max_iter
            num_steps=(500, 200),
        ),
    )