Keras 2DConvolution 产生不合逻辑的输出张量 - 32x32 图像变成 32xN 特征图,而不是 32x32xN
Keras 2DConvolution produces unlogical output tensor - 32x32 image becomes 32xN feature maps, instead of 32x32xN
根据我的理解,2D-Convolution 将 N 个滤波器应用于输入图像,产生 N 个新的 "images"(=特征图)。
如果我们忽略stride/subsampling,一个32x32的图像经过N个通道的二维卷积后变成了一个Nx32x32的张量:
但是,在 Keras 中,32x32 的输入会产生 32xN 的输出。所以,我的问题是,降维是如何应用的?这一步是不是又涉及了一个隐藏层?
如果是这样,网络是否会失去按原样查看图像(即二维实体)的能力?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
此代码可以重现我的上述声明:
inputs = Input(shape=(1, 32, 32,))
shared = Convolution2D(nb_filter=10, nb_row=8, nb_col=8, subsample=(1, 1), border_mode='same', activation='relu')(inputs)
print("1 => ", inputs.shape)
print("2 => ", shared.shape)
当我们对 32x32 灰度图像应用 10 个过滤器 (==N) 时,它会产生结果
1 => (?, 1, 32, 32)
2 => (?, 1, 32, 10)
在哪里?是未指定的批量大小,1 是输入通道的数量(RGB 为 3,灰度为 1)。
正如预期的那样,输入的形状为 32x32。
但是,卷积的输出具有 32xN 维度而不是 32x32xN
可能是输入格式顺序问题
来自 Keras 文档 Conv2D 采用此参数
data_format: A string, one of channels_last (default) or
channels_first. The ordering of the dimensions in the inputs.
channels_last corresponds to inputs with shape (batch, height, width,
channels) while channels_first corresponds to inputs with shape
(batch, channels, height, width). It defaults to the image_data_format
value found in your Keras config file at ~/.keras/keras.json. If you
never set it, then it will be "channels_last".
所以你的 (1, 32, 32) 实际上是一个 1x32 的图像,有 32 个通道。切换到 (32, 32, 1) 或将 data_format 参数设置为 channels_first
.
有关更改默认行为的更多信息:https://keras.io/backend/
您可以使用 keras.backend.image_data_format()
获取设置并使用 set_image_data_format(data_format)
设置 channels_first
或 channels_last
.
有这个设置是因为 Theano 和 TF 处理维度排序的方式不同,这取决于您使用的支持。
不,你做错了什么。以下代码:
import keras
from keras.layers import Input, Convolution2D
from keras.models import Model
inputs = Input(shape=(1, 32, 32,))
shared = Convolution2D(nb_filter=10, nb_row=8, nb_col=8,
subsample=(1, 1), border_mode='same', activation='relu')(inputs)
model = Model(inputs, shared)
model.summary()
打印以下内容:
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 1, 32, 32) 0
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D) (None, 10, 32, 32) 650 input_1[0][0]
====================================================================================================
Total params: 650
____________________________________________________________________________________________________
并且可以看到Convolution2D的输出形状确实有10个通道。如果您得到不同的结果,请检查 image_ordering 是否正确(并且有意义)。
根据我的理解,2D-Convolution 将 N 个滤波器应用于输入图像,产生 N 个新的 "images"(=特征图)。
如果我们忽略stride/subsampling,一个32x32的图像经过N个通道的二维卷积后变成了一个Nx32x32的张量:
如果是这样,网络是否会失去按原样查看图像(即二维实体)的能力?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
此代码可以重现我的上述声明:
inputs = Input(shape=(1, 32, 32,))
shared = Convolution2D(nb_filter=10, nb_row=8, nb_col=8, subsample=(1, 1), border_mode='same', activation='relu')(inputs)
print("1 => ", inputs.shape)
print("2 => ", shared.shape)
当我们对 32x32 灰度图像应用 10 个过滤器 (==N) 时,它会产生结果
1 => (?, 1, 32, 32)
2 => (?, 1, 32, 10)
在哪里?是未指定的批量大小,1 是输入通道的数量(RGB 为 3,灰度为 1)。
正如预期的那样,输入的形状为 32x32。 但是,卷积的输出具有 32xN 维度而不是 32x32xN
可能是输入格式顺序问题
来自 Keras 文档 Conv2D 采用此参数
data_format: A string, one of channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch, height, width, channels) while channels_first corresponds to inputs with shape (batch, channels, height, width). It defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be "channels_last".
所以你的 (1, 32, 32) 实际上是一个 1x32 的图像,有 32 个通道。切换到 (32, 32, 1) 或将 data_format 参数设置为 channels_first
.
有关更改默认行为的更多信息:https://keras.io/backend/
您可以使用 keras.backend.image_data_format()
获取设置并使用 set_image_data_format(data_format)
设置 channels_first
或 channels_last
.
有这个设置是因为 Theano 和 TF 处理维度排序的方式不同,这取决于您使用的支持。
不,你做错了什么。以下代码:
import keras
from keras.layers import Input, Convolution2D
from keras.models import Model
inputs = Input(shape=(1, 32, 32,))
shared = Convolution2D(nb_filter=10, nb_row=8, nb_col=8,
subsample=(1, 1), border_mode='same', activation='relu')(inputs)
model = Model(inputs, shared)
model.summary()
打印以下内容:
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 1, 32, 32) 0
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D) (None, 10, 32, 32) 650 input_1[0][0]
====================================================================================================
Total params: 650
____________________________________________________________________________________________________
并且可以看到Convolution2D的输出形状确实有10个通道。如果您得到不同的结果,请检查 image_ordering 是否正确(并且有意义)。