Anyway to use c++ api on mobile to do inference task?

ngap_wei_Tham: <p>As the title mentioned, anyway to compile the c++ api and use them on android/ios? Or there exist prebuild binary?</p>

supriyar: <p>Hi,<br>
We do have a binary that you can build in order to test/run your model on an android device.<br>
Here is a link to the file (in C++) - <a href="https://github.com/pytorch/pytorch/blob/master/binaries/speed_benchmark_torch.cc" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/binaries/speed_benchmark_torch.cc</a></p>
<p>You can build the binary by running</p>
<pre><code class="lang-auto">./scripts/build_android.sh \
-DBUILD_BINARY=ON \
-DBUILD_CAFFE2_MOBILE=OFF \
-DCMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())') \
-DPYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')
</code></pre>
<p>To execute you can run</p>
<pre><code class="lang-auto">./speed_benchmark_torch --model test.pt --input_dims="1,3,224,224" --input_type=float --warmup=5 --iter 20
</code></pre>

ngap_wei_Tham: <p>Any plan to release prebuild lib like linux, mac and windows?</p>

IvanKobzarev: <p><a class="mention" href="/u/ngap_wei_tham">@ngap_wei_Tham</a><br>
Pytorch mobile is moving towards providing tooling for building custom mobile builds which include  only operators that particular model needs. As all-ops default build maybe overshoot by lib size.</p>

David_Reiss: <p>We have prebuilt libraries for Android and iOS.  The iOS tutorial uses the C++ API: <a href="https://pytorch.org/mobile/ios/" rel="nofollow noopener">https://pytorch.org/mobile/ios/</a> .  We will probably support the C++ API on Android eventually, but for now the easiest path is to use the Java API.</p>

divinho: <p>I have an example app here showing how to use a pytorch model in C++:</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="32" height="32">
      <a href="https://github.com/RuABraun/android-with-pytorch/" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/12561072?s=400&amp;v=4" class="thumbnail onebox-avatar" width="420" height="420">

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

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


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


ngap_wei_Tham: <aside class="quote no-group" data-username="David_Reiss" data-post="5" data-topic="61047">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/david_reiss/48/16743_2.png" class="avatar"> David_Reiss:</div>
<blockquote>
<p>We will probably support the C++ API on Android eventually, but for now the easiest path is to use the Java API.</p>
</blockquote>
</aside>
<p>It would be much easier to create cross-platform app if we could use c++ api, I have tried tensorflow-lite, but old of the tutorials of how to build the c++ api are too old, now trying with caffe2, hope it work.</p>
<p>ps : Hope one day there will have a c++ library which could run the inference part of deep learning models efficiently on major platforms(desktop, mobiles and embedded devices), today we have to use x-tools for y platforms for performance(and other) sake.</p>

ngap_wei_Tham: <p>Thanks, could you run object detection model by c++ api?mask-rcnn do not support yet(even it is, maybe too slow for mobile), have you tried with mobileNet quantized?Thanks</p>

ngap_wei_Tham: <p>When I build the pytorch from source, I always get the error message</p>
<p><code>src.cxx:1:10: fatal error: 'glog/stl_logging.h' file not found</code></p>
<p>os : ubuntu18.04.3 LTS 64bits<br>
ndk : r18b<br>
sdk : 29(minimum 21)<br>
gradle: 4.1.6<br>
python : 3.6, install pytorch1.3 with conda</p>
<p>Do you know how to solve it? Thanks</p>

divinho: <p>You probably need to install glog: <code>sudo apt install libgoogle-glog-dev</code></p>
<p>Don’t forget you have to build pytorch for android and set <code>TORCHPATH</code> correctly in the CMakeLists.txt file!</p>
<p>Let me know if it works.</p>

ngap_wei_Tham: <p>Thanks, don’t work for 1.3.0, haven’t tried with 1.4.0 yet</p>