Helper to make a zip file of a bunch of TensorFlow models. This does a cartestian product of the dictionary of test_parameters and calls make_graph() for each item in the cartestian product set. If the graph is built successfully, then make_test_inputs() is called to build expected input/ou
(options,
test_parameters,
make_graph,
make_test_inputs,
extra_toco_options=ExtraTocoOptions(),
use_frozen_graph=False,
expected_tf_failures=0)
| 355 | |
| 356 | |
| 357 | def make_zip_of_tests(options, |
| 358 | test_parameters, |
| 359 | make_graph, |
| 360 | make_test_inputs, |
| 361 | extra_toco_options=ExtraTocoOptions(), |
| 362 | use_frozen_graph=False, |
| 363 | expected_tf_failures=0): |
| 364 | """Helper to make a zip file of a bunch of TensorFlow models. |
| 365 | |
| 366 | This does a cartestian product of the dictionary of test_parameters and |
| 367 | calls make_graph() for each item in the cartestian product set. |
| 368 | If the graph is built successfully, then make_test_inputs() is called to |
| 369 | build expected input/output value pairs. The model is then converted to tflite |
| 370 | with toco, and the examples are serialized with the tflite model into a zip |
| 371 | file (2 files per item in the cartesian product set). |
| 372 | |
| 373 | Args: |
| 374 | options: An Options instance. |
| 375 | test_parameters: Dictionary mapping to lists for each parameter. |
| 376 | e.g. `{"strides": [[1,3,3,1], [1,2,2,1]], "foo": [1.2, 1.3]}` |
| 377 | make_graph: function that takes current parameters and returns tuple |
| 378 | `[input1, input2, ...], [output1, output2, ...]` |
| 379 | make_test_inputs: function taking `curr_params`, `session`, `input_tensors`, |
| 380 | `output_tensors` and returns tuple `(input_values, output_values)`. |
| 381 | extra_toco_options: Additional toco options. |
| 382 | use_frozen_graph: Whether or not freeze graph before toco converter. |
| 383 | expected_tf_failures: Number of times tensorflow is expected to fail in |
| 384 | executing the input graphs. In some cases it is OK for TensorFlow to |
| 385 | fail because the one or more combination of parameters is invalid. |
| 386 | |
| 387 | Raises: |
| 388 | RuntimeError: if there are converter errors that can't be ignored. |
| 389 | """ |
| 390 | zip_path = os.path.join(options.output_path, options.zip_to_output) |
| 391 | parameter_count = 0 |
| 392 | for parameters in test_parameters: |
| 393 | parameter_count += functools.reduce( |
| 394 | operator.mul, [len(values) for values in parameters.values()]) |
| 395 | |
| 396 | if parameter_count > _MAX_TESTS_PER_ZIP: |
| 397 | raise RuntimeError( |
| 398 | "Too many parameter combinations for generating '%s'.\n" |
| 399 | "There are %d combinations while the upper limit is %d.\n" |
| 400 | "Having too many combinations will slow down the tests.\n" |
| 401 | "Please consider splitting the test into multiple functions.\n" |
| 402 | % (zip_path, parameter_count, _MAX_TESTS_PER_ZIP)) |
| 403 | |
| 404 | # TODO(aselle): Make this allow multiple inputs outputs. |
| 405 | archive = zipfile.PyZipFile(zip_path, "w") |
| 406 | zip_manifest = [] |
| 407 | convert_report = [] |
| 408 | toco_errors = 0 |
| 409 | |
| 410 | processed_labels = set() |
| 411 | |
| 412 | if options.make_edgetpu_tests: |
| 413 | extra_toco_options.inference_input_type = tf.lite.constants.QUANTIZED_UINT8 |
| 414 | extra_toco_options.inference_output_type = tf.lite.constants.QUANTIZED_UINT8 |
no test coverage detected