问题训练 CNN 与素数输入维度 [英] Issues Training CNN with Prime number input dimensions

查看:47
本文介绍了问题训练 CNN 与素数输入维度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Keras(一种自动编码器)开发 CNN 模型.这种类型的输入是 (47,47,3) 形状,即具有 3 (RGB) 层的 47x47 图像.

I am currently developing a CNN model with Keras (an autoencoder). This type my inputs are of shape (47,47,3), that is a 47x47 image with 3 (RGB) layers.

我过去曾与一些 CNN 合作过,但这次我的输入维度是质数(47 像素).我认为这会导致我的实现出现问题,特别是在我的模型中使用 MaxPooling2DUpSampling2D 时.我注意到在最大池化然后向上采样时会丢失一些维度.

I have worked with some CNN's in the past, but this time my input dimensions are prime numbers (47 pixels). This I think is causing issues with my implementation, specifically when using MaxPooling2D and UpSampling2D in my model. I noticed that some dimensions are lost when max pooling and then up sampling.

使用 model.summary() 我可以看到在通过 传递我的 (47,47,3) 输入后Conv2D(24) 和带有 (2,2) 内核的 MaxPooling(即 24 个过滤器和一半的形状)我得到 (24,24, 24).

Using model.summary() I can see that after passing my (47,47,3) input through a Conv2D(24) and MaxPooling with a (2,2) kernel (that is 24 filters and half the shape) I get a output shape of (24, 24, 24).

现在,如果我尝试通过使用 (2,2) 内核(形状加倍)进行 UpSampling 来反转它并再次卷积,我会得到一个 (48,48,3) 整形输出.这比需要的多一列.

Now, if I try to reverse that by UpSampling with a (2,2) kernel (double the shape) and convolving again I get a (48,48,3) shaped output. That is one extra row and column than needed.

对此我认为没问题,只需选择一个内核大小,在上采样时为您提供所需的 47 个像素",但鉴于 47 是一个质数,在我看来,有没有内核大小可以做到这一点.

To this I thought "no problem, just chose a kernel size that gives you the desired 47 pixels when up sampling", but given that 47 is a prime number it seems to me that there is no kernel size that can do that.

有什么方法可以绕过这个问题而不涉及将我的输入维度更改为非素数?也许我在我的方法中遗漏了一些东西,或者 Keras 有一些我可以忽略的功能在这里提供帮助.

Is there any way to bypass this problem that does not involve changing my input dimensions to a non-prime? Maybe I am missing something in my approach or maybe Keras has some feature I ignore that could help here.

推荐答案

我建议你使用 ZeroPadding2DCropping2D.您可以使用 0 不对称地填充图像,并在不调整图像大小的情况下获得均匀大小的图像.这应该可以解决上采样的问题.此外 - 请记住在所有卷积层中设置 padding=same.

I advice you to use ZeroPadding2D and Cropping2D. You can pad your image asymmetrically with 0s and obtain an even size of your image without resizing it. This should solve the problem with upsampling. Moreover - remember about setting padding=same in all of your convolutional layers.

只是给你一个关于如何执行此类操作的示例策略:

Just to give you an example strategy on how to perform such operations:

  1. 如果在池化之前网络的大小是奇数 - 将其填充为零以使其均匀.
  2. 在相应的上采样操作之后,使用裁剪将您的特征图恢复到原始奇数大小.

这篇关于问题训练 CNN 与素数输入维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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