Is there any way to run PyTorch code on a drone?

Prateek_Malhotra: <p>I’m currently using PyTorch for working on a crowd density analysis application and I’m very interested in analysing, in real-time, the video captured using a camera-equipped drone. How does one go about doing this? Sorry if this isn’t the most appropriate forum for this question!</p>

ptrblck: <p>Which micro controller are you using in your drone?<br>
Depending on this, it could be better to export your Pytorch model with <a href="https://github.com/onnx/onnx" rel="nofollow noopener">onnx</a> to another framework, e.g. Caffe2.</p>

Prateek_Malhotra: <p>Thanks for the helpful reply! I have an Arduino UNO microcontroller to control my drone</p>

ptrblck: <p>I’m not familiar with Arduino, but you could try to build Pytorch from source.<br>
What kind of use case do you have in mind?</p>

Prateek_Malhotra: <p>I noticed in one of your answers you talk about running PyTorch on Raspberry Pi 3; I might use that! The specific use case is that I want to run my model real-time on top of a drone (w camera) and observe the results on my computer.</p>

ptrblck: <p>Sure, you could try that.<br>
Let me know, if you got stuck compiling it.</p>
<p>May I know what your use case is?<br>
Real-time on image data on an embedded platform could be tricky <img src="//discuss.pytorch.org/images/emoji/apple/wink.png?v=5" title=":wink:" class="emoji" alt=":wink:"></p>

Rodo_Lillo: <p>I suggest you the use of opencv in case you need to work with rt video in a raspberry. Adrian Rosebrock has a complete blog about it  <a href="https://www.pyimagesearch.com" rel="nofollow noopener">https://www.pyimagesearch.com</a></p>

srohit0: <p>We are working on making pytorch models run on microcontrollers including arduino family. Let us know, if you want to collaborate.</p>

srohit0: <p>Check out  <a href="http://bit.ly/deep-C" rel="nofollow noopener">deepC compiler</a> and <a href="https://bit.ly/aits-arduino" rel="nofollow noopener">arduino library</a> for an easy approach to put pytorch &amp; onnx models into Arduino micro-controller applications including drone.</p>

mohaimen: <p><a class="mention" href="/u/srohit0">@srohit0</a> Is there a way to use your compiler for a quantized pytorch model?</p>

mohaimen: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>: I have quantized my pytorch model using post training quantization. Now, I want to export with ONNX and I see that ONNX does not support quantized models. Is there any other alternative you know? I am aiming to deploy my model to Arduino.</p>

mohaimen: <p><a class="mention" href="/u/srohit0">@srohit0</a> Is there a way to use your compiler for a quantized pytorch model?</p>

srohit0: <p>Of course <a class="mention" href="/u/mohaimen">@mohaimen</a>.</p>
<p>Use <a href="https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html#post-training-static-quantization" rel="noopener nofollow ugc">https://pytorch.org/tutorials/advanced/static_quantization_tutorial.html#post-training-static-quantization</a> to quantize the model before exporting and you’re all set.</p>
<p>Now, you can use <a href="http://tinyml.studio/" rel="noopener nofollow ugc">http://tinyml.studio/</a> without downloading and installing deepC.</p>

srohit0: <p>Here is a brief talk on how to bring PyTorch models to IoT or MCU device on a drone.</p>
<div class="youtube-onebox lazy-video-container" data-video-id="CLKATaIQHi8" data-video-title="ONNX on MCUs" data-provider-name="youtube">
  <a href="https://www.youtube.com/watch?v=CLKATaIQHi8" target="_blank" rel="noopener nofollow ugc">
    <img class="youtube-thumbnail" src="https://img.youtube.com/vi/CLKATaIQHi8/hqdefault.jpg" title="ONNX on MCUs" width="480" height="360">
  </a>
</div>


kamathis4: <p><a class="mention" href="/u/mohaimen">@mohaimen</a> <a class="mention" href="/u/srohit0">@srohit0</a> I tried using quant wrapper for a pretrained faster RCNN model, the model size reduced significantly but the speed didn’t.<br>
Could you tell if there are better ways for pre-trained model quantization?</p>

mohaimen: <p><a class="mention" href="/u/kamathis4">@kamathis4</a> I tried using deepC last year ago and I could not make it work. At that time, it did not support quantised models. Finally, what I did was building the same model in tensorflow (TF), quantize the model using TF Lite and then deploy the model in Sony Spresense using TF Lite Micro. The TF Lite quantization reduced the model accuracy significantly (about 14%).</p>
<p>Therefore, the answer to your question is Yes. There are different frameworks in the market, and found TF was the only matured framework that would let you deploy something on microcontrollers. Having said that, there is always option to build your nets in C/C++ which can directly be ported to the microcontrollers. You just need some programming skills.</p>

Dicko: <p>Hi folks,</p>
<p>Has anyone had success putting the PyTorch model on an Arduino yet? I would like to put my trained DETR model onto an Arduino (WAN 1310). If anyone has had any success and would not mind sharing the details with me, that would be great.</p>
<p>Thanks.</p>
<p>Also here is a link to a Deep Learning discussion group in Discord.</p>
<p>hey come check out Discord with me <a href="https://discord.gg/GPtvhkWX" class="inline-onebox" rel="noopener nofollow ugc">Deep Learning and Microcontrollers</a></p>