-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathindex.html
1077 lines (863 loc) · 61.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T8XT4PS');</script>
<!-- End Google Tag Manager -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?">
<title>
Android | PyTorch
</title>
<meta name="robots" content="index, follow" />
<meta name="description" content="
Note
PyTorch Mobile is no longer actively supported. Please check out ExecuTorch, PyTorch’s all-new on-device inference library. You can also review this page to learn more about how to use ExecuTorch to build an Android app.
" />
<meta property="og:image" content="https://pytorch.org/assets/images/social-share.jpg" />
<meta name="twitter:image" content="https://pytorch.org/assets/images/social-share.jpg" />
<meta property="og:locale" content="en_US" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Android" />
<meta property="og:description" content="
Note
PyTorch Mobile is no longer actively supported. Please check out ExecuTorch, PyTorch’s all-new on-device inference library. You can also review this page to learn more about how to use ExecuTorch to build an Android app.
" />
<meta property="og:site_name" content="PyTorch" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="Android" />
<meta name="twitter:description" content="
Note
PyTorch Mobile is no longer actively supported. Please check out ExecuTorch, PyTorch’s all-new on-device inference library. You can also review this page to learn more about how to use ExecuTorch to build an Android app.
" />
<link rel="stylesheet" href="/assets/main.css">
<script src="/assets/vendor/jquery.min.js"></script>
<script src="/assets/vendor/popper.min.js"></script>
<script src="/assets/vendor/bootstrap.min.js"></script>
<script src="/assets/vendor/anchor.min.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
inlineMath: [['$','$']]
}
});
</script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js"></script>
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window,document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '243028289693773');
fbq('track', 'PageView');
</script>
<noscript>
<img height="1" width="1"
src="https://www.facebook.com/tr?id=243028289693773&ev=PageView
&noscript=1"/>
</noscript>
<!-- Twitter universal website tag code -->
<img height="1" width="1" style="display:none;" alt="" src="https://analytics.twitter.com/i/adsct?p_id=Twitter&p_user_id=0&txn_id=o2gi1&events=%5B%5B%22pageview%22%2Cnull%5D%5D&tw_sale_amount=0&tw_order_quantity=0 (https://urldefense.proofpoint.com/v2/url?u=https-3A__analytics.twitter.com_i_adsct-3Fp-5Fid-3DTwitter-26p-5Fuser-5Fid-3D0-26txn-5Fid-3Do2gi1-26events-3D-255B-255B-2522pageview-2522-252Cnull-255D-255D-26tw-5Fsale-5Famount-3D0-26tw-5Forder-5Fquantity-3D0&d=DwMGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=GMr8XYCDyeQQZuD3noL91A&m=dAJyokk16UvYy-vMrGn_JwYiGfp_eEgo25B9iGDCG-A&s=o6i4D0V0088WH2RnzIoqiF-vj45PL-2sTrsxQ0SNO3A&e=)" />
<img height="1" width="1" style="display:none;" alt="" src="//t.co/i/adsct?p_id=Twitter&p_user_id=0&txn_id=o2gi1&events=%5B%5B%22pageview%22%2Cnull%5D%5D&tw_sale_amount=0&tw_order_quantity=0 (https://urldefense.proofpoint.com/v2/url?u=https-3A__linkprotect.cudasvc.com_url-3Fa-3Dhttp-253a-252f-252ft.co-252fi-252fadsct-253fp-5Fid-253dTwitter-2526p-5Fuser-5Fid-253d0-2526txn-5Fid-253do2gi1-2526events-253d-25255B-25255B-252522pageview-252522-25252Cnull-25255D-25255D-2526tw-5Fsale-5Famount-253d0-2526tw-5Forder-5Fquantity-253d0-26c-3DE-2C1-2CC33dLwIhtuEcl5FhdztSnUwsioeej5k-2DWy0RYREBAq51kGji32A2Cw94YU9vQBpY5tPN3AukEw3C-5F-2DlbtndnLoR7-5FA-5FLoH0Rr7zLtP1ykptN-26typo-3D1&d=DwMGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=GMr8XYCDyeQQZuD3noL91A&m=dAJyokk16UvYy-vMrGn_JwYiGfp_eEgo25B9iGDCG-A&s=Abgc3XBkhESv8XBYtLchdDZyISGsK6v_BB6cLMJGyCw&e=)" />
<!-- End Twitter universal website tag code -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
<link href="/feed.xml" type="application/atom+xml" rel="alternate" title="Pythorch Blog Posts" />
</head>
<body class="mobile">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T8XT4PS"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<div class="hello-bar">
<div class="container">
Join us at PyTorch Conference in San Francisco, October 22-23. CFP open now! <a target="_blank" href="https://events.linuxfoundation.org/pytorch-conference/">Learn more</a>.
</div>
</div>
<div class="container-fluid header-holder mobile-header">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://pytorch.org" aria-label="PyTorch"></a>
<div class="main-menu">
<ul>
<li class="main-menu-item">
<div id="dropdownMenuButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Learn
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/get-started">
<span class=dropdown-title>Get Started</span>
<p>Run PyTorch locally or get started quickly with one of the supported cloud platforms</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/">
<span class="dropdown-title">Tutorials</span>
<p>Whats new in PyTorch tutorials</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/beginner/basics/intro.html">
<span class="dropdown-title">Learn the Basics</span>
<p>Familiarize yourself with PyTorch concepts and modules</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/recipes/recipes_index.html">
<span class="dropdown-title">PyTorch Recipes</span>
<p>Bite-size, ready-to-deploy PyTorch code examples</p>
</a>
<a class="nav-dropdown-item" href="https://pytorch.org/tutorials/beginner/introyt.html">
<span class="dropdown-title">Intro to PyTorch - YouTube Series</span>
<p>Master PyTorch basics with our engaging YouTube tutorial series</p>
</a>
<a class="nav-dropdown-item" href="/new">
<span class="dropdown-title">New to PyTorch Foundation</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="dropdownMenuButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Ecosystem
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://landscape.pytorch.org/" target="_blank">
<span class="dropdown-title">Tools</span>
<p>Learn about the tools and frameworks in the PyTorch Ecosystem</p>
</a>
<a class="nav-dropdown-item" href="/join-ecosystem">
<span class="dropdown-title">Join the Ecosystem</span>
</a>
<a class="nav-dropdown-item" href="/#community-module">
<span class=dropdown-title>Community</span>
<p>Join the PyTorch developer community to contribute, learn, and get your questions answered.</p>
</a>
<a class="nav-dropdown-item" href="https://discuss.pytorch.org" target="_blank">
<span class=dropdown-title>Forums</span>
<p>A place to discuss PyTorch code, issues, install, research</p>
</a>
<a class="nav-dropdown-item" href="/resources">
<span class=dropdown-title>Developer Resources</span>
<p>Find resources and get questions answered</p>
</a>
<a class="nav-dropdown-item" href="/ecosystem/contributor-awards-2024">
<span class="dropdown-title">Contributor Awards - 2024</span>
<p>Award winners announced at this year's PyTorch Conference</p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="dropdownMenuButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Edge
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/edge">
<span class="dropdown-title">About PyTorch Edge</span>
<p>Build innovative and privacy-aware AI experiences for edge devices</p>
</a>
<a class="nav-dropdown-item" href="/executorch-overview">
<span class="dropdown-title">ExecuTorch</span>
<p>End-to-end solution for enabling on-device inference capabilities across mobile and edge devices</p>
</a>
<a class="nav-dropdown-item" target="_blank" href="https://pytorch.org/executorch/stable/index.html">
<span class="dropdown-title">ExecuTorch Documentation</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="docsDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Docs
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="https://pytorch.org/docs">
<span class="dropdown-title">PyTorch</span>
<p>Explore the documentation for comprehensive guidance on how to use PyTorch.</p>
</a>
<a class="nav-dropdown-item" href="/pytorch-domains">
<span class="dropdown-title">PyTorch Domains</span>
<p> Read the PyTorch Domains documentation to learn more about domain-specific libraries.</p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="dropdownMenuButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
Blog & News
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/blog">
<span class="dropdown-title">PyTorch Blog</span>
<p>Catch up on the latest technical news and happenings</p>
</a>
<a class="nav-dropdown-item" href="/community-blog">
<span class="dropdown-title">Community Blog</span>
<p>Stories from the PyTorch ecosystem</p>
</a>
<a class="nav-dropdown-item" href="/videos">
<span class="dropdown-title">Videos</span>
<p>Learn about the latest PyTorch tutorials, new, and more </p>
</a>
<a class="nav-dropdown-item" href="/community-stories">
<span class="dropdown-title">Community Stories</span>
<p>Learn how our community solves real, everyday machine learning problems with PyTorch</p>
</a>
<a class="nav-dropdown-item" href="/events">
<span class=dropdown-title>Events</span>
<p>Find events, webinars, and podcasts</p>
</a>
<a class="nav-dropdown-item" href="/newsletter">
<span class=dropdown-title>Newsletter</span>
<p>Stay up-to-date with the latest updates</p>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<div id="resourcesDropdownButton" data-toggle="resources-dropdown" class="resources-dropdown">
<a class="with-down-arrow">
About
</a>
<div class="resources-dropdown-menu">
<a class="nav-dropdown-item" href="/foundation">
<span class=dropdown-title>PyTorch Foundation</span>
<p>Learn more about the PyTorch Foundation.</p>
</a>
<a class="nav-dropdown-item" href="/governing-board">
<span class=dropdown-title>Governing Board</span>
</a>
<a class="nav-dropdown-item" href="/credits">
<span class=dropdown-title>Cloud Credit Program</span>
</a>
<a class="nav-dropdown-item" href="/tac">
<span class=dropdown-title>Technical Advisory Council</span>
</a>
<a class="nav-dropdown-item" href="/staff">
<span class=dropdown-title>Staff</span>
</a>
<a class="nav-dropdown-item" href="/contact-us">
<span class=dropdown-title>Contact Us</span>
</a>
</div>
</div>
</li>
<li class="main-menu-item">
<a href="/join" data-cta="join">
Become a Member
</a>
</li>
<li class="main-menu-item" id="github-main-menu-link">
<a href="https://github.com/pytorch/pytorch" title="Go to PyTorch GitHub">
<div id="topnav-gh-icon"></div>
</a>
</li>
<li class="navSearchWrapper reactNavSearchWrapper" key="search">
<div class="search-border">
<div id="search-icon"></div>
<input
id="search-input"
type="text"
title="Search"
/>
<div id="close-search">X</div>
</div>
</li>
</ul>
</div>
<script src="/assets/main-menu-dropdown.js"></script>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<div class="main-background mobile-background"></div>
<div class="jumbotron jumbotron-fluid on-dark-background">
<div class="container">
<h1>PyTorch Mobile</h1>
<p class="lead">End-to-end workflow from Training to Deployment for iOS and Android mobile devices</p>
</div>
</div>
<div class="main-content-wrapper">
<div class="main-content">
<div class="container-fluid nav-menu-wrapper">
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light main-content-menu">
<ul class="navbar-nav">
<li class="nav-item nav-select">
<a class="nav-link" data-id="home" href="/mobile/home/">Home</a>
</li>
<li class="nav-item nav-select">
<a class="nav-link" data-id="ios" href="/mobile/ios/">iOS</a>
</li>
<li class="nav-item nav-select">
<a class="nav-link" data-id="android" href="/mobile/android/">Android</a>
</li>
</ul>
</nav>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="sticky-top mobile-page-sidebar">
<p id="shortcuts-menu">Shortcuts</p>
<ul id="mobile-page-sidebar-list"></ul>
</div>
</div>
<div class="col-md-8 offset-md-1">
<div class="article-wrapper" data-id="">
<article class="pytorch-article">
<div class="note-card">
<h4>Note</h4>
<p>PyTorch Mobile is no longer actively supported. Please check out <a href="/executorch-overview">ExecuTorch</a>, PyTorch’s all-new on-device inference library. You can also review <a href="https://pytorch.org/executorch/stable/demo-apps-android.html">this page</a> to learn more about how to use ExecuTorch to build an Android app.</p>
</div>
<h1 id="android">Android</h1>
<h2 id="quickstart-with-a-helloworld-example">Quickstart with a HelloWorld Example</h2>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp">HelloWorld</a> is a simple image classification application that demonstrates how to use PyTorch Android API.
This application runs TorchScript serialized TorchVision pretrained resnet18 model on static image which is packaged inside the app as android asset.</p>
<h4 id="1-model-preparation">1. Model Preparation</h4>
<p>Let’s start with model preparation. If you are familiar with PyTorch, you probably should already know how to train and save your model. In case you don’t, we are going to use a pre-trained image classification model (<a href="https://pytorch.org/hub/pytorch_vision_mobilenet_v2/">MobileNetV2</a>).
To install it, run the command below:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip install torchvision
</code></pre></div></div>
<p>To serialize the model you can use python <a href="https://github.com/pytorch/android-demo-app/blob/master/HelloWorldApp/trace_model.py">script</a> in the root folder of HelloWorld app:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>import torch
import torchvision
from torch.utils.mobile_optimizer import optimize_for_mobile
model = torchvision.models.mobilenet_v2(pretrained=True)
model.eval()
example = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example)
traced_script_module_optimized = optimize_for_mobile(traced_script_module)
traced_script_module_optimized._save_for_lite_interpreter("app/src/main/assets/model.ptl")
</code></pre></div></div>
<p>If everything works well, we should have our model - <code class="language-plaintext highlighter-rouge">model.ptl</code> generated in the assets folder of android application.
That will be packaged inside android application as <code class="language-plaintext highlighter-rouge">asset</code> and can be used on the device.</p>
<p>More details about TorchScript you can find in <a href="https://pytorch.org/docs/stable/jit.html">tutorials on pytorch.org</a></p>
<h4 id="2-cloning-from-github">2. Cloning from github</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/pytorch/android-demo-app.git
cd HelloWorldApp
</code></pre></div></div>
<p>If <a href="https://developer.android.com/studio/index.html#command-tools">Android SDK</a> and <a href="https://developer.android.com/ndk/downloads">Android NDK</a> are already installed you can install this application to the connected android device or emulator with:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>./gradlew installDebug
</code></pre></div></div>
<p>We recommend you to open this project in <a href="https://developer.android.com/studio">Android Studio 3.5.1+</a>. At the moment PyTorch Android and demo applications use <a href="https://developer.android.com/studio/releases/gradle-plugin#3-5-0">android gradle plugin of version 3.5.0</a>, which is supported only by Android Studio version 3.5.1 and higher.
Using Android Studio you will be able to install Android NDK and Android SDK with Android Studio UI.</p>
<h4 id="3-gradle-dependencies">3. Gradle dependencies</h4>
<p>Pytorch android is added to the HelloWorld as <a href="https://github.com/pytorch/android-demo-app/blob/master/HelloWorldApp/app/build.gradle#L28-L29">gradle dependencies</a> in build.gradle:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>repositories {
jcenter()
}
dependencies {
implementation 'org.pytorch:pytorch_android_lite:1.9.0'
implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
}
</code></pre></div></div>
<p>Where <code class="language-plaintext highlighter-rouge">org.pytorch:pytorch_android</code> is the main dependency with PyTorch Android API, including libtorch native library for all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64).
Further in this doc you can find how to rebuild it only for specific list of android abis.</p>
<p><code class="language-plaintext highlighter-rouge">org.pytorch:pytorch_android_torchvision</code> - additional library with utility functions for converting <code class="language-plaintext highlighter-rouge">android.media.Image</code> and <code class="language-plaintext highlighter-rouge">android.graphics.Bitmap</code> to tensors.</p>
<h4 id="4-reading-image-from-android-asset">4. Reading image from Android Asset</h4>
<p>All the logic happens in <a href="https://github.com/pytorch/android-demo-app/blob/master/HelloWorldApp/app/src/main/java/org/pytorch/helloworld/MainActivity.java#L31-L69"><code class="language-plaintext highlighter-rouge">org.pytorch.helloworld.MainActivity</code></a>.
As a first step we read <code class="language-plaintext highlighter-rouge">image.jpg</code> to <code class="language-plaintext highlighter-rouge">android.graphics.Bitmap</code> using the standard Android API.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("image.jpg"));
</code></pre></div></div>
<h4 id="5-loading-mobile-module">5. Loading Mobile Module</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Module module = Module.load(assetFilePath(this, "model.ptl"));
</code></pre></div></div>
<p><code class="language-plaintext highlighter-rouge">org.pytorch.Module</code> represents <code class="language-plaintext highlighter-rouge">torch::jit::mobile::Module</code> that can be loaded with <code class="language-plaintext highlighter-rouge">load</code> method specifying file path to the serialized to file model.</p>
<h4 id="6-preparing-input">6. Preparing Input</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);
</code></pre></div></div>
<p><code class="language-plaintext highlighter-rouge">org.pytorch.torchvision.TensorImageUtils</code> is part of <code class="language-plaintext highlighter-rouge">org.pytorch:pytorch_android_torchvision</code> library.
The <code class="language-plaintext highlighter-rouge">TensorImageUtils#bitmapToFloat32Tensor</code> method creates tensors in the <a href="https://pytorch.org/vision/stable/models.html">torchvision format</a> using <code class="language-plaintext highlighter-rouge">android.graphics.Bitmap</code> as a source.</p>
<blockquote>
<p>All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224.
The images have to be loaded in to a range of <code class="language-plaintext highlighter-rouge">[0, 1]</code> and then normalized using <code class="language-plaintext highlighter-rouge">mean = [0.485, 0.456, 0.406]</code> and <code class="language-plaintext highlighter-rouge">std = [0.229, 0.224, 0.225]</code></p>
</blockquote>
<p><code class="language-plaintext highlighter-rouge">inputTensor</code>’s shape is <code class="language-plaintext highlighter-rouge">1x3xHxW</code>, where <code class="language-plaintext highlighter-rouge">H</code> and <code class="language-plaintext highlighter-rouge">W</code> are bitmap height and width appropriately.</p>
<h4 id="7-run-inference">7. Run Inference</h4>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
float[] scores = outputTensor.getDataAsFloatArray();
</code></pre></div></div>
<p><code class="language-plaintext highlighter-rouge">org.pytorch.Module.forward</code> method runs loaded module’s <code class="language-plaintext highlighter-rouge">forward</code> method and gets result as <code class="language-plaintext highlighter-rouge">org.pytorch.Tensor</code> outputTensor with shape <code class="language-plaintext highlighter-rouge">1x1000</code>.</p>
<h4 id="8-processing-results">8. Processing results</h4>
<p>Its content is retrieved using <code class="language-plaintext highlighter-rouge">org.pytorch.Tensor.getDataAsFloatArray()</code> method that returns java array of floats with scores for every image net class.</p>
<p>After that we just find index with maximum score and retrieve predicted class name from <code class="language-plaintext highlighter-rouge">ImageNetClasses.IMAGENET_CLASSES</code> array that contains all ImageNet classes.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>float maxScore = -Float.MAX_VALUE;
int maxScoreIdx = -1;
for (int i = 0; i < scores.length; i++) {
if (scores[i] > maxScore) {
maxScore = scores[i];
maxScoreIdx = i;
}
}
String className = ImageNetClasses.IMAGENET_CLASSES[maxScoreIdx];
</code></pre></div></div>
<p>In the following sections you can find detailed explanations of PyTorch Android API, code walk through for a bigger <a href="https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp">demo application</a>,
implementation details of the API, how to customize and build it from source.</p>
<h2 id="pytorch-demo-application">PyTorch Demo Application</h2>
<p>We have also created another more complex PyTorch Android demo application that does image classification from camera output and text classification in the <a href="https://github.com/pytorch/android-demo-app/tree/master/PyTorchDemoApp">same github repo</a>.</p>
<p>To get device camera output it uses <a href="https://developer.android.com/training/camerax">Android CameraX API</a>.
All the logic that works with CameraX is separated to <a href="https://github.com/pytorch/android-demo-app/blob/master/PyTorchDemoApp/app/src/main/java/org/pytorch/demo/vision/AbstractCameraXActivity.java"><code class="language-plaintext highlighter-rouge">org.pytorch.demo.vision.AbstractCameraXActivity</code></a> class.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>void setupCameraX() {
final PreviewConfig previewConfig = new PreviewConfig.Builder().build();
final Preview preview = new Preview(previewConfig);
preview.setOnPreviewOutputUpdateListener(output -> mTextureView.setSurfaceTexture(output.getSurfaceTexture()));
final ImageAnalysisConfig imageAnalysisConfig =
new ImageAnalysisConfig.Builder()
.setTargetResolution(new Size(224, 224))
.setCallbackHandler(mBackgroundHandler)
.setImageReaderMode(ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
.build();
final ImageAnalysis imageAnalysis = new ImageAnalysis(imageAnalysisConfig);
imageAnalysis.setAnalyzer(
(image, rotationDegrees) -> {
analyzeImage(image, rotationDegrees);
});
CameraX.bindToLifecycle(this, preview, imageAnalysis);
}
void analyzeImage(android.media.Image, int rotationDegrees)
</code></pre></div></div>
<p>Where the <code class="language-plaintext highlighter-rouge">analyzeImage</code> method process the camera output, <code class="language-plaintext highlighter-rouge">android.media.Image</code>.</p>
<p>It uses the aforementioned <a href="https://github.com/pytorch/pytorch/blob/master/android/pytorch_android_torchvision/src/main/java/org/pytorch/torchvision/TensorImageUtils.java#L90"><code class="language-plaintext highlighter-rouge">TensorImageUtils.imageYUV420CenterCropToFloat32Tensor</code></a> method to convert <code class="language-plaintext highlighter-rouge">android.media.Image</code> in <code class="language-plaintext highlighter-rouge">YUV420</code> format to input tensor.</p>
<p>After getting predicted scores from the model it finds top K classes with the highest scores and shows on the UI.</p>
<h4 id="language-processing-example">Language Processing Example</h4>
<p>Another example is natural language processing, based on an LSTM model, trained on a reddit comments dataset.
The logic happens in <a href="https://github.com/pytorch/android-demo-app/blob/master/PyTorchDemoApp/app/src/main/java/org/pytorch/demo/nlp/TextClassificationActivity.java"><code class="language-plaintext highlighter-rouge">TextClassificattionActivity</code></a>.</p>
<p>Result class names are packaged inside the TorchScript model and initialized just after initial module initialization.
The module has a <code class="language-plaintext highlighter-rouge">get_classes</code> method that returns <code class="language-plaintext highlighter-rouge">List[str]</code>, which can be called using method <code class="language-plaintext highlighter-rouge">Module.runMethod(methodName)</code>:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> mModule = Module.load(moduleFileAbsoluteFilePath);
IValue getClassesOutput = mModule.runMethod("get_classes");
</code></pre></div></div>
<p>The returned <code class="language-plaintext highlighter-rouge">IValue</code> can be converted to java array of <code class="language-plaintext highlighter-rouge">IValue</code> using <code class="language-plaintext highlighter-rouge">IValue.toList()</code> and processed to an array of strings using <code class="language-plaintext highlighter-rouge">IValue.toStr()</code>:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> IValue[] classesListIValue = getClassesOutput.toList();
String[] moduleClasses = new String[classesListIValue.length];
int i = 0;
for (IValue iv : classesListIValue) {
moduleClasses[i++] = iv.toStr();
}
</code></pre></div></div>
<p>Entered text is converted to java array of bytes with <code class="language-plaintext highlighter-rouge">UTF-8</code> encoding. <code class="language-plaintext highlighter-rouge">Tensor.fromBlobUnsigned</code> creates tensor of <code class="language-plaintext highlighter-rouge">dtype=uint8</code> from that array of bytes.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code> byte[] bytes = text.getBytes(Charset.forName("UTF-8"));
final long[] shape = new long[]{1, bytes.length};
final Tensor inputTensor = Tensor.fromBlobUnsigned(bytes, shape);
</code></pre></div></div>
<p>Running inference of the model is similar to previous examples:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Tensor outputTensor = mModule.forward(IValue.from(inputTensor)).toTensor()
</code></pre></div></div>
<p>After that, the code processes the output, finding classes with the highest scores.</p>
<h2 id="more-pytorch-android-demo-apps">More PyTorch Android Demo Apps</h2>
<h3 id="d2go">D2go</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/D2Go">D2Go</a> demonstrates a Python script that creates the much lighter and much faster Facebook <a href="https://github.com/facebookresearch/d2go">D2Go</a> model that is powered by PyTorch 1.8, torchvision 0.9, and Detectron2 with built-in SOTA networks for mobile, and an Android app that uses it to detect objects from pictures in your photos, taken with camera, or with live camera. This demo app also shows how to use the native pre-built torchvision-ops library.</p>
<h3 id="image-segmentation">Image Segmentation</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/ImageSegmentation">Image Segmentation</a> demonstrates a Python script that converts the PyTorch <a href="https://pytorch.org/hub/pytorch_vision_deeplabv3_resnet101/">DeepLabV3</a> model and an Android app that uses the model to segment images.</p>
<h3 id="object-detection">Object Detection</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection">Object Detection</a> demonstrates how to convert the popular <a href="https://pytorch.org/hub/ultralytics_yolov5/">YOLOv5</a> model and use it in an Android app that detects objects from pictures in your photos, taken with camera, or with live camera.</p>
<h3 id="neural-machine-translation">Neural Machine Translation</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/Seq2SeqNMT">Neural Machine Translation</a> demonstrates how to convert a sequence-to-sequence neural machine translation model trained with the code in the <a href="https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html">PyTorch NMT tutorial</a> and use the model in an Android app to do French-English translation.</p>
<h3 id="question-answering">Question Answering</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/QuestionAnswering">Question Answering</a> demonstrates how to convert a powerful transformer QA model and use the model in an Android app to answer questions about PyTorch Mobile and more.</p>
<h3 id="vision-transformer">Vision Transformer</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/ViT4MNIST">Vision Transformer</a> demonstrates how to use Facebook’s latest Vision Transformer <a href="https://github.com/facebookresearch/deit">DeiT</a> model to do image classification, and how convert another Vision Transformer model and use it in an Android app to perform handwritten digit recognition.</p>
<h3 id="speech-recognition">Speech recognition</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/SpeechRecognition">Speech Recognition</a> demonstrates how to convert Facebook AI’s wav2vec 2.0, one of the leading models in speech recognition, to TorchScript and how to use the scripted model in an Android app to perform speech recognition.</p>
<h3 id="video-classification">Video Classification</h3>
<p><a href="https://github.com/pytorch/android-demo-app/tree/master/TorchVideo">TorchVideo</a> demonstrates how to use a pre-trained video classification model, available at the newly released <a href="https://github.com/facebookresearch/pytorchvideo">PyTorchVideo</a>, on Android to see video classification results, updated per second while the video plays, on tested videos, videos from the Photos library, or even real-time videos.</p>
<h2 id="pytorch-android-tutorial-and-recipes">PyTorch Android Tutorial and Recipes</h2>
<h3 id="image-segmentation-deeplabv3-on-android"><a href="https://pytorch.org/tutorials/beginner/deeplabv3_on_android.html">Image Segmentation DeepLabV3 on Android</a></h3>
<p>A comprehensive step-by-step tutorial on how to prepare and run the PyTorch DeepLabV3 image segmentation model on Android.</p>
<h3 id="pytorch-mobile-performance-recipes"><a href="https://pytorch.org/tutorials/recipes/mobile_perf.html">PyTorch Mobile Performance Recipes</a></h3>
<p>List of recipes for performance optimizations for using PyTorch on Mobile.</p>
<h3 id="making-android-native-application-that-uses-pytorch-android-prebuilt-libraries"><a href="https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html">Making Android Native Application That Uses PyTorch Android Prebuilt Libraries</a></h3>
<p>Learn how to make Android application from the scratch that uses LibTorch C++ API and uses TorchScript model with custom C++ operator.</p>
<h3 id="fuse-modules-recipe"><a href="https://pytorch.org/tutorials/recipes/fuse.html">Fuse Modules recipe</a></h3>
<p>Learn how to fuse a list of PyTorch modules into a single module to reduce the model size before quantization.</p>
<h3 id="quantization-for-mobile-recipe"><a href="https://pytorch.org/tutorials/recipes/quantization.html">Quantization for Mobile Recipe</a></h3>
<p>Learn how to reduce the model size and make it run faster without losing much on accuracy.</p>
<h3 id="script-and-optimize-for-mobile"><a href="https://pytorch.org/tutorials/recipes/script_optimized.html">Script and Optimize for Mobile</a></h3>
<p>Learn how to convert the model to TorchScipt and (optional) optimize it for mobile apps.</p>
<h3 id="model-preparation-for-android-recipe"><a href="https://pytorch.org/tutorials/recipes/model_preparation_android.html">Model Preparation for Android Recipe</a></h3>
<p>Learn how to add the model in an Android project and use the PyTorch library for Android.</p>
<h2 id="building-pytorch-android-from-source">Building PyTorch Android from Source</h2>
<p>In some cases you might want to use a local build of PyTorch android, for example you may build custom LibTorch binary with another set of operators or to make local changes, or try out the latest PyTorch code.</p>
<p>For this you can use <code class="language-plaintext highlighter-rouge">./scripts/build_pytorch_android.sh</code> script.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone https://github.com/pytorch/pytorch.git
cd pytorch
sh ./scripts/build_pytorch_android.sh
</code></pre></div></div>
<p>The workflow contains several steps:</p>
<p>1. Build libtorch for android for all 4 android abis (armeabi-v7a, arm64-v8a, x86, x86_64)</p>
<p>2. Create symbolic links to the results of those builds:
<code class="language-plaintext highlighter-rouge">android/pytorch_android/src/main/jniLibs/${abi}</code> to the directory with output libraries
<code class="language-plaintext highlighter-rouge">android/pytorch_android/src/main/cpp/libtorch_include/${abi}</code> to the directory with headers. These directories are used to build <code class="language-plaintext highlighter-rouge">libpytorch_jni.so</code> library, as part of the <code class="language-plaintext highlighter-rouge">pytorch_android-release.aar</code> bundle, that will be loaded on android device.</p>
<p>3. And finally run <code class="language-plaintext highlighter-rouge">gradle</code> in <code class="language-plaintext highlighter-rouge">android/pytorch_android</code> directory with task <code class="language-plaintext highlighter-rouge">assembleRelease</code></p>
<p>Script requires that Android SDK, Android NDK, Java SDK, and gradle are installed.
They are specified as environment variables:</p>
<p><code class="language-plaintext highlighter-rouge">ANDROID_HOME</code> - path to <a href="https://developer.android.com/studio/command-line/sdkmanager.html">Android SDK</a></p>
<p><code class="language-plaintext highlighter-rouge">ANDROID_NDK</code> - path to <a href="https://developer.android.com/studio/projects/install-ndk">Android NDK</a>. It’s recommended to use NDK 21.x.</p>
<p><code class="language-plaintext highlighter-rouge">GRADLE_HOME</code> - path to <a href="https://gradle.org/releases/">gradle</a></p>
<p><code class="language-plaintext highlighter-rouge">JAVA_HOME</code> - path to <a href="https://www.oracle.com/java/technologies/javase-downloads.html#javasejdk">JAVA JDK</a></p>
<p>After successful build, you should see the result as aar file:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ find android -type f -name *aar
android/pytorch_android/build/outputs/aar/pytorch_android-release.aar
android/pytorch_android_torchvision/build/outputs/aar/pytorch_android_torchvision-release.aar
</code></pre></div></div>
<h2 id="using-the-pytorch-android-libraries-built-from-source-or-nightly">Using the PyTorch Android Libraries Built from Source or Nightly</h2>
<p>First add the two aar files built above, or downloaded from the nightly built PyTorch Android repos at <a href="https://oss.sonatype.org/#nexus-search;quick~pytorch_android">here</a> and <a href="https://oss.sonatype.org/#nexus-search;quick~torchvision_android">here</a>, to the Android project’s <code class="language-plaintext highlighter-rouge">lib</code> folder, then add in the project’s app <code class="language-plaintext highlighter-rouge">build.gradle</code> file:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>allprojects {
repositories {
flatDir {
dirs 'libs'
}
}
}
dependencies {
// if using the libraries built from source
implementation(name:'pytorch_android-release', ext:'aar')
implementation(name:'pytorch_android_torchvision-release', ext:'aar')
// if using the nightly built libraries downloaded above, for example the 1.8.0-snapshot on Jan. 21, 2021
// implementation(name:'pytorch_android-1.8.0-20210121.092759-172', ext:'aar')
// implementation(name:'pytorch_android_torchvision-1.8.0-20210121.092817-173', ext:'aar')
...
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.facebook.fbjni:fbjni-java-only:0.0.3'
}
</code></pre></div></div>
<p>Also we have to add all transitive dependencies of our aars. As <code class="language-plaintext highlighter-rouge">pytorch_android</code> depends on <code class="language-plaintext highlighter-rouge">com.android.support:appcompat-v7:28.0.0</code> or <code class="language-plaintext highlighter-rouge">androidx.appcompat:appcompat:1.2.0</code>, we need to one of them. (In case of using maven dependencies they are added automatically from <code class="language-plaintext highlighter-rouge">pom.xml</code>).</p>
<h2 id="using-the-nightly-pytorch-android-libraries">Using the Nightly PyTorch Android Libraries</h2>
<p>Other than using the aar files built from source or downloaded from the links in the previous section, you can also use the nightly built Android PyTorch and TorchVision libraries by adding in your app <code class="language-plaintext highlighter-rouge">build.gradle</code> file the maven url and the nightly libraries implementation as follows:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>repositories {
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
...
implementation 'org.pytorch:pytorch_android:1.8.0-SNAPSHOT'
implementation 'org.pytorch:pytorch_android_torchvision:1.8.0-SNAPSHOT'
}
</code></pre></div></div>
<p>This is the easiest way to try out the latest PyTorch code and the Android libraries, if you do not need to make any local changes. But be aware you may need to build the model used on mobile in the latest PyTorch - using either the latest PyTorch code or a quick nightly install with commands like <code class="language-plaintext highlighter-rouge">pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html</code> - to avoid possible model version mismatch errors when running the model on mobile.</p>
<h2 id="custom-build">Custom Build</h2>
<p>To reduce the size of binaries you can do custom build of PyTorch Android with only set of operators required by your model.
This includes two steps: preparing the list of operators from your model, rebuilding pytorch android with specified list.</p>
<p>1. Verify your PyTorch version is 1.4.0 or above. You can do that by checking the value of <code class="language-plaintext highlighter-rouge">torch.__version__</code>.</p>
<p>2. Preparation of the list of operators</p>
<p>List of operators of your serialized torchscript model can be prepared in yaml format using python api function <code class="language-plaintext highlighter-rouge">torch.jit.export_opnames()</code>.
To dump the operators in your model, say <code class="language-plaintext highlighter-rouge">MobileNetV2</code>, run the following lines of Python code:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Dump list of operators used by MobileNetV2:
import torch, yaml
model = torch.jit.load('MobileNetV2.pt')
ops = torch.jit.export_opnames(model)
with open('MobileNetV2.yaml', 'w') as output:
yaml.dump(ops, output)
</code></pre></div></div>
<p>3. Building PyTorch Android with prepared operators list.</p>
<p>To build PyTorch Android with the prepared yaml list of operators, specify it in the environment variable <code class="language-plaintext highlighter-rouge">SELECTED_OP_LIST</code>. Also in the arguments, specify which Android ABIs it should build; by default it builds all 4 Android ABIs.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code># Build PyTorch Android library customized for MobileNetV2:
SELECTED_OP_LIST=MobileNetV2.yaml scripts/build_pytorch_android.sh arm64-v8a
</code></pre></div></div>
<p>After successful build you can integrate the result aar files to your android gradle project, following the steps from previous section of this tutorial (Building PyTorch Android from Source).</p>
<h2 id="use-pytorch-jit-interpreter">Use PyTorch JIT interpreter</h2>
<p>PyTorch JIT interpreter is the default interpreter before 1.9 (a version of our PyTorch interpreter that is not as size-efficient). It will still be supported in 1.9, and can be used via <code class="language-plaintext highlighter-rouge">build.gradle</code>:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>repositories {
jcenter()
}
dependencies {
implementation 'org.pytorch:pytorch_android:1.9.0'
implementation 'org.pytorch:pytorch_android_torchvision:1.9.0'
}
</code></pre></div></div>
<h2 id="android-tutorials">Android Tutorials</h2>
<p>Watch the following <a href="https://youtu.be/5Lxuu16_28o">video</a> as PyTorch Partner Engineer Brad Heintz walks through steps for setting up the PyTorch Runtime for Android projects:</p>
<p><a href="https://youtu.be/5Lxuu16_28o" title="PyTorch Mobile Runtime for Android"><img src="https://i.ytimg.com/vi/O_2KBhkIvnc/maxresdefault.jpg" alt="PyTorch Mobile Runtime for Android" height="75%" width="75%" /></a></p>
<p>The corresponding code can be found <a href="https://github.com/pytorch/workshops/tree/master/PTMobileWalkthruAndroid">here</a>.</p>
<p>Checkout our <a href="https://pytorch.org/tutorials/recipes/mobile_perf.html">Mobile Performance Recipes</a> which cover how to optimize your model and check if optimizations helped via benchmarking.</p>
<p>In addition, follow this recipe to learn how to <a href="https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html">make Native Android Application that use PyTorch prebuilt libraries</a>.</p>
<h2 id="api-docs">API Docs</h2>
<p>You can find more details about the PyTorch Android API in the <a href="https://pytorch.org/javadoc/">Javadoc</a>.</p>
<!-- Do not remove the below script -->
<script page-id="android" src="/assets/menu-tab-selection.js"></script>
</article>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid docs-tutorials-resources">
<div class="container">
<div class="row">
<div class="col-md-4 text-center">
<h2>Docs</h2>
<p>Access comprehensive developer documentation for PyTorch</p>
<a class="with-right-arrow" href="/docs">View Docs</a>
</div>
<div class="col-md-4 text-center">
<h2>Tutorials</h2>
<p>Get in-depth tutorials for beginners and advanced developers</p>
<a class="with-right-arrow" href="https://pytorch.org/tutorials">View Tutorials</a>
</div>
<div class="col-md-4 text-center">
<h2>Resources</h2>
<p>Find development resources and get your questions answered</p>
<a class="with-right-arrow" href="/resources">View Resources</a>
</div>
</div>
</div>
</div>
<footer class="site-footer">
<div class="container footer-container">
<div class="newsletter" id="newsletter">
<p
class="newsletter__title is-style-max-width-800"><strong>Stay in touch</strong> for updates, event info, and the latest news</p>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"></script>
<script>
hbspt.forms.create({
region: "na1",
portalId: "8112310",
formId: "2fb2231c-000b-4ec5-88a0-1ab242549c9e"
});
</script>
<p
class="newsletter__privacy">By submitting this form, I consent to receive marketing emails from the LF and its projects regarding their events, training, research, developments, and related announcements. I understand that I can unsubscribe at any time using the links in the footers of the emails I receive. <a href="https://www.linuxfoundation.org/privacy/">Privacy Policy</a>.</p>
</div>
<div class="lf-grid">
<div class="footer-logo-wrapper">
<a href="https://pytorch.org" class="footer-logo">
<img src="/assets/images/logo-icon.svg" alt="PyTorch logo" width="40">
</a>
</div>
<ul class="social-links">
<li><a href="https://www.facebook.com/pytorch" target="_blank" title="PyTorch on Facebook">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="-0.51 -0.26 26.45 26.45" aria-label="Facebook"><path fill="currentColor" d="M25.497 13.075c0-2.45-.698-4.848-2.011-6.911a12.765 12.765 0 0 0-5.398-4.73A12.671 12.671 0 0 0 11.008.38a12.705 12.705 0 0 0-6.529 2.95A12.827 12.827 0 0 0 .563 9.358a12.896 12.896 0 0 0-.07 7.201 12.831 12.831 0 0 0 3.801 6.103 12.709 12.709 0 0 0 6.471 3.078v-8.957H7.53v-3.708h3.235v-2.824c0-3.213 1.903-4.988 4.813-4.988.956.014 1.909.097 2.852.25V8.67h-1.607a1.83 1.83 0 0 0-1.518.497 1.854 1.854 0 0 0-.561 1.505v2.404h3.535l-.563 3.708h-2.97v8.957a12.725 12.725 0 0 0 7.697-4.337 12.87 12.87 0 0 0 3.054-8.328z"/></svg>
</a></li>
<li><a href="https://twitter.com/pytorch" target="_blank" title="PyTorch on X">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 300 300" aria-label="X"><path fill="currentColor" d="M178.57 127.15 290.27 0h-26.46l-97.03 110.38L89.34 0H0l117.13 166.93L0 300.25h26.46l102.4-116.59 81.8 116.59h89.34M36.01 19.54H76.66l187.13 262.13h-40.66"/></svg>
</a></li>
<li><a href="https://www.youtube.com/pytorch" target="_blank" title="PyTorch on YouTube">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="0.21 0.27 34.45 25.07" aria-label="YouTube"><path fill="currentColor" d="M33.729 6.084s-.327-2.33-1.317-3.356a4.691 4.691 0 0 0-3.32-1.432c-4.634-.34-11.589-.34-11.589-.34h-.014s-6.954 0-11.59.342a4.692 4.692 0 0 0-3.32 1.432c-.993 1.025-1.315 3.354-1.315 3.354a52.189 52.189 0 0 0-.331 5.473v2.566c.014 1.829.125 3.656.331 5.472 0 0 .322 2.33 1.316 3.36 1.26 1.345 2.916 1.3 3.653 1.445 2.65.26 11.263.34 11.263.34s6.96-.01 11.597-.353a4.691 4.691 0 0 0 3.32-1.432c.993-1.026 1.316-3.356 1.316-3.356.206-1.817.316-3.644.33-5.473v-2.57a52.26 52.26 0 0 0-.33-5.472zM14.076 17.232V7.729l8.951 4.768-8.95 4.735z"/></svg>
</a></li>
<li><a href="https://www.linkedin.com/company/pytorch" target="_blank" title="PyTorch on LinkedIn">
<svg xmlns="http://www.w3.org/2000/svg" viewbox="-10.23 -10.23 531.96 531.96" aria-label="LinkedIn"><rect width="512" height="512" rx="0" fill="currentColor"/><circle fill="#000" cx="142" cy="138" r="37"/><path stroke="#000" stroke-width="66" d="M244 194v198M142 194v198"/><path fill="#000" d="M276 282c0-20 13-40 36-40 24 0 33 18 33 45v105h66V279c0-61-32-89-76-89-34 0-51 19-59 32"/></svg>
</a></li>
<li><a href="https://join.slack.com/t/pytorch/shared_invite/zt-2j2la612p-miUinTTaxXczKOJw48poHA" target="_blank" title="PyTorch Slack">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0.16 -0.03 21.19 21.19" aria-label="Slack"><path fill="currentColor" d="M4.896 13.27a2.147 2.147 0 0 1-2.141 2.142A2.147 2.147 0 0 1 .613 13.27c0-1.178.963-2.141 2.142-2.141h2.141v2.141zm1.08 0c0-1.178.962-2.141 2.141-2.141s2.142.963 2.142 2.141v5.363a2.147 2.147 0 0 1-2.142 2.141 2.147 2.147 0 0 1-2.141-2.142V13.27zm2.141-8.6a2.147 2.147 0 0 1-2.141-2.14c0-1.18.962-2.142 2.141-2.142s2.142.963 2.142 2.141v2.142H8.117zm0 1.08c1.179 0 2.141.962 2.141 2.141a2.147 2.147 0 0 1-2.141 2.142H2.755A2.147 2.147 0 0 1 .613 7.89c0-1.179.963-2.141 2.142-2.141h5.362zm8.599 2.141c0-1.179.963-2.141 2.141-2.141 1.179 0 2.143.962 2.143 2.14a2.147 2.147 0 0 1-2.142 2.142h-2.141V7.89zm-1.08 0a2.147 2.147 0 0 1-2.141 2.142 2.147 2.147 0 0 1-2.141-2.142V2.53c0-1.178.962-2.141 2.141-2.141s2.142.963 2.142 2.141v5.362zm-2.141 8.6c1.179 0 2.142.962 2.142 2.14a2.147 2.147 0 0 1-2.142 2.142 2.147 2.147 0 0 1-2.141-2.141V16.49h2.141zm0-1.08a2.147 2.147 0 0 1-2.141-2.141c0-1.179.962-2.142 2.141-2.142h5.362c1.179 0 2.142.963 2.142 2.142a2.147 2.147 0 0 1-2.142 2.142h-5.362z"></path></svg>
</a></li>
<li><a href="/wechat" title="PyTorch on WeChat">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0.14 -0.17 38.02 33.02" aria-label="WeChat"><path fill="currentColor" d="M26.289 10.976a12.972 12.972 0 0 0-8.742 3.53 10.386 10.386 0 0 0-3.224 8.795c-1.326-.164-2.535-.345-3.75-.448a2.332 2.332 0 0 0-1.273.216c-1.18.666-2.311 1.418-3.652 2.255.246-1.112.405-2.087.687-3.024a1.15 1.15 0 0 0-.523-1.52C1.737 17.902.02 13.601 1.307 9.165c1.189-4.1 4.11-6.587 8.077-7.884A13.54 13.54 0 0 1 24.18 5.617a10.135 10.135 0 0 1 2.109 5.359zM10.668 9.594a1.564 1.564 0 0 0-2.095-1.472 1.52 1.52 0 0 0-.895 1.964 1.502 1.502 0 0 0 1.391.966 1.545 1.545 0 0 0 1.598-1.46v.002zm8.15-1.566a1.567 1.567 0 0 0-1.528 1.543 1.528 1.528 0 0 0 1.571 1.492 1.52 1.52 0 0 0 1.375-2.117 1.518 1.518 0 0 0-1.415-.919l-.003.001z"></path><path fill="currentColor" d="M33.914 32.137c-1.075-.478-2.062-1.196-3.11-1.306-1.049-.11-2.145.494-3.24.605a10.821 10.821 0 0 1-8.781-2.864c-4.682-4.33-4.013-10.97 1.403-14.518 4.811-3.154 11.874-2.102 15.268 2.273a8.671 8.671 0 0 1-1.002 12.095c-1.046.929-1.422 1.693-.751 2.917.102.257.174.525.213.798zM21.68 20.292a1.264 1.264 0 1 0 .01-2.528 1.264 1.264 0 0 0-.01 2.528zm7.887-2.526a1.266 1.266 0 0 0-1.256 1.21 1.247 1.247 0 1 0 1.256-1.21z"></path></svg>
</a></li>
</ul>
</div>
<div class="privacy-policy">
<div class="copyright">
<p>© Copyright The Linux Foundation. The PyTorch Foundation is a project of The Linux Foundation.
For web site terms of use, trademark policy and other policies applicable to The PyTorch Foundation please see
<a href="https://www.linuxfoundation.org/legal/policies/">Linux Foundation Policies</a>. The PyTorch Foundation supports the PyTorch open source
project, which has been established as PyTorch Project a Series of LF Projects, LLC. For policies applicable to the PyTorch Project a Series of LF Projects, LLC,
please see <a href="https://www.lfprojects.org/policies/">LF Projects, LLC Policies</a>. <a href="https://www.linuxfoundation.org/privacy">Privacy Policy</a> and <a href="https://www.linuxfoundation.org/terms">Terms of Use</a>.</p>
</div>
</div>
</div>
</footer>
<div class="mobile-main-menu">
<div class="container-fluid">
<div class="container">
<div class="mobile-main-menu-header-container">
<a class="header-logo" href="https://pytorch.org" aria-label="PyTorch"></a>
<a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
</div>
</div>
</div>
<div class="mobile-main-menu-links-container">
<div class="main-menu">
<ul>
<li class="navSearchWrapper reactNavSearchWrapper tabletSearchWrapper" key="search">
<div class="mobile-search-border">
<input
id="mobile-search-input"
type="text"
title="Search"
/>
<div id="mobile-search-icon"></div>
</div>
</li>
<li class="resources-mobile-menu-title">
<a>Learn</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="/get-started">Get Started</a>
</li>
<li>
<a href="https://pytorch.org/tutorials">Tutorials</a>
</li>
<li>
<a href="https://pytorch.org/tutorials/beginner/basics/intro.html">Learn the Basics</a>
</li>
<li>
<a href="https://pytorch.org/tutorials/recipes/recipes_index.html">PyTorch Recipes</a>
</li>
<li>
<a href="https://pytorch.org/tutorials/beginner/introyt.html">Introduction to PyTorch - YouTube Series</a>
</li>
<li>
<a href="/new">New to PyTorch Foundation</a>
</li>
</ul>
<li class="resources-mobile-menu-title">
<a>Ecosystem</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="https://landscape.pytorch.org/">Tools</a>
</li>
<li>
<a href="/join-ecosystem">Join the Ecosystem</a>
</li>
<li>
<a href="/#community-module">Community</a>
</li>
<li>
<a href="https://discuss.pytorch.org">Forums</a>
</li>
<li>
<a href="/resources">Developer Resources</a>
</li>
<li>
<a href="/ecosystem/contributor-awards-2024">Contributor Awards - 2024</a>
</li>
</ul>
<li class="resources-mobile-menu-title">
<a>Edge</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="/edge">About PyTorch Edge</a>
</li>
<li>
<a href="/executorch-overview">ExecuTorch</a>
</li>
<li>
<a href="https://pytorch.org/executorch/stable/index.html">ExecuTorch Documentation</a>
</li>
</ul>
<li class="resources-mobile-menu-title">
<a>Docs</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="https://pytorch.org/docs">PyTorch</a>
</li>
<li>
<a href="/pytorch-domains">PyTorch Domains</a>
</li>
</ul>
<li class="resources-mobile-menu-title">
<a>Blog & News</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="/blog">PyTorch Blog</a>
</li>
<li>
<a href="/community-blog">Community Blog</a>
</li>
<li>
<a href="/videos">Videos</a>
</li>
<li>
<a href="/community-stories">Community Stories</a>
</li>
<li>
<a href="/events">Events</a>
</li>
<li>
<a href="/newsletter">Newsletter</a>
</li>
</ul>
<li class="resources-mobile-menu-title">
<a>About</a>
</li>
<ul class="resources-mobile-menu-items">
<li>
<a href="/foundation">PyTorch Foundation</a>
</li>
<li>
<a href="/governing-board">Governing Board</a>
</li>
<li>
<a href="/credits">Cloud Credit Program</a>
</li>
<li>
<a href="/tac">Technical Advisory Council</a>