Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
check_onnx_model_mobile_usability.py48 linesDownload Raw Back to tools
1# Copyright (c) Microsoft Corporation. All rights reserved.
2# Licensed under the MIT License.
3
4import argparse
5import logging
6import pathlib
7
8# need this before the mobile helper imports for some reason
9logging.basicConfig(format="%(levelname)s:  %(message)s")
10
11from .mobile_helpers import usability_checker  # noqa: E402
12
13
14def check_usability():
15    parser = argparse.ArgumentParser(
16        description="""Analyze an ONNX model to determine how well it will work in mobile scenarios.""",
17        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18    )
19    parser.add_argument("--log_level", choices=["debug", "info"], default="info", help="Logging level")
20    parser.add_argument("model_path", help="Path to ONNX model to check", type=pathlib.Path)
21
22    args = parser.parse_args()
23    logger = logging.getLogger("check_usability")
24
25    if args.log_level == "debug":
26        logger.setLevel(logging.DEBUG)
27    elif args.log_level == "info":
28        logger.setLevel(logging.INFO)
29    elif args.log_level == "warning":
30        logger.setLevel(logging.WARNING)
31    else:
32        logger.setLevel(logging.ERROR)
33
34    try_eps = usability_checker.analyze_model(args.model_path, skip_optimize=False, logger=logger)
35
36    if try_eps:
37        logger.info(
38            "As NNAPI or CoreML may provide benefits with this model it is recommended to compare the "
39            "performance of the model using the NNAPI EP on Android, and the CoreML EP on iOS, "
40            "against the performance using the CPU EP."
41        )
42    else:
43        logger.info("For optimal performance the model should be used with the CPU EP. ")
44
45
46if __name__ == "__main__":
47    check_usability()
48 
codekingpro/portable-devtools · Team Ai