Skip to content
This repository was archived by the owner on Oct 16, 2019. It is now read-only.

Commit dd8e1b2

Browse files
committed
Update app to work with Caffe2 from PyTorch master
* Android app is working with my own `resnet18_init_net_v1.pb` and `resnet18_predict_net_v1.pb` Protobuf files
1 parent e67a4ff commit dd8e1b2

File tree

1,625 files changed

+263826
-21867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,625 files changed

+263826
-21867
lines changed

.idea/caches/build_file_checksums.ser

582 Bytes
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

.idea/copyright/profiles_settings.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

.idea/misc.xml

Lines changed: 1 addition & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,96 @@ PyTorch on Android is a project to demo how to use [PyTorch](https://pytorch.org
66

77
_The source code for the demo in this repo was originally based on [AICamera repo](https://github.com/bwasti/AICamera) unchanged._
88

9-
**Project Status**: Early release. Still in heavy development. What this means is, things might be moved around quickly and things will break.
9+
**Project Status**:
10+
11+
- Early release. Still in heavy development. What this means is, things might be moved around quickly and things will break.
12+
- 2018-12-31:
13+
- 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.
14+
- [Android OSS fixes PR](https://github.com/pytorch/pytorch/pull/15509).
15+
- 2019-01-01:
16+
- I have tested the Android OSS fixes with my own `resnet18_init_net_v1.pb` and `resnet18_predict_net_v1.pb` Protobuf files and the Android app is working fine.
1017

1118
## Goal
1219

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

1522
## Documentation
1623

17-
We created a developer guide on [how to ship a convolutional neural network on Android with PyTorch and Android Studio](https://github.com/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md). Check it out!
24+
### Updating the AICamera Android app to work with Caffe2 from PyTorch/master
25+
26+
This is an example for using Caffe2 on Android.
27+
28+
1. git clone PyTorch source and switch to remote branch, `android_oss_fixes`:
29+
30+
```sh
31+
$ git clone --recursive https://github.com/pytorch/pytorch.git
32+
$ cd pytorch
33+
# 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")
34+
$ git checkout --track origin/android_oss_fixes
35+
36+
M third_party/QNNPACK
37+
M third_party/cpuinfo
38+
M third_party/fbgemm
39+
M third_party/ideep
40+
Branch 'android_oss_fixes' set up to track remote branch 'android_oss_fixes' from 'origin'.
41+
Switched to a new branch 'android_oss_fixes'
42+
```
43+
44+
2. You'll need to download the [Android NDK (latest r18 release)](https://developer.android.com/ndk/downloads/) if you have not.
45+
46+
3. First, we will compile Caffe2 Android libraries like libcaffe2, libqnnpack for arm-v7a ABI and then for x86.
47+
48+
Set build environment:
49+
- PyTorch folder is at `$PYTORCH_ROOT`
50+
- This repository folder is at `$AICAMERA_ROOT`
51+
- Android NDK folder is at `$ANDROID_NDK`
52+
53+
```sh
54+
# make sure $PYTORCH_ROOT, $AICAMERA_ROOT and $ANDROID_NDK are set
55+
export ANDROID_NDK=~/android/sdk/ndk-bundle/
56+
export PYTORCH_ROOT=~/dev/gh/pytorch/
57+
export AICAMERA_ROOT=~/dev/android/android-studio-projects/aicamera/
58+
59+
pushd $PYTORCH_ROOT
60+
```
61+
62+
Then, do the following:
63+
64+
Build Caffe2 android libs and copy them over into AICamera app folder
65+
66+
```sh
67+
./scripts/build_android.sh
68+
```
69+
If you encountered build errors related to "quantized/int8_*.cc", get the patch from this "[Update QNNPACK](https://github.com/pytorch/pytorch/pull/15561/files)" PR. Patch these files in your local copies.
70+
71+
You should see "Install configuration: "Release"" output in your terminal if the process completed successfully.
72+
73+
```sh
74+
mv build_android build_android_arm
75+
76+
# copy headers
77+
cp -r install/include/* $AICAMERA_ROOT/app/src/main/cpp/
78+
79+
# copy arm libs
80+
rm -rf $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a/
81+
mkdir $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a
82+
cp -r build_android_arm/lib/lib* $AICAMERA_ROOT/app/src/main/jniLibs/armeabi-v7a/
83+
84+
85+
./scripts/build_android.sh -DANDROID_ABI=x86
86+
mv build_android build_android_x86
87+
88+
# copy x86 libs
89+
rm -rf $AICAMERA_ROOT/app/src/main/jniLibs/x86/
90+
mkdir $AICAMERA_ROOT/app/src/main/jniLibs/x86
91+
cp -r build_android_x86/lib/lib* $AICAMERA_ROOT/app/src/main/jniLibs/x86/
92+
```
93+
94+
3. Build the AICamera app using the `Build -> Make Project` menu option in Android Studio
95+
96+
### Developer Guide
97+
98+
We created a developer guide on [how to ship a convolutional neural network (Resnet18 and SqueezeNet) on Android with PyTorch and Android Studio](https://github.com/cedrickchee/data-science-notebooks/blob/master/notebooks/deep_learning/fastai_mobile/README.md). Check it out!
1899

19100
### Step by Step Guide
20101

@@ -26,7 +107,7 @@ We'll walk you through every step, from problem all the way to building and depl
26107
- [x] Test SqueezeNet v1.1 model with your own video stream from camera
27108
- [x] Live (real-time) detection
28109
- [ ] Performance optimization
29-
- [ ] Test build in models (MobileNetV2, ShuffleNet) with your own video stream from camera
110+
- [x] Test build in models (MobileNetV2, ShuffleNet, Resnet18) with your own video stream from camera
30111
- [ ] Download your own model on the fly and test it
31112
- [ ] Manage models locally on your Android device
32113
- [ ] Memory consumption and time elapse data
@@ -36,7 +117,7 @@ We'll walk you through every step, from problem all the way to building and depl
36117
- [ ] New classifier for insect species dataset using pre-trained ImageNet weights (transfer learning)
37118
- [ ] Upload pre-trained models
38119
- [ ] Bug fixes
39-
- [ ] Fix intermittent crashes
120+
- [x] Fix intermittent crashes
40121
- [ ] React Native native module
41122

42123
## Android Project
@@ -49,17 +130,18 @@ You can download the Android project source code by running this command:
49130

50131
### Dependencies
51132

52-
- Android Studio: 3.0 and above
133+
- Android Studio: 3.2.1 and above
53134
- Android SDK
54-
- Android NDK
135+
- Android NDK r18 and above
55136
- CMake Android SKD component
56-
- Gradle 3.x
137+
- Gradle 4.6 and above
57138

58139
## Tests
59140

60141
| Device | Network | FPS (^) |
61142
| ------------------ | ------------- | --------- |
62143
| Google Nexus 6P | SqueezeNet | 3.0 |
144+
| Google Nexus 6P | Resnet18 | 0.6 |
63145
| Samsung Note 8 | SqueezeNet | _TBD_ |
64146
| Galaxy Note 3 | SqueezeNet | 4.0 |
65147
| Samsung Galaxy S7 | SqueezeNet | 5.8 |

0 commit comments

Comments
 (0)