blob: a4d0717cb8094923442be847378c88d5f2c561bb [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1" name="viewport"/>
<title>Deep Learning in a Single File for Smart Devices — mxnet documentation</title>
<link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="../_static/basic.css" rel="stylesheet" type="text/css">
<link href="../_static/pygments.css" rel="stylesheet" type="text/css">
<link href="../_static/mxnet.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: ''
};
</script>
<script src="../_static/jquery-1.11.1.js" type="text/javascript"></script>
<script src="../_static/underscore.js" type="text/javascript"></script>
<script src="../_static/searchtools_custom.js" type="text/javascript"></script>
<script src="../_static/doctools.js" type="text/javascript"></script>
<script src="../_static/selectlang.js" type="text/javascript"></script>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script>
<script type="text/javascript"> jQuery(function() { Search.loadIndex("/searchindex.js"); Search.init();}); </script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new
Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-96378503-1', 'auto');
ga('send', 'pageview');
</script>
<!-- -->
<!-- <script type="text/javascript" src="../_static/jquery.js"></script> -->
<!-- -->
<!-- <script type="text/javascript" src="../_static/underscore.js"></script> -->
<!-- -->
<!-- <script type="text/javascript" src="../_static/doctools.js"></script> -->
<!-- -->
<!-- <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> -->
<!-- -->
<link href="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-icon.png" rel="icon" type="image/png"/>
</link></link></head>
<body role="document"><!-- Previous Navbar Layout
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="../" class="navbar-brand">
<img src="http://data.mxnet.io/theme/mxnet.png">
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul id="navbar" class="navbar navbar-left">
<li> <a href="../get_started/index.html">Get Started</a> </li>
<li> <a href="../tutorials/index.html">Tutorials</a> </li>
<li> <a href="../how_to/index.html">How To</a> </li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">Packages <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="../packages/python/index.html">
Python
</a></li>
<li><a href="../packages/r/index.html">
R
</a></li>
<li><a href="../packages/julia/index.html">
Julia
</a></li>
<li><a href="../packages/c++/index.html">
C++
</a></li>
<li><a href="../packages/scala/index.html">
Scala
</a></li>
<li><a href="../packages/perl/index.html">
Perl
</a></li>
</ul>
</li>
<li> <a href="../system/index.html">System</a> </li>
<li>
<form class="" role="search" action="../search.html" method="get" autocomplete="off">
<div class="form-group inner-addon left-addon">
<i class="glyphicon glyphicon-search"></i>
<input type="text" name="q" class="form-control" placeholder="Search">
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form> </li>
</ul>
<ul id="navbar" class="navbar navbar-right">
<li> <a href="../index.html"><span class="flag-icon flag-icon-us"></span></a> </li>
<li> <a href="..//zh/index.html"><span class="flag-icon flag-icon-cn"></span></a> </li>
</ul>
</div>
</div>
</div>
Previous Navbar Layout End -->
<div class="navbar navbar-fixed-top">
<div class="container" id="navContainer">
<div class="innder" id="header-inner">
<h1 id="logo-wrap">
<a href="../" id="logo"><img src="http://data.mxnet.io/theme/mxnet.png"/></a>
</h1>
<nav class="nav-bar" id="main-nav">
<a class="main-nav-link" href="../get_started/install.html">Install</a>
<a class="main-nav-link" href="../tutorials/index.html">Tutorials</a>
<a class="main-nav-link" href="../how_to/index.html">How To</a>
<span id="dropdown-menu-position-anchor">
<a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">API <span class="caret"></span></a>
<ul class="dropdown-menu" id="package-dropdown-menu">
<li><a class="main-nav-link" href="../api/python/index.html">Python</a></li>
<li><a class="main-nav-link" href="../api/scala/index.html">Scala</a></li>
<li><a class="main-nav-link" href="../api/r/index.html">R</a></li>
<li><a class="main-nav-link" href="../api/julia/index.html">Julia</a></li>
<li><a class="main-nav-link" href="../api/c++/index.html">C++</a></li>
<li><a class="main-nav-link" href="../api/perl/index.html">Perl</a></li>
</ul>
</span>
<a class="main-nav-link" href="../architecture/index.html">Architecture</a>
<!-- <a class="main-nav-link" href="../community/index.html">Community</a> -->
<a class="main-nav-link" href="https://github.com/dmlc/mxnet">Github</a>
<span id="dropdown-menu-position-anchor-version" style="position: relative"><a href="#" class="main-nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">Versions(master)<span class="caret"></span></a><ul id="package-dropdown-menu" class="dropdown-menu"><li><a class="main-nav-link" href=http://mxnet.incubator.apache.org/test/>v0.10.14</a></li><li><a class="main-nav-link" href=http://mxnet.incubator.apache.org/test/versions/0.10/index.html>0.10</a></li><li><a class="main-nav-link" href=http://mxnet.incubator.apache.org/test/versions/master/index.html>master</a></li></ul></span></nav>
<script> function getRootPath(){ return "../" } </script>
<div class="burgerIcon dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button"></a>
<ul class="dropdown-menu dropdown-menu-right" id="burgerMenu">
<li><a href="../get_started/install.html">Install</a></li>
<li><a href="../tutorials/index.html">Tutorials</a></li>
<li><a href="../how_to/index.html">How To</a></li>
<li class="dropdown-submenu">
<a href="#" tabindex="-1">API</a>
<ul class="dropdown-menu">
<li><a href="../api/python/index.html" tabindex="-1">Python</a>
</li>
<li><a href="../api/scala/index.html" tabindex="-1">Scala</a>
</li>
<li><a href="../api/r/index.html" tabindex="-1">R</a>
</li>
<li><a href="../api/julia/index.html" tabindex="-1">Julia</a>
</li>
<li><a href="../api/c++/index.html" tabindex="-1">C++</a>
</li>
<li><a href="../api/perl/index.html" tabindex="-1">Perl</a>
</li>
</ul>
</li>
<li><a href="../architecture/index.html">Architecture</a></li>
<li><a class="main-nav-link" href="https://github.com/dmlc/mxnet">Github</a></li>
<li id="dropdown-menu-position-anchor-version-mobile" class="dropdown-submenu" style="position: relative"><a href="#" tabindex="-1">Versions(master)</a><ul class="dropdown-menu"><li><a tabindex="-1" href=http://mxnet.incubator.apache.org/test/>v0.10.14</a></li><li><a tabindex="-1" href=http://mxnet.incubator.apache.org/test/versions/0.10/index.html>0.10</a></li><li><a tabindex="-1" href=http://mxnet.incubator.apache.org/test/versions/master/index.html>master</a></li></ul></li></ul>
</div>
<div class="plusIcon dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button"><span aria-hidden="true" class="glyphicon glyphicon-plus"></span></a>
<ul class="dropdown-menu dropdown-menu-right" id="plusMenu"></ul>
</div>
<div id="search-input-wrap">
<form action="../search.html" autocomplete="off" class="" method="get" role="search">
<div class="form-group inner-addon left-addon">
<i class="glyphicon glyphicon-search"></i>
<input class="form-control" name="q" placeholder="Search" type="text"/>
</div>
<input name="check_keywords" type="hidden" value="yes">
<input name="area" type="hidden" value="default"/>
</input></form>
<div id="search-preview"></div>
</div>
<div id="searchIcon">
<span aria-hidden="true" class="glyphicon glyphicon-search"></span>
</div>
<!-- <div id="lang-select-wrap"> -->
<!-- <label id="lang-select-label"> -->
<!-- <\!-- <i class="fa fa-globe"></i> -\-> -->
<!-- <span></span> -->
<!-- </label> -->
<!-- <select id="lang-select"> -->
<!-- <option value="en">Eng</option> -->
<!-- <option value="zh">中文</option> -->
<!-- </select> -->
<!-- </div> -->
<!-- <a id="mobile-nav-toggle">
<span class="mobile-nav-toggle-bar"></span>
<span class="mobile-nav-toggle-bar"></span>
<span class="mobile-nav-toggle-bar"></span>
</a> -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div aria-label="main navigation" class="sphinxsidebar leftsidebar" role="navigation">
<div class="sphinxsidebarwrapper">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../api/python/index.html">Python Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/r/index.html">R Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/julia/index.html">Julia Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/c++/index.html">C++ Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/scala/index.html">Scala Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/perl/index.html">Perl Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="index.html">HowTo Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../architecture/index.html">System Documents</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorials/index.html">Tutorials</a></li>
</ul>
</div>
</div>
<div class="content">
<div class="section" id="deep-learning-in-a-single-file-for-smart-devices">
<span id="deep-learning-in-a-single-file-for-smart-devices"></span><h1>Deep Learning in a Single File for Smart Devices<a class="headerlink" href="#deep-learning-in-a-single-file-for-smart-devices" title="Permalink to this headline"></a></h1>
<p>Deep learning (DL) systems are complex and often depend on a number of libraries.
Porting a DL library to different platforms can be painful, especially for smart devices.
One simple solution to this problem is to provide a light interface to the library, complete with all required code in a single file with minimal dependencies.
In this document, we explain how to amalgamate all necessary code into a single file,
and demonstrate the approach with an example in which we run image recognition on a mobile device.</p>
<div class="section" id="amalgamation-making-the-whole-system-a-single-file">
<span id="amalgamation-making-the-whole-system-a-single-file"></span><h2>Amalgamation: Making the Whole System a Single File<a class="headerlink" href="#amalgamation-making-the-whole-system-a-single-file" title="Permalink to this headline"></a></h2>
<p>We come to the idea of of amalgamation following the example of SQLite,
which pack all the code needed to run a simple database into a single source file.
All that’s necessary to create the library is to compile that single file.
This simplifies the problem of porting to various platforms.</p>
<p>Thanks to <a class="reference external" href="https://github.com/jdeng">Jack Deng</a>,
MXNet provides an <a class="reference external" href="https://github.com/dmlc/mxnet/tree/master/amalgamation">amalgamation</a> script
that compiles all code needed for prediction based on trained DL models into a single <code class="docutils literal"><span class="pre">.cc</span></code> file,
containing approximately 30K lines of code. This code only depends on the BLAS library.
Moreover, we’ve also created an even more minimal version,
with the BLAS dependency removed.
You can compile the single file into JavaScript by using <a class="reference external" href="https://github.com/kripken/emscripten">emscripten</a>.</p>
<p>The compiled library can be used by any other programming language.
The <code class="docutils literal"><span class="pre">.h</span></code> file contains a light prediction API.
Porting to another language with a C foreign function interface requires little effort.</p>
<p>For examples, see the following examples on GitHub:</p>
<ul class="simple">
<li>Go: <a class="reference external" href="https://github.com/jdeng/gomxnet">https://github.com/jdeng/gomxnet</a></li>
<li>Java: <a class="reference external" href="https://github.com/dmlc/mxnet/tree/master/amalgamation/jni">https://github.com/dmlc/mxnet/tree/master/amalgamation/jni</a></li>
<li>Python: <a class="reference external" href="https://github.com/dmlc/mxnet/tree/master/amalgamation/python">https://github.com/dmlc/mxnet/tree/master/amalgamation/python</a></li>
</ul>
<p>If you plan to amalgamate your system, there are a few guidelines you ought to observe when building the project:</p>
<ul class="simple">
<li>Minimize dependence on other libraries.</li>
<li>Use namespace to encapsulate the types and operators.</li>
<li>Avoid running commands such as <code class="docutils literal"><span class="pre">using</span> <span class="pre">namespace</span> <span class="pre">xyz</span></code> on the global scope.</li>
<li>Avoid cyclic include dependencies.</li>
</ul>
</div>
<div class="section" id="image-recognition-demo-on-mobile-devices">
<span id="image-recognition-demo-on-mobile-devices"></span><h2>Image Recognition Demo on Mobile Devices<a class="headerlink" href="#image-recognition-demo-on-mobile-devices" title="Permalink to this headline"></a></h2>
<p>With amalgamation, deploying the system on smart devices (such as Android or iOS) is simple. But there are two additional considerations:</p>
<ul class="simple">
<li>The model should be small enough to fit into the device’s memory.</li>
<li>The model shouldn’t be too expensive to run given the relatively low computational power of these devices.</li>
</ul>
<p>Let’s use image recognition as an example.
We start with the state-of-the-art inception model.
We train it on an ImageNet dataset,
using multiple servers with GTX 980 cards.
The resulting model fits into memory,
but it’s too expensive to run.
We remove some layers, but now the results are poor.</p>
<p>Finally, we show an Android example, thanks to Leliana, <a class="reference external" href="https://github.com/Leliana/WhatsThis">https://github.com/Leliana/WhatsThis</a> to demonstrate how to run on Android.</p>
<p><img height="488" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/apk/subinception.png" width="274"/></p>
<p>By using amalgamation, we can easily port the prediction library to mobile devices, with nearly no dependency.
After compiling the library for smart platforms, the last thing we must do is to call C-API in the target language (Java/Swift).</p>
<p><img height="488" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/apk/poolnet.png" width="274"/></p>
<p>Besides this pre-trained Inception-BatchNorm network, we’ve provided two pre-trained models.</p>
<p>We tested our model on a Nexus 5:</p>
<table border="1" class="docutils">
<colgroup>
<col width="20%"/>
<col width="20%"/>
<col width="20%"/>
<col width="20%"/>
<col width="20%"/>
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"> </th>
<th class="head">Top-1 Validation on ILSVRC2012</th>
<th class="head">Time</th>
<th class="head">App Size</th>
<th class="head">Runtime Temp Memory Req</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>FastPoorNet</td>
<td>around 52%, similar to 2011 winner</td>
<td>1s</td>
<td><10MB</td>
<td><5MB</td>
</tr>
<tr class="row-odd"><td>Sub InceptionBN</td>
<td>around 64%, similar to 2013 winner</td>
<td>2.7s</td>
<td><40MB</td>
<td><10MB</td>
</tr>
<tr class="row-even"><td>InceptionBN</td>
<td>around 70%</td>
<td>4s-5s</td>
<td><60MB</td>
<td>10MB</td>
</tr>
</tbody>
</table>
<p>These models are for demonstration only.
They aren’t fine-tuned for mobile devices,
and there is definitely room for improvement.We believe that making a lightweight, portable,
and fast deep learning library is fun and interesting,
and hope you enjoy using the library.</p>
</div>
<div class="section" id="source-code">
<span id="source-code"></span><h2>Source Code<a class="headerlink" href="#source-code" title="Permalink to this headline"></a></h2>
<p><a class="reference external" href="https://github.com/Leliana/WhatsThis">https://github.com/Leliana/WhatsThis</a></p>
</div>
<div class="section" id="demo-apk-download">
<span id="demo-apk-download"></span><h2>Demo APK Download<a class="headerlink" href="#demo-apk-download" title="Permalink to this headline"></a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference external" href="https://github.com/dmlc/web-data/blob/master/mxnet/apk/fastpoornet.apk?raw=true">FastPoorNet</a></li>
</ul>
</div>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference external" href="https://github.com/dmlc/web-data/blob/master/mxnet/apk/subinception.apk?raw=true">SubInception</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="footer">
<p> © 2015-2017 DMLC. All rights reserved. </p>
</div>
</div>
</div>
<div aria-label="main navigation" class="sphinxsidebar rightsidebar" role="navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Deep Learning in a Single File for Smart Devices</a><ul>
<li><a class="reference internal" href="#amalgamation-making-the-whole-system-a-single-file">Amalgamation: Making the Whole System a Single File</a></li>
<li><a class="reference internal" href="#image-recognition-demo-on-mobile-devices">Image Recognition Demo on Mobile Devices</a></li>
<li><a class="reference internal" href="#source-code">Source Code</a></li>
<li><a class="reference internal" href="#demo-apk-download">Demo APK Download</a><ul>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- pagename != index -->
<script crossorigin="anonymous" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="../_static/js/sidebar.js" type="text/javascript"></script>
<script src="../_static/js/search.js" type="text/javascript"></script>
<script src="../_static/js/navbar.js" type="text/javascript"></script>
<script src="../_static/js/clipboard.min.js" type="text/javascript"></script>
<script src="../_static/js/copycode.js" type="text/javascript"></script>
<script type="text/javascript">
$('body').ready(function () {
$('body').css('visibility', 'visible');
});
</script>
</div></body>
</html>