Pytorch model running in Android

I have made a simple MNIST digit recognition model using pytorch.
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…
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…
please someone either tell me how to run the model directly on android or how can i convert the model into tenorflow…

kindly tell in step by step that i can follow easily… Thank you in Advance !

1 Like

You can use ONNX to export your models to Caffe2 and run it on an Android device.
Have a look at this or this tutorial.

1 Like

Bro thanks for replying, but one more issue that i am facing is -

I run all my python code in Jupyter notebook, i have created a virtual environment and installed all the libraries in it…
I have also installed onnx using pip… It says successfuly installed.
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…

I have installed protobuf also… Still no luck!

Probably you didn’t select the right kernel. You can see your current selected kernel in the upper right.
To change it go to Kernel -> Change Kernel -> Select your kernel.

Hi Rahul,

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:

The specific error “No module named onnx” and the resolutions are available in this project. You can jump to this specific Jupyter notebook and find your answer there.

I hope that is useful.

Hi Rahul,

you can also install pytorch directly on Android using Termux/Archlinux

then take picture with Termux-API [termux-camera-photo]

The performance is enough for testings

I started this github to describe how to do it.


Since we have pytorch 1 now, Should we install caffe2 also?, How about if the project contains a pytorch c++ layer?

@herleeyandi did you find a solution for pytorch 1.0 with c++ layer?

In the meantime, I put up my patch to build libtorch for Android. I was going to write a tutorial, too, but it’s hard to find time for it.

Best regards



Will wait a merging!

VERY useful PR, great work!

@Oktai15 Not yet, since not many tutorials available, I am just waiting :slight_smile: .

chatbot model using pytorch.
How i embed on android app, how can i implement pytorch model in andoroid?
please someone either tell how can i convert the model into tenorflow…

kindly tell in step by step that i can follow easily… Thanks in Advance !

Using onnx --> caffe2 pipeline had never really succeed for me. It’s always there’s cpp compile error regarding to some undefined Tensor function. Sad.