Pytorch model running in Android

rahul_aggarwal: <p>I have made a simple MNIST digit recognition model using pytorch.<br>
Now i want an android app in which a camera opens and takes a picture from it, then that image runs in that model of mnist and tells the output…<br>
My question is that how can i implement pytorch model in andoroid; do i need to convert it to Tensorflow/keras first? i tried that also but couldn’t do it…<br>
please someone either tell me how to run the model directly on android or how can i convert the model into tenorflow…</p>
<p>kindly tell in step by step that i can follow easily… Thank you in Advance !</p>

ptrblck: <p>You can use ONNX to export your models to Caffe2 and run it on an Android device.<br>
Have a look at <a href="https://pytorch.org/tutorials/advanced/super_resolution_with_caffe2.html">this</a> or <a href="https://github.com/onnx/tutorials/blob/master/tutorials/PytorchCaffe2MobileSqueezeNet.ipynb">this</a> tutorial.</p>

rahul_aggarwal: <p>Bro thanks for replying, but one more issue that i am facing is -</p>
<p>I run all my python code in Jupyter notebook, i have created a virtual environment and installed all the libraries in it…<br>
I have also installed onnx using pip… It says successfuly installed.<br>
But when i try to import onnx in Jupyter it says - “No module named onnx” as if it is not installed, what to do please help…</p>
<p>I have installed protobuf also… Still no luck!</p>

ptrblck: <p>Probably you didn’t select the right kernel. You can see your current selected kernel in the upper right.<br>
To change it go to Kernel -&gt; Change Kernel -&gt; Select your kernel.</p>

cedrickchee: <p>Hi Rahul,</p>
<p>I am experiencing the same problem. I started this project to document down all the steps, issues and problems I experience along the way in my attempt to ship a neural network developed in PyTorch 1.0 nightly to Android devices:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md" target="_blank" rel="nofollow noopener">cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md</a></h4>
<pre><code class="lang-md"># Fast.ai Mobile Camera

:tada: Check out a working PyTorch-Caffe2 implementation on mobile: :tada:

- [Android Camera app demo (video)](https://youtu.be/TYkoaVNCMos)

**Guide - How I Shipped a Neural Network on Android/iOS Phones with PyTorch and Android Studio/Xcode**

I'll walk you through every step, from problem all the way to building and deploying the Android/iOS app to mobile phones.

- Learn how to ship SqueezeNet from PyTorch to Caffe2 + Android/iOS app. Please take a look at this [notebook](https://nbviewer.jupyter.org/github/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/shipping_squeezenet_from_pytorch_to_android.ipynb).
  - Android project for AI Camera app tutorial in this [notebook](https://nbviewer.jupyter.org/github/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/shipping_squeezenet_from_pytorch_to_android.ipynb#Fast.ai-Mobile-Camera-Project).
  - iOS project (TBD)
- Get started with an introduction to [Open Neural Network Exchange format (ONNX)](https://onnx.ai/) in this Jupyter [notebook](https://nbviewer.jupyter.org/github/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/onnx_from_pytorch_to_caffe2.ipynb).

[Source code for the Android app](https://github.com/cedrickchee/pytorch-android).

---

## Background
</code></pre>

  This file has been truncated. <a href="https://github.com/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>The specific error “No module named onnx” and the resolutions are available in this project. You can jump to this specific Jupyter <a href="https://nbviewer.jupyter.org/github/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/onnx_from_pytorch_to_caffe2.ipynb#Install-ONNX" rel="nofollow noopener">notebook</a> and find your answer there.</p>
<p>I hope that is useful.</p>

Phoum: <p>Hi Rahul,</p>
<p>you can also install pytorch directly on Android using Termux/Archlinux</p>
<p>then take picture with Termux-API [termux-camera-photo]</p>
<p>The performance is enough for testings <a href="https://youtu.be/ox9TxZhmJ30" rel="nofollow noopener">https://youtu.be/ox9TxZhmJ30</a></p>
<p>I started this github to describe how to do it.</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://assets-cdn.github.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/smscryptor/pytorch-on-android" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars3.githubusercontent.com/u/4968486?s=400&amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/smscryptor/pytorch-on-android" target="_blank" rel="nofollow noopener">smscryptor/pytorch-on-android</a></h3>

<p>setup pytorch on android. Contribute to smscryptor/pytorch-on-android development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Regards</p>

herleeyandi: <p>Since we have pytorch 1 now, Should we install caffe2 also?, How about if the project contains a pytorch c++ layer?</p>

Oktai15: <p><a class="mention" href="/u/herleeyandi">@herleeyandi</a> did you find a solution for pytorch 1.0 with c++ layer?</p>

tom: <p>In the meantime, I put up my <a href="https://github.com/pytorch/pytorch/pull/16242" rel="nofollow noopener">patch to build libtorch for Android</a>. I was going to write a tutorial, too, but it’s hard to find time for it.</p>
<p>Best regards</p>
<p>Thomas</p>

Oktai15: <p>Will wait a merging!</p>
<p>VERY useful PR, great work!</p>

herleeyandi: <p><a class="mention" href="/u/oktai15">@Oktai15</a> Not yet, since not many tutorials available, I am just waiting <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"> .</p>

Abdullah_Shah: <p><a class="mention" href="/u/ptrblck">@ptrblck</a><br>
chatbot model using pytorch.<br>
How i embed on android app, how can i implement pytorch model in andoroid?<br>
please someone either tell how can i convert the model into tenorflow…</p>
<p>kindly tell in step by step that i can follow easily… Thanks in Advance !</p>

Khairul_Imam: <p>Using onnx --&gt; caffe2 pipeline had never really succeed for me. It’s always there’s cpp compile error regarding to some undefined Tensor function. Sad.</p>