{"id":3074,"date":"2023-02-16T15:33:47","date_gmt":"2023-02-16T23:33:47","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/surface-duo\/?p=3074"},"modified":"2023-12-20T10:10:55","modified_gmt":"2023-12-20T18:10:55","slug":"onnx-machine-learning-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/surface-duo\/onnx-machine-learning-3\/","title":{"rendered":"Bringing ONNX models to Android"},"content":{"rendered":"<p>\n  Hello Android developers,\n<\/p>\n<p>\n  One of the advantages of the ONNX runtime is the ability to run locally on a variety of devices, including mobile devices. This means that your users get fast response times, but also comes with the need to respect mobile device limitations such as app size and the ability to support performance enhancements.\n<\/p>\n<p>\n  For a general overview of how to build a mobile app with ONNX machine learning, follow this <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/\">high level development flow<\/a>:\n<\/p>\n<p>\n  <img decoding=\"async\" width=\"500\" height=\"550\" src=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/02\/steps-to-build-for-mobile-platforms.png\" class=\"wp-image-3075\" alt=\"Steps to build for mobile platforms\" srcset=\"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/02\/steps-to-build-for-mobile-platforms.png 500w, https:\/\/devblogs.microsoft.com\/surface-duo\/wp-content\/uploads\/sites\/53\/2023\/02\/steps-to-build-for-mobile-platforms-273x300.png 273w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/>\n<\/p>\n<h2>Mobile vs full package reference<\/h2>\n<p>\n  When you <a href=\"https:\/\/onnxruntime.ai\/docs\/install\/#install-on-web-and-mobile\">add<\/a> the ONNX runtime to your app project, you can choose to reference pre-built packages that include support for selected operators and opset versions that work for most popular models. These \u201cmobile packages\u201d incur a smaller size footprint for your app, but mean your model can only use the <a href=\"https:\/\/onnxruntime.ai\/docs\/reference\/operators\/mobile_package_op_type_support_1.9.html\">supported opsets and operators<\/a>.\n<\/p>\n<p>\n  It\u2019s also possible to use a \u201cfull package\u201d with all ONNX opsets and operators, at the cost of larger app binary size.\n<\/p>\n<p>\n  For Android developers, packages are hosted on <code>mavenCentral<\/code> and referenced in your module\u2019s <strong>build.gradle<\/strong> file:\n<\/p>\n<pre>dependencies {\r\n   \/\/ choose one of the two below:\r\n   implementation 'com.microsoft.onnxruntime:onnxruntime-android:latest.release'  \/\/ full package\r\n   \/\/implementation 'com.microsoft.onnxruntime:onnxruntime-mobile:latest.release'  \/\/ mobile package\r\n}<\/pre>\n<p>\n  To help you decide, the <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/helpers\/\">mobile model export helpers<\/a> for ONNX runtime are scripts that can help you determine whether a model can work on the mobile package, detect or update the opset version for a model, and more. \n<\/p>\n<h3>Mobile usability checker<\/h3>\n<p>\n  The <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/helpers\/model-usability-checker.html\">model usability checker for mobile<\/a> will help you determine whether the model you wish to use will work with the \u201cmobile package\u201d (ie. check the opsets\/versions required) and also whether the platform native execution providers (such as <a href=\"https:\/\/onnxruntime.ai\/docs\/execution-providers\/NNAPI-ExecutionProvider.html\">NNAPI<\/a> for Android) are likely to improve performance. \n<\/p>\n<p>\n  The guidance from the usability checker will help you confirm that your model can be deployed to mobile devices, which runtime package you need, how to update the ONNX opset (if required), and how to convert the model to the ORT format required to use with the runtime.\n<\/p>\n<h2>Examples<\/h2>\n<p>\n  Examples using the ONNX runtime mobile package on Android include the <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/deploy-android.html\">image classification<\/a> and super <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/superres.html\">resolution demos<\/a>. You can see where to apply some of these scripts in the <a href=\"https:\/\/onnxruntime.ai\/docs\/tutorials\/mobile\/deploy-android.html#prepare-the-model-and-data-used-in-the-application\">sample build instructions<\/a>. \n<\/p>\n<h2>Resources and feedback<\/h2>\n<p>\n  More information about the ONNX Runtime is available at\u00a0<a href=\"https:\/\/onnxruntime.ai\/\" target=\"_blank\" rel=\"noopener\">onnxruntime.ai<\/a>\u00a0and also on\u00a0<a href=\"https:\/\/www.youtube.com\/onnxruntime\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>.\n<\/p>\n<p>\n  If you have any questions about applying machine learning, or would like to tell us about your apps, use the\u00a0<a href=\"http:\/\/aka.ms\/SurfaceDuoSDK-Feedback\" target=\"_blank\" rel=\"noopener\">feedback forum<\/a>\u00a0or message us on\u00a0<a href=\"https:\/\/twitter.com\/surfaceduodev\" target=\"_blank\" rel=\"noopener\">Twitter @surfaceduodev<\/a>.\n<\/p>\n<p>\n  There won\u2019t be a livestream this week, but check out the\u00a0<a href=\"https:\/\/youtube.com\/c\/surfaceduodev\" target=\"_blank\" rel=\"noopener\">archives on YouTube<\/a>. We\u2019ll see you online again soon!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Android developers, One of the advantages of the ONNX runtime is the ability to run locally on a variety of devices, including mobile devices. This means that your users get fast response times, but also comes with the need to respect mobile device limitations such as app size and the ability to support performance [&hellip;]<\/p>\n","protected":false},"author":570,"featured_media":3075,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[740],"tags":[473,729,728],"class_list":["post-3074","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-kotlin","tag-machine-learning","tag-onnx"],"acf":[],"blog_post_summary":"<p>Hello Android developers, One of the advantages of the ONNX runtime is the ability to run locally on a variety of devices, including mobile devices. This means that your users get fast response times, but also comes with the need to respect mobile device limitations such as app size and the ability to support performance [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/3074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/users\/570"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/comments?post=3074"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/posts\/3074\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media\/3075"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/media?parent=3074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/categories?post=3074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/surface-duo\/wp-json\/wp\/v2\/tags?post=3074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}