You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.5 KiB

6 years ago
  1. # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Tests for graph_rewriter_builder."""
  16. import mock
  17. import tensorflow as tf
  18. from object_detection.builders import graph_rewriter_builder
  19. from object_detection.protos import graph_rewriter_pb2
  20. class QuantizationBuilderTest(tf.test.TestCase):
  21. def testQuantizationBuilderSetsUpCorrectTrainArguments(self):
  22. with mock.patch.object(
  23. tf.contrib.quantize, 'create_training_graph') as mock_quant_fn:
  24. with mock.patch.object(tf.contrib.layers,
  25. 'summarize_collection') as mock_summarize_col:
  26. graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter()
  27. graph_rewriter_proto.quantization.delay = 10
  28. graph_rewriter_proto.quantization.weight_bits = 8
  29. graph_rewriter_proto.quantization.activation_bits = 8
  30. graph_rewrite_fn = graph_rewriter_builder.build(
  31. graph_rewriter_proto, is_training=True)
  32. graph_rewrite_fn()
  33. _, kwargs = mock_quant_fn.call_args
  34. self.assertEqual(kwargs['input_graph'], tf.get_default_graph())
  35. self.assertEqual(kwargs['quant_delay'], 10)
  36. mock_summarize_col.assert_called_with('quant_vars')
  37. def testQuantizationBuilderSetsUpCorrectEvalArguments(self):
  38. with mock.patch.object(tf.contrib.quantize,
  39. 'create_eval_graph') as mock_quant_fn:
  40. with mock.patch.object(tf.contrib.layers,
  41. 'summarize_collection') as mock_summarize_col:
  42. graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter()
  43. graph_rewriter_proto.quantization.delay = 10
  44. graph_rewrite_fn = graph_rewriter_builder.build(
  45. graph_rewriter_proto, is_training=False)
  46. graph_rewrite_fn()
  47. _, kwargs = mock_quant_fn.call_args
  48. self.assertEqual(kwargs['input_graph'], tf.get_default_graph())
  49. mock_summarize_col.assert_called_with('quant_vars')
  50. if __name__ == '__main__':
  51. tf.test.main()