| <!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"/> |
| <meta content="How to | Convert from Caffe to MXNet" property="og:title"> |
| <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image"> |
| <meta content="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/og-logo.png" property="og:image:secure_url"> |
| <meta content="How to | Convert from Caffe to MXNet" property="og:description"/> |
| <title>How to | Convert from Caffe to MXNet — 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: '.txt' |
| }; |
| </script> |
| <script src="https://code.jquery.com/jquery-1.11.1.min.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://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> |
| <script type="text/javascript"> jQuery(function() { Search.loadIndex("/versions/0.12.1/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://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> --> |
| <!-- --> |
| <link href="../genindex.html" rel="index" title="Index"> |
| <link href="../search.html" rel="search" title="Search"/> |
| <link href="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-icon.png" rel="icon" type="image/png"/> |
| </link></link></link></meta></meta></meta></head> |
| <body background="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-background-compressed.jpeg" role="document"> |
| <div class="content-block"><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="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet_logo.png"/></a> |
| </h1> |
| <nav class="nav-bar" id="main-nav"> |
| <a class="main-nav-link" href="/versions/0.12.1/install/index.html">Install</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">Gluon <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/0.12.1/tutorials/gluon/gluon.html">About</a></li> |
| <li><a class="main-nav-link" href="https://www.d2l.ai/">Dive into Deep Learning</a></li> |
| <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> |
| <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> |
| </ul> |
| </span> |
| <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 navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/python/index.html">Python</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/c++/index.html">C++</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/julia/index.html">Julia</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/perl/index.html">Perl</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/r/index.html">R</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/scala/index.html">Scala</a></li> |
| </ul> |
| </span> |
| <span id="dropdown-menu-position-anchor-docs"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Docs <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-docs"> |
| <li><a class="main-nav-link" href="/versions/0.12.1/faq/index.html">FAQ</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/tutorials/index.html">Tutorials</a> |
| <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/0.12.1/example">Examples</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/architecture/index.html">Architecture</a></li> |
| <li><a class="main-nav-link" href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home">Developer Wiki</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/model_zoo/index.html">Model Zoo</a></li> |
| <li><a class="main-nav-link" href="https://github.com/onnx/onnx-mxnet">ONNX</a></li> |
| </li></ul> |
| </span> |
| <span id="dropdown-menu-position-anchor-community"> |
| <a aria-expanded="true" aria-haspopup="true" class="main-nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button">Community <span class="caret"></span></a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu-community"> |
| <li><a class="main-nav-link" href="http://discuss.mxnet.io">Forum</a></li> |
| <li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/0.12.1">Github</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/community/contribute.html">Contribute</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/community/powered_by.html">Powered By</a></li> |
| </ul> |
| </span> |
| <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">0.12.1<span class="caret"></span></a><ul id="package-dropdown-menu" class="dropdown-menu"><li><a href="/">master</a></li><li><a href="/versions/1.7.0/">1.7.0</a></li><li><a href=/versions/1.6.0/>1.6.0</a></li><li><a href=/versions/1.5.0/index.html>1.5.0</a></li><li><a href=/versions/1.4.1/index.html>1.4.1</a></li><li><a href=/versions/1.3.1/index.html>1.3.1</a></li><li><a href=/versions/1.2.1/index.html>1.2.1</a></li><li><a href=/versions/1.1.0/index.html>1.1.0</a></li><li><a href=/versions/1.0.0/index.html>1.0.0</a></li><li><a href=/versions/0.12.1/index.html>0.12.1</a></li><li><a href=/versions/0.11.0/index.html>0.11.0</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" id="burgerMenu"> |
| <li><a href="/versions/0.12.1/install/index.html">Install</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/tutorials/index.html">Tutorials</a></li> |
| <li class="dropdown-submenu dropdown"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Gluon</a> |
| <ul class="dropdown-menu navbar-menu" id="package-dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/0.12.1/tutorials/gluon/gluon.html">About</a></li> |
| <li><a class="main-nav-link" href="http://gluon.mxnet.io">The Straight Dope (Tutorials)</a></li> |
| <li><a class="main-nav-link" href="https://gluon-cv.mxnet.io">GluonCV Toolkit</a></li> |
| <li><a class="main-nav-link" href="https://gluon-nlp.mxnet.io/">GluonNLP Toolkit</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">API</a> |
| <ul class="dropdown-menu"> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/python/index.html">Python</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/c++/index.html">C++</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/julia/index.html">Julia</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/perl/index.html">Perl</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/r/index.html">R</a></li> |
| <li><a class="main-nav-link" href="/versions/0.12.1/api/scala/index.html">Scala</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu"> |
| <a aria-expanded="true" aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" tabindex="-1">Docs</a> |
| <ul class="dropdown-menu"> |
| <li><a href="/versions/0.12.1/faq/index.html" tabindex="-1">FAQ</a></li> |
| <li><a href="/versions/0.12.1/tutorials/index.html" tabindex="-1">Tutorials</a></li> |
| <li><a href="https://github.com/apache/incubator-mxnet/tree/0.12.1/example" tabindex="-1">Examples</a></li> |
| <li><a href="/versions/0.12.1/architecture/index.html" tabindex="-1">Architecture</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/MXNET/Apache+MXNet+Home" tabindex="-1">Developer Wiki</a></li> |
| <li><a href="/versions/0.12.1/model_zoo/index.html" tabindex="-1">Gluon Model Zoo</a></li> |
| <li><a href="https://github.com/onnx/onnx-mxnet" tabindex="-1">ONNX</a></li> |
| </ul> |
| </li> |
| <li class="dropdown-submenu dropdown"> |
| <a aria-haspopup="true" class="dropdown-toggle burger-link" data-toggle="dropdown" href="#" role="button" tabindex="-1">Community</a> |
| <ul class="dropdown-menu"> |
| <li><a href="http://discuss.mxnet.io" tabindex="-1">Forum</a></li> |
| <li><a href="https://github.com/apache/incubator-mxnet/tree/0.12.1" tabindex="-1">Github</a></li> |
| <li><a href="/versions/0.12.1/community/contribute.html" tabindex="-1">Contribute</a></li> |
| <li><a href="/versions/0.12.1/community/powered_by.html" tabindex="-1">Powered By</a></li> |
| </ul> |
| </li> |
| <li id="dropdown-menu-position-anchor-version-mobile" class="dropdown-submenu" style="position: relative"><a href="#" tabindex="-1">0.12.1</a><ul class="dropdown-menu"><li><a tabindex="-1" href=/>master</a></li><li><a tabindex="-1" href=/versions/1.6.0/>1.6.0</a></li><li><a tabindex="-1" href=/versions/1.5.0/index.html>1.5.0</a></li><li><a tabindex="-1" href=/versions/1.4.1/index.html>1.4.1</a></li><li><a tabindex="-1" href=/versions/1.3.1/index.html>1.3.1</a></li><li><a tabindex="-1" href=/versions/1.2.1/index.html>1.2.1</a></li><li><a tabindex="-1" href=/versions/1.1.0/index.html>1.1.0</a></li><li><a tabindex="-1" href=/versions/1.0.0/index.html>1.0.0</a></li><li><a tabindex="-1" href=/versions/0.12.1/index.html>0.12.1</a></li><li><a tabindex="-1" href=/versions/0.11.0/index.html>0.11.0</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> |
| <script type="text/javascript"> |
| $('body').css('background', 'white'); |
| </script> |
| <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> |
| <li class="toctree-l1"><a class="reference internal" href="../community/index.html">Community</a></li> |
| </ul> |
| </div> |
| </div> |
| <div class="content"> |
| <div class="page-tracker"></div> |
| <div class="section" id="how-to-convert-from-caffe-to-mxnet"> |
| <span id="how-to-convert-from-caffe-to-mxnet"></span><h1>How to | Convert from Caffe to MXNet<a class="headerlink" href="#how-to-convert-from-caffe-to-mxnet" title="Permalink to this headline">¶</a></h1> |
| <p>Key topics covered include the following:</p> |
| <ul class="simple"> |
| <li><a class="reference external" href="#converting-caffe-trained-models-to-mxnet">Converting Caffe trained models to MXNet</a></li> |
| <li><a class="reference external" href="#calling-caffe-operators-in-mxnet">Calling Caffe operators in MXNet</a></li> |
| </ul> |
| <div class="section" id="converting-caffe-trained-models-to-mxnet"> |
| <span id="converting-caffe-trained-models-to-mxnet"></span><h2>Converting Caffe trained models to MXNet<a class="headerlink" href="#converting-caffe-trained-models-to-mxnet" title="Permalink to this headline">¶</a></h2> |
| <p>The converting tool is available at |
| <a class="reference external" href="https://github.com/dmlc/mxnet/tree/master/tools/caffe_converter">tools/caffe_converter</a>. On |
| the remaining of this section, we assume we are on the <code class="docutils literal"><span class="pre">tools/caffe_converter</span></code> |
| directory.</p> |
| <div class="section" id="how-to-build"> |
| <span id="how-to-build"></span><h3>How to build<a class="headerlink" href="#how-to-build" title="Permalink to this headline">¶</a></h3> |
| <p>If Caffe’s python package is installed, namely we can run <code class="docutils literal"><span class="pre">import</span> <span class="pre">caffe</span></code> in |
| python, then we are ready to go.</p> |
| <p>For example, we can used |
| <a class="reference external" href="https://aws.amazon.com/marketplace/pp/B06VSPXKDX">AWS Deep Learning AMI</a> with |
| both Caffe and MXNet installed.</p> |
| <p>Otherwise we can install the |
| <a class="reference external" href="https://developers.google.com/protocol-buffers/?hl=en">Google protobuf</a> |
| compiler and its python binding. It is easier to install, but may be slower |
| during running.</p> |
| <ol class="simple"> |
| <li>Install the compiler:</li> |
| </ol> |
| <ul class="simple"> |
| <li>Linux: install <code class="docutils literal"><span class="pre">protobuf-compiler</span></code> e.g. <code class="docutils literal"><span class="pre">sudo</span> <span class="pre">apt-get</span> <span class="pre">install</span> <span class="pre">protobuf-compiler</span></code> for Ubuntu and <code class="docutils literal"><span class="pre">sudo</span> <span class="pre">yum</span> <span class="pre">install</span> <span class="pre">protobuf-compiler</span></code> for |
| Redhat/Fedora.</li> |
| <li>Windows: Download the win32 build of |
| <a class="reference external" href="https://github.com/google/protobuf/releases">protobuf</a>. Make sure to |
| download the version that corresponds to the version of the python binding |
| on the next step. Extract to any location then add that location to your |
| <code class="docutils literal"><span class="pre">PATH</span></code></li> |
| <li>Mac OS X: <code class="docutils literal"><span class="pre">brew</span> <span class="pre">install</span> <span class="pre">protobuf</span></code></li> |
| </ul> |
| <ol class="simple"> |
| <li>Install the python binding by either <code class="docutils literal"><span class="pre">conda</span> <span class="pre">install</span> <span class="pre">-c</span> <span class="pre">conda-forge</span> <span class="pre">protobuf</span></code> |
| or <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">protobuf</span></code>.</li> |
| <li>Compile Caffe proto definition. Run <code class="docutils literal"><span class="pre">make</span></code> in Linux or Mac OS X, or |
| <code class="docutils literal"><span class="pre">make_win32.bat</span></code> in Windows</li> |
| </ol> |
| </div> |
| <div class="section" id="how-to-use"> |
| <span id="how-to-use"></span><h3>How to use<a class="headerlink" href="#how-to-use" title="Permalink to this headline">¶</a></h3> |
| <p>There are three tools:</p> |
| <ul class="simple"> |
| <li><code class="docutils literal"><span class="pre">convert_symbol.py</span></code> : convert Caffe model definition in protobuf into MXNet’s |
| Symbol in JSON format.</li> |
| <li><code class="docutils literal"><span class="pre">convert_model.py</span></code> : convert Caffe model parameters into MXNet’s NDArray format</li> |
| <li><code class="docutils literal"><span class="pre">convert_mean.py</span></code> : convert Caffe input mean file into MXNet’s NDArray format</li> |
| </ul> |
| <p>In addition, there are two tools:</p> |
| <ul class="simple"> |
| <li><code class="docutils literal"><span class="pre">convert_caffe_modelzoo.py</span></code> : download and convert models from Caffe model |
| zoo.</li> |
| <li><code class="docutils literal"><span class="pre">test_converter.py</span></code> : test the converted models by checking the prediction |
| accuracy.</li> |
| </ul> |
| </div> |
| </div> |
| <div class="section" id="calling-caffe-operators-in-mxnet"> |
| <span id="calling-caffe-operators-in-mxnet"></span><h2>Calling Caffe operators in MXNet<a class="headerlink" href="#calling-caffe-operators-in-mxnet" title="Permalink to this headline">¶</a></h2> |
| <p>Besides converting Caffe models, MXNet supports calling most Caffe operators, |
| including network layer, data layer, and loss function, directly. It is |
| particularly useful if there are customized operators implemented in Caffe, then |
| we do not need to re-implement them in MXNet.</p> |
| <div class="section" id="how-to-install"> |
| <span id="how-to-install"></span><h3>How to install<a class="headerlink" href="#how-to-install" title="Permalink to this headline">¶</a></h3> |
| <p>This feature requires Caffe. In particular, we need to re-compile Caffe before |
| <a class="reference external" href="https://github.com/BVLC/caffe/pull/4527">PR #4527</a> is merged into Caffe. There |
| are the steps of how to rebuild Caffe:</p> |
| <ol class="simple"> |
| <li>Download <a class="reference external" href="https://github.com/BVLC/caffe">Caffe</a>. E.g. <code class="docutils literal"><span class="pre">git</span> <span class="pre">clone</span> <span class="pre">https://github.com/BVLC/caffe</span></code></li> |
| <li>Download the |
| <a class="reference external" href="https://github.com/BVLC/caffe/pull/4527.patch">patch for the MXNet interface</a> |
| and apply to Caffe. E.g.</li> |
| </ol> |
| <div class="highlight-bash"><div class="highlight"><pre><span></span> <span class="nb">cd</span> caffe <span class="o">&&</span> wget https://github.com/BVLC/caffe/pull/4527.patch <span class="o">&&</span> git apply <span class="m">4527</span>.patch |
| </pre></div> |
| </div> |
| <ol class="simple"> |
| <li>Build and install Caffe by following the |
| <a class="reference external" href="http://caffe.berkeleyvision.org/installation.html">official guide</a>.</li> |
| </ol> |
| <p>Next we need to compile MXNet with Caffe supports</p> |
| <ol class="simple"> |
| <li>Copy <code class="docutils literal"><span class="pre">make/config.mk</span></code> (for Linux) or <code class="docutils literal"><span class="pre">make/osx.mk</span></code> |
| (for Mac) into the MXNet root folder as <code class="docutils literal"><span class="pre">config.mk</span></code> if you have not done it yet</li> |
| <li>Open the copied <code class="docutils literal"><span class="pre">config.mk</span></code> and uncomment these two lines</li> |
| </ol> |
| <div class="highlight-bash"><div class="highlight"><pre><span></span> <span class="nv">CAFFE_PATH</span> <span class="o">=</span> <span class="k">$(</span>HOME<span class="k">)</span>/caffe |
| <span class="nv">MXNET_PLUGINS</span> <span class="o">+=</span> plugin/caffe/caffe.mk |
| </pre></div> |
| </div> |
| <p>Modify <code class="docutils literal"><span class="pre">CAFFE_PATH</span></code> to your Caffe installation, if necessary.</p> |
| <ol class="simple"> |
| <li>Then build with 8 threads <code class="docutils literal"><span class="pre">make</span> <span class="pre">clean</span> <span class="pre">&&</span> <span class="pre">make</span> <span class="pre">-j8</span></code>.</li> |
| </ol> |
| </div> |
| <div class="section" id="how-to-use"> |
| <span id="id1"></span><h3>How to use<a class="headerlink" href="#how-to-use" title="Permalink to this headline">¶</a></h3> |
| <p>This Caffe plugin adds three components into MXNet:</p> |
| <ul class="simple"> |
| <li><code class="docutils literal"><span class="pre">sym.CaffeOp</span></code> : Caffe neural network layer</li> |
| <li><code class="docutils literal"><span class="pre">sym.CaffeLoss</span></code> : Caffe loss functions</li> |
| <li><code class="docutils literal"><span class="pre">io.CaffeDataIter</span></code> : Caffe data layer</li> |
| </ul> |
| <div class="section" id="use-sym-caffeop"> |
| <span id="use-sym-caffeop"></span><h4>Use <code class="docutils literal"><span class="pre">sym.CaffeOp</span></code><a class="headerlink" href="#use-sym-caffeop" title="Permalink to this headline">¶</a></h4> |
| <p>The following example shows the definition of a 10 classes multi-layer perceptron:</p> |
| <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="s1">'data'</span><span class="p">)</span> |
| <span class="n">fc1</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeOp</span><span class="p">(</span><span class="n">data_0</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">num_weight</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'fc1'</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">InnerProduct</span><span class="se">\"</span><span class="s2"> inner_product_param{num_output: 128} }"</span><span class="p">)</span> |
| <span class="n">act1</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeOp</span><span class="p">(</span><span class="n">data_0</span><span class="o">=</span><span class="n">fc1</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">TanH</span><span class="se">\"</span><span class="s2">}"</span><span class="p">)</span> |
| <span class="n">fc2</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeOp</span><span class="p">(</span><span class="n">data_0</span><span class="o">=</span><span class="n">act1</span><span class="p">,</span> <span class="n">num_weight</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'fc2'</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">InnerProduct</span><span class="se">\"</span><span class="s2"> inner_product_param{num_output: 64} }"</span><span class="p">)</span> |
| <span class="n">act2</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeOp</span><span class="p">(</span><span class="n">data_0</span><span class="o">=</span><span class="n">fc2</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">TanH</span><span class="se">\"</span><span class="s2">}"</span><span class="p">)</span> |
| <span class="n">fc3</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeOp</span><span class="p">(</span><span class="n">data_0</span><span class="o">=</span><span class="n">act2</span><span class="p">,</span> <span class="n">num_weight</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'fc3'</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">InnerProduct</span><span class="se">\"</span><span class="s2"> inner_product_param{num_output: 10}}"</span><span class="p">)</span> |
| <span class="n">mlp</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">SoftmaxOutput</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">fc3</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'softmax'</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p>Let’s break it down. First, <code class="docutils literal"><span class="pre">data</span> <span class="pre">=</span> <span class="pre">mx.sym.Variable('data')</span></code> defines a variable |
| as a placeholder for input. Then, it’s fed through Caffe operators with <code class="docutils literal"><span class="pre">fc1</span> <span class="pre">=</span> <span class="pre">mx.sym.CaffeOp(...)</span></code>. <code class="docutils literal"><span class="pre">CaffeOp</span></code> accepts several arguments:</p> |
| <ul class="simple"> |
| <li>The inputs to Caffe operators are named as <code class="docutils literal"><span class="pre">data_i</span></code> for <em>i=0, ..., num_data-1</em></li> |
| <li><code class="docutils literal"><span class="pre">num_data</span></code> is the number of inputs. In default it is 1, and therefore |
| skipped in the above example.</li> |
| <li><code class="docutils literal"><span class="pre">num_out</span></code> is the number of outputs. In default it is 1 and also skipped.</li> |
| <li><code class="docutils literal"><span class="pre">num_weight</span></code> is the number of weights (<code class="docutils literal"><span class="pre">blobs_</span></code>). Its default value is 0. We |
| need to explicitly specify it for a non-zero value.</li> |
| <li><code class="docutils literal"><span class="pre">prototxt</span></code> is the protobuf configuration string.</li> |
| </ul> |
| </div> |
| <div class="section" id="use-sym-caffeloss"> |
| <span id="use-sym-caffeloss"></span><h4>Use <code class="docutils literal"><span class="pre">sym.CaffeLoss</span></code><a class="headerlink" href="#use-sym-caffeloss" title="Permalink to this headline">¶</a></h4> |
| <p>Using Caffe loss is similar. |
| We can replace the MXNet loss with Caffe loss. |
| We can replace</p> |
| <p>Replacing the last line of the above example with the following two lines we can |
| call Caffe loss instead of MXNet loss.</p> |
| <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">label</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">Variable</span><span class="p">(</span><span class="s1">'softmax_label'</span><span class="p">)</span> |
| <span class="n">mlp</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">sym</span><span class="o">.</span><span class="n">CaffeLoss</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">fc3</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span> <span class="n">grad_scale</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'softmax'</span><span class="p">,</span> <span class="n">prototxt</span><span class="o">=</span><span class="s2">"layer{type:</span><span class="se">\"</span><span class="s2">SoftmaxWithLoss</span><span class="se">\"</span><span class="s2">}"</span><span class="p">)</span> |
| </pre></div> |
| </div> |
| <p>Similar to <code class="docutils literal"><span class="pre">CaffeOp</span></code>, <code class="docutils literal"><span class="pre">CaffeLoss</span></code> has arguments <code class="docutils literal"><span class="pre">num_data</span></code> (2 in default) and |
| <code class="docutils literal"><span class="pre">num_out</span></code> (1 in default). But there are two differences</p> |
| <ol class="simple"> |
| <li>Inputs are <code class="docutils literal"><span class="pre">data</span></code> and <code class="docutils literal"><span class="pre">label</span></code>. And we need to explicitly create a variable |
| placeholder for label, which is implicitly done in MXNet loss.</li> |
| <li><code class="docutils literal"><span class="pre">grad_scale</span></code> is the weight of this loss.</li> |
| </ol> |
| </div> |
| <div class="section" id="use-io-caffedataiter"> |
| <span id="use-io-caffedataiter"></span><h4>Use <code class="docutils literal"><span class="pre">io.CaffeDataIter</span></code><a class="headerlink" href="#use-io-caffedataiter" title="Permalink to this headline">¶</a></h4> |
| <p>We can also wrap a Caffe data layer into MXNet’s data iterator. Below is an |
| example for creating a data iterator for MNIST</p> |
| <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">train</span> <span class="o">=</span> <span class="n">mx</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">CaffeDataIter</span><span class="p">(</span> |
| <span class="n">prototxt</span> <span class="o">=</span> |
| <span class="s1">'layer { </span><span class="se">\</span> |
| <span class="s1"> name: "mnist" </span><span class="se">\</span> |
| <span class="s1"> type: "Data" </span><span class="se">\</span> |
| <span class="s1"> top: "data" </span><span class="se">\</span> |
| <span class="s1"> top: "label" </span><span class="se">\</span> |
| <span class="s1"> include { </span><span class="se">\</span> |
| <span class="s1"> phase: TEST </span><span class="se">\</span> |
| <span class="s1"> } </span><span class="se">\</span> |
| <span class="s1"> transform_param { </span><span class="se">\</span> |
| <span class="s1"> scale: 0.00390625 </span><span class="se">\</span> |
| <span class="s1"> } </span><span class="se">\</span> |
| <span class="s1"> data_param { </span><span class="se">\</span> |
| <span class="s1"> source: "caffe/examples/mnist/mnist_test_lmdb" </span><span class="se">\</span> |
| <span class="s1"> batch_size: 100 </span><span class="se">\</span> |
| <span class="s1"> backend: LMDB </span><span class="se">\</span> |
| <span class="s1"> } </span><span class="se">\</span> |
| <span class="s1"> }'</span><span class="p">,</span> |
| <span class="n">flat</span> <span class="o">=</span> <span class="n">flat</span><span class="p">,</span> |
| <span class="n">num_examples</span> <span class="o">=</span> <span class="mi">60000</span><span class="p">,</span> |
| <span class="p">)</span> |
| </pre></div> |
| </div> |
| </div> |
| </div> |
| <div class="section" id="put-it-all-together"> |
| <span id="put-it-all-together"></span><h3>Put it all together<a class="headerlink" href="#put-it-all-together" title="Permalink to this headline">¶</a></h3> |
| <p>The complete example is available at |
| <a class="reference external" href="https://github.com/dmlc/mxnet/blob/master/example/caffe/">example/caffe</a></p> |
| </div> |
| </div> |
| </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="#">How to | Convert from Caffe to MXNet</a><ul> |
| <li><a class="reference internal" href="#converting-caffe-trained-models-to-mxnet">Converting Caffe trained models to MXNet</a><ul> |
| <li><a class="reference internal" href="#how-to-build">How to build</a></li> |
| <li><a class="reference internal" href="#how-to-use">How to use</a></li> |
| </ul> |
| </li> |
| <li><a class="reference internal" href="#calling-caffe-operators-in-mxnet">Calling Caffe operators in MXNet</a><ul> |
| <li><a class="reference internal" href="#how-to-install">How to install</a></li> |
| <li><a class="reference internal" href="#how-to-use">How to use</a><ul> |
| <li><a class="reference internal" href="#use-sym-caffeop">Use <code class="docutils literal"><span class="pre">sym.CaffeOp</span></code></a></li> |
| <li><a class="reference internal" href="#use-sym-caffeloss">Use <code class="docutils literal"><span class="pre">sym.CaffeLoss</span></code></a></li> |
| <li><a class="reference internal" href="#use-io-caffedataiter">Use <code class="docutils literal"><span class="pre">io.CaffeDataIter</span></code></a></li> |
| </ul> |
| </li> |
| <li><a class="reference internal" href="#put-it-all-together">Put it all together</a></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| </div> |
| </div> |
| </div><div class="footer"> |
| <div class="section-disclaimer"> |
| <div class="container"> |
| <div> |
| <img height="60" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/apache_incubator_logo.png"/> |
| <p> |
| Apache MXNet is an effort undergoing incubation at The Apache Software Foundation (ASF), <strong>sponsored by the <i>Apache Incubator</i></strong>. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF. |
| </p> |
| <p> |
| "Copyright © 2017-2018, The Apache Software Foundation |
| Apache MXNet, MXNet, Apache, the Apache feather, and the Apache MXNet project logo are either registered trademarks or trademarks of the Apache Software Foundation." |
| </p> |
| </div> |
| </div> |
| </div> |
| </div> <!-- pagename != index --> |
| </div> |
| <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 src="../_static/js/page.js" type="text/javascript"></script> |
| <script src="../_static/js/docversion.js" type="text/javascript"></script> |
| <script type="text/javascript"> |
| $('body').ready(function () { |
| $('body').css('visibility', 'visible'); |
| }); |
| </script> |
| </body> |
| </html> |