computer_vision.gramian

Image style reconstruction with Gram matrices.

https://en.wikipedia.org/wiki/Gram_matrix https://en.wikipedia.org/wiki/Neural_style_transfer https://arxiv.org/pdf/1603.08155#page=7&zoom=auto,-294,3

Functions

gram_loss(→ numpy.float64)

Calculates the squared Frobenius norm of the difference between

gram_matrix(→ numpy.ndarray)

Returns the Gram (Gramian) matrix of an image.

Module Contents

computer_vision.gramian.gram_loss(input_features: numpy.ndarray, reference_features: numpy.ndarray) numpy.float64

Calculates the squared Frobenius norm of the difference between the Gram matrices of the input and reference image.

Parameters:
  • input_features (np.ndarray) – Feature map of shape (C, H, W)

  • reference_features (np.ndarray) – Feature map of shape (C, H, W)

Returns:

Gram loss between the two feature maps.

Return type:

float64

Examples

>>> a = np.random.randn(3,5,5)
>>> gram_loss(a, a)
np.float64(0.0)
>>> a = np.zeros((3,5,5))
>>> b = np.ones((3,5,5))
>>> gram_loss(a, b)
np.float64(1.0)
computer_vision.gramian.gram_matrix(mat: numpy.ndarray) numpy.ndarray

Returns the Gram (Gramian) matrix of an image.

Parameters:

mat (np.ndarray) – matrix of shape (C, H, W); C = color channels, H = height, W = width.

Returns:

matrix of shape (C, C).

Return type:

np.ndarray

Examples

>>> gram_matrix(np.ones((2,5,5)))
array([[0.5, 0.5],
       [0.5, 0.5]])
>>> gram_matrix(np.ones((3,5,5)))
array([[0.33333333, 0.33333333, 0.33333333],
       [0.33333333, 0.33333333, 0.33333333],
       [0.33333333, 0.33333333, 0.33333333]])
>>> gram_matrix(np.ones((3,5,5))).shape
(3, 3)