blob: 058b2526490486cd31ffb6e3f7d5d56ac49a3ba0 [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"/>
<meta content="Classify Images with a PreTrained Model" 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="Classify Images with a PreTrained Model" property="og:description"/>
<title>Classify Images with a PreTrained Model — 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/1.2.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/1.2.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/1.2.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/1.2.1/api/python/index.html">Python</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/c++/index.html">C++</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/julia/index.html">Julia</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/perl/index.html">Perl</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/r/index.html">R</a></li>
<li><a class="main-nav-link" href="/versions/1.2.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/1.2.1/faq/index.html">FAQ</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/tutorials/index.html">Tutorials</a>
<li><a class="main-nav-link" href="https://github.com/apache/incubator-mxnet/tree/1.2.1/example">Examples</a></li>
<li><a class="main-nav-link" href="/versions/1.2.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/1.2.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/1.2.1">Github</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/community/contribute.html">Contribute</a></li>
<li><a class="main-nav-link" href="/versions/1.2.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">1.2.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/">1.7</a></li><li><a href=/versions/1.6/>1.6</a></li><li><a href=/versions/1.5.0/>1.5.0</a></li><li><a href=/versions/1.4.1/>1.4.1</a></li><li><a href=/versions/1.3.1/>1.3.1</a></li><li><a href=/versions/1.2.1/>1.2.1</a></li><li><a href=/versions/1.1.0/>1.1.0</a></li><li><a href=/versions/1.0.0/>1.0.0</a></li><li><a href=/versions/0.12.1/>0.12.1</a></li><li><a href=/versions/0.11.0/>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/1.2.1/install/index.html">Install</a></li>
<li><a class="main-nav-link" href="/versions/1.2.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/1.2.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/1.2.1/api/python/index.html">Python</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/c++/index.html">C++</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/julia/index.html">Julia</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/perl/index.html">Perl</a></li>
<li><a class="main-nav-link" href="/versions/1.2.1/api/r/index.html">R</a></li>
<li><a class="main-nav-link" href="/versions/1.2.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/1.2.1/faq/index.html" tabindex="-1">FAQ</a></li>
<li><a href="/versions/1.2.1/tutorials/index.html" tabindex="-1">Tutorials</a></li>
<li><a href="https://github.com/apache/incubator-mxnet/tree/1.2.1/example" tabindex="-1">Examples</a></li>
<li><a href="/versions/1.2.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/1.2.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/1.2.1" tabindex="-1">Github</a></li>
<li><a href="/versions/1.2.1/community/contribute.html" tabindex="-1">Contribute</a></li>
<li><a href="/versions/1.2.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">1.2.1</a><ul class="dropdown-menu"><li><a tabindex="-1" href=/>master</a></li><li><a tabindex="-1" href=/versions/1.6/>1.6</a></li><li><a tabindex="-1" href=/versions/1.5.0/>1.5.0</a></li><li><a tabindex="-1" href=/versions/1.4.1/>1.4.1</a></li><li><a tabindex="-1" href=/versions/1.3.1/>1.3.1</a></li><li><a tabindex="-1" href=/versions/1.2.1/>1.2.1</a></li><li><a tabindex="-1" href=/versions/1.1.0/>1.1.0</a></li><li><a tabindex="-1" href=/versions/1.0.0/>1.0.0</a></li><li><a tabindex="-1" href=/versions/0.12.1/>0.12.1</a></li><li><a tabindex="-1" href=/versions/0.11.0/>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="../../faq/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="../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="classify-images-with-a-pretrained-model">
<span id="classify-images-with-a-pretrained-model"></span><h1>Classify Images with a PreTrained Model<a class="headerlink" href="#classify-images-with-a-pretrained-model" title="Permalink to this headline"></a></h1>
<p>MXNet is a flexible and efficient deep learning framework. One of the interesting things that a deep learning
algorithm can do is classify real world images.</p>
<p>In this tutorial, we show how to use a pre-trained Inception-BatchNorm network to predict the class of an
image. For information about the network architecture, see [1].</p>
<p>The pre-trained Inception-BatchNorm network is able to be downloaded from <a class="reference external" href="http://data.mxnet.io/mxnet/data/Inception.zip">this link</a>
This model gives the recent state-of-art prediction accuracy on image net dataset.</p>
<div class="section" id="load-the-mxnet-package">
<span id="load-the-mxnet-package"></span><h2>Load the MXNet Package<a class="headerlink" href="#load-the-mxnet-package" title="Permalink to this headline"></a></h2>
<p>To get started, load the mxnet package:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">require</span><span class="p">(</span><span class="n">mxnet</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Loading required package: mxnet</span>
<span class="c1">## Loading required package: methods</span>
</pre></div>
</div>
<p>Now load the imager package to load and preprocess the images in R:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">require</span><span class="p">(</span><span class="n">imager</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## Loading required package: imager</span>
<span class="c1">## Loading required package: plyr</span>
<span class="c1">## Loading required package: magrittr</span>
<span class="c1">## Loading required package: stringr</span>
<span class="c1">## Loading required package: png</span>
<span class="c1">## Loading required package: jpeg</span>
<span class="c1">##</span>
<span class="c1">## Attaching package: 'imager'</span>
<span class="c1">##</span>
<span class="c1">## The following object is masked from 'package:magrittr':</span>
<span class="c1">##</span>
<span class="c1">## add</span>
<span class="c1">##</span>
<span class="c1">## The following object is masked from 'package:plyr':</span>
<span class="c1">##</span>
<span class="c1">## liply</span>
<span class="c1">##</span>
<span class="c1">## The following objects are masked from 'package:stats':</span>
<span class="c1">##</span>
<span class="c1">## convolve, spectrum</span>
<span class="c1">##</span>
<span class="c1">## The following object is masked from 'package:graphics':</span>
<span class="c1">##</span>
<span class="c1">## frame</span>
<span class="c1">##</span>
<span class="c1">## The following object is masked from 'package:base':</span>
<span class="c1">##</span>
<span class="c1">## save.image</span>
</pre></div>
</div>
</div>
<div class="section" id="load-the-pretrained-model">
<span id="load-the-pretrained-model"></span><h2>Load the PreTrained Model<a class="headerlink" href="#load-the-pretrained-model" title="Permalink to this headline"></a></h2>
<p>Make sure you unzip the pre-trained model in the current folder. Use the model
loading function to load the model into R:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">model</span> <span class="o">=</span> <span class="nf">mx.model.load</span><span class="p">(</span><span class="s">"Inception/Inception_BN"</span><span class="p">,</span> <span class="n">iteration</span><span class="o">=</span><span class="m">39</span><span class="p">)</span>
</pre></div>
</div>
<p>Load in the mean image, which is used for preprocessing using:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">mean.img</span> <span class="o">=</span> <span class="nf">as.array</span><span class="p">(</span><span class="nf">mx.nd.load</span><span class="p">(</span><span class="s">"Inception/mean_224.nd"</span><span class="p">)</span><span class="n">[[</span><span class="s">"mean_img"</span><span class="n">]]</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="load-and-preprocess-the-image">
<span id="load-and-preprocess-the-image"></span><h2>Load and Preprocess the Image<a class="headerlink" href="#load-and-preprocess-the-image" title="Permalink to this headline"></a></h2>
<p>Now, we are ready to classify a real image. In this example, we simply take the parrots image
from the imager package. You can use another image, if you prefer.</p>
<p>Load and plot the image:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">im</span> <span class="o"><-</span> <span class="nf">load.image</span><span class="p">(</span><span class="nf">system.file</span><span class="p">(</span><span class="s">"extdata/parrots.png"</span><span class="p">,</span> <span class="n">package</span><span class="o">=</span><span class="s">"imager"</span><span class="p">))</span>
<span class="nf">plot</span><span class="p">(</span><span class="n">im</span><span class="p">)</span>
</pre></div>
</div>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://raw.githubusercontent.com/dmlc/web-data/master/mxnet/knitr/classifyRealImageWithPretrainedModel-unnamed-chunk-5-1.png"/></p>
<p>Before feeding the image to the deep network, we need to perform some preprocessing
to make the image meet the deep network input requirements. Preprocessing
includes cropping and subtracting the mean.
Because MXNet is deeply integrated with R, we can do all the processing in an R function:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">preproc.image</span> <span class="o"><-</span> <span class="nf">function</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">mean.image</span><span class="p">)</span> <span class="p">{</span>
<span class="c1"># crop the image</span>
<span class="n">shape</span> <span class="o"><-</span> <span class="nf">dim</span><span class="p">(</span><span class="n">im</span><span class="p">)</span>
<span class="n">short.edge</span> <span class="o"><-</span> <span class="nf">min</span><span class="p">(</span><span class="n">shape[1</span><span class="o">:</span><span class="m">2</span><span class="n">]</span><span class="p">)</span>
<span class="n">xx</span> <span class="o"><-</span> <span class="nf">floor</span><span class="p">((</span><span class="n">shape[1]</span> <span class="o">-</span> <span class="n">short.edge</span><span class="p">)</span> <span class="o">/</span> <span class="m">2</span><span class="p">)</span>
<span class="n">yy</span> <span class="o"><-</span> <span class="nf">floor</span><span class="p">((</span><span class="n">shape[2]</span> <span class="o">-</span> <span class="n">short.edge</span><span class="p">)</span> <span class="o">/</span> <span class="m">2</span><span class="p">)</span>
<span class="n">cropped</span> <span class="o"><-</span> <span class="nf">crop.borders</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">xx</span><span class="p">,</span> <span class="n">yy</span><span class="p">)</span>
<span class="c1"># resize to 224 x 224, needed by input of the model.</span>
<span class="n">resized</span> <span class="o"><-</span> <span class="nf">resize</span><span class="p">(</span><span class="n">cropped</span><span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">)</span>
<span class="c1"># convert to array (x, y, channel)</span>
<span class="n">arr</span> <span class="o"><-</span> <span class="nf">as.array</span><span class="p">(</span><span class="n">resized</span><span class="p">)</span> <span class="o">*</span> <span class="m">255</span>
<span class="nf">dim</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="o"><-</span> <span class="nf">c</span><span class="p">(</span><span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">3</span><span class="p">)</span>
<span class="c1"># subtract the mean</span>
<span class="n">normed</span> <span class="o"><-</span> <span class="n">arr</span> <span class="o">-</span> <span class="n">mean.img</span>
<span class="c1"># Reshape to format needed by mxnet (width, height, channel, num)</span>
<span class="nf">dim</span><span class="p">(</span><span class="n">normed</span><span class="p">)</span> <span class="o"><-</span> <span class="nf">c</span><span class="p">(</span><span class="m">224</span><span class="p">,</span> <span class="m">224</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">1</span><span class="p">)</span>
<span class="nf">return</span><span class="p">(</span><span class="n">normed</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Use the defined preprocessing function to get the normalized image:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">normed</span> <span class="o"><-</span> <span class="nf">preproc.image</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">mean.img</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="classify-the-image">
<span id="classify-the-image"></span><h2>Classify the Image<a class="headerlink" href="#classify-the-image" title="Permalink to this headline"></a></h2>
<p>Now we are ready to classify the image! Use the <code class="docutils literal"><span class="pre">predict</span></code> function
to get the probability over classes:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">prob</span> <span class="o"><-</span> <span class="nf">predict</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">X</span><span class="o">=</span><span class="n">normed</span><span class="p">)</span>
<span class="nf">dim</span><span class="p">(</span><span class="n">prob</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## [1] 1000 1</span>
</pre></div>
</div>
<p>As you can see, <code class="docutils literal"><span class="pre">prob</span></code> is a 1 times 1000 array, which gives the probability
over the 1000 image classes of the input.</p>
<p>Use the <code class="docutils literal"><span class="pre">max.col</span></code> on the transpose of <code class="docutils literal"><span class="pre">prob</span></code> to get the class index:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">max.idx</span> <span class="o"><-</span> <span class="nf">max.col</span><span class="p">(</span><span class="nf">t</span><span class="p">(</span><span class="n">prob</span><span class="p">))</span>
<span class="n">max.idx</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## [1] 89</span>
</pre></div>
</div>
<p>The index doesn’t make much sense, so let’s see what it really means.
Read the names of the classes from the following file:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="n">synsets</span> <span class="o"><-</span> <span class="nf">readLines</span><span class="p">(</span><span class="s">"Inception/synset.txt"</span><span class="p">)</span>
</pre></div>
</div>
<p>Let’s see what the image really is:</p>
<div class="highlight-r"><div class="highlight"><pre><span></span> <span class="nf">print</span><span class="p">(</span><span class="nf">paste0</span><span class="p">(</span><span class="s">"Predicted Top-class: "</span><span class="p">,</span> <span class="n">synsets</span> <span class="n">[[max.idx]]</span><span class="p">))</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span> <span class="c1">## [1] "Predicted Top-class: n01818515 macaw"</span>
</pre></div>
</div>
<p>It’s a macaw!</p>
</div>
<div class="section" id="reference">
<span id="reference"></span><h2>Reference<a class="headerlink" href="#reference" title="Permalink to this headline"></a></h2>
<p>[1] Ioffe, Sergey, and Christian Szegedy. “Batch normalization: Accelerating deep network training by reducing internal covariate shift.” arXiv preprint arXiv:1502.03167 (2015).</p>
</div>
<div class="section" id="next-steps">
<span id="next-steps"></span><h2>Next Steps<a class="headerlink" href="#next-steps" title="Permalink to this headline"></a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference external" href="/versions/1.2.1/tutorials/r/mnistCompetition.html">Handwritten Digits Classification Competition</a></li>
<li class="toctree-l1"><a class="reference external" href="/versions/1.2.1/tutorials/r/charRnnModel.html">Character Language Model using RNN</a></li>
</ul>
</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="#">Classify Images with a PreTrained Model</a><ul>
<li><a class="reference internal" href="#load-the-mxnet-package">Load the MXNet Package</a></li>
<li><a class="reference internal" href="#load-the-pretrained-model">Load the PreTrained Model</a></li>
<li><a class="reference internal" href="#load-and-preprocess-the-image">Load and Preprocess the Image</a></li>
<li><a class="reference internal" href="#classify-the-image">Classify the Image</a></li>
<li><a class="reference internal" href="#reference">Reference</a></li>
<li><a class="reference internal" href="#next-steps">Next Steps</a></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>