Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / utils / pipeline_config_test.py
1 """
2  Copyright (c) 2018-2019 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import os
18 import tempfile
19 import unittest
20
21 from mo.utils.error import Error
22 from mo.utils.pipeline_config import PipelineConfig
23
24 file_content = """model {
25   faster_rcnn {
26     num_classes: 90
27     image_resizer {
28       keep_aspect_ratio_resizer {
29         min_dimension: 600
30         max_dimension: 1024
31       }
32     }
33     feature_extractor {
34       type: "faster_rcnn_inception_v2"
35       first_stage_features_stride: 16
36     }
37     first_stage_anchor_generator {
38       grid_anchor_generator {
39         height_stride: 16
40         width_stride: 16
41         scales: 0.25
42         scales: 0.5
43         scales: 1.0
44         scales: 2.0
45         aspect_ratios: 0.5
46         aspect_ratios: 1.0
47         aspect_ratios: 2.0
48       }
49     }
50     first_stage_box_predictor_conv_hyperparams {
51       op: CONV
52       regularizer {
53         l2_regularizer {
54           weight: 0.0
55         }
56       }
57       initializer {
58         truncated_normal_initializer {
59           stddev: 0.00999999977648
60         }
61       }
62     }
63     first_stage_nms_score_threshold: 0.0
64     first_stage_nms_iou_threshold: 0.699999988079
65     first_stage_max_proposals: 100
66     first_stage_localization_loss_weight: 2.0
67     first_stage_objectness_loss_weight: 1.0
68     initial_crop_size: 14
69     maxpool_kernel_size: 2
70     maxpool_stride: 2
71     second_stage_box_predictor {
72       mask_rcnn_box_predictor {
73         fc_hyperparams {
74           op: FC
75           regularizer {
76             l2_regularizer {
77               weight: 0.0
78             }
79           }
80           initializer {
81             variance_scaling_initializer {
82               factor: 1.0
83               uniform: true
84               mode: FAN_AVG
85             }
86           }
87             }
88           }
89         }
90         use_dropout: false
91         dropout_keep_probability: 1.0
92       }
93     }
94     second_stage_post_processing {
95       batch_non_max_suppression {
96         score_threshold: 0.300000011921
97         iou_threshold: 0.600000023842
98         max_detections_per_class: 100
99         max_total_detections: 100
100       }
101       score_converter: SOFTMAX
102     }
103     second_stage_localization_loss_weight: 2.0
104     second_stage_classification_loss_weight: 1.0
105   }
106 }
107 """
108
109
110 class TestingSimpleProtoParser(unittest.TestCase):
111     def test_pipeline_config_not_existing_file(self):
112         self.assertRaises(Error, PipelineConfig, "/abc/def")
113
114     def test_pipeline_config_non_model_file(self):
115         file = tempfile.NamedTemporaryFile('wt', delete=False)
116         file.write("non_model {}")
117         file_name = file.name
118         file.close()
119
120         self.assertRaises(Error, PipelineConfig, file_name)
121
122     def test_pipeline_config_existing_file(self):
123         file = tempfile.NamedTemporaryFile('wt', delete=False)
124         file.write(file_content)
125         file_name = file.name
126         file.close()
127
128         pipeline_config = PipelineConfig(file_name)
129         expected_result = {'resizer_min_dimension': 600,
130                            'first_stage_nms_score_threshold': 0.0,
131                            'anchor_generator_aspect_ratios': [0.5, 1.0, 2.0],
132                            'num_classes': 90,
133                            'anchor_generator_scales': [0.25, 0.5, 1.0, 2.0],
134                            'first_stage_max_proposals': 100,
135                            'first_stage_nms_iou_threshold': 0.699999988079,
136                            'resizer_max_dimension': 1024,
137                            'initial_crop_size': 14,
138                            'frcnn_variance_height': 5.0,
139                            'frcnn_variance_width': 5.0,
140                            'frcnn_variance_x': 10.0,
141                            'frcnn_variance_y': 10.0,
142                            'ssd_anchor_generator_base_anchor_width': 1.0,
143                            'ssd_anchor_generator_base_anchor_height': 1.0,
144                            'anchor_generator_height': 256,
145                            'anchor_generator_width': 256,
146                            'anchor_generator_height_stride': 16,
147                            'anchor_generator_width_stride': 16,
148                            }
149         os.unlink(file_name)
150         self.assertDictEqual(pipeline_config._model_params, expected_result)