-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Halide backend support for deep learning layers
- Loading branch information
Showing
26 changed files
with
2,810 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
// | ||
// Copyright (C) 2017, Intel Corporation, all rights reserved. | ||
// Third party copyrights are property of their respective owners. | ||
|
||
namespace cvtest | ||
{ | ||
|
||
#ifdef HAVE_HALIDE | ||
using namespace cv; | ||
using namespace dnn; | ||
|
||
static void loadNet(const std::string& weights, const std::string& proto, | ||
const std::string& scheduler, int inWidth, int inHeight, | ||
const std::string& outputLayer, const std::string& framework, | ||
Net* net, int* outputLayerId) | ||
{ | ||
Mat input(inHeight, inWidth, CV_32FC3); | ||
randu(input, 0.0f, 1.0f); | ||
|
||
if (framework == "caffe") | ||
{ | ||
*net = cv::dnn::readNetFromCaffe(proto, weights); | ||
} | ||
else if (framework == "torch") | ||
{ | ||
*net = cv::dnn::readNetFromTorch(weights); | ||
} | ||
else if (framework == "tensorflow") | ||
{ | ||
*net = cv::dnn::readNetFromTensorflow(weights); | ||
} | ||
else | ||
CV_Error(Error::StsNotImplemented, "Unknown framework " + framework); | ||
|
||
net->setBlob("", cv::dnn::blobFromImage(input, 1.0, false)); | ||
net->setPreferableBackend(HALIDE); | ||
net->compileHalide(HALIDE_CPU, scheduler); | ||
*outputLayerId = net->getLayerId(outputLayer); | ||
net->forward(*outputLayerId); | ||
} | ||
|
||
PERF_TEST(GoogLeNet, HalidePerfTest) | ||
{ | ||
Net net; | ||
int outputLayerId; | ||
loadNet(findDataFile("dnn/bvlc_googlenet.caffemodel"), | ||
findDataFile("dnn/bvlc_googlenet.prototxt"), | ||
"", 227, 227, "prob", "caffe", &net, &outputLayerId); | ||
|
||
TEST_CYCLE_N(10) | ||
{ | ||
net.forward(outputLayerId); | ||
} | ||
SANITY_CHECK_NOTHING(); | ||
} | ||
|
||
PERF_TEST(AlexNet, HalidePerfTest) | ||
{ | ||
Net net; | ||
int outputLayerId; | ||
loadNet(findDataFile("dnn/bvlc_alexnet.caffemodel"), | ||
findDataFile("dnn/bvlc_alexnet.prototxt"), | ||
findDataFile("dnn/halide_scheduler_alexnet.yml"), | ||
227, 227, "prob", "caffe", &net, &outputLayerId); | ||
|
||
TEST_CYCLE_N(10) | ||
{ | ||
net.forward(outputLayerId); | ||
} | ||
SANITY_CHECK_NOTHING(); | ||
} | ||
|
||
// PERF_TEST(ResNet50, HalidePerfTest) | ||
// { | ||
// Net net; | ||
// int outputLayerId; | ||
// loadNet(findDataFile("dnn/ResNet-50-model.caffemodel"), | ||
// findDataFile("dnn/ResNet-50-deploy.prototxt"), | ||
// findDataFile("dnn/halide_scheduler_resnet_50.yml"), | ||
// 224, 224, "prob", "caffe", &net, &outputLayerId); | ||
// | ||
// TEST_CYCLE_N(10) | ||
// { | ||
// net.forward(outputLayerId); | ||
// } | ||
// SANITY_CHECK_NOTHING(); | ||
// } | ||
|
||
// PERF_TEST(SqueezeNet_v1_1, HalidePerfTest) | ||
// { | ||
// Net net; | ||
// int outputLayerId; | ||
// loadNet(findDataFile("dnn/squeezenet_v1_1.caffemodel"), | ||
// findDataFile("dnn/squeezenet_v1_1.prototxt"), | ||
// findDataFile("dnn/halide_scheduler_squeezenet_v1_1.yml"), | ||
// 227, 227, "prob", "caffe", &net, &outputLayerId); | ||
// | ||
// TEST_CYCLE_N(10) | ||
// { | ||
// net.forward(outputLayerId); | ||
// } | ||
// SANITY_CHECK_NOTHING(); | ||
// } | ||
|
||
PERF_TEST(Inception_5h, HalidePerfTest) | ||
{ | ||
Net net; | ||
int outputLayerId; | ||
loadNet(findDataFile("dnn/tensorflow_inception_graph.pb"), "", | ||
findDataFile("dnn/halide_scheduler_inception_5h.yml"), | ||
224, 224, "softmax2", "tensorflow", &net, &outputLayerId); | ||
|
||
TEST_CYCLE_N(10) | ||
{ | ||
net.forward(outputLayerId); | ||
} | ||
SANITY_CHECK_NOTHING(); | ||
} | ||
|
||
PERF_TEST(ENet, HalidePerfTest) | ||
{ | ||
Net net; | ||
int outputLayerId; | ||
loadNet(findDataFile("dnn/Enet-model-best.net"), "", | ||
findDataFile("dnn/halide_scheduler_enet.yml"), | ||
512, 256, "l367_Deconvolution", "torch", &net, &outputLayerId); | ||
|
||
TEST_CYCLE_N(10) | ||
{ | ||
net.forward(outputLayerId); | ||
} | ||
SANITY_CHECK_NOTHING(); | ||
} | ||
#endif // HAVE_HALIDE | ||
|
||
} // namespace cvtest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
#include "perf_precomp.hpp" | ||
|
||
CV_PERF_TEST_MAIN(dnn) | ||
static const char* extraTestDataPath = | ||
#ifdef WINRT | ||
NULL; | ||
#else | ||
getenv("OPENCV_DNN_TEST_DATA_PATH"); | ||
#endif | ||
|
||
CV_PERF_TEST_MAIN(dnn, | ||
extraTestDataPath ? (void)cvtest::addDataSearchPath(extraTestDataPath) : (void)0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// This file is part of OpenCV project. | ||
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
// | ||
// Copyright (C) 2017, Intel Corporation, all rights reserved. | ||
// Third party copyrights are property of their respective owners. | ||
|
||
// Sample of using Halide backend in OpenCV deep learning module. | ||
// Based on dnn/samples/caffe_googlenet.cpp. | ||
|
||
#include <opencv2/dnn.hpp> | ||
#include <opencv2/imgproc.hpp> | ||
#include <opencv2/highgui.hpp> | ||
using namespace cv; | ||
using namespace cv::dnn; | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
#include <cstdlib> | ||
|
||
/* Find best class for the blob (i. e. class with maximal probability) */ | ||
void getMaxClass(const Mat &probBlob, int *classId, double *classProb) | ||
{ | ||
Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix | ||
Point classNumber; | ||
|
||
minMaxLoc(probMat, NULL, classProb, NULL, &classNumber); | ||
*classId = classNumber.x; | ||
} | ||
|
||
std::vector<std::string> readClassNames(const char *filename = "synset_words.txt") | ||
{ | ||
std::vector<std::string> classNames; | ||
|
||
std::ifstream fp(filename); | ||
if (!fp.is_open()) | ||
{ | ||
std::cerr << "File with classes labels not found: " << filename << std::endl; | ||
exit(-1); | ||
} | ||
|
||
std::string name; | ||
while (!fp.eof()) | ||
{ | ||
std::getline(fp, name); | ||
if (name.length()) | ||
classNames.push_back( name.substr(name.find(' ')+1) ); | ||
} | ||
|
||
fp.close(); | ||
return classNames; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
initModule(); // Required if OpenCV is built as static libs. | ||
|
||
std::string modelTxt = "train_val.prototxt"; | ||
std::string modelBin = "squeezenet_v1.1.caffemodel"; | ||
std::string scheduler = "halide_scheduler_squeezenet_v1_1.yml"; | ||
std::string imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg"; | ||
|
||
//! [Read and initialize network] | ||
Net net = dnn::readNetFromCaffe(modelTxt, modelBin); | ||
//! [Read and initialize network] | ||
|
||
//! [Check that network was read successfully] | ||
if (net.empty()) | ||
{ | ||
std::cerr << "Can't load network by using the following files: " << std::endl; | ||
std::cerr << "prototxt: " << modelTxt << std::endl; | ||
std::cerr << "caffemodel: " << modelBin << std::endl; | ||
std::cerr << "SqueezeNet v1.1 can be downloaded from:" << std::endl; | ||
std::cerr << "https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1" << std::endl; | ||
exit(-1); | ||
} | ||
//! [Check that network was read successfully] | ||
|
||
//! [Prepare blob] | ||
Mat img = imread(imageFile); | ||
if (img.empty()) | ||
{ | ||
std::cerr << "Can't read image from the file: " << imageFile << std::endl; | ||
exit(-1); | ||
} | ||
if (img.channels() != 3) | ||
{ | ||
std::cerr << "Image " << imageFile << " isn't 3-channel" << std::endl; | ||
exit(-1); | ||
} | ||
|
||
resize(img, img, Size(227, 227)); // SqueezeNet v1.1 predict class by 3x227x227 input image. | ||
Mat inputBlob = blobFromImage(img, 1.0, false); // Convert Mat to 4-dimensional batch. | ||
//! [Prepare blob] | ||
|
||
//! [Set input blob] | ||
net.setBlob("", inputBlob); // Set the network input. | ||
//! [Set input blob] | ||
|
||
//! [Enable Halide backend] | ||
net.setPreferableBackend(HALIDE); // Tell engine to use Halide where it possible. | ||
//! [Enable Halide backend] | ||
|
||
//! [Compile Halide pipeline] | ||
net.compileHalide(); // Compile Halide pipeline. | ||
//! [Compile Halide pipeline] | ||
|
||
//! [Make forward pass] | ||
net.forward(); // Compute output. | ||
//! [Make forward pass] | ||
|
||
//! [Gather output] | ||
Mat prob = net.getBlob("prob"); // Gather output of "prob" layer. | ||
|
||
int classId; | ||
double classProb; | ||
getMaxClass(prob, &classId, &classProb); // Find the best class. | ||
//! [Gather output] | ||
|
||
//! [Print results] | ||
std::vector<std::string> classNames = readClassNames(); | ||
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl; | ||
std::cout << "Probability: " << classProb * 100 << "%" << std::endl; | ||
//! [Print results] | ||
|
||
return 0; | ||
} //main |
Oops, something went wrong.