blob: d6d417dd68983cdff99c3e7249e866618ab959fd [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>Use data from S3 for training — 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="index.html" rel="up" title="MXNet How To">
<link href="../get_started/index.html" rel="next" title="<no title>"/>
<link href="index.html" rel="prev" title="MXNet How To"/>
<link href="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/image/mxnet-icon.png" rel="icon" type="image/png"/>
</link></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 class="current">
<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 current"><a class="reference internal" href="index.html">HowTo Documents</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="index.html#modeling">Modeling</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#speed">Speed</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="index.html#deployment-environments">Deployment Environments</a><ul class="current">
<li class="toctree-l3"><a class="reference external" href="http://mxnet.io/how_to/smart_device.html">Can I run MXNet on smart or mobile devices?</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="">How to use data from S3 for training?</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#step-1-build-mxnet-with-s3-integration-enabled">Step 1: Build MXNet with S3 integration enabled</a></li>
<li class="toctree-l4"><a class="reference internal" href="#step-2-configure-s3-authentication-tokens">Step 2: Configure S3 authentication tokens</a></li>
<li class="toctree-l4"><a class="reference internal" href="#step-3-upload-data-to-s3">Step 3: Upload data to S3</a></li>
<li class="toctree-l4"><a class="reference internal" href="#step-4-train-with-data-from-s3">Step 4: Train with data from S3</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference external" href="http://docs.aws.amazon.com/mxnet/latest/dg/mxnet-on-ec2-instance.html">How to setup MXNet on AWS?</a></li>
<li class="toctree-l3"><a class="reference external" href="http://docs.aws.amazon.com/mxnet/latest/dg/mxnet-on-ec2-cluster.html">How to do distributed training using MXNet on AWS?</a></li>
<li class="toctree-l3"><a class="reference external" href="http://mxnet.io/tutorials/embedded/wine_detector.html">How do I run MXNet on a Raspberry Pi for computer vision?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="index.html#extend-and-contribute-to-mxnet">Extend and Contribute to MXNet</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#questions-about-using-mxnet">Questions about Using MXNet</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#issue-tracker">Issue Tracker</a></li>
<li class="toctree-l2"><a class="reference internal" href="index.html#roadmap">Roadmap</a></li>
</ul>
</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="use-data-from-s3-for-training">
<span id="use-data-from-s3-for-training"></span><h1>Use data from S3 for training<a class="headerlink" href="#use-data-from-s3-for-training" title="Permalink to this headline"></a></h1>
<p>AWS S3 is a cloud-based object storage service that allows storage and retrieval of large amounts of data at a very low cost. This makes it an attractive option to store large training datasets. MXNet is deeply integrated with S3 for this purpose.</p>
<p>An S3 protocol URL (like <code class="docutils literal"><span class="pre">s3://bucket-name/training-data</span></code>) can be provided as a parameter for any data iterator that takes a file path as input. For example,</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">data_iter</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">ImageRecordIter</span><span class="p">(</span>
<span class="n">path_imgrec</span><span class="o">=</span><span class="s2">"s3://bucket-name/training-data/caltech_train.rec"</span><span class="p">,</span>
<span class="n">data_shape</span><span class="o">=</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">227</span><span class="p">,</span> <span class="mi">227</span><span class="p">),</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span>
<span class="n">resize</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
</pre></div>
</div>
<p>Following are detailed instructions on how to use data from S3 for training.</p>
<div class="section" id="step-1-build-mxnet-with-s3-integration-enabled">
<span id="step-1-build-mxnet-with-s3-integration-enabled"></span><h2>Step 1: Build MXNet with S3 integration enabled<a class="headerlink" href="#step-1-build-mxnet-with-s3-integration-enabled" title="Permalink to this headline"></a></h2>
<p>Follow instructions <a class="reference external" href="http://mxnet.io/get_started/install.html">here</a> to install MXNet from source with the following additional steps to enable S3 integration.</p>
<ol>
<li><p class="first">Install <code class="docutils literal"><span class="pre">libcurl4-openssl-dev</span></code> and <code class="docutils literal"><span class="pre">libssl-dev</span></code> before building MXNet. These packages are required to read/write from AWS S3.</p>
</li>
<li><p class="first">Append <code class="docutils literal"><span class="pre">USE_S3=1</span></code> to <code class="docutils literal"><span class="pre">config.mk</span></code> before building MXNet.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>echo "USE_S3=1" >> config.mk
</pre></div>
</div>
</li>
</ol>
</div>
<div class="section" id="step-2-configure-s3-authentication-tokens">
<span id="step-2-configure-s3-authentication-tokens"></span><h2>Step 2: Configure S3 authentication tokens<a class="headerlink" href="#step-2-configure-s3-authentication-tokens" title="Permalink to this headline"></a></h2>
<p>MXNet requires the S3 environment variables <code class="docutils literal"><span class="pre">AWS_ACCESS_KEY_ID</span></code> and <code class="docutils literal"><span class="pre">AWS_SECRET_ACCESS_KEY</span></code> to be set. <a class="reference external" href="https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/">Here</a> are instructions to get the access keys from AWS console.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>export AWS_ACCESS_KEY_ID=<your-access-key-id>
AWS_SECRET_ACCESS_KEY=<your-secret-access-key>
</pre></div>
</div>
</div>
<div class="section" id="step-3-upload-data-to-s3">
<span id="step-3-upload-data-to-s3"></span><h2>Step 3: Upload data to S3<a class="headerlink" href="#step-3-upload-data-to-s3" title="Permalink to this headline"></a></h2>
<p>There are several ways to upload data to S3. One easy way is to use the AWS command line utility. For example, the following <code class="docutils literal"><span class="pre">sync</span></code> command will recursively copy contents from a local directory to a directory in S3.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>aws s3 sync ./training-data s3://bucket-name/training-data
</pre></div>
</div>
</div>
<div class="section" id="step-4-train-with-data-from-s3">
<span id="step-4-train-with-data-from-s3"></span><h2>Step 4: Train with data from S3<a class="headerlink" href="#step-4-train-with-data-from-s3" title="Permalink to this headline"></a></h2>
<p>Once the data is in S3, it is very straightforward to use it from MXNet. Any data iterator that can read/write data from a local drive can also read/write data from S3.</p>
<p>Let’s modify an existing example code in MXNet repository to read data from S3 instead of local disk. <a class="reference external" href="https://github.com/dmlc/mxnet/blob/master/tests/python/train/test_conv.py"><code class="docutils literal"><span class="pre">mxnet/tests/python/train/test_conv.py</span></code></a> trains a convolutional network using MNIST data from local disk. We’ll do the following change to read the data from S3 instead.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>~/mxnet$ sed -i -- 's/data\//s3:\/\/bucket-name\/training-data\//g' ./tests/python/train/test_conv.py
~/mxnet$ git diff ./tests/python/train/test_conv.py
diff --git a/tests/python/train/test_conv.py b/tests/python/train/test_conv.py
index 039790e..66a60ce 100644
--- a/tests/python/train/test_conv.py
+++ b/tests/python/train/test_conv.py
@@ -39,14 +39,14 @@ def get_iters():
batch_size = 100
train_dataiter = mx.io.MNISTIter(
- image="data/train-images-idx3-ubyte",
- label="data/train-labels-idx1-ubyte",
+ image="s3://bucket-name/training-data/train-images-idx3-ubyte",
+ label="s3://bucket-name/training-data/train-labels-idx1-ubyte",
data_shape=(1, 28, 28),
label_name='sm_label',
batch_size=batch_size, shuffle=True, flat=False, silent=False, seed=10)
val_dataiter = mx.io.MNISTIter(
- image="data/t10k-images-idx3-ubyte",
- label="data/t10k-labels-idx1-ubyte",
+ image="s3://bucket-name/training-data/t10k-images-idx3-ubyte",
+ label="s3://bucket-name/training-data/t10k-labels-idx1-ubyte",
data_shape=(1, 28, 28),
label_name='sm_label',
batch_size=batch_size, shuffle=True, flat=False, silent=False)
</pre></div>
</div>
<p>After the above change <code class="docutils literal"><span class="pre">test_conv.py</span></code> will fetch data from S3 instead of the local disk.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span>python ./tests/python/train/test_conv.py
[21:59:19] src/io/s3_filesys.cc:878: No AWS Region set, using default region us-east-1
[21:59:21] src/io/iter_mnist.cc:94: MNISTIter: load 60000 images, shuffle=1, shape=(100,1,28,28)
[21:59:21] src/io/iter_mnist.cc:94: MNISTIter: load 10000 images, shuffle=1, shape=(100,1,28,28)
INFO:root:Start training with [cpu(0)]
Start training with [cpu(0)]
INFO:root:Epoch[0] Resetting Data Iterator
Epoch[0] Resetting Data Iterator
INFO:root:Epoch[0] Time cost=11.277
Epoch[0] Time cost=11.277
INFO:root:Epoch[0] Validation-accuracy=0.955100
Epoch[0] Validation-accuracy=0.955100
INFO:root:Finish fit...
Finish fit...
INFO:root:Finish predict...
Finish predict...
INFO:root:final accuracy = 0.955100
final accuracy = 0.955100
</pre></div>
</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="#">Use data from S3 for training</a><ul>
<li><a class="reference internal" href="#step-1-build-mxnet-with-s3-integration-enabled">Step 1: Build MXNet with S3 integration enabled</a></li>
<li><a class="reference internal" href="#step-2-configure-s3-authentication-tokens">Step 2: Configure S3 authentication tokens</a></li>
<li><a class="reference internal" href="#step-3-upload-data-to-s3">Step 3: Upload data to S3</a></li>
<li><a class="reference internal" href="#step-4-train-with-data-from-s3">Step 4: Train with data from S3</a></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>