|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
# TEST Counting the size of total memory consumed by the A and b matrices |
|
5
|
|
|
# required for Building the Computational Graph is the expected one |
|
6
|
|
|
# Currently the Production Weight Matrices come from the pretrained VGG19 model |
|
7
|
|
|
# and the Toy Weight Matrices come from the Toy Model |
|
8
|
|
|
|
|
9
|
|
|
# when using pytest as Test Runner |
|
10
|
|
|
# this test case requires the --run-integration flag to be picked |
|
11
|
|
|
@pytest.mark.integration |
|
12
|
|
|
def test_prod_weight_matrices_memory_consumption_is_expected_one( |
|
13
|
|
|
|
|
14
|
|
|
): |
|
15
|
|
|
# GIVEN the default layers our NST algorithm requires to build as part of |
|
16
|
|
|
# its Computational Graph, which require to load weight matrices from the |
|
17
|
|
|
# pretrained model (ie A, b matrices) |
|
18
|
|
|
from artificial_artwork.production_networks.image_model import LAYERS as DEFAULT_VGG_LAYERS_TO_BUILD |
|
19
|
|
|
layer_types_with_weights = {'conv'} |
|
20
|
|
|
from artificial_artwork.style_model.graph_factory import LayerMaker |
|
21
|
|
|
regex = LayerMaker(None, None).regex |
|
22
|
|
|
|
|
23
|
|
|
runtime_layers_with_weights = [l for l in DEFAULT_VGG_LAYERS_TO_BUILD if regex.match(l) and regex.match(l).group(1) in layer_types_with_weights] |
|
24
|
|
|
|
|
25
|
|
|
# GIVEN a method to extract A and b matrices from the loaded layers of a |
|
26
|
|
|
# pretrained model |
|
27
|
|
|
from artificial_artwork.pretrained_model import ModelHandlerFacility |
|
28
|
|
|
|
|
29
|
|
|
# Equip the entrypoint with a concrete implementaion tailored |
|
30
|
|
|
# to our prod vgg model |
|
31
|
|
|
from artificial_artwork.pre_trained_models import vgg |
|
32
|
|
|
|
|
33
|
|
|
# create object to delegate all vgg related operations |
|
34
|
|
|
vgg_ops = ModelHandlerFacility.create('vgg') |
|
35
|
|
|
|
|
36
|
|
|
# GIVEN all the pretrained model layers are loaded in memory |
|
37
|
|
|
_layers = vgg_ops.load_model_layers() |
|
38
|
|
|
# the above equips the vgg_ops object with a reporter attribute |
|
39
|
|
|
# see src/artificial_artwork/pretrained_model/layers_getter.py |
|
40
|
|
|
|
|
41
|
|
|
# GIVEN the total memory requirements in bytes to store the A and b matrices |
|
42
|
|
|
expected_mem_consumption = 80097536 |
|
43
|
|
|
|
|
44
|
|
|
# WHEN we count the total memory consumed by the weight matrices |
|
45
|
|
|
runtime_memory_consumption = 0 |
|
46
|
|
|
|
|
47
|
|
|
# WHEN we extract the weight matrices for the corresponding NST layers that require them (ie conv, but no avgpool) |
|
48
|
|
|
for nst_layer_requiring_weights in runtime_layers_with_weights: |
|
49
|
|
|
# # extract the A, b matrices from the loaded pretrained image model |
|
50
|
|
|
A_vgg, b_vgg = vgg_ops.reporter.get_weights(nst_layer_requiring_weights) |
|
51
|
|
|
runtime_memory_consumption += A_vgg.nbytes + b_vgg.nbytes |
|
52
|
|
|
|
|
53
|
|
|
# THEN the total memory consumed by the weight matrices is the expected one |
|
54
|
|
|
assert runtime_memory_consumption == expected_mem_consumption |
|
55
|
|
|
|