• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    C++
  • License
    Other
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[EXPERIMENTAL] Demo of using PyTorch 1.0 inside an Android app. Test with your own deep neural network such as ResNet18/SqueezeNet/MobileNet v2 and a phone camera.

Deprecation Notice

As of this announcement PyTorch 1.3 now officialy supports an end-to-end workflow from Python to deployment on iOS and Android through PyTorch Mobile. Thank you for your interest with this project.


PyTorch on Android

PyTorch on Android is a project to demo how to use PyTorch and ONNX to build an Android mobile application doing real time object classification.

demo

The source code for the demo in this repo was originally based on AICamera repo and as of 2018-12-31, the codebase was based on Soumith's AICamera repo.

Project Status:

  • 2019-05-13:
    • Introducing PyTorch Lite โ€” a lightweight machine learning framework for ON-DEVICE mobile inference. No cumbersome ONNX, no learning Caffe2. Just PyTorch and libtorch.
  • 2018-12-31:
    • PyTorch core maintainers have updated AICamera example to work with latest PyTorch master. Once that PR is merged into PyTorch master, you can use the README below to get a working Android app, including changing the Protobuf with your own init.pb / predict.pb files.
    • Android OSS fixes PR.
  • 2019-01-01:
    • I have tested the Android OSS fixes with my own ResNet18 pre-trained ImageNet model (resnet18_init_net_v1.pb and resnet18_predict_net_v1.pb Protobuf files) and the Android app is working fine.
  • Early release. Still in heavy development. What this means is, important features are missing, things might be moved around quickly and things will break.

Goal

Make it easier to ship and test your neural network model in PyTorch on mobile devices.

Documentation

Updating the AICamera Android app to work with Caffe2 from PyTorch/master

This is an example for using Caffe2 on Android.

  1. git clone PyTorch source and switch to remote branch, android_oss_fixes:
$ git clone --recursive https://github.com/pytorch/pytorch.git
$ cd pytorch
# We are using this PyTorch master commit 39381964bbb2686cf84c78aed638985455f6d3fe (Dec 23 02:08:33 2018 -0500, "fix build_android for newest NDK, and make install work")
$ git checkout --track origin/android_oss_fixes

M third_party/QNNPACK
M third_party/cpuinfo
M third_party/fbgemm
M third_party/ideep
Branch 'android_oss_fixes' set up to track remote branch 'android_oss_fixes' from 'origin'.
Switched to a new branch 'android_oss_fixes'
  1. You'll need to download the Android NDK (latest r18 release) if you have not.

  2. First, we will compile Caffe2 Android libraries like libcaffe2, libqnnpack for arm-v7a ABI and then for x86.

Set build environment:

  • PyTorch folder is at $PYTORCH_ROOT
  • This repository folder is at $AICAMERA_ROOT
  • Android NDK folder is at $ANDROID_NDK
# make sure $PYTORCH_ROOT, $AICAMERA_ROOT and $ANDROID_NDK are set
export ANDROID_NDK=~/android/sdk/ndk-bundle/
export PYTORCH_ROOT=~/dev/gh/pytorch/
export AICAMERA_ROOT=~/dev/android/android-studio-projects/aicamera/

pushd $PYTORCH_ROOT

Then, do the following:

Build Caffe2 android libs and copy them over into AICamera app folder

./scripts/build_android.sh

If you encountered build errors related to "quantized/int8_*.cc", get the patch from this "Update QNNPACK" PR. Patch these files in your local copies.

You should see "Install configuration: "Release"" output in your terminal if the process completed successfully.

mv build_android build_android_arm

# copy headers
cp -r install/include/* $AICAMERA_ROOT/app/src/main/cpp/

# copy arm libs
rm -rf $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a/
mkdir $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a
cp -r build_android_arm/lib/lib* $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a/


./scripts/build_android.sh -DANDROID_ABI=x86
mv build_android build_android_x86

# copy x86 libs
rm -rf $AICAMERA_ROOT/app/src/main/jniLibs/x86/
mkdir $AICAMERA_ROOT/app/src/main/jniLibs/x86
cp -r build_android_x86/lib/lib* $AICAMERA_ROOT/app/src/main/jniLibs/x86/
  1. Build the AICamera app using the Build -> Make Project menu option in Android Studio

If you prefer not to build PyTorch from master, I have commited the files for the Caffe2 bits into this git repo. Unfortunately, not all files are uploaded to GitHub due to file size limit.

Grab the remaining large files from my Google Drive.

Developer Guide

We created a developer guide on how to ship a convolutional neural network (Resnet18 and SqueezeNet) on Android with PyTorch and Android Studio. Check it out!

Step by Step Guide

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

Features, Functionalities and TODO

  • Android (Java/C++) with Caffe2
  • Test SqueezeNet v1.1 model with your own video stream from camera
  • Live (real-time) detection
  • Performance optimization
  • Test build in models (MobileNetV2, ShuffleNet, Resnet18) with your own video stream from camera
  • Download your own model on the fly and test it
  • Manage models locally on your Android device
  • Memory consumption and time elapse data
  • Overall control on every layer (from beginner to expert)
  • Warm community and welcome to contribute
  • Transfer learning SqueezeNet with new datasets (i.e. insect species, not hotdog)
  • New classifier for insect species dataset using pre-trained ImageNet weights (transfer learning)
  • Upload pre-trained models
  • Bug fixes
  • Fix intermittent crashes
  • React Native native module
  • Upload large files from PyTorch 'build_android' to Google Drive and share it here

Android Project

You can download the Android project source code by running this command:

git clone https://github.com/cedrickchee/pytorch-android.git

Android Development Environment

Dependencies

  • Android Studio: 3.2.1 and above
  • Android SDK
  • Android NDK r18 and above
  • CMake Android SKD component
  • Gradle 4.6 and above

Tests

Device Network FPS (^)
Google Nexus 6P SqueezeNet 3.0
Google Nexus 6P Resnet18 0.6
Samsung Note 8 SqueezeNet TBD
Galaxy Note 3 SqueezeNet 4.0
Samsung Galaxy S7 SqueezeNet 5.8
Google Pixel SqueezeNet 5.7

Note: ^ the number of FPS is subjective to the camera photo (image) size you send to the mobile device as well as type of the device.

Demo

If building this repo is too much of a trouble for you, we also plan to put this in Google Play Store (TBD).

Network Graph Comparison

Visualize SqueezeNet 1.1 prediction network serialized as Protobuf file:

Before update (Oct 2018) After update (Jan 2019)

Performance

TBD

Future Work

Scope of work for this project: TBD

Other Useful Resources

PyTorch official forums

GitHub Issues and Commits

Fast.ai

Fast Neural Style Transfer (PyTorch โ†’ ONNXโ†’ CoreML/NNAPI)

Support

Feel free to ask any questions, from preparing development environment to debugging on Android Studio. We are happy to help you.

License

This repository contains a variety of content; some developed by Cedric Chee, and some from third-parties. The third-party content is distributed under the license provided by those parties.

I am providing code and resources in this repository to you under an open source license. Because this is my personal repository, the license you receive to my code and resources is from me and not my employer.

The content developed by Cedric Chee is distributed under the following license:

Code

The code in this repository, including all code samples in the notebooks listed above, is released under the MIT license. Read more at the Open Source Initiative.

Text

The text content of the book is released under the CC-BY-NC-ND license. Read more at Creative Commons.

More Repositories

1

awesome-transformer-nlp

A curated list of NLP resources focused on Transformer networks, attention mechanism, GPT, BERT, ChatGPT, LLMs, and transfer learning.
904
star
2

awesome-wireguard

A curated list of WireGuard tools, projects, and resources.
398
star
3

awesome-ml-model-compression

Awesome machine learning model compression research papers, tools, and learning material.
328
star
4

chatgpt-universe

ChatGPT Universe is fleeting notes on ChatGPT, GPT, and large language models (LLMs)
288
star
5

capsule-net-pytorch

[NO MAINTENANCE INTENDED] A PyTorch implementation of CapsNet architecture in the NIPS 2017 paper "Dynamic Routing Between Capsules".
Python
166
star
6

knowledge

Everything I know. My knowledge wiki. My notes (mostly for fast.ai). Document everything. Brain dump.
123
star
7

data-science-notebooks

Data science Python notebooksโ€”a collection of Jupyter notebooks on machine learning, deep learning, statistical inference, data analysis and visualization.
Jupyter Notebook
87
star
8

react-typescript-jest-enzyme-testing

Testing React.JS + TypeScript component with Jest and Enzyme. A simple example for reference.
TypeScript
52
star
9

saas-starter

Everything you need to get your next Unicorn-For-X startup off the ground.
JavaScript
43
star
10

realtime-detectron

Real-time Detectron using webcam.
Python
41
star
11

transformers-llama

LLaMA implementation for HuggingFace Transformers
Python
25
star
12

YDKGo

You Don't Know Go Yet book.
Go
24
star
13

rnnoise-nodejs

Node.js bindings to Xiph's RNNoise denoising C library
Rust
17
star
14

e-mart

Open source full stack React and Next.js online mart complete with shopping cart and real credit checkout.
JavaScript
16
star
15

pytorch-serving

[UNMAINTAINED] A starter pack for creating a lightweight responsive web app for Fast.AI PyTorch models.
Python
16
star
16

ssd-yolo-retinanet

Multi-class object detection pipelineโ€”Single Shot MultiBox Detector (SSD) + YOLOv3 (real-time) + focal loss (RetinaNet) + Pascal VOC 2007 dataset
Python
16
star
17

pytorch-lite

[Deprecated] PyTorch Lite is a lightweight machine learning framework for on-device mobile inference.
Jupyter Notebook
14
star
18

tch-js

A JavaScript and TypeScript port of PyTorch C++ library (libtorch) - Node.js N-API bindings for libtorch.
C++
9
star
19

pytorch-mobile-kit

PyTorch Mobile starter kit.
Java
8
star
20

experiments

A collection of little snippets of programs I write when I test out ideas. A code "playground".
Go
8
star
21

wasserstein-gan

PyTorch implementation of Wasserstein GAN paper
Jupyter Notebook
6
star
22

neural-network-in-13-lines

A neural network in 13 lines of Python.
Python
6
star
23

openintercom

An open source modern Intercom alternative.
JavaScript
5
star
24

react-18-beta

React 18 Beta (Suspense, concurrent rendering, HTTP streaming, Server Components) + Next.js 12.0.4 demo & benchmark (performance & UX)
JavaScript
5
star
25

painless-pg-node

Painless PostgreSQL Node.js backend with Objection + Knex + Express
JavaScript
4
star
26

fastai-course-v3

My notebooks for the 3rd edition of course.fast.ai - coming in 2019
Jupyter Notebook
4
star
27

kaggle-facial-detection

Facial keypoints detection challenge tutorial and solution for Singapore Kaggle ML Challenge meetup.
Jupyter Notebook
3
star
28

neocargo

neoCargo microservices in Go with PostgreSQL, MongoDB, Terraform, Google Kubernetes Engine, and CircleCI
Go
3
star
29

nodejs-in-depth

Master and understand deeper Node.js fundamentals and internals
JavaScript
3
star
30

kafka-eventsourcing-restapi

REST API service using Apache Kafka for event sourcing
Go
2
star
31

hou

Hou ๐Ÿ’ programming language interpreter and compiler
Go
2
star
32

dawnbench-analysis

DAWNBench analysis of CIFAR-10 time-to-accuracy.
Jupyter Notebook
2
star
33

learn-ts-handbook

Learn TypeScript in 2021 by reading the Handbook.
TypeScript
2
star
34

fastai-dl2-2017

My notebooks for fast.ai cutting edge deep learning for coders part 2 2017 course.
Jupyter Notebook
2
star
35

data-science-hacks

A collection of notebooks for engineer practicing machine learning / deep learning through hacking project-based learning.
Jupyter Notebook
2
star
36

feed

A feed of things I'm reading and will read. It's sort of like bookmarks or favorites.
2
star
37

myapp

A ruby on rails app experiments
Ruby
2
star
38

snippetbox

A web app to paste and share snippets of text
Go
2
star
39

min-torrent

Yet another minimalistic torrent client
Go
2
star
40

advent-of-code-2022

Advent of Code (AoC) 2022 in Rust
Rust
2
star
41

bitcask

My key/value store (embedded database) solution for PingCAP training courses
Rust
1
star
42

migraine_diary

My personal migraine log.
Ruby
1
star
43

personal-website

The personal website of Cedric Chee
HTML
1
star
44

postgresql-consul-demo

A minimal demo app showing PostgreSQL HA cluster managed by Patroni and Consul in Docker
Python
1
star
45

squidgame

[WIP] Red Light, Green Light game inspired by Squid Game implemented in Rust, TypeScript & WebSocket. Play in your browser, multiplayer (at least 2 players), and tiny.
Rust
1
star
46

rails323_testing

An app to learn about what's new in Rails 3.2.3 compared to 3.0.10.
Ruby
1
star
47

shuttlecock

Badminton news & results
1
star
48

first_app

Testing new workstation for rails+git installation
Ruby
1
star
49

todos

Super simple todos app. Develop using Rails 3 to learn the changes to Rails 2.3.8.
Ruby
1
star
50

tensorflow-community-builds

TensorFlow prebuilt binary (Python wheels) from source by the community.
1
star
51

rl-algorithms

A collection of Reinforcement Learning algorithms.
Jupyter Notebook
1
star
52

amethyst

My digital garden
SCSS
1
star
53

skel

Skel is an idiomatic and flexible code structure for REST API project - practical code patterns and best practices for building (developing, managing, and deploying) APIs in Go.
Go
1
star
54

dockerfile-fastai

Dockerfile for building NVIDIA CUDA image for PyTorch 1.0 and fastai 1.0 deep learning
Dockerfile
1
star
55

soshiok

A full stack restaurant app
JavaScript
1
star