Created
December 18, 2018 10:44
-
-
Save berak/b239d84edc667c9b0039a688b7fa1978 to your computer and use it in GitHub Desktop.
test10.cpp
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
// Sample of using Halide backend in OpenCV deep learning module. | |
// Based on caffe_googlenet.cpp. | |
#include <opencv2/dnn.hpp> | |
#include <opencv2/imgproc.hpp> | |
#include <opencv2/highgui.hpp> | |
using namespace cv; | |
using namespace cv::dnn; | |
using namespace std; | |
#include <fstream> | |
#include <iostream> | |
#include <cstdlib> | |
int main(int argc, char **argv) | |
{ | |
std::string model = "test10.pb"; | |
std::string imageFile = (argc > 1) ? argv[1] : "1.bmp"; | |
//! [Read and initialize network] | |
Net net = dnn::readNetFromTensorflow(model); | |
//! [Read and initialize network] | |
//! [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(256, 256)); | |
Mat inputBlob = blobFromImage(img, 1.0, Size(), Scalar(), false); // Convert Mat to 4-dimensional batch. | |
//! [Prepare blob] | |
//! [Set input blob] | |
net.setInput(inputBlob); // Set the network input. | |
//! [Set input blob] | |
//! [Make forward pass] | |
Mat prob = net.forward("dense_2"); // Compute output. | |
//! [Make forward pass] | |
// debug code to print out layers | |
MatShape ms1 { inputBlob.size[0], inputBlob.size[1] , inputBlob.size[2], inputBlob.size[3] }; | |
size_t nlayers = net.getLayerNames().size() + 1; // one off for the hidden input layer | |
for (size_t i=0; i<nlayers; i++) { | |
Ptr<Layer> lyr = net.getLayer((unsigned)i); | |
std::vector<MatShape> in,out; | |
net.getLayerShapes(ms1,i,in,out); | |
int id = net.getLayerId(lyr->name); | |
cout << format("%-3d%-38s %-13s ", id, (i==0?"data":lyr->name.c_str()), (i==0?"Input":lyr->type.c_str())); | |
for (auto j:in) cout << "i" << Mat(j).t() << " "; // input(s) size | |
for (auto j:out) cout << "o" << Mat(j).t() << " "; // output(s) size | |
for (auto b:lyr->blobs) { // what the net trains on, e.g. weights and bias | |
cout << "b[" << b.size[0]; | |
for (size_t d=1; d<b.dims; d++) cout << ", " << b.size[d]; | |
cout << "] "; | |
} | |
if (id>=0) { | |
cout << "ci "; | |
std::vector<Ptr<Layer> > inp = net.getLayerInputs(id); | |
for (auto k:inp) cout << net.getLayerId(k->name) << " "; | |
} | |
cout << endl; | |
} | |
return 0; | |
} //main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment