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
- 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.
- __contains__(layer)¶
Is the layer part of the multi-layer encoder?
- encode(input, layers)¶
Encode the input on layers.
- extract_encoder(layer)¶
Extract a
SingleLayerEncoder
for the layer and register it.- Parameters
layer (
str
) – Layer.- Return type
- 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 theinput
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 toregistered_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
- propagate_guide(guide, layers, method='simple', allow_empty=False)¶
Propagate the guide on the given layers.
- class pystiche.enc.SingleLayerEncoder(multi_layer_encoder, layer)¶
Encoder extracted from a
MultiLayerEncoder
that operates on a single layer. InvokesSingleLayerEncoder.forward()
if called.- multi_layer_encoder¶
Corresponding multi-layer encoder.
- layer¶
Encoding layer.
- forward(input)¶
Encode the given input on
SingleLayerEncoder.layer
ofSingleLayerEncoder.multi_layer_encoder
.- Parameters
input_image – Input image.
- Return type
- propagate_guide(guide)¶
Propagate the given guide on
SingleLayerEncoder.layer
ofSingleLayerEncoder.multi_layer_encoder
.
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
) – IfTrue
, loads builtin weights. Defaults toTrue
.framework (
str
) – Name of the framework that was used to train the builtin weights. Defaults to"torch"
.internal_preprocessing (
bool
) – IfTrue
, adds a preprocessing layer for the selectedframework
as first layer. Defaults toTrue
.allow_inplace (
bool
) –If
True
, allows inplace operations to reduce the memory requirement during the forward pass. Defaults toFalse
.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.
- load_state_dict(state_dict, strict=True, map_names=True, framework='unknown')¶
Loads parameters and buffers from the
state_dict
.- Parameters
strict (
bool
) – Enforce matching keys instate_dict
and the internal states.map_names (
bool
) – IfTrue
, maps the names names instate_dict
of the underlying model to the more descriptive names generated bycollect_modules()
. Defaults toTrue
.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
andunexpected_keys
fields.
See also
- 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 thestate_dict
.strict (
bool
) – Enforce matching keys instate_dict
and the internal states.map_names (
bool
) – IfTrue
, maps the names names instate_dict
of the underlying model to the more descriptive names generated bycollect_modules()
. Defaults toTrue
.check_hash (
bool
) – IfTrue
, checks if the hash postfix of the URL matches the SHA256 hash of the downloadedstate_dict
. Defaults toTrue
.kwargs (
Any
) – Optional arguments fortorch.hub.load_state_dict_from_url()
.
See also
torch.hub.load_state_dict_from_url()
- Return type
- 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
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 toTrue
.framework – Name of the framework that was used to train the builtin weights. Defaults to
"torch"
.kwargs (
Any
) – Optional arguments ofModelMultiLayerEncoder
.
- Raises
RuntimeError – If
pretrained is True
and no weights are available for the combination ofarch
andframework
.
- 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 configurationA
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationA
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationB
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationB
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationD
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationD
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationE
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
- 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 configurationE
in the paper.- Parameters
kwargs (
Any
) – Optional arguments ofVGGMultiLayerEncoder
.- Return type
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
) – IfTrue
, loads builtin weights. Defaults toTrue
.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 theframework` –
- 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 ofAlexNetMultiLayerEncoder
.- Return type