Skip to content

Commit c19a1df

Browse files
committed
Merge pull request #1232 from dkurt:update_halide_tests
2 parents 1a018c2 + 8c03c63 commit c19a1df

File tree

6 files changed

+160
-171
lines changed

6 files changed

+160
-171
lines changed

modules/dnn/perf/perf_halide_net.cpp

Lines changed: 96 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ namespace cvtest
1212
using namespace cv;
1313
using namespace dnn;
1414

15-
static void loadNet(const std::string& weights, const std::string& proto,
16-
const std::string& scheduler, int inWidth, int inHeight,
17-
const std::string& outputLayer, const std::string& framework,
18-
int targetId, Net* net, int* outputLayerId)
15+
static void loadNet(std::string weights, std::string proto, std::string scheduler,
16+
int inWidth, int inHeight, const std::string& outputLayer,
17+
const std::string& framework, int targetId, Net* net)
1918
{
2019
Mat input(inHeight, inWidth, CV_32FC3);
2120
randu(input, 0.0f, 1.0f);
2221

22+
weights = findDataFile(weights, false);
23+
if (!proto.empty())
24+
proto = findDataFile(proto, false);
25+
if (!scheduler.empty())
26+
scheduler = findDataFile(scheduler, false);
2327
if (framework == "caffe")
2428
{
2529
*net = cv::dnn::readNetFromCaffe(proto, weights);
@@ -35,106 +39,116 @@ static void loadNet(const std::string& weights, const std::string& proto,
3539
else
3640
CV_Error(Error::StsNotImplemented, "Unknown framework " + framework);
3741

38-
net->setBlob("", cv::dnn::blobFromImage(input, 1.0, false));
42+
net->setInput(blobFromImage(input, 1.0, false));
3943
net->setPreferableBackend(DNN_BACKEND_HALIDE);
40-
net->compileHalide(scheduler);
41-
*outputLayerId = net->getLayerId(outputLayer);
42-
net->forward(*outputLayerId);
44+
net->setHalideScheduler(scheduler);
45+
net->forward(outputLayer);
4346
}
4447

4548
PERF_TEST(GoogLeNet, HalidePerfTest)
4649
{
47-
Net net;
48-
int outputLayerId;
49-
loadNet(findDataFile("dnn/bvlc_googlenet.caffemodel"),
50-
findDataFile("dnn/bvlc_googlenet.prototxt"),
51-
"", 227, 227, "prob", "caffe", DNN_TARGET_CPU, &net, &outputLayerId);
52-
53-
TEST_CYCLE_N(10)
54-
{
55-
net.forward(outputLayerId);
50+
try {
51+
Net net;
52+
loadNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
53+
"", 227, 227, "prob", "caffe", DNN_TARGET_CPU, &net);
54+
55+
TEST_CYCLE_N(10)
56+
{
57+
net.forward();
58+
}
59+
SANITY_CHECK_NOTHING();
60+
} catch (SkipTestException& e) {
61+
throw PerfSkipTestException();
5662
}
57-
SANITY_CHECK_NOTHING();
5863
}
5964

6065
PERF_TEST(AlexNet, HalidePerfTest)
6166
{
62-
Net net;
63-
int outputLayerId;
64-
loadNet(findDataFile("dnn/bvlc_alexnet.caffemodel"),
65-
findDataFile("dnn/bvlc_alexnet.prototxt"),
66-
findDataFile("dnn/halide_scheduler_alexnet.yml"),
67-
227, 227, "prob", "caffe", DNN_TARGET_CPU, &net, &outputLayerId);
68-
69-
TEST_CYCLE_N(10)
70-
{
71-
net.forward(outputLayerId);
67+
try {
68+
Net net;
69+
loadNet("dnn/bvlc_alexnet.caffemodel", "dnn/bvlc_alexnet.prototxt",
70+
"dnn/halide_scheduler_alexnet.yml", 227, 227, "prob", "caffe",
71+
DNN_TARGET_CPU, &net);
72+
73+
TEST_CYCLE_N(10)
74+
{
75+
net.forward();
76+
}
77+
SANITY_CHECK_NOTHING();
78+
} catch (SkipTestException& e) {
79+
throw PerfSkipTestException();
7280
}
73-
SANITY_CHECK_NOTHING();
7481
}
7582

76-
// PERF_TEST(ResNet50, HalidePerfTest)
77-
// {
78-
// Net net;
79-
// int outputLayerId;
80-
// loadNet(findDataFile("dnn/ResNet-50-model.caffemodel"),
81-
// findDataFile("dnn/ResNet-50-deploy.prototxt"),
82-
// findDataFile("dnn/halide_scheduler_resnet_50.yml"),
83-
// 224, 224, "prob", "caffe", DNN_TARGET_CPU, &net, &outputLayerId);
84-
//
85-
// TEST_CYCLE_N(10)
86-
// {
87-
// net.forward(outputLayerId);
88-
// }
89-
// SANITY_CHECK_NOTHING();
90-
// }
91-
92-
// PERF_TEST(SqueezeNet_v1_1, HalidePerfTest)
93-
// {
94-
// Net net;
95-
// int outputLayerId;
96-
// loadNet(findDataFile("dnn/squeezenet_v1_1.caffemodel"),
97-
// findDataFile("dnn/squeezenet_v1_1.prototxt"),
98-
// findDataFile("dnn/halide_scheduler_squeezenet_v1_1.yml"),
99-
// 227, 227, "prob", "caffe", DNN_TARGET_CPU, &net, &outputLayerId);
100-
//
101-
// TEST_CYCLE_N(10)
102-
// {
103-
// net.forward(outputLayerId);
104-
// }
105-
// SANITY_CHECK_NOTHING();
106-
// }
83+
PERF_TEST(ResNet50, HalidePerfTest)
84+
{
85+
try {
86+
Net net;
87+
loadNet("dnn/ResNet-50-model.caffemodel", "dnn/ResNet-50-deploy.prototxt",
88+
"dnn/halide_scheduler_resnet_50.yml", 224, 224, "prob", "caffe",
89+
DNN_TARGET_CPU, &net);
90+
91+
TEST_CYCLE_N(10)
92+
{
93+
net.forward();
94+
}
95+
SANITY_CHECK_NOTHING();
96+
} catch (SkipTestException& e) {
97+
throw PerfSkipTestException();
98+
}
99+
}
100+
101+
PERF_TEST(SqueezeNet_v1_1, HalidePerfTest)
102+
{
103+
try {
104+
Net net;
105+
loadNet("dnn/squeezenet_v1_1.caffemodel", "dnn/squeezenet_v1_1.prototxt",
106+
"dnn/halide_scheduler_squeezenet_v1_1.yml", 227, 227, "prob",
107+
"caffe", DNN_TARGET_CPU, &net);
108+
109+
TEST_CYCLE_N(10)
110+
{
111+
net.forward();
112+
}
113+
SANITY_CHECK_NOTHING();
114+
} catch (SkipTestException& e) {
115+
throw PerfSkipTestException();
116+
}
117+
}
107118

108119
PERF_TEST(Inception_5h, HalidePerfTest)
109120
{
110-
Net net;
111-
int outputLayerId;
112-
loadNet(findDataFile("dnn/tensorflow_inception_graph.pb"), "",
113-
findDataFile("dnn/halide_scheduler_inception_5h.yml"),
114-
224, 224, "softmax2", "tensorflow", DNN_TARGET_CPU,
115-
&net, &outputLayerId);
116-
117-
TEST_CYCLE_N(10)
118-
{
119-
net.forward(outputLayerId);
121+
try {
122+
Net net;
123+
loadNet("dnn/tensorflow_inception_graph.pb", "",
124+
"dnn/halide_scheduler_inception_5h.yml",
125+
224, 224, "softmax2", "tensorflow", DNN_TARGET_CPU, &net);
126+
127+
TEST_CYCLE_N(10)
128+
{
129+
net.forward("softmax2");
130+
}
131+
SANITY_CHECK_NOTHING();
132+
} catch (SkipTestException& e) {
133+
throw PerfSkipTestException();
120134
}
121-
SANITY_CHECK_NOTHING();
122135
}
123136

124137
PERF_TEST(ENet, HalidePerfTest)
125138
{
126-
Net net;
127-
int outputLayerId;
128-
loadNet(findDataFile("dnn/Enet-model-best.net"), "",
129-
findDataFile("dnn/halide_scheduler_enet.yml"),
130-
512, 256, "l367_Deconvolution", "torch", DNN_TARGET_CPU,
131-
&net, &outputLayerId);
132-
133-
TEST_CYCLE_N(10)
134-
{
135-
net.forward(outputLayerId);
139+
try {
140+
Net net;
141+
loadNet("dnn/Enet-model-best.net", "", "dnn/halide_scheduler_enet.yml",
142+
512, 256, "l367_Deconvolution", "torch", DNN_TARGET_CPU, &net);
143+
144+
TEST_CYCLE_N(10)
145+
{
146+
net.forward("l367_Deconvolution");
147+
}
148+
SANITY_CHECK_NOTHING();
149+
} catch (SkipTestException& e) {
150+
throw PerfSkipTestException();
136151
}
137-
SANITY_CHECK_NOTHING();
138152
}
139153
#endif // HAVE_HALIDE
140154

modules/dnn/samples/squeezenet_halide.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,22 @@ int main(int argc, char **argv)
9393
//! [Prepare blob]
9494

9595
//! [Set input blob]
96-
net.setInput(inputBlob); // Set the network input.
96+
net.setInput(inputBlob); // Set the network input.
9797
//! [Set input blob]
9898

9999
//! [Enable Halide backend]
100100
net.setPreferableBackend(DNN_BACKEND_HALIDE); // Tell engine to use Halide where it possible.
101101
//! [Enable Halide backend]
102102

103-
//! [Compile Halide pipeline]
104-
// net.compileHalide(); // Compile Halide pipeline.
105-
//! [Compile Halide pipeline]
106-
107103
//! [Make forward pass]
108-
Mat prob = net.forward("prob"); // Compute output.
104+
Mat prob = net.forward("prob"); // Compute output.
109105
//! [Make forward pass]
110106

111-
//! [Gather output]
112-
// net.getBlob(); // Gather output of "prob" layer.
113-
107+
//! [Determine the best class]
114108
int classId;
115109
double classProb;
116110
getMaxClass(prob, &classId, &classProb); // Find the best class.
117-
//! [Gather output]
111+
//! [Determine the best class]
118112

119113
//! [Print results]
120114
std::vector<std::string> classNames = readClassNames();

modules/dnn/test/test_halide_layers.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ static void test(LayerParams& params, Mat& input)
2424
int lid = net.addLayer(params.name, params.type, params);
2525
net.connect(0, 0, lid, 0);
2626

27-
net.setBlob("", input);
28-
net.allocate();
29-
net.forward();
30-
Mat outputDefault = net.getBlob(params.name).clone();
27+
net.setInput(input);
28+
Mat outputDefault = net.forward(params.name).clone();
3129

3230
net.setPreferableBackend(DNN_BACKEND_HALIDE);
33-
net.forward();
34-
Mat outputHalide = net.getBlob(params.name).clone();
31+
Mat outputHalide = net.forward(params.name).clone();
3532
normAssert(outputDefault, outputHalide);
3633
}
3734

@@ -346,14 +343,12 @@ TEST(MaxPoolUnpool_Halide, Accuracy)
346343

347344
Mat input({1, 1, 4, 4}, CV_32F);
348345
randu(input, -1.0f, 1.0f);
349-
net.setBlob("", input);
350-
net.forward();
351-
Mat outputDefault = net.getBlob("testUnpool").clone();
346+
net.setInput(input);
347+
Mat outputDefault = net.forward("testUnpool").clone();
352348

353349
net.setPreferableBackend(DNN_BACKEND_HALIDE);
354-
net.setBlob("", input);
355-
net.forward();
356-
Mat outputHalide = net.getBlob("testUnpool").clone();
350+
net.setInput(input);
351+
Mat outputHalide = net.forward("testUnpool").clone();
357352
normAssert(outputDefault, outputHalide);
358353
}
359354

@@ -381,14 +376,12 @@ void testInPlaceActivation(LayerParams& lp)
381376

382377
Mat input({1, kNumChannels, 10, 10}, CV_32F);
383378
randu(input, -1.0f, 1.0f);
384-
net.setBlob("", input);
385-
net.forward();
386-
Mat outputDefault = net.getBlob(lp.name).clone();
379+
net.setInput(input);
380+
Mat outputDefault = net.forward(lp.name).clone();
387381

388-
net.setBlob("", input);
382+
net.setInput(input);
389383
net.setPreferableBackend(DNN_BACKEND_HALIDE);
390-
net.forward();
391-
Mat outputHalide = net.getBlob(lp.name).clone();
384+
Mat outputHalide = net.forward(lp.name).clone();
392385
normAssert(outputDefault, outputHalide);
393386
}
394387

@@ -555,13 +548,11 @@ TEST_P(Concat, Accuracy)
555548
Mat input({1, inSize[0], inSize[1], inSize[2]}, CV_32F);
556549
randu(input, -1.0f, 1.0f);
557550

558-
net.setBlob("", input);
559-
net.forward();
560-
Mat outputDefault = net.getBlob(concatParam.name).clone();
551+
net.setInput(input);
552+
Mat outputDefault = net.forward(concatParam.name).clone();
561553

562554
net.setPreferableBackend(DNN_BACKEND_HALIDE);
563-
net.forward();
564-
Mat outputHalide = net.getBlob(concatParam.name).clone();
555+
Mat outputHalide = net.forward(concatParam.name).clone();
565556
normAssert(outputDefault, outputHalide);
566557
}
567558

@@ -617,13 +608,11 @@ TEST_P(Eltwise, Accuracy)
617608
Mat input({1, inSize[0], inSize[1], inSize[2]}, CV_32F);
618609
randu(input, -1.0f, 1.0f);
619610

620-
net.setBlob("", input);
621-
net.forward();
622-
Mat outputDefault = net.getBlob(eltwiseParam.name).clone();
611+
net.setInput(input);
612+
Mat outputDefault = net.forward(eltwiseParam.name).clone();
623613

624614
net.setPreferableBackend(DNN_BACKEND_HALIDE);
625-
net.forward();
626-
Mat outputHalide = net.getBlob(eltwiseParam.name).clone();
615+
Mat outputHalide = net.forward(eltwiseParam.name).clone();
627616
normAssert(outputDefault, outputHalide);
628617
}
629618

0 commit comments

Comments
 (0)