pystiche.enc

class pystiche.enc.Encoder(named_children=None, indexed_children=None)

ABC for all encoders. Invokes Encoder.forward() if called.

abstract forward(input)

Encodes the given input.

Note

This method has to be overwritten in every subclass.

Return type

Tensor

abstract propagate_guide(guide)

Encodes the given guide.

Note

This method has to be overwritten in every subclass.

Return type

Tensor

class pystiche.enc.SequentialEncoder(modules)

Encoder that operates in sequential manner. Invokes Encoder.forward() if called.

Parameters

modules (Sequence[Module]) – Sequential modules.

class pystiche.enc.MultiLayerEncoder(modules)

Sequential encoder with convenient access to intermediate layers.

Parameters

modules (Sequence[Tuple[str, Module]]) – Named modules that serve as basis for the encoding.

registered_layers

Layers, on which the encodings will be cached during the forward() pass.

__call__(*args, **kwargs)

Invokes forward().

Return type

Any

__contains__(layer)

Is the layer part of the multi-layer encoder?

Parameters

layer (str) – Layer to be checked.

Return type

bool

clear_cache()

Clear the internal cache.

Return type

None

encode(input, layers)

Encode the input on layers.

Parameters
  • input (Tensor) – Input to be encoded.

  • layers (Sequence[str]) – Layers on which the input should be encoded.

Return type

Tuple[Tensor, …]

extract_encoder(layer)

Extract a SingleLayerEncoder for the layer and register it.

Parameters

layer (str) – Layer.

Return type

SingleLayerEncoder

forward(input, layer=None, cache=None, to_cache=None)

Encode the input.

Parameters
  • input (Tensor) – Input to be encoded.

  • layer (Optional[str]) – Layer on which the input should be encoded. If omitted, defaults to the last layer in the multi-layer encoder.

  • cache (Optional[Dict[str, Tensor]]) – Encoding cache. If omitted, defaults to the the internal cache.

  • to_cache (Optional[Collection[str]]) – Layers, of which the encodings should be cached. If omitted, defaults to registered_layers.

Examples

>>> modules = [("conv", nn.Conv2d(3, 3, 3)), ("pool", nn.MaxPool2d(2))]
>>> mle = pystiche.enc.MultiLayerEncoder(modules)
>>> input = torch.rand(1, 3, 128, 128)
>>> output = mle(input, "conv")
Return type

Tensor

propagate_guide(guide, layers, method='simple', allow_empty=False)

Propagate the guide on the given layers.

Parameters
  • guide (Tensor) – Guide.

  • layers (Sequence[str]) – Layers.

  • allow_empty (bool) – If True, allow the propagated guides to become empty. Defaults to False.

Return type

Tuple[Tensor, …]

Returns

Tuple of guides which order corresponds to layers.

register_layer(layer)

Register a layer for caching the encodings in the forward() pass.

Parameters

layer (str) – Layer to be registered.

Return type

None

class pystiche.enc.SingleLayerEncoder(multi_layer_encoder, layer)

Encoder extracted from a MultiLayerEncoder that operates on a single layer. Invokes SingleLayerEncoder.forward() if called.

multi_layer_encoder

Corresponding multi-layer encoder.

layer

Encoding layer.

forward(input)

Encode the given input on SingleLayerEncoder.layer of SingleLayerEncoder.multi_layer_encoder.

Parameters

input_image – Input image.

Return type

Tensor

propagate_guide(guide)

Propagate the given guide on SingleLayerEncoder.layer of SingleLayerEncoder.multi_layer_encoder.

Parameters

guide (Tensor) – Guide.

Return type

Tensor

Models

class pystiche.enc.ModelMultiLayerEncoder(pretrained=True, framework='torch', internal_preprocessing=True, allow_inplace=False)

Multi-layer encoder based on a pre-defined model.

Parameters
  • pretrained (bool) – If True, loads builtin weights. Defaults to True.

  • framework (str) – Name of the framework that was used to train the builtin weights. Defaults to "torch".

  • internal_preprocessing (bool) – If True, adds a preprocessing layer for the selected framework as first layer. Defaults to True.

  • allow_inplace (bool) –

    If True, allows inplace operations to reduce the memory requirement during the forward pass. Defaults to False.

    Warning

    After performing an inplace operation the encodings of the previous layer is no longer accessible. Only use this if you are sure that you do not need these encodings.

abstract collect_modules(inplace)

Collect modules of a base model with more descriptive names.

Parameters

inplace (bool) – If True, when possible, modules should use inplace operations.

Return type

Tuple[List[Tuple[str, Module]], Dict[str, str]]

Returns

List of name-module-pairs as well as a dictionary mapping the new, more descriptive names to the original ones.

load_state_dict(state_dict, strict=True, map_names=True, framework='unknown')

Loads parameters and buffers from the state_dict.

Parameters
  • state_dict (Dict[str, Tensor]) – State dictionary.

  • strict (bool) – Enforce matching keys in state_dict and the internal states.

  • map_names (bool) – If True, maps the names names in state_dict of the underlying model to the more descriptive names generated by collect_modules(). Defaults to True.

  • framework (str) –

    Name of the framework that was used to train the weights in state_dict. Defaults to "unknown".

    Note

    This has no effect on the behavior, but makes the representation of the ModelMultiLayerEncoder more descriptive.

Return type

_IncompatibleKeys

Returns

Named tuple with missing_keys and unexpected_keys fields.

load_state_dict_from_url(framework, strict=True, map_names=True, check_hash=True, **kwargs)

Downloads and loads parameters and buffers trained with framework.

Parameters
  • framework (str) – Name of the framework that was used to train the weights of the state_dict.

  • strict (bool) – Enforce matching keys in state_dict and the internal states.

  • map_names (bool) – If True, maps the names names in state_dict of the underlying model to the more descriptive names generated by collect_modules(). Defaults to True.

  • check_hash (bool) – If True, checks if the hash postfix of the URL matches the SHA256 hash of the downloaded state_dict. Defaults to True.

  • kwargs (Any) – Optional arguments for torch.hub.load_state_dict_from_url() .

See also

Return type

None

abstract state_dict_url(framework)

Select URL of a downloadable state_dict.

Parameters

framework (str) – Name of the framework that was used to train the weights.

Raises

RuntimeError – If no state_dict is available.

Return type

str

VGG

class pystiche.enc.VGGMultiLayerEncoder(arch, **kwargs)

Multi-layer encoder based on VGG.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]

Parameters
  • arch (str) – VGG architecture. Has to match "vgg(11|13|16|19)(_bn)?".

  • pretrained – If True, loads builtin weights. Defaults to True.

  • framework – Name of the framework that was used to train the builtin weights. Defaults to "torch".

  • kwargs (Any) – Optional arguments of ModelMultiLayerEncoder .

Raises

RuntimeError – If pretrained is True and no weights are available for the combination of arch and framework.

pystiche.enc.vgg11_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 11.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG11 corresponds to configuration A in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg11_bn_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 11 with batch normalization.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG11 corresponds to configuration A in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg13_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 13.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG13 corresponds to configuration B in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg13_bn_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 13 with batch normalization.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG13 corresponds to configuration B in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg16_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 16.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG16 corresponds to configuration D in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg16_bn_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 16 with batch normalization.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG16 corresponds to configuration D in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg19_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 19.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG19 corresponds to configuration E in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

pystiche.enc.vgg19_bn_multi_layer_encoder(**kwargs)

Multi-layer encoder based on VGG 19 with batch normalization.

The VGG architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12]. VGG19 corresponds to configuration E in the paper.

Parameters

kwargs (Any) – Optional arguments of VGGMultiLayerEncoder .

Return type

VGGMultiLayerEncoder

AlexNet

class pystiche.enc.AlexNetMultiLayerEncoder(pretrained=True, framework='torch', internal_preprocessing=True, allow_inplace=False)

Multi-layer encoder based on AlexNet.

The AlexNet architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12].

Parameters
  • pretrained (bool) – If True, loads builtin weights. Defaults to True.

  • framework (str) – Name of the framework that was used to train the builtin weights. Defaults to "torch".

  • kwargs – Optional arguments of ModelMultiLayerEncoder .

Raises
  • RuntimeError – If pretrained and no weights are available for the

  • framework`

pystiche.enc.alexnet_multi_layer_encoder(**kwargs)

Multi-layer encoder based on AlexNet.

The AlexNet architecture was introduced by Krizhevsky, Sutskever, and Hinton in [KSH12].

Parameters

kwargs (Any) – Optional arguments of AlexNetMultiLayerEncoder .

Return type

AlexNetMultiLayerEncoder