Caffe自定义python层为“准确性”

时间:2018-03-09 11:15:56

标签: python neural-network deep-learning caffe pycaffe

我正在尝试创建自己的自定义python层来计算网络准确性(在Phase:TEST中使用)。

我的问题:它是否仍然具有所有这四个功能:

  • 设置 - 使用从图层变量
  • 获取的参数初始化图层
  • 转发 - 图层的输入和输出是什么

  • 向后 - 给定下一层的预测和渐变,计算前一层的渐变

  • 重塑 - 根据需要重塑您的blob

如果是,为什么?我只想在TEST阶段使用它并计算准确性,而不是在学习中(前进和后退似乎是用于训练)。

谢谢大家!

1 个答案:

答案 0 :(得分:1)

虽然我不确定如果你没有定义所有这四种方法,Caffe可能会输出错误,你肯定需要设置转发

  • 设置:正是您所说的。例如,在我的Accuracy图层中,我通常会为我的整个测试集和每个样本的softmax概率保存一些指标(真和假阳性/阴性,f-得分),以防我想要组合/融合不同的网络/方法后来。这是我打开文件的地方,我将写下这些信息;
  • 转发:您可以在此处计算准确度本身,将预测与批次中每个样本的标签进行比较。通常这个层将有两个输入,标签(可能由数据/输入层提供的基本事实)和一个输出每个类中每个样本的预测/得分/概率的层(我通常使用SoftMax层);
  • 重塑向后:不要担心这些。你不需要担心向后传球也不需要重塑你的blob。

以下是精度图层的示例:

# Remark: This class is designed for a binary problem with classes '0' and '1'
# Saving this file as accuracyLayer.py

import caffe
TRAIN = 0
TEST = 1

class Accuracy_Layer(caffe.Layer):
    #Setup method
    def setup(self, bottom, top):
        #We want two bottom blobs, the labels and the predictions
        if len(bottom) != 2:
            raise Exception("Wrong number of bottom blobs (prediction and label)") 

        #Initialize some attributes
        self.correctPredictions = 0.0
        self.totalImgs = 0

    #Forward method
    def forward(self, bottom, top):
        #The order of these depends on the prototxt definition
        predictions = bottom[0].data
        labels = bottom[1].data

        self.totalImgs += len(labels)

        for i in range(len(labels)): #len(labels) is equal to the batch size
                pred = predictions[i]   #pred is a tuple with the normalized probability 
                                        #of a sample i.r.t. two classes
                lab = labels[i]

                if pred[0] > pred[1]:   #this means it was predicted as class 0
                        if lab == 0.0:
                                self.correctPredictions += 1.0

                else:                  #else, predicted as class 1
                        if lab == 1.0:
                                self.correctPredictions += 1.0

        acc = correctPredictions / self.totalImgs

       #output data to top blob
       top[0].data = acc

    def reshape(self, bottom, top):
        """
        We don't need to reshape or instantiate anything that is input-size sensitive
        """
        pass

    def backward(self, bottom, top):
        """
        This layer does not back propagate
        """
        pass

以及如何在原型文件中定义它。您可以在此处向Caffe说明此层将仅在TEST阶段出现:

layer {
  name: "metrics"
  type: "Python"
  top: "Acc"
  top: "FPR"
  top: "FNR"

  bottom: "prediction"   #let's suppose we have these two bottom blobs
  bottom: "label"

  python_param {
    module: "accuracyLayer"
    layer: "Accuracy_Layer"
  }
  include {
    phase: TEST.    #This will ensure it will only be executed in TEST phase
  }
}

BTW,I've written a gist有一个更复杂的精确python层示例,可能就是你要找的。