blob: 1d2ff85cba5c9ba6907ed85b677e437bd6275d72 [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>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: ''
};
</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="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>
<li><p class="first">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></p>
</li>
<li><p class="first">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.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nb">cd</span> caffe <span class="o">&amp;&amp;</span> wget https://github.com/BVLC/caffe/pull/4527.patch <span class="o">&amp;&amp;</span> git apply <span class="m">4527</span>.patch
</pre></div>
</div>
</li>
<li><p class="first">Build and install Caffe by following the
<a class="reference external" href="http://caffe.berkeleyvision.org/installation.html">official guide</a>.</p>
</li>
</ol>
<p>Next we need to compile MXNet with Caffe supports</p>
<ol>
<li><p class="first">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</p>
</li>
<li><p class="first">Open the copied <code class="docutils literal"><span class="pre">config.mk</span></code> and uncomment these two lines</p>
<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>
</li>
<li><p class="first">Then build with 8 threads <code class="docutils literal"><span class="pre">make</span> <span class="pre">clean</span> <span class="pre">&amp;&amp;</span> <span class="pre">make</span> <span class="pre">-j8</span></code>.</p>
</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 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="#">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> <!-- 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>