Spaces:
Running
Running
File size: 6,843 Bytes
0b8359d |
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 |
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for lstm_object_detection.lstm.utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow.compat.v1 as tf
from lstm_object_detection.lstm import utils
class QuantizableUtilsTest(tf.test.TestCase):
def test_quantizable_concat_is_training(self):
inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32)
inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32)
concat_in_train = utils.quantizable_concat([inputs_1, inputs_2],
axis=3,
is_training=True)
self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list())
self._check_min_max_ema(tf.get_default_graph())
self._check_min_max_vars(tf.get_default_graph())
def test_quantizable_concat_inference(self):
inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32)
inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32)
concat_in_train = utils.quantizable_concat([inputs_1, inputs_2],
axis=3,
is_training=False)
self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list())
self._check_no_min_max_ema(tf.get_default_graph())
self._check_min_max_vars(tf.get_default_graph())
def test_quantizable_concat_not_quantized_is_training(self):
inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32)
inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32)
concat_in_train = utils.quantizable_concat([inputs_1, inputs_2],
axis=3,
is_training=True,
is_quantized=False)
self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list())
self._check_no_min_max_ema(tf.get_default_graph())
self._check_no_min_max_vars(tf.get_default_graph())
def test_quantizable_concat_not_quantized_inference(self):
inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32)
inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32)
concat_in_train = utils.quantizable_concat([inputs_1, inputs_2],
axis=3,
is_training=False,
is_quantized=False)
self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list())
self._check_no_min_max_ema(tf.get_default_graph())
self._check_no_min_max_vars(tf.get_default_graph())
def test_quantize_op_is_training(self):
inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32)
outputs = utils.quantize_op(inputs)
self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list())
self._check_min_max_ema(tf.get_default_graph())
self._check_min_max_vars(tf.get_default_graph())
def test_quantize_op_inference(self):
inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32)
outputs = utils.quantize_op(inputs, is_training=False)
self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list())
self._check_no_min_max_ema(tf.get_default_graph())
self._check_min_max_vars(tf.get_default_graph())
def test_fixed_quantize_op(self):
inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32)
outputs = utils.fixed_quantize_op(inputs)
self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list())
self._check_no_min_max_ema(tf.get_default_graph())
self._check_no_min_max_vars(tf.get_default_graph())
def _check_min_max_vars(self, graph):
op_types = [op.type for op in graph.get_operations()]
self.assertTrue(
any('FakeQuantWithMinMaxVars' in op_type for op_type in op_types))
def _check_min_max_ema(self, graph):
op_names = [op.name for op in graph.get_operations()]
self.assertTrue(any('AssignMinEma' in name for name in op_names))
self.assertTrue(any('AssignMaxEma' in name for name in op_names))
self.assertTrue(any('SafeQuantRangeMin' in name for name in op_names))
self.assertTrue(any('SafeQuantRangeMax' in name for name in op_names))
def _check_no_min_max_vars(self, graph):
op_types = [op.type for op in graph.get_operations()]
self.assertFalse(
any('FakeQuantWithMinMaxVars' in op_type for op_type in op_types))
def _check_no_min_max_ema(self, graph):
op_names = [op.name for op in graph.get_operations()]
self.assertFalse(any('AssignMinEma' in name for name in op_names))
self.assertFalse(any('AssignMaxEma' in name for name in op_names))
self.assertFalse(any('SafeQuantRangeMin' in name for name in op_names))
self.assertFalse(any('SafeQuantRangeMax' in name for name in op_names))
class QuantizableSeparableConv2dTest(tf.test.TestCase):
def test_quantizable_separable_conv2d(self):
inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32)
num_outputs = 64
kernel_size = [3, 3]
scope = 'QuantSeparable'
outputs = utils.quantizable_separable_conv2d(
inputs, num_outputs, kernel_size, scope=scope)
self.assertAllEqual([4, 10, 10, num_outputs], outputs.shape.as_list())
self._check_depthwise_bias_add(tf.get_default_graph(), scope)
def test_quantizable_separable_conv2d_not_quantized(self):
inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32)
num_outputs = 64
kernel_size = [3, 3]
scope = 'QuantSeparable'
outputs = utils.quantizable_separable_conv2d(
inputs, num_outputs, kernel_size, is_quantized=False, scope=scope)
self.assertAllEqual([4, 10, 10, num_outputs], outputs.shape.as_list())
self._check_no_depthwise_bias_add(tf.get_default_graph(), scope)
def _check_depthwise_bias_add(self, graph, scope):
op_names = [op.name for op in graph.get_operations()]
self.assertTrue(
any('%s_bias/BiasAdd' % scope in name for name in op_names))
def _check_no_depthwise_bias_add(self, graph, scope):
op_names = [op.name for op in graph.get_operations()]
self.assertFalse(
any('%s_bias/BiasAdd' % scope in name for name in op_names))
if __name__ == '__main__':
tf.test.main()
|