File size: 8,354 Bytes
a01ef8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

import os
import pytest
import shutil
import tempfile
import uuid

from unittest.mock import patch, MagicMock
from tlt.models import model_factory

try:
    # Do TF specific imports in a try/except to prevent pytest test loading from failing when running in a PyTorch env
    from tlt.models.image_classification.tf_image_classification_model import TFImageClassificationModel  # noqa: F401
    from tlt.models.image_classification.tf_image_classification_model import TFCustomImageClassificationDataset
except ModuleNotFoundError:
    print("WARNING: Unable to import TFImageClassificationModel. TensorFlow may not be installed")


@pytest.mark.tensorflow
def test_tf_image_classification_quantization():
    """
    Given a valid directory for the output dir, test the quantization function with the actual Intel Neural Compressor
    call mocked out.
    """
    try:
        output_dir = tempfile.mkdtemp()

        model = model_factory.get_model('efficientnet_b0', 'tensorflow')
        with patch('tlt.models.image_classification.tf_image_classification_model.TFCustomImageClassificationDataset') \
                as mock_dataset:
            with patch('neural_compressor.quantization.fit') as mock_q:
                mock_dataset.dataset_dir = "/tmp/data/my_photos"
                mock_dataset.__class__ = TFCustomImageClassificationDataset
                mock_dataset.get_inc_dataloaders.return_value = (1, 2)
                model.quantize(output_dir, mock_dataset)
                mock_q.assert_called_once()
    finally:
        if os.path.exists(output_dir):
            shutil.rmtree(output_dir)


@pytest.mark.tensorflow
@patch('tlt.models.tf_model.quantization.fit')
def test_tf_image_classification_quantize_overwrite_saved_model(mock_quantization_fit):
    """
    Given a valid directory for the output dir, test the quantize function with the actual Intel Neural
    Compressor call mocked out. Tests that the model will be overwritten or not using the overwrite_model flag.
    """

    from tlt.models import model_factory

    try:
        # Specify a directory for output
        output_dir = tempfile.mkdtemp()

        model = model_factory.get_model(model_name='resnet_v1_50', framework='tensorflow')

        # Mock the dataset
        mock_dataset = MagicMock()
        mock_dataset.__class__ = TFCustomImageClassificationDataset
        mock_dataset.get_inc_dataloaders.return_value = 1, 2

        # Method to create a dummy model.pt file in the specified directory
        def create_dummy_file(output_dir):
            with open(os.path.join(output_dir, 'saved_model.pb'), 'w') as fp:
                fp.close()

        # Mock an INC quantized model that will create a dummy file when saved
        mock_quantized_model = MagicMock()
        mock_quantized_model.save.side_effect = create_dummy_file

        # Mock the INC quantization.fit method
        def mock_fit(**args):
            return mock_quantized_model
        mock_quantization_fit.side_effect = mock_fit

        # Call quantize when a model does not exist
        model.quantize(output_dir=output_dir, dataset=mock_dataset, overwrite_model=False)

        # Call quantize when the model exists, but overwrite_model=True
        model.quantize(output_dir=output_dir, dataset=mock_dataset, overwrite_model=True)
        model.quantize(output_dir=output_dir, dataset=mock_dataset, overwrite_model=True)

        with pytest.raises(FileExistsError):  # Model exists, so this should be true
            model.quantize(output_dir=output_dir, dataset=mock_dataset, overwrite_model=False)

    finally:
        if os.path.exists(output_dir):
            shutil.rmtree(output_dir)


@patch('tlt.models.tf_model.Graph_Optimization')
@pytest.mark.tensorflow
def test_tf_image_classification_optimize_graph_overwrite_saved_model(mock_graph_optimization):
    """
    Given a valid directory for the output dir, test the quantize function with the actual Intel Neural
    Compressor call mocked out. Tests that the model will be overwritten or not using the overwrite_model flag.
    """

    # tlt imports
    from tlt.models.image_classification.tf_image_classification_model import TFCustomImageClassificationDataset
    from tlt.models import model_factory

    try:
        # Specify a directory for output
        output_dir = tempfile.mkdtemp()

        model = model_factory.get_model(model_name='resnet_v1_50', framework='tensorflow')

        # Mock the dataset
        mock_dataset = MagicMock()
        mock_dataset.__class__ = TFCustomImageClassificationDataset
        mock_dataset.get_inc_dataloaders.return_value = 1, 2

        # Method to create a dummy model.pt file in the specified directory
        def create_dummy_file():
            with open(os.path.join(output_dir, 'saved_model.pb'), 'w') as fp:
                fp.close()
            return MagicMock()

        # Mock an INC quantized model that will create a dummy file when saved
        mock_graph_optimization.side_effect = create_dummy_file

        # Call optimize_graph when a model does not exist
        model.optimize_graph(output_dir=output_dir)

        # Call optimize_graph when the model exists, but overwrite_model=True
        model.optimize_graph(output_dir=output_dir, overwrite_model=True)
        model.optimize_graph(output_dir=output_dir, overwrite_model=True)

        with pytest.raises(FileExistsError):  # Model exists, so this should be true
            model.optimize_graph(output_dir=output_dir, overwrite_model=False)

    finally:
        if os.path.exists(output_dir):
            shutil.rmtree(output_dir)


@pytest.mark.tensorflow
def test_tf_image_classification_benchmark_model_does_not_exist():
    """
    Verifies the error that gets raise if benchmarking is done with a model that does not exist
    """
    try:
        model = model_factory.get_model('efficientnet_b0', 'tensorflow')
        with patch('tlt.models.image_classification.tf_image_classification_model.TFCustomImageClassificationDataset') \
                as mock_dataset:
            mock_dataset.dataset_dir = "/tmp/data/my_photos"
            mock_dataset.__class__ = TFCustomImageClassificationDataset
            random_dir = str(uuid.uuid4())
            saved_model_dir = tempfile.mkdtemp()
            with patch('neural_compressor.benchmark.fit'):
                # It's not a directory, so we expect an error
                with pytest.raises(NotADirectoryError):
                    model.benchmark(mock_dataset, saved_model_dir=random_dir)

                # An empty directory with no saved model should also generate an error
                with pytest.raises(FileNotFoundError):
                    model.benchmark(mock_dataset, saved_model_dir=saved_model_dir)
    finally:
        if os.path.exists(saved_model_dir):
            shutil.rmtree(saved_model_dir)


@pytest.mark.tensorflow
def test_tf_image_classification_inc_benchmark():
    """
    Verifies that if we have a valid model and dataset, benchmarking is called. The actual benchmarking calls to Intel
    Neural Compressor are mocked out.
    """
    model = model_factory.get_model('efficientnet_b0', 'tensorflow')
    with patch('tlt.models.image_classification.tf_image_classification_model.TFCustomImageClassificationDataset') \
            as mock_dataset:
        with patch('neural_compressor.benchmark.fit') as mock_bench:
            mock_dataset.dataset_dir = "/tmp/data/my_photos"
            mock_dataset.__class__ = TFCustomImageClassificationDataset
            mock_dataset.get_inc_dataloaders.return_value = (1, 2)
            model.benchmark(mock_dataset)
            mock_bench.assert_called_once()