这篇文章的主要目的是想把yolo转到caffe框架上去做检测,训练检测模型用的是github上https://github.com/yeahkun/caffe-yolo 的框架,训练了一个试卷中教师批注分数位置检测的模型,然后在用c++实现预测功能。
环境配置
ubuntu16.04上训练caffe-yolo模型
windows操作系统实现预测功能
编译工具
Visual Studio 2013
参考深度学习框架
这里的作者有介绍详细的模型训练和测试过程,只需要修改适合自己检测模型的参数即可
caffe-yolo模型训练
参考原作者即可,这里省略略。。。
C++实现预测目标位置
yolo_predict项目可以建在和caffe同一层级下,在这个项目里同样用到了caffe中实现的层;
1.yolo_predict.cpp
// yolo_predict.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "yolo_predict.h"
#include <string>
#include <vector>
#include "head.h"
#include<windows.h>
#include "boost/algorithm/string.hpp"
#include "google/protobuf/text_format.h"
#include <opencv2/opencv.hpp>
#include "caffe/blob.hpp"
#include "caffe/layer.hpp"
#include "caffe/common.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/db.hpp"
#include "caffe/util/format.hpp"
#include "caffe/util/io.hpp"
#include <stdio.h>
#include <malloc.h>
#include <fstream>
#include <boost/progress.hpp>
#include <boost/math/special_functions/next.hpp>
#include <boost/random.hpp>
#include <limits>
#include "caffe/common.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/util/rng.hpp"
/*argv[1]网络的prototxt
argv[2]权重文件(如果编译时候使用的是loadweight,argv[2]对应的是txt文件夹,如果使用的是CopyTrainedLayersFromBinaryProto,argv[2]对应的是caffemodel)
argv[3]图片文件*/
//#include "caffe/util/math_functions.hpp"
using namespace caffe;
using caffe::Blob;
using caffe::Caffe;
using caffe::Datum;
using caffe::Net;
using caffe::Layer;
using std::string;
namespace db = caffe::db;
void loaddata(boost::shared_ptr<Net<float> >& net, std::string image_path);
void getbox(std::vector<float> result, float* pro_obj, int* idx_class,