Keras 可以处理不同大小的输入图像吗?

问题描述:

Keras 可以处理不同大小的输入图像吗?例如,在全卷积神经网络中,输入图像可以有任意大小.但是,我们在通过 Keras 创建网络时需要指定输入形状.因此,我们如何使用 Keras 处理不同的输入尺寸而不将输入图像调整为相同尺寸?感谢您的帮助.

Can the Keras deal with input images with different size? For example, in the fully convolutional neural network, the input images can have any size. However, we need to specify the input shape when we create a network by Keras. Therefore, how can we use Keras to deal with different input size without resizing the input images to the same size? Thanks for any help.

是的.只需将您的输入形状更改为 shape=(n_channels, None, None).其中 n_channels 是输入图像中的通道数.

Yes. Just change your input shape to shape=(n_channels, None, None). Where n_channels is the number of channels in your input image.

我使用的是 Theano 后端,所以如果您使用的是 tensorflow,您可能需要将其更改为 (None,None,n_channels)

I'm using Theano backend though, so if you are using tensorflow you might have to change it to (None,None,n_channels)

你应该使用:

input_shape=(1, None, None)

input_shape=(1, None, None)

形状中的无"表示可变尺寸.请注意,并非所有图层将适用于此类可变尺寸,因为某些层需要形状信息(例如 Flatten).https://github.com/fchollet/keras/issues/1920

None in a shape denotes a variable dimension. Note that not all layers will work with such variable dimensions, since some layers require shape information (such as Flatten). https://github.com/fchollet/keras/issues/1920

例如,使用 keras 的函数式 API,您的输入层将是:

For example, using keras's functional API your input layer would be:

对于 RGB 数据集

inp = Input(shape=(3,None,None))

对于灰色数据集

inp = Input(shape=(1,None,None))