def test_HyperParameters_iter(params): hyper_parameters = HyperParameters(sub_param=HyperParameters(**params), **params) names = tuple(iter(hyper_parameters)) assert set(names[:-1]) == set(params.keys()) assert names[-1] == "sub_param"
def test_HyperParameters_deepcopy(): x = HyperParameters(param=object(), sub_param=HyperParameters(param=object())) y = deepcopy(x) assert isinstance(y, HyperParameters) assert y is not x assert y.param is not x.param assert y.sub_param is not x.sub_param assert y.sub_param.param is not x.sub_param.param
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
def test_HyperParameters_named_children(params): hyper_parameters = HyperParameters(**params) named_children = tuple(hyper_parameters.named_children()) assert not named_children name = tuple(params.keys())[0] val = HyperParameters() setattr(hyper_parameters, name, val) named_children = tuple(hyper_parameters.named_children()) assert len(named_children) == 1 assert named_children[0][0] == name assert named_children[0][1] is val
def hyper_parameters(impl_params: bool = True) -> HyperParameters: r"""Hyper parameters from :cite:`GEB2016`.""" # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [8] 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) # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [8] style_loss_layer_weights = (compute_layer_weights(style_loss_layers) if impl_params else "mean") return HyperParameters( content_loss=HyperParameters( # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [8] layer=f"{'relu' if impl_params else 'conv'}4_2", # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [8] score_weight=1e0, ), style_loss=HyperParameters( layers=style_loss_layers, layer_weights=style_loss_layer_weights, # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [8] score_weight=1e3, ), nst=HyperParameters( # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [9] num_steps=500, # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [6] starting_point="content" if impl_params else "random", # https://github.com/pmeier/PytorchNeuralStyleTransfer/blob/master/NeuralStyleTransfer.ipynb # Cell [4] image_size=512, ), )
def hyper_parameters() -> HyperParameters: r"""Hyper parameters from :cite:`JAL2016`.""" return HyperParameters( content_loss=HyperParameters( layer="relu2_2", # The paper reports no score weight so we go with the default value of the # implementation instead # https://github.com/pmeier/fast-neural-style/blob/813c83441953ead2adb3f65f4cc2d5599d735fa7/train.lua#L36 score_weight=1e0, ), style_loss=HyperParameters( layers=("relu1_2", "relu2_2", "relu3_3", "relu4_3"), layer_weights="sum", # The paper reports no style score weight so we go with the default value # of the implementation instead # https://github.com/pmeier/fast-neural-style/blob/813c83441953ead2adb3f65f4cc2d5599d735fa7/train.lua#L43 score_weight=5e0, ), regularization=HyperParameters( # The paper reports a range of regularization score weights so we go with # the default value of the implementation instead # https://github.com/pmeier/fast-neural-style/blob/813c83441953ead2adb3f65f4cc2d5599d735fa7/train.lua#L33 score_weight=1e-6, ), content_transform=HyperParameters(image_size=(256, 256)), style_transform=HyperParameters(edge_size=256), batch_sampler=HyperParameters(num_batches=40000, batch_size=4), )
def test_HyperParameters_delattr(params): hyper_parameters = HyperParameters(**params) for name in params.keys(): getattr(hyper_parameters, name) delattr(hyper_parameters, name) with pytest.raises(AttributeError): getattr(hyper_parameters, name)
def hyper_parameters(impl_params: bool = True) -> HyperParameters: r"""Hyper parameters from :cite:`LW2016`. Args: impl_params: Switch the behavior and hyper-parameters between the reference implementation of the original authors and what is described in the paper. For details see :ref:`here <li_wand_2016-impl_params>`. """ return HyperParameters( content_loss=HyperParameters( # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L57 layer="relu4_1" if impl_params else "relu4_2", # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L58 score_weight=2e1 if impl_params else 1e0, ), target_transforms=HyperParameters( # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L52 num_scale_steps=0 if impl_params else 3, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L67 scale_step_width=5e-2, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L51 num_rotate_steps=0 if impl_params else 2, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L66 rotate_step_width=7.5, ), style_loss=HyperParameters( # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L48 layers=("relu3_1", "relu4_1"), layer_weights="sum", # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L50 patch_size=3, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L53 stride=2 if impl_params else 1, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L49 score_weight=1e-4 if impl_params else 1e0, ), regularization=HyperParameters( # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L59 score_weight=1e-3), image_pyramid=HyperParameters( # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L40 max_edge_size=384, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L44 num_steps=100 if impl_params else 200, # https://github.com/pmeier/CNNMRF/blob/fddcf4d01e2a6ce201059d8bc38597f74a09ba3f/cnnmrf.lua#L43 num_levels=3 if impl_params else None, min_edge_size=64, edge="long", ), nst=HyperParameters( starting_point="content" if impl_params else "random"), )
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), ), )
def test_HyperParameters_getattr_unknown_attribute(): hyper_parameters = HyperParameters() with pytest.raises(AttributeError): getattr(hyper_parameters, "unknown_attribute")
def hyper_parameters(impl_params: bool = True, instance_norm: bool = True) -> HyperParameters: r"""Hyper parameters from :cite:`ULVL2016,UVL2017`.""" # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L44 style_loss_layers = (("relu1_1", "relu2_1", "relu3_1", "relu4_1") if impl_params and instance_norm else ("relu1_1", "relu2_1", "relu3_1", "relu4_1", "relu5_1")) return HyperParameters( content_loss=HyperParameters( layer="relu4_2", # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L22 score_weight=6e-1 if impl_params and not instance_norm else 1e0, ), style_loss=HyperParameters( # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L44 layers=style_loss_layers, # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L23 # The backward pass of the GramLoss'es in style_loss is manipulated and # this is not independent of the score_weight. For this reason the # layer_weights are set here to have the correct score_weight in the # individual GramLoss'es. layer_weights=( [1e3 if impl_params and not instance_norm else 1e0] * len(style_loss_layers)), score_weight=1e0, ), content_transform=HyperParameters(edge_size=256, ), style_transform=HyperParameters( edge_size=256, interpolation=InterpolationMode.BICUBIC # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L152 if impl_params and instance_norm # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/src/descriptor_net.lua#L17 else InterpolationMode.BILINEAR, ), # The number of iterations is split up into multiple epochs with # corresponding num_batches: num_batches=( # 50000 = 25 * 2000 # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L48 2000 if instance_norm # 3000 = 10 * 300 # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L30 else 300) if impl_params else 200, batch_size=(( # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L50 1 if instance_norm # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L32 else 4) if impl_params else 16), optimizer=HyperParameters( # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L29 lr=1e-3 if impl_params and instance_norm else 1e-1, ), lr_scheduler=HyperParameters( # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L260 # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L201 lr_decay=0.8 if impl_params else 0.7, delay=0 if impl_params else 4, ), # The number of iterations is split up into multiple epochs with # corresponding num_batches: num_epochs=( # 50000 = 25 * 2000 # https://github.com/pmeier/texture_nets/blob/aad2cc6f8a998fedc77b64bdcfe1e2884aa0fb3e/train.lua#L48 25 if impl_params and instance_norm # 3000 = 10 * 300 / 2000 = 10 * 200 # https://github.com/pmeier/texture_nets/blob/b2097eccaec699039038970b191780f97c238816/stylization_train.lua#L30 else 10), )
def test_HyperParameters_len(params): hyper_parameters = HyperParameters(sub_param=HyperParameters(**params), **params) assert len(hyper_parameters) == 1 + len(params)
def test_HyperParameters_dict_unpacking(params): hyper_parameters = HyperParameters(**params) assert dict(hyper_parameters) == params assert {**hyper_parameters} == params
def test_HyperParameters_getitem(params): hyper_parameters = HyperParameters(sub_param=HyperParameters(**params), **params) for name in (*params.keys(), "sub_param"): assert hyper_parameters[name] is getattr(hyper_parameters, name)
def test_HyperParameters_delattr_unknown_attr(params): hyper_parameters = HyperParameters() with pytest.raises(AttributeError): delattr(hyper_parameters, "unknown_attribute")
def test_HyperParameters_properties(params): hyper_parameters = HyperParameters(**params) assert dict(hyper_parameters.properties()) == params
def test_HyperParameters_getattr(params): hyper_parameters = HyperParameters(**params) for name, val in params.items(): assert getattr(hyper_parameters, name) == val