(self)
| 165 | # interpreter API after support has been added. |
| 166 | |
| 167 | def testQuantization(self): |
| 168 | with ops.Graph().as_default(): |
| 169 | in_tensor_1 = array_ops.placeholder( |
| 170 | shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputA') |
| 171 | in_tensor_2 = array_ops.placeholder( |
| 172 | shape=[1, 16, 16, 3], dtype=dtypes.float32, name='inputB') |
| 173 | out_tensor = array_ops.fake_quant_with_min_max_args( |
| 174 | in_tensor_1 + in_tensor_2, min=0., max=1., name='output') |
| 175 | sess = session.Session() |
| 176 | |
| 177 | # Convert model and ensure model is not None. |
| 178 | converter = lite.TFLiteConverter.from_session(sess, |
| 179 | [in_tensor_1, in_tensor_2], |
| 180 | [out_tensor]) |
| 181 | converter.inference_type = lite_constants.QUANTIZED_UINT8 |
| 182 | converter.quantized_input_stats = { |
| 183 | 'inputA': (0., 1.), |
| 184 | 'inputB': (0., 1.) |
| 185 | } # mean, std_dev |
| 186 | tflite_model = converter.convert() |
| 187 | self.assertTrue(tflite_model) |
| 188 | |
| 189 | # Check values from converted model. |
| 190 | interpreter = Interpreter(model_content=tflite_model) |
| 191 | interpreter.allocate_tensors() |
| 192 | |
| 193 | input_details = interpreter.get_input_details() |
| 194 | self.assertEqual(2, len(input_details)) |
| 195 | self.assertEqual('inputA', input_details[0]['name']) |
| 196 | self.assertEqual(np.uint8, input_details[0]['dtype']) |
| 197 | self.assertTrue(([1, 16, 16, 3] == input_details[0]['shape']).all()) |
| 198 | self.assertEqual((1., 0.), |
| 199 | input_details[0]['quantization']) # scale, zero_point |
| 200 | |
| 201 | self.assertEqual('inputB', input_details[1]['name']) |
| 202 | self.assertEqual(np.uint8, input_details[1]['dtype']) |
| 203 | self.assertTrue(([1, 16, 16, 3] == input_details[1]['shape']).all()) |
| 204 | self.assertEqual((1., 0.), |
| 205 | input_details[1]['quantization']) # scale, zero_point |
| 206 | |
| 207 | output_details = interpreter.get_output_details() |
| 208 | self.assertEqual(1, len(output_details)) |
| 209 | self.assertEqual('output', output_details[0]['name']) |
| 210 | self.assertEqual(np.uint8, output_details[0]['dtype']) |
| 211 | self.assertTrue(([1, 16, 16, 3] == output_details[0]['shape']).all()) |
| 212 | self.assertTrue(output_details[0]['quantization'][0] > 0) # scale |
| 213 | |
| 214 | def testQuantizationInvalid(self): |
| 215 | with ops.Graph().as_default(): |
nothing calls this directly
no test coverage detected