-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.cpp
171 lines (147 loc) · 5.06 KB
/
utils.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "utils.h"
using namespace std;
using namespace cv;
using namespace cv::datasets;
using namespace caffe;
void trainNet()
{
boost::shared_ptr <Net <float>> net;
NetParameter caffenetParam, goturnParam;
SolverParameter solverParam;
ReadSolverParamsFromTextFileOrDie("goturnSolver.prototxt", &solverParam);
ReadNetParamsFromBinaryFileOrDie("bvlc_reference_caffenet.caffemodel", &caffenetParam);
boost::shared_ptr<caffe::Solver<float>> solver(caffe::SolverRegistry<float>::CreateSolver(solverParam));
//Replace CONV layers weights with Caffenet values
int numSourceLayers = caffenetParam.layer_size();
net = solver->net();
for (int i = 0; i < numSourceLayers; i++)
{
const LayerParameter& sourceLayer = caffenetParam.layer(i);
boost::shared_ptr <Layer<float>> targetLayer[2];
if (sourceLayer.name() == "conv1")
{
targetLayer[0] = net->layer_by_name("conv11");
targetLayer[1] = net->layer_by_name("conv21");
}
else if (sourceLayer.name() == "conv2")
{
targetLayer[0] = net->layer_by_name("conv12");
targetLayer[1] = net->layer_by_name("conv22");
}
else if (sourceLayer.name() == "conv3")
{
targetLayer[0] = net->layer_by_name("conv13");
targetLayer[1] = net->layer_by_name("conv23");
}
else if (sourceLayer.name() == "conv4")
{
targetLayer[0] = net->layer_by_name("conv14");
targetLayer[1] = net->layer_by_name("conv24");
}
else if (sourceLayer.name() == "conv5")
{
targetLayer[0] = net->layer_by_name("conv15");
targetLayer[1] = net->layer_by_name("conv25");
}
else
continue;
if (sourceLayer.blobs_size() != 2) continue;
if (targetLayer[0]->blobs().size() != 2 ||
targetLayer[1]->blobs().size() != 2)
{
cout << "Target Blobs are not Conv";
getchar();
break;
}
for (int j = 0; j < targetLayer[0]->blobs().size(); ++j) {
if (!targetLayer[0]->blobs()[j]->ShapeEquals(sourceLayer.blobs(j))) {
Blob<float > source_blob;
const bool kReshape = true;
source_blob.FromProto(sourceLayer.blobs(j), kReshape);
}
const bool kReshape = false;
targetLayer[0]->blobs()[j]->FromProto(sourceLayer.blobs(j), kReshape);
}
/*for (int j = 0; j < sourceLayer.blobs_size(); j++)
{
targetLayer[0]->blobs()[j]->FromProto(sourceLayer.blobs(j));
targetLayer[1]->blobs()[j]->FromProto(sourceLayer.blobs(j));
}*/
//cout << "Replaced by: " << sourceLayer.name() << endl;
//cout << "Replaced by: " << sourceLayer.name() << endl;
}
solver->Solve();
}
void testNet(string modelPath)
{
Caffe::set_mode(Caffe::GPU);
Net<float> net("goturnDeploy.prototxt", TEST);
net.CopyTrainedLayersFrom(modelPath);
boost::shared_ptr<Blob <float>> data1Layer;
boost::shared_ptr<Blob <float>> data2Layer;
boost::shared_ptr<Blob <float>> labelLayer;
boost::shared_ptr<Blob <float>> outputLayer;
outputLayer = net.blob_by_name("out");
data1Layer = net.blob_by_name("data1");
data2Layer = net.blob_by_name("data2");
labelLayer = net.blob_by_name("label");
net.Forward();
float *out = outputLayer->mutable_cpu_data();
float *data1 = data1Layer->mutable_cpu_data();
float *data2 = data2Layer->mutable_cpu_data();
float *label = labelLayer->mutable_cpu_data();
for (int k = 0; k < 100; k++)
{
//Print GTBB
cout << label[k * 4 + 0] << " " << label[k * 4 + 1] << " " << label[k * 4 + 2] << " " << label[k * 4 + 3] << endl;
//Print GOTURN_BB
cout << out[k * 4 + 0] << " " << out[k * 4 + 1] << " " << out[k * 4 + 2] << " " << out[k * 4 + 3] << endl;
//Make GTBB and GOTURN_BB
Rect2f gtbb, res_bb;
gtbb.x = label[k * 4 + 0];
gtbb.y = label[k * 4 + 1];
gtbb.width = label[k * 4 + 2] - label[k * 4 + 0];
gtbb.height = label[k * 4 + 3] - label[k * 4 + 1];
res_bb.x = out[k * 4 + 0];
res_bb.y = out[k * 4 + 1];
res_bb.width = out[k * 4 + 2] - out[k * 4 + 0];
res_bb.height = out[k * 4 + 3] - out[k * 4 + 1];
//Construct Target/Search patches from data1/data2
vector <Mat> channelsTargetPatch;
vector <Mat> channelsSearchPatch;
Mat targetPatch;
Mat searchPatch;
for (int i = 0; i < 3; i++)
{
Mat channelTarget(227, 227, CV_32FC1, data1 + i * 227 * 227 + k * 3 * 227 * 227);
Mat channelSearch(227, 227, CV_32FC1, data2 + i * 227 * 227 + k * 3 * 227 * 227);
channelsTargetPatch.push_back(channelTarget);
channelsSearchPatch.push_back(channelSearch);
}
//RGB -> BGR and Merge
//reverse(channelsTargetPatch.begin(), channelsTargetPatch.end());
//reverse(channelsSearchPatch.begin(), channelsSearchPatch.end());
merge(channelsTargetPatch, targetPatch);
merge(channelsSearchPatch, searchPatch);
//Add mean
targetPatch = targetPatch + 128;
searchPatch = searchPatch + 128;
targetPatch.convertTo(targetPatch, CV_8U);
searchPatch.convertTo(searchPatch, CV_8U);
//Draw GT/GOTURN bounding boxes and show patches
rectangle(searchPatch, gtbb, Scalar(0, 255, 0));
rectangle(searchPatch, res_bb, Scalar(0, 0, 255));
imshow("Target", targetPatch);
imshow("Search", searchPatch);
waitKey();
}
}
void buildDB()
{
//Generate training datasets
for (int i = 1; i <= 10; i++)
{
string fileName = "D:/ALOV300++/trainDataset_" + to_string(i) + ".h5";
buildH5Datasets(fileName, 500);
}
}