pystiche

pystiche.home()

Local directory to save downloaded images and guides. Defaults to ~/.cache/pystiche but can be overwritten with the PYSTICHE_HOME environment variable.

Return type

str

Objects

class pystiche.ComplexObject

Object with a complex representation. See pystiche.misc.build_complex_obj_repr() for details.

_named_children()
Yields

Internal named children.

Note

If subclassed, this method should yield the named children of the superclass alongside yielding the new named children.

Return type

Iterator[Tuple[str, Any]]

_properties()
Return type

Dict[str, Any]

Returns

Internal properties.

Note

If subclassed, this method should integrate the new properties in the properties of the superclass.

extra_named_children()
Yields

Extra named children.

Return type

Iterator[Tuple[str, Any]]

extra_properties()
Return type

Dict[str, Any]

Returns

Extra properties.

named_children()
Yields

Internal and extra named children.

Return type

Iterator[Tuple[str, Any]]

properties()
Return type

Dict[str, Any]

Returns

Internal and extra properties.

class pystiche.LossDict(losses=())

Hierarchic dictionary of scalar torch.Tensor losses. Levels are seperated by "." in the names.

Parameters

losses (Sequence[Tuple[str, Union[Tensor, LossDict]]]) – Optional named losses.

__mul__(other)

Multiplies all entries with a scalar.

Parameters

other (SupportsFloat) – Scalar multiplier.

Return type

LossDict

__setitem__(name, loss)

Add a named loss to the entries.

Parameters
Raises

TypeError – If loss is torch.Tensor but isn’t scalar.

Return type

None

aggregate(max_depth)

Aggregate all entries up to a given maximum depth.

Parameters

max_depth (int) – If 0 returns sum of all entries as scalar torch.Tensor.

Return type

Union[Tensor, LossDict]

backward(*args, **kwargs)

Computes the gradient of all entries with respect to the graph leaves. See torch.Tensor.backward() for details.

Return type

None

item()
Return type

float

Returns

The sum of all entries as standard Python number.

total()
Return type

Tensor

Returns

Sum of all entries as scalar tensor.

class pystiche.Module(named_children=None, indexed_children=None)

torch.nn.Module with the enhanced representation options of pystiche.ComplexObject.

Parameters

Note

named_children and indexed_children are mutually exclusive parameters.

torch_repr()
Return type

str

Returns

Native torch representation.

Math

pystiche.nonnegsqrt(x)

Safely calculates the square-root of a non-negative input

\[\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} \begin{split}\fun{nonnegsqrt}{x} = \begin{cases} \sqrt{x} &\quad\text{if } x \ge 0 \\ 0 &\quad\text{otherwise} \end{cases}\end{split}\]

Note

This operation is useful in situations where the input tensor is strictly non-negative from a theoretical standpoint, but might be negative due to numerical instabilities.

Parameters

x (Tensor) – Input tensor.

Return type

Tensor

pystiche.gram_matrix(x, normalize=False)

Calculates the channel-wise Gram matrix of a batched input tensor.

Given a tensor \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} x\) of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times C \times N_1 \times \dots \times N_D\) each element of the single-sample Gram matrix \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} G_{b,c_1 c_2}\) with \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} b \in 1,\dots,B\) and \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} c_1,\,c_2 \in 1,\dots,C\) is calculated by

\[\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} G_{b,c_1 c_2} = \dotproduct{\fun{vec}{x_{b, c_1}}}{\fun{vec}{x_{b, c_2}}}\]

where \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} \dotproduct{\cdot}{\cdot}\) denotes the dot product and \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} \fun{vec}{\cdot}\) denotes the vectorization function .

Parameters
  • x (Tensor) – Input tensor of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times C \times N_1 \times \dots \times N_D\)

  • normalize (bool) – If True, normalizes the Gram matrix \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} G\) by \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} \prod\limits_{d=1}^{D} N_d\) to keep the value range similar for different sized inputs. Defaults to False.

Return type

Tensor

Returns

Channel-wise Gram matrix G of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times C \times C\).

pystiche.cosine_similarity(x1, x2, eps=1e-08, batched_input=None)

Calculates the cosine similarity between the samples of x1 and x2.

Parameters
  • x1 (Tensor) – First input of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times S_1 \times N_1 \times \dots \times N_D\).

  • x2 (Tensor) – Second input of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times S_2 \times N_1 \times \dots \times N_D\).

  • eps (float) – Small value to avoid zero division. Defaults to 1e-8.

  • batched_input (Optional[bool]) – If False, treat the first dimension of the inputs as sample dimension, i.e. \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} S \times N_1 \times \dots \times N_D\). Defaults to True.

Return type

Tensor

Returns

Similarity matrix of shape \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} B \times S_1 \times S_2\) in which every element represents the cosine similarity between the corresponding samples \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} S\) of x1 and x2. If batched_input is False, the output shape is \(\newcommand{\parentheses}[1]{\left( #1 \right)} \newcommand{\brackets}[1]{\left[ #1 \right]} \newcommand{\mean}[1][]{\overline{\sum #1}} \newcommand{\fun}[2]{\text{#1}\of{#2}} \newcommand{\of}[1]{\parentheses{#1}} \newcommand{\dotproduct}[2]{\left\langle #1 , #2 \right\rangle} \newcommand{\openinterval}[2]{\parentheses{#1, #2}} \newcommand{\closedinterval}[2]{\brackets{#1, #2}} S_1 \times S_2\)