And*_*rea 3 python conv-neural-network keras max-pooling functional-api
我第一次构建用于图像分类的 CNN 模型,我对每种类型(1D CNN、2D CNN、3D CNN)的输入形状以及如何固定滤波器中的滤波器数量感到有点困惑。卷积层。我的数据是 100x100x30,其中 30 是特征。这是我使用函数式 API Keras 编写的 1D CNN 文章:
def create_CNN1D_model(pool_type='max',conv_activation='relu'):
input_layer = (30,1)
conv_layer1 = Conv1D(filters=16, kernel_size=3, activation=conv_activation)(input_layer)
max_pooling_layer1 = MaxPooling1D(pool_size=2)(conv_layer1)
conv_layer2 = Conv1D(filters=32, kernel_size=3, activation=conv_activation)(max_pooling_layer1)
max_pooling_layer2 = MaxPooling1D(pool_size=2)(conv_layer2)
flatten_layer = Flatten()(max_pooling_layer2)
dense_layer = Dense(units=64, activation='relu')(flatten_layer)
output_layer = Dense(units=10, activation='softmax')(dense_layer)
CNN_model = Model(inputs=input_layer, outputs=output_layer)
return CNN_model
CNN1D = create_CNN1D_model()
CNN1D.compile(loss = 'categorical_crossentropy', optimizer = "adam",metrics = ['accuracy'])
Trace = CNN1D.fit(X, y, epochs=50, batch_size=100)
Run Code Online (Sandbox Code Playgroud)
然而,在尝试通过将 Conv1D、Maxpooling1D 更改为 Conv2D 和 Maxpooling2D 来尝试 2D CNN 模型时,我收到以下错误:
ValueError: Input 0 of layer conv2d_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (None, 30, 1)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我 2D CNN 和 3D CNN 的输入形状如何?输入数据预处理可以做什么?
太长了;您X_train
可以被视为(批量、空间变暗...、通道)。内核并行应用于所有通道的空间维度。因此,2D CNN 需要两个空间维度(batch, dim 1, dim 2, channels)
。
因此,对于(100,100,3)
成形图像,您将需要一个 2D CNN,在所有 3 个通道上对 100 个高度和 100 个宽度进行卷积。
让我们来理解一下上面的说法。
首先,您需要了解 CNN(总体而言)在做什么。
内核在其特征图/通道上对张量的空间维度进行卷积,同时对相应的值执行简单的矩阵运算(如点积)。
现在,假设您有 100 张图像(称为“批次”)。每个图像都是 28 x 28 像素,有 3 个通道 R、G、B(在 CNN 中也称为特征图)。如果我将此数据存储为张量,则形状将为(100,28,28,3)
。
但是,我可以只拥有没有任何高度的图像(可能像信号),或者,我可以拥有具有额外空间维度的数据,例如视频(高度、宽度和时间)。
一般来说,基于 CNN 的神经网络的输入如下所示。
您需要知道的第二个关键点是,2D 内核将在 2 个空间维度上进行卷积,但相同的内核将在所有特征图/通道上执行此操作。所以,如果我有一个(3,3)
内核。这个相同的内核将应用于 R、G、B 通道(并行)并在图像的Height
和上移动。Width
最后,操作(对于单个特征图/通道和单个卷积窗口)可以如下所示可视化。
因此,简而言之——
让我们以具有单个特征图/通道的张量为例(因此,对于图像,它将是灰度化的) -
因此,凭着直觉,我们看到,如果我想使用1D CNN,您的数据必须具有 1 个空间维度,这意味着每个样本都需要是 2D(空间维度和通道),这意味着必须
X_train
是 3D 张量(batch, spatial dimensions, channels)
。类似地,对于2D CNN,您将有 2 个空间维度(例如 H、W)并且将是 3D 样本
(H, W, Channels)
并且X_train
将是(Samples, H, W, Channels)
让我们用代码来尝试一下 -
import tensorflow as tf
from tensorflow.keras import layers
X_2D = tf.random.normal((100,7,3)) #Samples, width/time, channels (feature maps)
X_3D = tf.random.normal((100,5,7,3)) #Samples, height, width, channels (feature maps)
X_4D = tf.random.normal((100,6,6,2,3)) #Samples, height, width, time, channels (feature maps)
Run Code Online (Sandbox Code Playgroud)
对于应用一维 CNN -
#With padding = same, the edge pixels are padded to not skip a few
#Out featuremaps = 10, kernel (3,)
cnn1d = layers.Conv1D(10, 3, padding='same')(X_2D)
print(X_2D.shape,'->',cnn1d.shape)
#(100, 7, 3) -> (100, 7, 10)
Run Code Online (Sandbox Code Playgroud)
对于应用 2D CNN -
#Out featuremaps = 10, kernel (3,3)
cnn2d = layers.Conv2D(10, (3,3), padding='same')(X_3D)
print(X_3D.shape,'->',cnn2d.shape)
#(100, 5, 7, 3) -> (100, 5, 7, 10)
Run Code Online (Sandbox Code Playgroud)
对于 3D CNN -
#Out featuremaps = 10, kernel (3,3)
cnn3d = layers.Conv3D(10, (3,3,2), padding='same')(X_4D)
print(X_4D.shape,'->',cnn3d.shape)
#(100, 6, 6, 2, 3) -> (100, 6, 6, 2, 10)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6860 次 |
最近记录: |