How to use caffe2 on Android?

I want to use my PyTorch (0.4.0) model on Android devices.

I convert my trained model to onnx and then to caffe2 followed the tutorial, and the onnx model can run correctly.

Then I build the caffe2, but I cant find the Include files installed from Cmake. Without these include files, I cannot build the jni code for my Android project. Is there any solution ?

For me, it was not very trivial to run caffe2 on Android. But, https://github.com/caffe2/AICamera was very helpful.
I’m not sure if this is still valid for PyTorch integration with caffe2 (but it is 1.0, so you seem to use caffe2 separated from PyTorch).

Thanks for your reply. I have tried the AICamera demo code.
But when I run it with my Caffe2 model which is converted from PyTorch 1.0 (PyTorch -> ONNX -> Caffe2), the code cannot load the model successfully ( actually the Init function keeps loading model but cannot finish it).

I guess the reason is the version of Caffe2 library in AICamera is different from the new Caffe2 in PyTorch 1.0. The old Caffe2 cannot load the new model, so it cannot finish loading model.

I will try to find other ways to solve it…

Hello @rivergold
I am also trying to insert a different model to AICamera example and it throws error:
[F given_tensor_fill_op.h:27] Check failed: output->size() == values_.size() output size: 159744 given size: 75129

I have succesfully created init and predict.pb files and stored inside assets folder.
Have you encountered the above error before?
Have you finishedyour project with succes?

Thanks

I think this error caused by the different size of the tensor. Please check your init.pb and predict.pb again. Maybe this link can help you.

Thanks @rivergold for the help. I will inform you how it goes

@rivergold It is working with my init and predict files that have been created from squeezenet!! Great work by this guy! Thank you also!

Hey, I’m a newbie here. I have an Android device. I would like to know how to use it. :slight_smile:

Soumith has a PR open to fix up Android builds:

Best regards

Thomas

1 Like

Is this still working now? I followed the steps exactly and I am getting the following error

Build command failed.
Error while executing process /home/yxchng/Android/Sdk/cmake/3.10.2.4988404/bin/cmake with arguments {–build /home/yxchng/git/new3/pytorch-android/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/4] Building C object CMakeFiles/cpufeatures.dir/home/yxchng/Android/Sdk/ndk-bundle/sources/android/cpufeatures/cpu-features.c.o
[2/4] Linking C static library libcpufeatures.a
[3/4] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
FAILED: CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
/home/yxchng/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi22 --gcc-toolchain=/home/yxchng/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/yxchng/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -Dnative_lib_EXPORTS -I/home/yxchng/Android/Sdk/ndk-bundle/sources/android/cpufeatures -I…/…/…/…/src/main/cpp -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mfpu=vfpv3-d16 -fno-addrsig -march=armv7-a -mthumb -Wa,–noexecstack -Wformat -Werror=format-security -stdlib=libc++ -frtti -fexceptions -std=c++11 -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c …/…/…/…/src/main/cpp/native-lib.cpp
In file included from …/…/…/…/src/main/cpp/native-lib.cpp:8:
In file included from …/…/…/…/src/main/cpp/caffe2/core/operator.h:18:
In file included from …/…/…/…/src/main/cpp/caffe2/core/operator_gradient.h:7:
…/…/…/…/src/main/cpp/caffe2/utils/proto_utils.h:58:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
In file included from …/…/…/…/src/main/cpp/native-lib.cpp:16:
In file included from …/…/…/…/src/main/cpp/ATen/ATen.h:11:
…/…/…/…/src/main/cpp/ATen/Functions.h:3441:66: error: too few arguments to function call, expected 4, have 3
return detail::infer_type(self).as_strided(self, size, stride);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
…/…/…/…/src/main/cpp/ATen/core/Type.h:208:3: note: ‘as_strided’ declared here
virtual Tensor as_strided(const Tensor & self, IntList size, IntList stride, c10::optional<int64_t> storage_offset) const = 0;
^
In file included from …/…/…/…/src/main/cpp/native-lib.cpp:16:
In file included from …/…/…/…/src/main/cpp/ATen/ATen.h:11:
…/…/…/…/src/main/cpp/ATen/Functions.h:3444:67: error: too few arguments to function call, expected 4, have 3
return detail::infer_type(self).as_strided_(self, size, stride);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
…/…/…/…/src/main/cpp/ATen/core/Type.h:209:3: note: ‘as_strided_’ declared here
virtual Tensor & as_strided_(Tensor & self, IntList size, IntList stride, c10::optional<int64_t> storage_offset) const = 0;
^
In file included from …/…/…/…/src/main/cpp/native-lib.cpp:16:
In file included from …/…/…/…/src/main/cpp/ATen/ATen.h:11:
…/…/…/…/src/main/cpp/ATen/Functions.h:5243:37: error: no member named ‘potrs’ in ‘at::TypeExtendedInterface’
return detail::infer_type(self).potrs(self, input2, upper);
~~~~~~~~~~~~~~~~~~~~~~~~ ^
1 warning and 3 errors generated.
ninja: build stopped: subcommand failed.