博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在caffe中用python添加confusion matrix层
阅读量:2445 次
发布时间:2019-05-10

本文共 4134 字,大约阅读时间需要 13 分钟。

confusion matrix(混淆矩阵)在分类问题中能比单纯的准确率更全面地反应模型的好坏,本文主要目的是在caffe中用python添加confusion matrix层。

Step1:准备confusion matrix的python实现代码

完整代码如下:

import caffeimport jsonimport numpy as npimport sysimport sklearn.metricsclass PythonConfMat(caffe.Layer):    """    Compute the Accuracy with a Python Layer    """    def setup(self, bottom, top):        # check input pair        if len(bottom) != 2:            raise Exception("Need two inputs.")        self.num_labels = bottom[0].channels        params = json.loads(self.param_str)        self.test_iter = params['test_iter']        self.conf_matrix = np.zeros((self.num_labels, self.num_labels))        self.current_iter = 0    def reshape(self, bottom, top):        # bottom[0] are the net's outputs        # bottom[1] are the ground truth labels        # Net outputs and labels must have the same number of elements        if bottom[0].num != bottom[1].num:            raise Exception("Inputs must have the same number of elements.")        # accuracy output is scalar        top[0].reshape(1)    def forward(self, bottom, top):        self.current_iter += 1        # predicted outputs        pred = np.argmax(bottom[0].data, axis=1)        accuracy = np.sum(pred == bottom[1].data).astype(np.float32) / bottom[0].num        top[0].data[...] = accuracy        # compute confusion matrix        self.conf_matrix += sklearn.metrics.confusion_matrix(bottom[1].data, pred, labels=range(self.num_labels))        if self.current_iter == self.test_iter:            self.current_iter = 0            sys.stdout.write('\nCAUTION!! test_iter = %i. Make sure this is the correct value' % self.test_iter)            sys.stdout.write('\n"param_str: \'{"test_iter":%i}\'" has been set in the definition of the PythonLayer' % self.test_iter)            sys.stdout.write('\n\nConfusion Matrix')            sys.stdout.write('\t'*(self.num_labels-2)+'| Accuracy')            sys.stdout.write('\n'+'-'*8*(self.num_labels+1))            sys.stdout.write('\n')            for i in range(len(self.conf_matrix)):                for j in range(len(self.conf_matrix[i])):                    sys.stdout.write(str(self.conf_matrix[i][j].astype(np.int))+'\t')                sys.stdout.write('| %3.2f %%' % (self.conf_matrix[i][i]*100 / self.conf_matrix[i].sum()))                sys.stdout.write('\n')            sys.stdout.write('Number of test samples: %i \n\n' % self.conf_matrix.sum())            # reset conf_matrix for next test phase            self.conf_matrix = np.zeros((self.num_labels, self.num_labels))    def backward(self, top, propagate_down, bottom):        pass

注意:此python脚本应放置在$caffe_root/python目录下。

Step2:编译用python新添加的层

若caffe还没有编译,可以打开makefile.config中 WITH_PYTHON_LAYER=1的开关,然后make编译,再然后make pycaffe编译。

若caffe已经编译好了,则需要先make clean,然后用WITH_PYTHON_LAYER=1 make all -j&& make pycaffe编译caffe源码。

Step3:在prototxt中使用confusion matrix layer

在测试网络的prototxt中添加把混淆矩阵层添加进去,列子如下

layer {  type: 'Python'  name: 'py_accuracy'  top: 'py_accuracy'  bottom: 'fc101'  bottom: 'label'  python_param {    # the module name -- usually the filename -- that needs to be in $PYTHONPATH    module: 'python_confmat'    # the layer name -- the class name in the module    layer: 'PythonConfMat'    # this is the number of test iterations, it must be the same as defined in the solver.    param_str: '{"test_iter":100}'  }  include {    phase: TEST  }}

注意:test_iter乘以batch size应cover所有测试集样本。

Step4:使用/build/tools/caffe test工具计算测试集的confusion matrix

这个需要提前把测试机生成lmdb文件。生成后用如下姿势使用

CAFFE_ROOT=/home/***/caffe                                                                         MODEL_ROOT=/home/***/work/resnet_18_age_7                                                $CAFFE_ROOT/build/tools/caffe test --model=$MODEL_ROOT/ResNet-18-test.prototxt \                            --weights ./model_lrstep6/resnet18_age_finetue_iter_43883.caffemodel \                               --iterations 1392 --gpu=1

caffe test中不需要solver.prototxt,所以在命令中需要指定--iterations. 这里的iterations需要与PythonConfMat层中的test_iter相同。

注意:调用test命令可能出现import error:no module named python_confmat

解决方案:终端输入export PYTHONPATH=/home/xxx/caffe/python即可。

最后的效果如下

这里写图片描述

Reference:

你可能感兴趣的文章
如何将音乐添加到PowerPoint演示文稿
查看>>
mozilla.pdf_Mozilla说它没有从Booking.com赚钱
查看>>
fitbit手表中文说明书_Fitbit OS达到3.0版,这是新功能
查看>>
ublock origin_Chrome可能会在打破uBlock起源的同时更快地阻止广告
查看>>
电邮地址_我如何找出电子邮件的真正来源?
查看>>
windows虚拟桌面_在Windows中使用虚拟桌面的最佳免费程序
查看>>
ipad iphone开发_如何在iPhone或iPad上的消息中快速选择表情符号
查看>>
在windows使用gpu_如何选择Windows 10上游戏使用的GPU
查看>>
minecraft启动器_如何使用外部编辑器编辑Minecraft地图
查看>>
什么是适用于iPhone和iPad的iOS最新版本?
查看>>
成为产品不一定是坏事
查看>>
Ubuntu 18.04 LTS现在在Microsoft Store中
查看>>
如何检查已安装的Ubuntu版本
查看>>
如何在Windows 10上禁用附近共享
查看>>
gmail_Gmail将提供自毁电子邮件
查看>>
google 禁止广告_是否应禁止针对个人的广告?
查看>>
Plover.io在本地设备之间快速共享文件
查看>>
如何在OS X照片中禁用iCloud照片同步
查看>>
Minecraft的官方网站分发了受恶意软件感染的皮肤
查看>>
word模板快速填内容_如何快速轻松地在Word中选择内容块
查看>>