index.html 12.4 KB
Newer Older
1
<!DOCTYPE html>
2 3 4 5 6 7 8 9
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
10 11
  <link rel="shortcut icon" href="../../img/favicon.ico">
  <title>离线量化 - PaddleSlim Docs</title>
12
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
13

14 15
  <link rel="stylesheet" href="../../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" type="text/css" />
16 17 18 19
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
  
  <script>
    // Current page data
20 21
    var mkdocs_page_name = "\u79bb\u7ebf\u91cf\u5316";
    var mkdocs_page_input_path = "tutorials/quant_post_demo.md";
22 23 24
    var mkdocs_page_url = null;
  </script>
  
25 26
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
27 28 29 30
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>
31

32
<body class="wy-body-for-nav" role="document">
33

34
  <div class="wy-grid-for-nav">
35

36 37 38
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
39
        <a href="../.." class="icon icon-home"> PaddleSlim Docs</a>
40
        <div role="search">
41
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
42 43 44 45
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>
46

47 48 49 50 51 52
      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
	<ul class="current">
	  
          
            <li class="toctree-l1">
		
53
    <a class="" href="../..">Home</a>
54 55 56 57
	    </li>
          
            <li class="toctree-l1">
		
58
    <span class="caption-text">教程</span>
59
    <ul class="subnav">
60
                <li class=" current">
61
                    
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    <a class="current" href="./">离线量化</a>
    <ul class="subnav">
            
    <li class="toctree-l3"><a href="#_1">离线量化示例</a></li>
    
        <ul>
        
            <li><a class="toctree-l4" href="#_2">接口介绍</a></li>
        
            <li><a class="toctree-l4" href="#_3">分类模型的离线量化流程</a></li>
        
        </ul>
    

    </ul>
77 78 79
                </li>
                <li class="">
                    
80
    <a class="" href="../quant_aware_demo/">量化训练</a>
81 82 83
                </li>
                <li class="">
                    
84
    <a class="" href="../quant_embedding_demo/">Embedding量化</a>
85
                </li>
86
                <li class="">
87
                    
88
    <a class="" href="../nas_demo/">SA搜索</a>
89
                </li>
90
    </ul>
91 92 93 94 95
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">API</span>
96
    <ul class="subnav">
97
                <li class="">
98
                    
99
    <a class="" href="../../api/quantization_api/">量化</a>
100
                </li>
101
                <li class="">
102
                    
103
    <a class="" href="../../api/prune_api/">剪枝</a>
104
                </li>
105
                <li class="">
106
                    
107
    <a class="" href="../../api/analysis_api/">敏感度分析</a>
108
                </li>
109
                <li class="">
110
                    
111
    <a class="" href="../../api/single_distiller_api/">蒸馏</a>
112
                </li>
113
                <li class="">
114
                    
115
    <a class="" href="../../api/nas_api/">SA搜索</a>
116
                </li>
117 118 119
                <li class="">
                    
    <a class="" href="../../api/search_space/">搜索空间</a>
120
                </li>
121
    </ul>
122 123 124 125 126 127
	    </li>
          
        </ul>
      </div>
      &nbsp;
    </nav>
128

129
    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
130

131 132 133
      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
134
        <a href="../..">PaddleSlim Docs</a>
135
      </nav>
136

137 138 139 140 141
      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
142
    <li><a href="../..">Docs</a> &raquo;</li>
143 144 145
    
      
        
146
          <li>教程 &raquo;</li>
147 148 149
        
      
    
150
    <li>离线量化</li>
151 152
    <li class="wy-breadcrumbs-aside">
      
153 154 155
        <a href="https://github.com/PaddlePaddle/PaddleSlim/edit/master/docs/tutorials/quant_post_demo.md"
          class="icon icon-github"> Edit on GitHub</a>
      
156 157 158 159 160 161 162
    </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
163
                <h1 id="_1">离线量化示例<a class="headerlink" href="#_1" title="Permanent link">#</a></h1>
164
<p>本示例介绍如何使用离线量化接口<code>paddleslim.quant.quant_post</code>来对训练好的分类模型进行离线量化, 该接口无需对模型进行训练就可得到量化模型,减少模型的存储空间和显存占用。</p>
165
<h2 id="_2">接口介绍<a class="headerlink" href="#_2" title="Permanent link">#</a></h2>
166
<p>请参考 <a href='../../../paddleslim/quant/quantization_api_doc.md'>量化API文档</a></p>
167 168
<h2 id="_3">分类模型的离线量化流程<a class="headerlink" href="#_3" title="Permanent link">#</a></h2>
<h3 id="_4">准备数据<a class="headerlink" href="#_4" title="Permanent link">#</a></h3>
169 170 171 172 173
<p>在当前文件夹下创建<code>data</code>文件夹,将<code>imagenet</code>数据集解压在<code>data</code>文件夹下,解压后<code>data</code>文件夹下应包含以下文件:
- <code>'train'</code>文件夹,训练图片
- <code>'train_list.txt'</code>文件
- <code>'val'</code>文件夹,验证图片
- <code>'val_list.txt'</code>文件</p>
174
<h3 id="_5">准备需要量化的模型<a class="headerlink" href="#_5" title="Permanent link">#</a></h3>
175 176 177
<p>因为离线量化接口只支持加载通过<code>fluid.io.save_inference_model</code>接口保存的模型,因此如果您的模型是通过其他接口保存的,那需要先将模型进行转化。本示例将以分类模型为例进行说明。</p>
<p>首先在<a href="https://github.com/PaddlePaddle/models/tree/develop/PaddleCV/image_classification#%E5%B7%B2%E5%8F%91%E5%B8%83%E6%A8%A1%E5%9E%8B%E5%8F%8A%E5%85%B6%E6%80%A7%E8%83%BD">imagenet分类模型</a>中下载训练好的<code>mobilenetv1</code>模型。</p>
<p>在当前文件夹下创建<code>'pretrain'</code>文件夹,将<code>mobilenetv1</code>模型在该文件夹下解压,解压后的目录为<code>pretrain/MobileNetV1_pretrained</code></p>
178
<h3 id="_6">导出模型<a class="headerlink" href="#_6" title="Permanent link">#</a></h3>
179
<p>通过运行以下命令可将模型转化为离线量化接口可用的模型:</p>
180 181 182
<div class="codehilite"><pre><span></span><span class="n">python</span> <span class="n">export_model</span><span class="p">.</span><span class="n">py</span> <span class="c1">--model &quot;MobileNet&quot; --pretrained_model ./pretrain/MobileNetV1_pretrained --data imagenet</span>
</pre></div>

183 184

<p>转化之后的模型存储在<code>inference_model/MobileNet/</code>文件夹下,可看到该文件夹下有<code>'model'</code>, <code>'weights'</code>两个文件。</p>
185
<h3 id="_7">离线量化<a class="headerlink" href="#_7" title="Permanent link">#</a></h3>
186
<p>接下来对导出的模型文件进行离线量化,离线量化的脚本为<a href="./quant_post.py">quant_post.py</a>,脚本中使用接口<code>paddleslim.quant.quant_post</code>对模型进行离线量化。运行命令为:</p>
187 188 189
<div class="codehilite"><pre><span></span><span class="n">python</span> <span class="n">quant_post</span><span class="p">.</span><span class="n">py</span> <span class="c1">--model_path ./inference_model/MobileNet --save_path ./quant_model_train/MobileNet --model_filename model --params_filename weights</span>
</pre></div>

190 191 192 193 194 195 196 197 198 199 200

<ul>
<li><code>model_path</code>: 需要量化的模型坐在的文件夹</li>
<li><code>save_path</code>: 量化后的模型保存的路径</li>
<li><code>model_filename</code>: 如果需要量化的模型的参数文件保存在一个文件中,则设置为该模型的模型文件名称,如果参数文件保存在多个文件中,则不需要设置。</li>
<li><code>params_filename</code>: 如果需要量化的模型的参数文件保存在一个文件中,则设置为该模型的参数文件名称,如果参数文件保存在多个文件中,则不需要设置。</li>
</ul>
<p>运行以上命令后,可在<code>${save_path}</code>下看到量化后的模型文件和参数文件。</p>
<blockquote>
<p>使用的量化算法为<code>'KL'</code>, 使用训练集中的160张图片进行量化参数的校正。</p>
</blockquote>
201
<h3 id="_8">测试精度<a class="headerlink" href="#_8" title="Permanent link">#</a></h3>
202 203
<p>使用<a href="./eval.py">eval.py</a>脚本对量化前后的模型进行测试,得到模型的分类精度进行对比。</p>
<p>首先测试量化前的模型的精度,运行以下命令:</p>
204 205 206
<div class="codehilite"><pre><span></span><span class="n">python</span> <span class="n">eval</span><span class="p">.</span><span class="n">py</span> <span class="c1">--model_path ./inference_model/MobileNet --model_name model --params_name weights</span>
</pre></div>

207 208

<p>精度输出为:</p>
209 210 211
<div class="codehilite"><pre><span></span><span class="n">top1_acc</span><span class="o">/</span><span class="n">top5_acc</span><span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">.</span><span class="mi">70913923</span> <span class="mi">0</span><span class="p">.</span><span class="mi">89548034</span><span class="p">]</span>
</pre></div>

212 213

<p>使用以下命令测试离线量化后的模型的精度:</p>
214 215 216
<div class="codehilite"><pre><span></span><span class="n">python</span> <span class="n">eval</span><span class="p">.</span><span class="n">py</span> <span class="c1">--model_path ./quant_model_train/MobileNet</span>
</pre></div>

217 218

<p>精度输出为</p>
219 220 221
<div class="codehilite"><pre><span></span><span class="n">top1_acc</span><span class="o">/</span><span class="n">top5_acc</span><span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">.</span><span class="mi">70141864</span> <span class="mi">0</span><span class="p">.</span><span class="mi">89086477</span><span class="p">]</span>
</pre></div>

222

223 224
<p>从以上精度对比可以看出,对<code>mobilenet</code><code>imagenet</code>上的分类模型进行离线量化后 <code>top1</code>精度损失为<code>0.77%</code><code>top5</code>精度损失为<code>0.46%</code>. </p>
              
225
            </div>
226 227 228 229 230
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
231 232
        <a href="../quant_aware_demo/" class="btn btn-neutral float-right" title="量化训练">Next <span class="icon icon-circle-arrow-right"></span></a>
      
233
      
234
        <a href="../.." class="btn btn-neutral" title="Home"><span class="icon icon-circle-arrow-left"></span> Previous</a>
235
      
236
    </div>
237 238 239 240 241 242 243 244 245 246 247 248
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
249
        </div>
250 251 252 253 254 255 256 257 258
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
259 260 261 262
          <a href="https://github.com/PaddlePaddle/PaddleSlim/" class="fa fa-github" style="float: left; color: #fcfcfc"> GitHub</a>
      
      
        <span><a href="../.." style="color: #fcfcfc;">&laquo; Previous</a></span>
263 264
      
      
265
        <span style="margin-left: 15px"><a href="../quant_aware_demo/" style="color: #fcfcfc">Next &raquo;</a></span>
266 267
      
    </span>
268
</div>
269 270 271 272 273
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="../../mathjax-config.js" defer></script>
      <script src="../../MathJax.js?config=TeX-AMS-MML_HTMLorMML" defer></script>
      <script src="../../search/main.js" defer></script>
274

275
</body>
276
</html>