转自:https://zhuanlan.zhihu.com/p/30197320
<div class="RichText ztext Post-RichText"><p><b>图像深度学习任务中,面对小数据集,我们往往需要利用Image Data Augmentation图像增广技术来扩充我们的数据集,而keras的内置ImageDataGenerator很好地帮我们实现图像增广。但是面对ImageDataGenerator中众多的参数,每个参数所得到的效果分别是怎样的呢?本文针对Keras中ImageDataGenerator的各项参数数值的效果进行了详细解释,为各位深度学习研究者们提供一个参考。</b></p><p>我们先来看看ImageDataGenerator的<a href="http://link.zhihu.com/?target=https%3A//keras.io/preprocessing/image/" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">官方说明</a>:</p><div class="highlight"><pre><code class="language-text"><span></span>keras.preprocessing.image.ImageDataGenerator(featurewise\_center=False, samplewise\_center=False, featurewise\_std\_normalization=False, samplewise\_std\_normalization=False, zca\_whitening=False, zca\_epsilon=1e-6, rotation\_range=0., width\_shift\_range=0., height\_shift\_range=0., shear\_range=0., zoom\_range=0., channel\_shift\_range=0., fill\_mode='nearest', cval=0., horizontal\_flip=False, vertical\_flip=False, rescale=None, preprocessing\_function=None, data\_format=K.image\_data\_format()) </code></pre></div><p class="ztext-empty-paragraph"><br></p><p>官方提供的参数解释因为太长就不贴出来了,大家可以直接点开上面的链接看英文原介绍,我们现在就从每一个参数开始看看它会带来何种效果。</p><p>我们测试选用的是kaggle dogs vs cats redux 猫狗大战的数据集,随机选取了9张狗狗的照片,这9张均被resize成224×224的尺寸,如图1:</p><figure><noscript><img src="https://pic3.zhimg.com/v2-bea4f8eb3dbb5ad3327c5069645d00be\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic3.zhimg.com/v2-bea4f8eb3dbb5ad3327c5069645d00be\_r.jpg"></noscript><img src="https://pic3.zhimg.com/80/v2-bea4f8eb3dbb5ad3327c5069645d00be\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic3.zhimg.com/v2-bea4f8eb3dbb5ad3327c5069645d00be\_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-bea4f8eb3dbb5ad3327c5069645d00be\_b.jpg" width="1200"><figcaption>图1</figcaption></figure><h2>1. featurewise</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(featurewise\_center=True, featurewise\_std\_normalization=True) </code></pre></div><p>featurewise\_center的官方解释:"Set input mean to 0 over the dataset, feature-wise." 大意为使数据集去中心化(使得其均值为0),而samplewise\_std\_normalization的官方解释是“ Divide inputs by std of the dataset, feature-wise.”,大意为将输入的每个样本除以其自身的标准差。这两个参数都是从数据集整体上对每张图片进行标准化处理,我们看看效果如何:</p><figure><noscript><img src="https://pic3.zhimg.com/v2-e0db407c41042b4bb0735a3f81353856\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic3.zhimg.com/v2-e0db407c41042b4bb0735a3f81353856\_r.jpg"></noscript><img src="https://pic3.zhimg.com/80/v2-e0db407c41042b4bb0735a3f81353856\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic3.zhimg.com/v2-e0db407c41042b4bb0735a3f81353856\_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-e0db407c41042b4bb0735a3f81353856\_b.jpg" width="1200"><figcaption>图2</figcaption></figure><p>与图1原图相比,经过处理后的图片在视觉上稍微“变暗”了一点。</p><h2>2. samplewise</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(samplewise\_center=True, samplewise\_std\_normalization=True) </code></pre></div><p>samplewise\_center的官方解释为:“ Set each sample mean to 0.”,使输入数据的每个样本均值为0;samplewise\_std\_normalization的官方解释为:“Divide each input by its std.”,将输入的每个样本除以其自身的标准差。这个月featurewise的处理不同,featurewise是从整个数据集的分布去考虑的,而samplewise只是针对自身图片,效果如图3:</p><figure><noscript><img src="https://pic4.zhimg.com/v2-7a5270fe9ba803e2e70a2021c00f685b\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic4.zhimg.com/v2-7a5270fe9ba803e2e70a2021c00f685b\_r.jpg"></noscript><img src="https://pic4.zhimg.com/80/v2-7a5270fe9ba803e2e70a2021c00f685b\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic4.zhimg.com/v2-7a5270fe9ba803e2e70a2021c00f685b\_r.jpg" data-actualsrc="https://pic4.zhimg.com/v2-7a5270fe9ba803e2e70a2021c00f685b\_b.jpg" width="1200"><figcaption>图3</figcaption></figure><p>看来针对自身数据分布的处理在猫狗大战数据集上没有什么意义,或许在mnist这类灰度图上有用?读者可以试试。</p><h2>3. zca\_whtening</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(zca\_whitening=True) </code></pre></div><p>zca白化的作用是针对图片进行PCA降维操作,减少图片的冗余信息,保留最重要的特征,细节可参看:<a href="http://link.zhihu.com/?target=https%3A//en.wikipedia.org/wiki/Whitening\_transformation" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">Whitening transformation--维基百科</a>,<a href="http://link.zhihu.com/?target=http%3A//ufldl.stanford.edu/wiki/index.php/Whitening" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">Whitening--斯坦福</a>。</p><p>很抱歉的是,本人使用keras的<a href="http://link.zhihu.com/?target=https%3A//keras.io/preprocessing/image/" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">官方演示代码</a>,并没有复现出zca\_whitening的效果,当我的图片resize成224×224时,代码报内存错误,应该是在计算SVD的过程中数值太大。后来resize成28×28,就没有内存错误了,但是代码运行了一晚上都不结束,因此使用猫狗大战图片无法复现效果,这里转发另外一个博客使用mnist复现出的结果,如下图4。针对mnist的其它DataAugmentation结果可以看这个博客:<a href="http://link.zhihu.com/?target=https%3A//machinelearningmastery.com/image-augmentation-deep-learning-keras/" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">Image Augmentation for Deep Learning With Keras</a>,有修改意见的朋友欢迎留言。</p><figure><noscript><img src="https://pic1.zhimg.com/v2-39b4adf59ef2c3e80ee3da2978a7f95c\_b.jpg" data-rawwidth="800" data-rawheight="600" class="origin\_image zh-lightbox-thumb" width="800" data-original="https://pic1.zhimg.com/v2-39b4adf59ef2c3e80ee3da2978a7f95c\_r.jpg"></noscript><img src="https://pic1.zhimg.com/80/v2-39b4adf59ef2c3e80ee3da2978a7f95c\_hd.jpg" data-rawwidth="800" data-rawheight="600" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic1.zhimg.com/v2-39b4adf59ef2c3e80ee3da2978a7f95c\_r.jpg" data-actualsrc="https://pic1.zhimg.com/v2-39b4adf59ef2c3e80ee3da2978a7f95c\_b.jpg" width="800"><figcaption>图4</figcaption></figure><h2>4. rotation range</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(rotation\_range=30) </code></pre></div><p>rotation range的作用是用户指定旋转角度范围,其参数只需指定一个整数即可,但并不是固定以这个角度进行旋转,而是在 \[0, 指定角度\] 范围内进行随机角度旋转。效果如图5:</p><figure><noscript><img src="https://pic2.zhimg.com/v2-d3ef75e5a92ed018d6e3b7c8f649e9cd\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-d3ef75e5a92ed018d6e3b7c8f649e9cd\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-d3ef75e5a92ed018d6e3b7c8f649e9cd\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-d3ef75e5a92ed018d6e3b7c8f649e9cd\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-d3ef75e5a92ed018d6e3b7c8f649e9cd\_b.jpg" width="1200"><figcaption>图5</figcaption></figure><h2>5. width\_shift\_range & height\_shift\_range</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(width\_shift\_range=0.5,height\_shift\_range=0.5) </code></pre></div><p>width\_shift\_range & height\_shift\_range 分别是水平位置评议和上下位置平移,其参数可以是\[0, 1\]的浮点数,也可以大于1,其最大平移距离为<b>图片长或宽的尺寸乘以参数</b>,同样平移距离并不固定为最大平移距离,平移距离在 \[0, 最大平移距离\] 区间内。效果如图6:</p><figure><noscript><img src="https://pic4.zhimg.com/v2-304c370ceb9b6d221009e1d2d62ddd47\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic4.zhimg.com/v2-304c370ceb9b6d221009e1d2d62ddd47\_r.jpg"></noscript><img src="https://pic4.zhimg.com/80/v2-304c370ceb9b6d221009e1d2d62ddd47\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic4.zhimg.com/v2-304c370ceb9b6d221009e1d2d62ddd47\_r.jpg" data-actualsrc="https://pic4.zhimg.com/v2-304c370ceb9b6d221009e1d2d62ddd47\_b.jpg" width="1200"><figcaption>图6</figcaption></figure><p>平移图片的时候一般会出现超出原图范围的区域,这部分区域会根据fill\_mode的参数来补全,具体参数看下文。当参数设置过大时,会出现图7的情况,因此尽量不要设置太大的数值。</p><figure><noscript><img src="https://pic2.zhimg.com/v2-9aff91273a5b9ef1a1cc7f04c135efd9\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-9aff91273a5b9ef1a1cc7f04c135efd9\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-9aff91273a5b9ef1a1cc7f04c135efd9\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-9aff91273a5b9ef1a1cc7f04c135efd9\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-9aff91273a5b9ef1a1cc7f04c135efd9\_b.jpg" width="1200"><figcaption>图7</figcaption></figure><h2>6. shear\_range</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(shear\_range=0.5) </code></pre></div><p>shear\_range就是错切变换,效果就是让所有点的x坐标(或者y坐标)保持不变,而对应的y坐标(或者x坐标)则按比例发生平移,且平移的大小和该点到x轴(或y轴)的垂直距离成正比。</p><p>如图8所示,一个黑色矩形图案变换为蓝色平行四边形图案。狗狗图片变换效果如图9所示。</p><figure><noscript><img src="https://pic2.zhimg.com/v2-0808ef3e12ab126663b8c6dabfa44a81\_b.jpg" data-rawwidth="424" data-rawheight="305" class="origin\_image zh-lightbox-thumb" width="424" data-original="https://pic2.zhimg.com/v2-0808ef3e12ab126663b8c6dabfa44a81\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-0808ef3e12ab126663b8c6dabfa44a81\_hd.jpg" data-rawwidth="424" data-rawheight="305" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-0808ef3e12ab126663b8c6dabfa44a81\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-0808ef3e12ab126663b8c6dabfa44a81\_b.jpg" width="424"><figcaption>图8</figcaption></figure><figure><noscript><img src="https://pic2.zhimg.com/v2-717868f2375d8fa454757478302f21e5\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-717868f2375d8fa454757478302f21e5\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-717868f2375d8fa454757478302f21e5\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-717868f2375d8fa454757478302f21e5\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-717868f2375d8fa454757478302f21e5\_b.jpg" width="1200"><figcaption>图9</figcaption></figure><h2>7. zoom\_range</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(zoom\_range=0.5) </code></pre></div><p>zoom\_range参数可以让图片在长或宽的方向进行放大,可以理解为某方向的resize,因此这个参数可以是一个数或者是一个list。当给出一个数时,图片同时在长宽两个方向进行同等程度的放缩操作;当给出一个list时,则代表\[width\_zoom\_range, height\_zoom\_range\],即分别对长宽进行不同程度的放缩。而参数大于0小于1时,执行的是放大操作,当参数大于1时,执行的是缩小操作。</p><p>参数大于0小于1时,效果如图10:</p><figure><noscript><img src="https://pic2.zhimg.com/v2-d4e1f8d1c461fb052165f3b865105591\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-d4e1f8d1c461fb052165f3b865105591\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-d4e1f8d1c461fb052165f3b865105591\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-d4e1f8d1c461fb052165f3b865105591\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-d4e1f8d1c461fb052165f3b865105591\_b.jpg" width="1200"><figcaption>图10</figcaption></figure><p>参数等于4时,效果如图11:</p><figure><noscript><img src="https://pic2.zhimg.com/v2-f8a76dc501e850b709570ef395cef7b5\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-f8a76dc501e850b709570ef395cef7b5\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-f8a76dc501e850b709570ef395cef7b5\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-f8a76dc501e850b709570ef395cef7b5\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-f8a76dc501e850b709570ef395cef7b5\_b.jpg" width="1200"><figcaption>图11</figcaption></figure><h2>8. channel\_shift\_range</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(channel\_shift\_range=10) </code></pre></div><p>channel\_shift\_range可以理解成改变图片的颜色,通过对颜色通道的数值偏移,改变图片的整体的颜色,这意味着是“整张图”呈现某一种颜色,像是加了一块有色玻璃在图片前面一样,因此它并不能单独改变图片某一元素的颜色,如黑色小狗不能变成白色小狗。当数值为10时,效果如图12;当数值为100时,效果如图13,可见当数值越大时,颜色变深的效果越强。</p><figure><noscript><img src="https://pic1.zhimg.com/v2-c472c272aa88e23fb173fccff155ac90\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic1.zhimg.com/v2-c472c272aa88e23fb173fccff155ac90\_r.jpg"></noscript><img src="https://pic1.zhimg.com/80/v2-c472c272aa88e23fb173fccff155ac90\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic1.zhimg.com/v2-c472c272aa88e23fb173fccff155ac90\_r.jpg" data-actualsrc="https://pic1.zhimg.com/v2-c472c272aa88e23fb173fccff155ac90\_b.jpg" width="1200"><figcaption>图12</figcaption></figure><figure><noscript><img src="https://pic3.zhimg.com/v2-63a24555ccd27b346c9fb5d04da66142\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic3.zhimg.com/v2-63a24555ccd27b346c9fb5d04da66142\_r.jpg"></noscript><img src="https://pic3.zhimg.com/80/v2-63a24555ccd27b346c9fb5d04da66142\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic3.zhimg.com/v2-63a24555ccd27b346c9fb5d04da66142\_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-63a24555ccd27b346c9fb5d04da66142\_b.jpg" width="1200"><figcaption>图13</figcaption></figure><h2>9. horizontal\_flip & vertical\_flip</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(horizontal\_flip=True) </code></pre></div><p>horizontal\_flip的作用是随机对图片执行水平翻转操作,意味着不一定对所有图片都会执行水平翻转,每次生成均是随机选取图片进行翻转。效果如图14。</p><figure><noscript><img src="https://pic3.zhimg.com/v2-cfb6e83dbf8eccf6b5a23c48b00cac5a\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic3.zhimg.com/v2-cfb6e83dbf8eccf6b5a23c48b00cac5a\_r.jpg"></noscript><img src="https://pic3.zhimg.com/80/v2-cfb6e83dbf8eccf6b5a23c48b00cac5a\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic3.zhimg.com/v2-cfb6e83dbf8eccf6b5a23c48b00cac5a\_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-cfb6e83dbf8eccf6b5a23c48b00cac5a\_b.jpg" width="1200"><figcaption>图14</figcaption></figure><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(vertical\_flip=True) </code></pre></div><p>vertical\_flip是作用是对图片执行上下翻转操作,和horizontal\_flip一样,每次生成均是随机选取图片进行翻转,效果如图15。</p><figure><noscript><img src="https://pic3.zhimg.com/v2-35cac7da2db432defc60c03dbbd349aa\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic3.zhimg.com/v2-35cac7da2db432defc60c03dbbd349aa\_r.jpg"></noscript><img src="https://pic3.zhimg.com/80/v2-35cac7da2db432defc60c03dbbd349aa\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic3.zhimg.com/v2-35cac7da2db432defc60c03dbbd349aa\_r.jpg" data-actualsrc="https://pic3.zhimg.com/v2-35cac7da2db432defc60c03dbbd349aa\_b.jpg" width="1200"><figcaption>图15</figcaption></figure><p>当然了,在猫狗大战数据集当中不适合使用vertical\_flip,因为一般没有倒过来的动物。</p><h2>10. rescale</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(rescale= 1/255, width\_shift\_range=0.1) </code></pre></div><p>rescale的作用是对图片的每个像素值均乘上这个放缩因子,这个操作在所有其它变换操作之前执行,在一些模型当中,直接输入原图的像素值可能会落入激活函数的“死亡区”,因此设置放缩因子为1/255,把像素值放缩到0和1之间有利于模型的收敛,避免神经元“死亡”。</p><p>图片经过rescale之后,保存到本地的图片用肉眼看是没有任何区别的,如果我们在内存中直接打印图片的数值,可以看到以下结果:</p><figure><noscript><img src="https://pic1.zhimg.com/v2-0fc98097a1feee974f8e97114e48a14c\_b.jpg" data-rawwidth="747" data-rawheight="392" class="origin\_image zh-lightbox-thumb" width="747" data-original="https://pic1.zhimg.com/v2-0fc98097a1feee974f8e97114e48a14c\_r.jpg"></noscript><img src="https://pic1.zhimg.com/80/v2-0fc98097a1feee974f8e97114e48a14c\_hd.jpg" data-rawwidth="747" data-rawheight="392" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic1.zhimg.com/v2-0fc98097a1feee974f8e97114e48a14c\_r.jpg" data-actualsrc="https://pic1.zhimg.com/v2-0fc98097a1feee974f8e97114e48a14c\_b.jpg" width="747"><figcaption>图16</figcaption></figure><p>可以从图16看到,图片像素值都被缩小到0和1之间,但如果打开保存在本地的图片,其数值依然不变,如图17。</p><figure><noscript><img src="https://pic1.zhimg.com/v2-f0ab6393deb2483f521bdcd57d938468\_b.jpg" data-rawwidth="752" data-rawheight="412" class="origin\_image zh-lightbox-thumb" width="752" data-original="https://pic1.zhimg.com/v2-f0ab6393deb2483f521bdcd57d938468\_r.jpg"></noscript><img src="https://pic1.zhimg.com/80/v2-f0ab6393deb2483f521bdcd57d938468\_hd.jpg" data-rawwidth="752" data-rawheight="412" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic1.zhimg.com/v2-f0ab6393deb2483f521bdcd57d938468\_r.jpg" data-actualsrc="https://pic1.zhimg.com/v2-f0ab6393deb2483f521bdcd57d938468\_b.jpg" width="752"><figcaption>图17</figcaption></figure><p>应该是在保存到本地的时候,keras把图像像素值恢复为原来的尺度了,在内存中查看则不会。</p><h2>11. fill\_mode</h2><div class="highlight"><pre><code class="language-text"><span></span>datagen = image.ImageDataGenerator(fill\_mode='wrap', zoom\_range=\[4, 4\]) </code></pre></div><p>fill\_mode为填充模式,如前面提到,当对图片进行平移、放缩、错切等操作时,图片中会出现一些缺失的地方,那这些缺失的地方该用什么方式补全呢?就由fill\_mode中的参数确定,包括:“constant”、“nearest”(默认)、“reflect”和“wrap”。这四种填充方式的效果对比如图18所示,从左到右,从上到下分别为:“reflect”、“wrap”、“nearest”、“constant”。</p><figure><noscript><img src="https://pic2.zhimg.com/v2-43c58d436696f190104204e550f2ae55\_b.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb" width="1200" data-original="https://pic2.zhimg.com/v2-43c58d436696f190104204e550f2ae55\_r.jpg"></noscript><img src="https://pic2.zhimg.com/80/v2-43c58d436696f190104204e550f2ae55\_hd.jpg" data-rawwidth="1200" data-rawheight="800" class="origin\_image zh-lightbox-thumb lazy" data-original="https://pic2.zhimg.com/v2-43c58d436696f190104204e550f2ae55\_r.jpg" data-actualsrc="https://pic2.zhimg.com/v2-43c58d436696f190104204e550f2ae55\_b.jpg" width="1200"><figcaption>图18</figcaption></figure><p>当设置为“constant”时,还有一个可选参数,cval,代表使用某个固定数值的颜色来进行填充。图19为cval=100时的效果,可以与图18右下角的无cval参数的图对比。</p><figure><noscript><img src="https://pic1.zhimg.com/v2-4e8101e378d7cfb64918b203ea3028c4\_b.jpg" data-rawwidth="224" data-rawheight="224" class="content\_image" width="224"></noscript><img src="https://pic1.zhimg.com/80/v2-4e8101e378d7cfb64918b203ea3028c4\_hd.jpg" data-rawwidth="224" data-rawheight="224" class="content\_image lazy" data-actualsrc="https://pic1.zhimg.com/v2-4e8101e378d7cfb64918b203ea3028c4\_b.jpg" width="224"><figcaption>图19</figcaption></figure><h2>自己动手来测试?</h2><p>这里给出一段小小的代码,作为进行这些参数调试时的代码,你也可以使用jupyter notebook来试验这些参数,把图片结果打印到你的网页上。</p><div class="highlight"><pre><code class="language-text"><span></span>%matplotlib inline import matplotlib.pyplot as plt from PIL import Image from keras.preprocessing import image import glob设置生成器参数
datagen = image.ImageDataGenerator(fill_mode='wrap', zoom_range=[4, 4])
gen_data = datagen.flow_from_directory(PATH, batch_size=1, shuffle=False, save_to_dir=SAVE_PATH, save_prefix='gen', target_size=(224, 224))
生成9张图
for i in range(9): gen_data.next()
找到本地生成图,把9张图打印到同一张figure上
name_list = glob.glob(gen_path+'16/*') fig = plt.figure() for i in range(9): img = Image.open(name_list[i]) sub_img = fig.add_subplot(331 + i) sub_img.imshow(img) plt.show() </code></pre></div><p class="ztext-empty-paragraph"><br></p><h2>结语</h2><p>面对小数据集时,使用DataAugmentation扩充你的数据集就变得非常重要,但在使用DataAugmentation之前,先要了解你的数据集需不需要这类图片,如猫狗大战数据集不需要上下翻转的图片,以及思考一下变换的程度是不是合理的,例如把目标水平偏移到图像外面就是不合理的。多试几次效果,再最终确定使用哪些参数。上面所有内容已经公布在我的<a href="http://link.zhihu.com/?target=https%3A//github.com/JustinhoCHN/keras-image-data-augmentation" class=" wrap external" target="\_blank" rel="nofollow noreferrer" data-za-detail-view-id="1043">github</a>上面,附上了实验时的jupyter notebook文件,大家可以玩一玩,have fun!</p><p class="ztext-empty-paragraph"><br></p><p><b>注:转载、翻译请直接私聊本人,经本人同意后方可进行转载。</b></p></div>