问题描述
我正在尝试将 Python 中经过训练的手语分类解决方案转换为 C 语言头文件,以便我可以在 M4-cortex cpu 板上进行部署。 在 Python 中,我能够构建模型并训练它,我可以看到它以 90% 的准确率进行预测。 但是我发现卷积层中使用/生成的权重数量存在问题
**Conv_1d configuration**
print(x_train.shape)
model = Sequential()
model.add(Conv1D(32,kernel_size=5,padding='same',input_shape=x_train.shape[1:],name='conv1d_1'))
print(model.layers[0].kernel.numpy().shape)
**output:**
(1742,45,45)
**(5,32)**
According to above configuration
input dimension = 45x45x1 pixels of image(gray scale)
input channels = 1
output dimension = 45x45x32
output channesls = 32
kernel size = 5
As per the concept(w.r.t https://cs231n.github.io/convolutional-networks/)
number of weights = (input_channels) x (kernel_size) x (kernel_size) x (output_channels)=1x5x5x32=800
But keras model produces weights array of size = [5][45][32]=7200
I'm not sure if my interpretation of weight array in keras model is correct,I would be glad if someone can help me with this
解决方法
一些应该可以澄清您的疑虑的要点。
-
您的权重数公式不正确,因为您使用的是
Conv1D
,因此内核大小只有一个维度。 -
定义输入形状
x_train.shape[1:] = (45,45)
对应于应用于具有 45 个元素的数组的 45 个过滤器(同样是因为它是一个Conv1D
)。 -
如是说,权重数为:
# of weights = input_filters x kernel_size x output_filters = 45x5x32 = 7200
(无偏见) -
考虑到您有图像,您可能正在寻找
Conv2D
。在这种情况下,输入形状应该是(45,45,1)
,内核有两个维度,并且参数数量正好是800(没有偏差)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(32,kernel_size=5,padding='same',input_shape=(45,1),use_bias=False))
model.summary()
# Layer (type) Output Shape Param #
# conv (Conv2D) (None,32) 800