Skip to content

graph_factory

GraphFactory

Source code in src/artificial_artwork/style_model/graph_factory.py
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
class GraphFactory:
    builder = GraphBuilder()

    @classmethod
    def create(cls, config: ImageSpecs, model_design) -> Dict[str, Any]:
        """Create a model for the purpose of 'painting'/generating a picture.

        Creates a Deep Learning Neural Network with most layers having weights
        (aka model parameters) with values extracted from a pre-trained model
        (ie another neural network trained on an image dataset suitably).

        Args:
            config ([type]): [description]
            model_parameters ([type], optional): [description]. Defaults to None.

        Returns:
            Dict[str, Any]: [description]
        """
        # each relu_conv_2d uses pretrained model's layer weights for W and b matrices
        # each average pooling layer uses custom weight values
        # all weights are guaranteed to remain constant (see GraphBuilder._conv_2d method)

        # Build the Input Layer of the NST Style Network, based on input Image Specs
        cls.builder.input(config)
        LayerMaker(
            cls.builder,
            model_design.pretrained_model.reporter,  # this reporter is able to extract weights
        ).make_layers(
            model_design.network_design.network_layers
        )  # all VGG layers

        return cls.builder.graph

create(config, model_design) classmethod

Create a model for the purpose of 'painting'/generating a picture.

Creates a Deep Learning Neural Network with most layers having weights (aka model parameters) with values extracted from a pre-trained model (ie another neural network trained on an image dataset suitably).

Parameters:

Name Type Description Default
config [type]

[description]

required
model_parameters [type]

[description]. Defaults to None.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: [description]

Source code in src/artificial_artwork/style_model/graph_factory.py
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
@classmethod
def create(cls, config: ImageSpecs, model_design) -> Dict[str, Any]:
    """Create a model for the purpose of 'painting'/generating a picture.

    Creates a Deep Learning Neural Network with most layers having weights
    (aka model parameters) with values extracted from a pre-trained model
    (ie another neural network trained on an image dataset suitably).

    Args:
        config ([type]): [description]
        model_parameters ([type], optional): [description]. Defaults to None.

    Returns:
        Dict[str, Any]: [description]
    """
    # each relu_conv_2d uses pretrained model's layer weights for W and b matrices
    # each average pooling layer uses custom weight values
    # all weights are guaranteed to remain constant (see GraphBuilder._conv_2d method)

    # Build the Input Layer of the NST Style Network, based on input Image Specs
    cls.builder.input(config)
    LayerMaker(
        cls.builder,
        model_design.pretrained_model.reporter,  # this reporter is able to extract weights
    ).make_layers(
        model_design.network_design.network_layers
    )  # all VGG layers

    return cls.builder.graph

LayerMaker

Automatically create the NST Style Computational Graph (Neural Net)

Automatically builds the NST Style Network as a Computational Graph of Tensor Operations, given a list of layer_ids (ie names) and a reporter (ie a ModelReporter instance) that can provide the weights for each layer_id.

Automatically creates relu or avg pooling layers, depending on the layer_id.

Source code in src/artificial_artwork/style_model/graph_factory.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@attr.s
class LayerMaker:
    """Automatically create the NST Style Computational Graph (Neural Net)

    Automatically builds the NST Style Network as a Computational Graph of Tensor
    Operations, given a list of layer_ids (ie names) and a reporter (ie a
    ModelReporter instance) that can provide the weights for each layer_id.

    Automatically creates relu or avg pooling layers, depending on the layer_id.
    """

    graph_builder = attr.ib()
    reporter = attr.ib()

    layer_callbacks = attr.ib(
        init=False,
        default=attr.Factory(
            lambda self: {
                "conv": self._create_relu_layer,
                "avgpool": self._create_avg_pool_layer,
            },
            takes_self=True,
        ),
    )
    regex = attr.ib(init=False, default=re.compile(r"(\w+?)[\d_]*$"))

    def make_layers(self, layers: Iterable[str]):
        for layer_id in layers:
            self._build_layer(layer_id)

    def _build_layer(self, layer_id: str):
        match_instance = self.regex.match(layer_id)
        if match_instance is not None:
            return self.layer_callbacks[match_instance.group(1)](layer_id)
        raise UnknownLayerError(
            f"Failed to construct layer '{layer_id}'. Supported layers are "
            f"[{', '.join((k for k in self.layer_callbacks))}] and regex"
            f"used to parse the layer is '{self.regex.pattern}'"
        )

    def _create_relu_layer(self, layer_id: str):
        """Create Layer Neurons by wrapping a ReLU activation function around a Conv2D Tensor"""
        return self.graph_builder.relu_conv_2d(layer_id, self.reporter.get_weights(layer_id))

    def _create_avg_pool_layer(self, layer_id: str):
        """Create Layer as Neurons performing Average Pooling with padding SAME"""
        return self.graph_builder.avg_pool(layer_id)