-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (140 loc) · 5.51 KB
/
Copy pathmain.cpp
File metadata and controls
158 lines (140 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include<string.h>
#include <caffe/caffe.hpp>
#include <string>
#include <vector>
#include <algorithm>
using namespace cv;
using namespace std;
using namespace caffe;
class Detector{//检测类
public:
Detector(const string& model_file,
const string& weights_file );
Mat Predict(const cv::Mat& img);//分类成员函数
private:
void WrapInputLayer(std::vector<cv::Mat>* input_channels);//私有函数,在各个检测成员函数中调用,用来输入图像
void caffePreprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels);//用来准备输入图像
private:
shared_ptr<caffe::Net <float> > net_;//网络指针
cv::Size input_geometry_;//输入图像size
int num_channels_;//输入通道
cv::Mat mean_;//均值图像
};
Detector::Detector(const string& model_file,
const string& weights_file ){
Caffe::set_mode(Caffe::GPU);
net_.reset(new caffe::Net<float>(model_file, TEST));
net_ ->CopyTrainedLayersFrom(weights_file);
}//构造函数,设定GPU模式,载入网络权重
void Detector::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
caffe::Blob<float>* input_layer = net_->input_blobs()[0];
int width = input_layer->width();
int height = input_layer->height();
float* input_data = input_layer->mutable_cpu_data();
for (int i = 0; i < input_layer->channels(); ++i) {
cv::Mat channel(height, width, CV_32FC1, input_data);
input_channels->push_back(channel);
input_data += width * height;
}
}//用来输入图像进入网络
void Detector::caffePreprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels)
{
//cv::Mat sample_resized=img;
cv::Mat sample_float;
img.convertTo(sample_float, CV_32FC3);
cv::split(sample_float,* input_channels);
}
Mat getMean(const size_t& imageHeight, const size_t& imageWidth)
{
Mat mean;
const int meanValues[3] = {104, 117, 123};
vector<Mat> meanChannels;
for(size_t i = 0; i < 3; i++)
{
Mat channel(imageHeight, imageWidth, CV_32F, Scalar(meanValues[i]));
meanChannels.push_back(channel);
}
cv::merge(meanChannels, mean);
return mean;
}
Mat fcnpreprocess(const Mat& crop)
{
Mat fcnpreprocessed;
crop.convertTo(fcnpreprocessed, CV_32FC3);
resize(fcnpreprocessed, fcnpreprocessed, Size(256, 256));
Mat mean = getMean(256, 256);
cv::subtract(fcnpreprocessed, mean, fcnpreprocessed);
return fcnpreprocessed;
}
/**********************************************************/
/**************************Predict**************************/
Mat Detector::Predict(const cv::Mat& img) {
caffe::Blob<float>* input_layer = net_->input_blobs()[0];//拿输入层数据
input_layer->Reshape(1, 3,256, 256);//reshape层blob格式
/* Forward dimension change to all layers. */
net_->Reshape();//reshape
std::vector<cv::Mat> input_channels;
WrapInputLayer(&input_channels);
caffePreprocess(img, &input_channels);//准备输入数据
net_->Forward();//前向
caffe::Blob<float>* output_layer = net_->output_blobs()[0];
float* results = output_layer->mutable_cpu_data();
Mat src =Mat::zeros(cv::Size(256,256),CV_32FC1);
//caffe::Datum;
int totalnum=net_->num_outputs();
ofstream ofresult( "result.txt ",ios::app);
for(int j=0;j<256;j++){
float* data =src.ptr<float>(j);
for(int i=0;i<256;i++){
float sum[totalnum];
for(int num=0;num<=totalnum;num++ )
{
float pixel =results[i+j*256+num*256*256];
sum[num]=pixel;
}
float summax=0;
int maxflag=0;
for(int max=0;max<=totalnum;max++)
{
if(sum[max]>summax)
{
summax=sum[max];
maxflag=max;
}
}
ofresult<<sum[totalnum-1]<<" "<<sum[totalnum]<<" "<<maxflag<<endl;
/**********************************************************************************/
/****************类别多可以根据自己标注情况,更改此处,若类别少,默认如下*******************/
/**********************************************************************************/
if(maxflag==0)
data[i]=0;
else
data[i]=255-maxflag;
/**********************************************************************************/
/**********************************************************************************/
}
}
return src;
}
int main(int argc, char *argv[])
{
static const string fcnType = "fcn32s";
String fcnmodelTxt = fcnType + "-heavy-pascal.prototxt";
String fcnmodelBin = fcnType + "-heavy-pascal.caffemodel";
Detector fcndetector(fcnmodelTxt,fcnmodelBin);
Mat frame=imread("test.jpg");
resize(frame,frame,Size(256,256));
clock_t doublestart,doubleend;
doublestart = clock();
Mat seg=fcndetector.Predict(frame);
doubleend = clock();
double dur = (double)(doubleend - doublestart);
cout<<" fcn use time:"<<dur/CLOCKS_PER_SEC<<endl;
imwrite("fcn result.jpg",seg);
return 0;
}