(self, use_rep_data, include_int8,
is_float16_quantized, is_error,
is_post_training_quantized)
| 688 | # Error if no rep data and int8 included. |
| 689 | ('NoRepresentativeDataIncludeInt8', False, True, False, True, False)) |
| 690 | def testQuantizeFloat16(self, use_rep_data, include_int8, |
| 691 | is_float16_quantized, is_error, |
| 692 | is_post_training_quantized): |
| 693 | with ops.Graph().as_default(): |
| 694 | inp, output, calibration_gen = self._getCalibrationQuantizeModel() |
| 695 | sess = session.Session() |
| 696 | |
| 697 | # Convert float model. |
| 698 | float_converter = lite.TFLiteConverter.from_session(sess, [inp], [output]) |
| 699 | float_tflite = float_converter.convert() |
| 700 | self.assertTrue(float_tflite) |
| 701 | interpreter = Interpreter(model_content=float_tflite) |
| 702 | interpreter.allocate_tensors() |
| 703 | self.assertEqual(interpreter.get_tensor_details()[0]['name'], 'Conv2D_bias') |
| 704 | self.assertEqual(interpreter.get_tensor_details()[0]['dtype'], |
| 705 | lite.constants.FLOAT) |
| 706 | # Convert model to quantized version |
| 707 | quantized_converter = lite.TFLiteConverter.from_session( |
| 708 | sess, [inp], [output]) |
| 709 | quantized_converter.optimizations = [lite.Optimize.DEFAULT] |
| 710 | quantized_converter.target_spec.supported_types = [lite.constants.FLOAT16] |
| 711 | if include_int8: |
| 712 | quantized_converter.target_spec.supported_types.append( |
| 713 | lite.constants.INT8) |
| 714 | if use_rep_data: |
| 715 | quantized_converter.representative_dataset = calibration_gen |
| 716 | |
| 717 | if is_error: |
| 718 | with self.assertRaises(ValueError) as error: |
| 719 | quantized_converter.convert() |
| 720 | self.assertEqual( |
| 721 | 'representative_dataset is required when specifying ' |
| 722 | 'TFLITE_BUILTINS_INT8 or INT8 supported types.', str(error.exception)) |
| 723 | |
| 724 | else: |
| 725 | quantized_tflite = quantized_converter.convert() |
| 726 | self.assertTrue(quantized_tflite) |
| 727 | interpreter = Interpreter(model_content=quantized_tflite) |
| 728 | interpreter.allocate_tensors() |
| 729 | self.assertEqual(interpreter.get_tensor_details()[0]['name'], |
| 730 | 'Conv2D_bias') |
| 731 | |
| 732 | if is_float16_quantized: |
| 733 | # Verify that bias constant is float16 type. |
| 734 | self.assertEqual(interpreter.get_tensor_details()[0]['dtype'], |
| 735 | lite.constants.FLOAT16) |
| 736 | elif is_post_training_quantized: |
| 737 | # Verify that bias constants is int32 type. |
| 738 | self.assertEqual(interpreter.get_tensor_details()[0]['dtype'], |
| 739 | lite.constants.INT32) |
| 740 | else: |
| 741 | raise ValueError('Invalid test options.') |
| 742 | |
| 743 | def testInvalidQuantizeFloat16(self): |
| 744 | with ops.Graph().as_default(): |
nothing calls this directly
no test coverage detected