Skip to content

model_routines

This modules defines the interface which must be implemented in order to utilize a pretrained model and its weights

PretrainedModelRoutines

Bases: ABC

Set of routines that are required in order to use a pretrained model for nst.

Source code in src/artificial_artwork/pretrained_model/model_routines.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class PretrainedModelRoutines(ABC):
    """Set of routines that are required in order to use a pretrained model for nst."""

    @abstractmethod
    def load_layers(self, file_path: str) -> NDArray:
        """Load a pretrained model from disk.

        Loads the model parameters, given the path to a file in the disk, that
        indicated where the pretrained model is.

        Args:
            file_path (str): the path corresponding to a file in the disk

        Returns:
            NDArray: the model parameters as a numpy array
        """
        raise NotImplementedError

    @abstractmethod
    def get_id(self, layer: NDArray) -> str:
        """Get the id of a model's network layer.

        The pretrained model being a neural network has a specific architecture
        and each layer should a unique string id that one can reference it.

        Args:
            layer (NDArray): the layer of a pretrained neural network model

        Returns:
            str: the layer id
        """
        raise NotImplementedError

    @abstractmethod
    def get_layers_dict(self, layers: NDArray) -> Dict[str, NDArray]:
        """Get a dict mapping strings to pretrained model layers.

        Args:
            layers (NDArray): the pretrained model layers

        Returns:
            Dict[str, NDArray]: the dictionary mapping strings to layers
        """
        raise NotImplementedError

    @abstractmethod
    def get_weights(self, layer: NDArray) -> Tuple[NDArray, NDArray]:
        """Get the values of the weights of a given network layer.

        Each pretrained model network layer has "learned" certain parameters in
        the form of "weights" (ie weight matrices A and b in equation Ax + b).

        Call this method to get a tuple of the A and b mathematical matrices.

        Args:
            layer (NDArray): the layer of a pretrained neural network model

        Returns:
            Tuple[NDArray, NDArray]: the weights in matrix A and b
        """
        raise NotImplementedError

get_id(layer) abstractmethod

Get the id of a model's network layer.

The pretrained model being a neural network has a specific architecture and each layer should a unique string id that one can reference it.

Parameters:

Name Type Description Default
layer NDArray

the layer of a pretrained neural network model

required

Returns:

Name Type Description
str str

the layer id

Source code in src/artificial_artwork/pretrained_model/model_routines.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@abstractmethod
def get_id(self, layer: NDArray) -> str:
    """Get the id of a model's network layer.

    The pretrained model being a neural network has a specific architecture
    and each layer should a unique string id that one can reference it.

    Args:
        layer (NDArray): the layer of a pretrained neural network model

    Returns:
        str: the layer id
    """
    raise NotImplementedError

get_layers_dict(layers) abstractmethod

Get a dict mapping strings to pretrained model layers.

Parameters:

Name Type Description Default
layers NDArray

the pretrained model layers

required

Returns:

Type Description
Dict[str, NDArray]

Dict[str, NDArray]: the dictionary mapping strings to layers

Source code in src/artificial_artwork/pretrained_model/model_routines.py
43
44
45
46
47
48
49
50
51
52
53
@abstractmethod
def get_layers_dict(self, layers: NDArray) -> Dict[str, NDArray]:
    """Get a dict mapping strings to pretrained model layers.

    Args:
        layers (NDArray): the pretrained model layers

    Returns:
        Dict[str, NDArray]: the dictionary mapping strings to layers
    """
    raise NotImplementedError

get_weights(layer) abstractmethod

Get the values of the weights of a given network layer.

Each pretrained model network layer has "learned" certain parameters in the form of "weights" (ie weight matrices A and b in equation Ax + b).

Call this method to get a tuple of the A and b mathematical matrices.

Parameters:

Name Type Description Default
layer NDArray

the layer of a pretrained neural network model

required

Returns:

Type Description
Tuple[NDArray, NDArray]

Tuple[NDArray, NDArray]: the weights in matrix A and b

Source code in src/artificial_artwork/pretrained_model/model_routines.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@abstractmethod
def get_weights(self, layer: NDArray) -> Tuple[NDArray, NDArray]:
    """Get the values of the weights of a given network layer.

    Each pretrained model network layer has "learned" certain parameters in
    the form of "weights" (ie weight matrices A and b in equation Ax + b).

    Call this method to get a tuple of the A and b mathematical matrices.

    Args:
        layer (NDArray): the layer of a pretrained neural network model

    Returns:
        Tuple[NDArray, NDArray]: the weights in matrix A and b
    """
    raise NotImplementedError

load_layers(file_path) abstractmethod

Load a pretrained model from disk.

Loads the model parameters, given the path to a file in the disk, that indicated where the pretrained model is.

Parameters:

Name Type Description Default
file_path str

the path corresponding to a file in the disk

required

Returns:

Name Type Description
NDArray NDArray

the model parameters as a numpy array

Source code in src/artificial_artwork/pretrained_model/model_routines.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@abstractmethod
def load_layers(self, file_path: str) -> NDArray:
    """Load a pretrained model from disk.

    Loads the model parameters, given the path to a file in the disk, that
    indicated where the pretrained model is.

    Args:
        file_path (str): the path corresponding to a file in the disk

    Returns:
        NDArray: the model parameters as a numpy array
    """
    raise NotImplementedError