<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[BatchNorm在预测的时候为什么要去掉？]]></title><description><![CDATA[<p>简单的分享一下，很多人在做网络推理的时候，并不会考虑batchnorm的作用，拿别人的网络来训练，训练完了之后预测的时候也直接预测。这是不对的，因为batchnorm本身在预测的时候没有任何作用，它实在训练的时候起到了一个归一话加速收敛的作用。</p>
<h3>batchnorm合并的必要性</h3>
<p>那么为什么要将batchnorm合并呢？一般放置在卷积层（conv层）或者全连接层之后，将数据归一化并加速了训练拟合速度。但是ｂｎ层虽然在深度学习模型训练时起到了一定的积极作用，但是在预测时因为凭空多了一些层，影响了整体的计算速度并占用了更多内存或者显存空间。所以我们设想如果能将ｂｎ层合并到相邻的卷积层或者全连接层之后就好了，于是就有了这篇文章所提到的工作。</p>
<h3>batchnorm合并的方法</h3>
<p>batchnorm以后iban其实放置在conv或者pooling之后。合并的方式就得分开进行。</p>
<ol>
<li>如果是在conv之后<br />
思考一下，假如是在conv之后，原来bn层的参数，也就是均值和方差，可以直接合并到卷积参数里面去，这样计算的时候就不用再进行这一步的计算了。看看代码是如何实现的：</li>
</ol>
<pre><code>                mean = bn[0].data
                var = bn[1].data
                scalef = bn[2].data

                scales = scale[0].data
                shift = scale[1].data

                if scalef != 0:
                    scalef = 1. / scalef
                mean = mean * scalef
                var = var * scalef
                rstd = 1. / np.sqrt(var + 1e-5)
                if bn_maps[key][&quot;type&quot;] == &quot;Convolution&quot;: 
                    rstd1 = rstd.reshape((channels,1,1,1))
                    scales1 = scales.reshape((channels,1,1,1))
                    wt = wt * rstd1 * scales1
                else:
                    rstd1 = rstd.reshape((1, channels,1,1))
                    scales1 = scales.reshape((1, channels,1,1))
                    wt = wt * rstd1 * scales1
                bias = (bias - mean) * rstd * scales + shift
                
                nobn.params[key][0].data[...] = wt
                nobn.params[key][1].data[...] = bias
</code></pre>
<p>这个是caffe的合并操作。这个代码在MobileNet-SSD里面有：<a href="https://github.com/chuanqi305/MobileNet-SSD/blob/master/merge_bn.py" target="_blank" rel="noopener noreferrer nofollow">https://github.com/chuanqi305/MobileNet-SSD/blob/master/merge_bn.py</a><br />
2. 如果是在pooling之后<br />
pooling之后的操作差不多，具体大家可以探索一下。</p>
<p>这样你的bn曾合并之后速度可以增加很多，有其实bn层很多的网络，将bn层合并是一个不可或缺的操作，否则你推理的速度将会大大的减慢。</p>
]]></description><link>http://t.manaai.cn/topic/24/batchnorm在预测的时候为什么要去掉</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 20:39:50 GMT</lastBuildDate><atom:link href="http://t.manaai.cn/topic/24.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 18 Feb 2019 02:30:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to BatchNorm在预测的时候为什么要去掉？ on Mon, 18 Feb 2019 02:30:05 GMT]]></title><description><![CDATA[<p>简单的分享一下，很多人在做网络推理的时候，并不会考虑batchnorm的作用，拿别人的网络来训练，训练完了之后预测的时候也直接预测。这是不对的，因为batchnorm本身在预测的时候没有任何作用，它实在训练的时候起到了一个归一话加速收敛的作用。</p>
<h3>batchnorm合并的必要性</h3>
<p>那么为什么要将batchnorm合并呢？一般放置在卷积层（conv层）或者全连接层之后，将数据归一化并加速了训练拟合速度。但是ｂｎ层虽然在深度学习模型训练时起到了一定的积极作用，但是在预测时因为凭空多了一些层，影响了整体的计算速度并占用了更多内存或者显存空间。所以我们设想如果能将ｂｎ层合并到相邻的卷积层或者全连接层之后就好了，于是就有了这篇文章所提到的工作。</p>
<h3>batchnorm合并的方法</h3>
<p>batchnorm以后iban其实放置在conv或者pooling之后。合并的方式就得分开进行。</p>
<ol>
<li>如果是在conv之后<br />
思考一下，假如是在conv之后，原来bn层的参数，也就是均值和方差，可以直接合并到卷积参数里面去，这样计算的时候就不用再进行这一步的计算了。看看代码是如何实现的：</li>
</ol>
<pre><code>                mean = bn[0].data
                var = bn[1].data
                scalef = bn[2].data

                scales = scale[0].data
                shift = scale[1].data

                if scalef != 0:
                    scalef = 1. / scalef
                mean = mean * scalef
                var = var * scalef
                rstd = 1. / np.sqrt(var + 1e-5)
                if bn_maps[key][&quot;type&quot;] == &quot;Convolution&quot;: 
                    rstd1 = rstd.reshape((channels,1,1,1))
                    scales1 = scales.reshape((channels,1,1,1))
                    wt = wt * rstd1 * scales1
                else:
                    rstd1 = rstd.reshape((1, channels,1,1))
                    scales1 = scales.reshape((1, channels,1,1))
                    wt = wt * rstd1 * scales1
                bias = (bias - mean) * rstd * scales + shift
                
                nobn.params[key][0].data[...] = wt
                nobn.params[key][1].data[...] = bias
</code></pre>
<p>这个是caffe的合并操作。这个代码在MobileNet-SSD里面有：<a href="https://github.com/chuanqi305/MobileNet-SSD/blob/master/merge_bn.py" target="_blank" rel="noopener noreferrer nofollow">https://github.com/chuanqi305/MobileNet-SSD/blob/master/merge_bn.py</a><br />
2. 如果是在pooling之后<br />
pooling之后的操作差不多，具体大家可以探索一下。</p>
<p>这样你的bn曾合并之后速度可以增加很多，有其实bn层很多的网络，将bn层合并是一个不可或缺的操作，否则你推理的速度将会大大的减慢。</p>
]]></description><link>http://t.manaai.cn/post/51</link><guid isPermaLink="true">http://t.manaai.cn/post/51</guid><dc:creator><![CDATA[刘看山]]></dc:creator><pubDate>Mon, 18 Feb 2019 02:30:05 GMT</pubDate></item><item><title><![CDATA[Reply to BatchNorm在预测的时候为什么要去掉？ on Sun, 31 Mar 2019 07:32:37 GMT]]></title><description><![CDATA[<p>batchnorm合并的方法受教</p>
]]></description><link>http://t.manaai.cn/post/206</link><guid isPermaLink="true">http://t.manaai.cn/post/206</guid><dc:creator><![CDATA[yzhcode]]></dc:creator><pubDate>Sun, 31 Mar 2019 07:32:37 GMT</pubDate></item><item><title><![CDATA[Reply to BatchNorm在预测的时候为什么要去掉？ on Sun, 31 Mar 2019 11:53:26 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://t.manaai.cn/uid/70">@yzhcode</a> 可以的</p>
]]></description><link>http://t.manaai.cn/post/212</link><guid isPermaLink="true">http://t.manaai.cn/post/212</guid><dc:creator><![CDATA[刘看山]]></dc:creator><pubDate>Sun, 31 Mar 2019 11:53:26 GMT</pubDate></item></channel></rss>