我们可以从SSD的caffe源码中得到test的mAP,mAP是不同阈值下的precision均值,但如果我们想看某个阈值下的recall
和precision
时,就需要对solver.cpp
源码做一定修改。
关于mAP, recall和precision的解释这里不赘述,可以参考以下博客:
中文介绍
英文介绍
修改caffe.proto
首先在src/caffe/proto/caffe.proto中的SolverParameter
这个message下加上一个参数rec_prec_thr
,该参数是判断样本是否为true positive (tp) 的score阈值,我们给他一个默认值0.6,代码如下(注意序列号在自己的SolverParameter最后的序列号上加1)
optional float rec_prec_thr = 45 [default = 0.6];
修改solver.cpp
接下来在src/caffe/solver.cpp的void Solver<Dtype>::TestDetection(const int test_net_id)
函数中加入计算recall和precision的代码:
template <typename Dtype>
void Solver<Dtype>::TestDetection(const int test_net_id) {
CHECK(Caffe::root_solver());
LOG(INFO) << "Iteration " << iter_
<< ", Testing net (#" << test_net_id << ")";
CHECK_NOTNULL(test_nets_[test_net_id].get())->
ShareTrainedLayersWith(net_.get());
map<int, map<int, vector<pair<float, int> > > > all_true_pos;
map<int, map<int, vector<pair<float, int> > > > all_false_pos;
map<int, map<int, int> > all_num_pos;
const shared_ptr<Net<Dtype> >& test_net = test_nets_[test_net_id];
Dtype loss = 0;
for (int i = 0; i < param_.test_iter(test_net_id); ++i) {
SolverAction::Enum request = GetRequestedAction();