Passed
Pull Request — master (#1)
by Konstantinos
59s
created

test_cv_model.test_pretrained_model()   A

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 19
rs 9.75
c 0
b 0
f 0
cc 2
nop 4
1
import os
2
import pytest
3
4
from artificial_artwork.pretrained_model.model_loader import get_vgg_19_model_path, load_default_model_parameters
5
6
my_dir = os.path.dirname(os.path.realpath(__file__))
7
8
# IMAGE_MODEL_FILE_NAME = 'imagenet-vgg-verydeep-19.mat'
9
10
11
PRODUCTION_IMAGE_MODEL = os.environ.get('AA_VGG_19', 'PRETRAINED_MODEL_NOT_FOUND')
12
13
14
@pytest.fixture
15
def model_parameters():
16
    from artificial_artwork.pretrained_model.model_loader import load_default_model_parameters
17
    return load_default_model_parameters()
18
    # return load_default_model_parameters()
19
    # from artificial_artwork.pretrained_model.model_loader import load_vgg_model_parameters
20
    # return load_vgg_model_parameters
21
22
23
@pytest.fixture
24
def vgg_layers():
25
    """Expected layers structure of the vgg image model."""
26
    from artificial_artwork.pretrained_model.vgg_architecture import LAYERS
27
    return LAYERS
28
29
30
@pytest.fixture
31
def style_network_architecture():
32
    from artificial_artwork.pretrained_model.image_model import LAYERS
33
    return LAYERS
34
35
36
@pytest.fixture
37
def graph_factory():
38
    from artificial_artwork.pretrained_model import graph_factory
39
    return graph_factory
40
41
42
@pytest.mark.xfail(not os.path.isfile(PRODUCTION_IMAGE_MODEL),
43
    reason="No file found to load the pretrained image (cv) model.")
44
def test_pretrained_model(model_parameters, graph_factory, vgg_layers, style_network_architecture):
45
    layers = model_parameters['layers']
46
47
    image_specs = type('ImageSpecs', (), {
48
        'width': 400,
49
        'height': 300,
50
        'color_channels': 3
51
    })()
52
53
    # verify original/loaded neural network has 43 layers
54
    assert len(layers[0]) == 43
55
56
    for i, name in enumerate(vgg_layers):
57
        assert layers[0][i][0][0][0][0] == name
58
59
    graph = graph_factory.create(image_specs, model_parameters=model_parameters)
60
    assert set(graph.keys()) == set(['input'] + list(style_network_architecture))
61
62
63
@pytest.fixture
64
def graph_builder():
65
    from artificial_artwork.pretrained_model.model_loader import GraphBuilder
66
    return GraphBuilder()
67
68
69
def test_building_layers(graph_builder):
70
    import numpy as np
71
    height = 2
72
    width = 6
73
    channels = 2
74
    expected_input_shape = (1, height, width, channels)
75
76
    graph_builder.input(width, height, nb_channels=channels)
77
    # assert previous layer is the 'input' layer we just added/created
78
    assert tuple(graph_builder._prev_layer.shape) == expected_input_shape
79
    for w in range(width):
80
        for h in range(height):
81
            for c in range(channels):
82
                assert graph_builder._prev_layer[0][h][w][c] == graph_builder.graph['input'][0][h][w][c] == 0
83
84
    # create relu(convolution) layer
85
    W = np.array(np.random.rand(*expected_input_shape[1:], channels), dtype=np.float32)
86
87
    b_weight = 6.0
88
    b = np.array([b_weight], dtype=np.float32)
89
    graph_builder.relu_conv_2d('convo1', (W, b))
90
    
91
    # assert the previous layer is the relu(convolution) layer we just added
92
    assert tuple(graph_builder._prev_layer.shape) == expected_input_shape
93
    for w in range(width):
94
        for h in range(height):
95
            for c in range(channels):
96
                assert graph_builder._prev_layer[0][h][w][c] == graph_builder.graph['convo1'][0][h][w][c]
97
                assert graph_builder._prev_layer[0][h][w][c] == b_weight # W[h][w][c][c] + b[0]
98
99
100
    # create Average Pooling layer
101
    layer_id = 'avgpool1'
102
    graph_builder.avg_pool(layer_id)
103
104
    # assert previous layer is the layer we just added/created
105
    expected_avg_pool_shape = (1, 1, 2, channels)
106
    expected_avg_output = np.array(
107
        [[[[b_weight, b_weight, b_weight],
108
            [b_weight, b_weight, b_weight],
109
            [b_weight, b_weight, b_weight]
110
        ]]]
111
    ,dtype=np.float32)
112
113
    for i in range(expected_avg_pool_shape[2]):
114
        for c in range(channels):
115
            assert graph_builder._prev_layer[0][0][i][c] == graph_builder.graph[layer_id][0][0][i][c]
116
            assert graph_builder._prev_layer[0][0][i][c] == expected_avg_output[0][0][i][c]
117