在 keras 中实现 LeNet。我怎样才能在卷积层中只组合一些特征图?

Implementing LeNet in keras. How can I only combine some feature maps in a convolution layer?

我目前正在尝试从其在 keras 中的原始论文中实现 LeNet。那里的架构定义如下

作者还描述,在卷积层 C3 中,并非所有过滤器都应用于前一层的所有特征图。他们还提供了这个 table 来定义,哪些特征图被组合用于什么过滤器。似乎我找到的每个实现都不会这样做。

所以我的问题是

这是我当前的代码,没有选择性地应用过滤器。

model = keras.Sequential()

#C1
model.add(layers.Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=(32,32,1)))
#S2    
model.add(layers.AveragePooling2D())
#C3
model.add(layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu'))
model.add(layers.AveragePooling2D())

model.add(layers.Flatten())

model.add(layers.Dense(units=120, activation='relu'))

model.add(layers.Dense(units=84, activation='relu'))

model.add(layers.Dense(units=10, activation = 'softmax'))

您可以相当容易地使用函数 API.

获取由 NN 层生成的某些张量切片

即,让conv_1=tf.keras.layers.Conv2D(parameters) 成为您要从中提取某些特征图的图层。相应地,在模型的定义中你会有

    features = conv_1(previous_features)

如果你想从通道 3、4 和 5 获取特征图,你可以使用 tf.slice 或使用 pythonic/numpy 索引(TF 支持):

    # assuming dimensions are (batch_size, height, width, channels)
    feature_maps = features[:,:,:,3:6] 

要获取任意频道,您可能首先需要通过使用 tf.unstack:

沿着频道轴拆开 features 来从 features 中列出一个列表
    features_list = tf.unstack(feature_maps, axis=-1)

这会生成一个张量列表,每个张量都是一个特定的特征图。然后你可以使用 tf.satck:

将它们组合成一个新的张量
    # let a1, ..., an are indexes of the channels you need.
    particular_features = tf.stack( [features_list[a1], ... , features_list[an] ], axis=-1 )

您可以将由此获得的特征图作为参数传递给更多层,从而在网络中进一步使用它们。这应该可以完成工作。

我不熟悉 LeNet 模型的任何实现,所以我无法对此发表评论。