如何在Keras Conv2D函数中确定'filter'参数 [英] How To Determine the 'filter' Parameter in the Keras Conv2D Function

查看:991
本文介绍了如何在Keras Conv2D函数中确定'filter'参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才刚刚开始ML之旅,并完成了一些教程。 (对我而言)尚不清楚的一件事是如何确定Keras Conv2D的'filter'参数。



我读过的大多数资料都只是简单地将参数设置为32说明。这仅仅是一个经验法则,还是输入图像的尺寸起作用?例如,CIFAR-10中的图像为32x32



具体来说:

  model = Sequential()
过滤器= 32
model.add(Conv2D(filters,(3,3),padding ='相同',input_shape = x_train.shape [1:]))

model.add(Activation('relu'))
model.add(Conv2D(filters,(3,3) ))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2,2)))
model.add(Dropout(0.25))

下一层的过滤器参数为filter * 2或64。又如何计算? / p>

Tx。



Joe

解决方案

实际上-您的问题没有很好的答案。大多数架构通常在许多实验中经过精心设计和微调。我可以与您分享一些在设计自己的体系结构时应遵循的经验法则:


  1. 避免维度折叠在第一层。让我们假设您的输入过滤器的(n,n)空间形状为 RGB 图片。在这种情况下,最好将过滤器号设置为大于 n * n * 3 ,因为这是单个过滤器输入的维数。如果设置较小的数字,则可能会因以下事实而遭受损失:由于初始化而丢失了很多有用的信息,从而降低了信息量。当然-这不是一般规则-例如对于图像复杂度较低的纹理识别-少量过滤器实际上可能会有所帮助。


  2. 比过滤器数量多考虑体积

  3. strong>-在设置过滤器数量时,重要的是考虑体积变化,而不是连续层之间的过滤器数量变化。例如。在 VGG 中-即使在合并层之后过滤器的数量增加了一倍-实际要素映射量也减少了2倍,因为合并使要素图减少了1倍 4 。通常,将卷的大小减小3倍以上是不明智的做法。大多数现代架构都使用介于1到2之间的体积下降系数。仍然-这不是一般规则-例如在层次结构狭窄的情况下-较大的数量下降实际上可能会有所帮助。


  4. 避免瓶颈 。正如在这个里程碑中所读到的那样,纸张瓶颈可能会严重损害您的培训过程。降低音量太严重时会发生这种情况。当然-这仍然可以实现-但随后您应该使用智能下采样,例如在 Inception v> 2

  5. 中,
  6. 检查1x1卷积-认为过滤器激活高度相关。可以通过使用 1x1 卷积来利用它,即卷积大小为1的卷积。他们降低音量,而不是池化或智能下采样(请参见示例这里)。您可以例如再构建两次滤镜,然后使用1x1转换作为连续图层将其减少25%。


看到。没有简单的方法来选择过滤器的数量。除上述提示外,我想与您分享我最喜欢的过滤器数量检查之一。它需要2个简单的步骤:


  1. 尝试使用正则化对500个随机图像进行过拟合。

  2. 尝试

通常-如果过滤器的数量太少(通常)-这两个测试将向您显示。如果-在训练过程中-经过正规化-您的网络严重超负荷-这清楚地表明您的网络过滤器太多。



干杯。


I'm just beginning my ML journey and have done a few tutorials. One thing that's not clear (to me) is how the 'filter' parameter is determined for Keras Conv2D.

Most sources I've read simply set the parameter to 32 without explanation. Is this just a rule of thumb or do the dimensions of the input images play a part? For example, the images in CIFAR-10 are 32x32

Specifically:

model = Sequential()
filters = 32
model.add(Conv2D(filters, (3, 3), padding='same', input_shape=x_train.shape[1:]))

model.add(Activation('relu'))
model.add(Conv2D(filters, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

The next layer has a filter parameter of filter*2 or 64. Again, how is this calculated?

Tx.

Joe

解决方案

Actually - there is no a good answer to your question. Most of the architectures are usually carefully designed and finetuned during many experiments. I could share with you some of the rules of thumbs one should apply when designing its own architecture:

  1. Avoid a dimension collapse in the first layer. Let's assume that your input filter has a (n, n) spatial shape for RGB image. In this case, it is a good practice to set the filter numbers to be greater than n * n * 3 as this is the dimensionality of the input of a single filter. If you set smaller number - you could suffer from the fact that many useful pieces of information about the image are lost due to initialization which dropped informative dimensions. Of course - this is not a general rule - e.g. for a texture recognition, where image complexity is lower - a small number of filters might actually help.

  2. Think more about volume than filters number - when setting the number of filters it's important to think about the volume change instead of the change of filter numbers between the consecutive layers. E.g. in VGG - even though the number of filters doubles after pooling layer - the actual feature map volume is decreased by a factor of 2, because of pooling decreasing the feature map by a factor of 4. Usually decreasing the size of the volume by more than 3 should be considered as a bad practice. Most of the modern architectures use the volume drop factor in the range between 1 and 2. Still - this is not a general rule - e.g. in case of a narrow hierarchy - the greater value of volume drop might actually help.

  3. Avoid bottlenecking. As one may read in this milestone paper bottlenecking might seriously harm your training process. It occurs when dropping the volume is too severe. Of course - this still might be achieved - but then you should use the intelligent downsampling, used e.g. in Inception v>2

  4. Check 1x1 convolutions - it's believed that filters activation are highly correlated. One may take advantage of it by using 1x1 convolutions - namely convolution with a filter size of 1. This makes possible e.g. volume dropping by them instead of pooling or intelligent downsampling (see example here). You could e.g. build twice more filters and then cut 25% of them by using 1x1 convs as a consecutive layer.

As you may see. There is no easy way to choose the number of filters. Except for the hints above, I'd like to share with you one of my favorite sanity checks on the number of filters. It takes 2 easy steps:

  1. Try to overfit at 500 random images with regularization.
  2. Try to overfit at the whole dataset without any regularization.

Usually - if the number of filters is too low (in general) - these two tests will show you that. If - during your training process - with regularization - your network severely overfits - this is a clear indicator that your network has way too many filters.

Cheers.

这篇关于如何在Keras Conv2D函数中确定'filter'参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆