在不使用Keras API的情况下,提取Keras层的重量对于向前传球有什么意义

2024-05-16 17:37:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目的是知道在卷积过程中使用的核权重,然后对图像进行前向传递以进行分类。这项工作使用kerasapi很容易完成,但这是我硕士论文的要求,因为我想在FPGA上建立一个CNN模型,只用于测试/分类。在

不使用Keras API:

1/ I will write a plain code where I will give my preprocessed image as an input

2/ I will write convolution algorithm and give the extracted information of the Kernel to do the convolution

3/ I will write the algorithm for Flatten and

4/ By using Dense algorithm I want to predict the class

我的问题是:

1/ What is the information actually is giving by layer.get_weights()? Is it giving us the kernel weight which will use for the convolution?

2/ If I want to do the classification with the help of extracted weight how can I approach?

下面是我的模型:(为了简单起见,我刚刚编写了一个具有最小层的模型)

def cnn_model():
    model = Sequential()

    model.add(Conv2D(1, (3, 3), padding='same',
                 input_shape=input_shape,
                 activation='relu'))
    model.add(Flatten())
    model.add(Dense(num_classes, activation='softmax'))

    return model
model = cnn_model()
lr = 0.01
sgd = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
      optimizer=sgd,
      metrics=['accuracy'])

输入图像为灰度和宽度,高度为80,80。 我使用以下代码训练了我的模型:

^{pr2}$

我使用以下方法提取了层权重:

^{3}$

weight_数组=[0]的输出为

 [array([[[[ 0.3856341 ]],

    [[-0.35276324]],

    [[-0.51678646]]],


   [[[-0.62636113]],

    [[ 0.43428165]],

    [[-0.26765126]]],


   [[[ 0.461921  ]],

    [[-0.14468761]],

    [[-0.3061749 ]]]], dtype=float32), array([-0.1087065], dtype=float32)] 

任何建议都是值得赞赏的。在


Tags: theto模型图像addinputmodel分类
2条回答

1)实际提供的信息是什么layer.get_权重()? 它给出了卷积的核重吗?在

对于卷积层,layer.get_weights()返回[kernel,bias]。在

2)如果我想借助提取的权重进行分类,我该如何处理?在

复制你的人际网络的每一个阶段,其中的数学是有据可查的。密切注意正在执行的精确运算,例如,深度学习中的“卷积”与标准数学中的“卷积”不完全相同(不应用变换)。我建议你通过网络传递一个已知的输入,并检查你在每个阶段都得到相同的答案。在

我试着解释我对我的问题所做的和理解的解决办法。我在这里给出我已经尝试过的the solution。我从here上给出了主要信息。在

在模型文件卷积核权重中,卷积偏差密集层权重密集层偏差。如果有人想用Python或C++中的函数从头开始写一个前向传递,这些内核权重是很重要的。详细信息见Github link

相关问题 更多 >