2022-02-17 21:35:25 +00:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import logging
|
|
|
|
|
import pathlib
|
|
|
|
|
|
|
|
|
|
# need this before the mobile helper imports for some reason
|
2022-04-26 16:35:16 +00:00
|
|
|
logging.basicConfig(format="%(levelname)s: %(message)s")
|
2022-02-17 21:35:25 +00:00
|
|
|
|
2024-06-17 21:50:33 +00:00
|
|
|
from .mobile_helpers import usability_checker # noqa: E402
|
2022-02-17 21:35:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_usability():
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2024-06-17 21:50:33 +00:00
|
|
|
description="""Analyze an ONNX model to determine how well it will work in mobile scenarios.""",
|
2022-04-26 16:35:16 +00:00
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
|
)
|
2024-06-17 21:50:33 +00:00
|
|
|
parser.add_argument("--log_level", choices=["debug", "info"], default="info", help="Logging level")
|
2022-04-26 16:35:16 +00:00
|
|
|
parser.add_argument("model_path", help="Path to ONNX model to check", type=pathlib.Path)
|
2022-02-17 21:35:25 +00:00
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2022-04-26 16:35:16 +00:00
|
|
|
logger = logging.getLogger("check_usability")
|
2022-02-17 21:35:25 +00:00
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
if args.log_level == "debug":
|
2022-02-17 21:35:25 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2022-04-26 16:35:16 +00:00
|
|
|
elif args.log_level == "info":
|
2022-02-17 21:35:25 +00:00
|
|
|
logger.setLevel(logging.INFO)
|
2022-04-26 16:35:16 +00:00
|
|
|
elif args.log_level == "warning":
|
2022-02-17 21:35:25 +00:00
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
else:
|
|
|
|
|
logger.setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
|
|
try_eps = usability_checker.analyze_model(args.model_path, skip_optimize=False, logger=logger)
|
|
|
|
|
|
|
|
|
|
if try_eps:
|
2022-04-26 16:35:16 +00:00
|
|
|
logger.info(
|
|
|
|
|
"As NNAPI or CoreML may provide benefits with this model it is recommended to compare the "
|
2024-06-17 21:50:33 +00:00
|
|
|
"performance of the model using the NNAPI EP on Android, and the CoreML EP on iOS, "
|
|
|
|
|
"against the performance using the CPU EP."
|
2022-04-26 16:35:16 +00:00
|
|
|
)
|
2022-02-17 21:35:25 +00:00
|
|
|
else:
|
2024-06-17 21:50:33 +00:00
|
|
|
logger.info("For optimal performance the model should be used with the CPU EP. ")
|
2022-02-17 21:35:25 +00:00
|
|
|
|
|
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
if __name__ == "__main__":
|
2022-02-17 21:35:25 +00:00
|
|
|
check_usability()
|