Skip to content

Instantly share code, notes, and snippets.

@mndar
Created October 10, 2017 10:33
Show Gist options
  • Select an option

  • Save mndar/a93cfa77080a1a490c88556a86e6cc86 to your computer and use it in GitHub Desktop.

Select an option

Save mndar/a93cfa77080a1a490c88556a86e6cc86 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/cc/ops/image_ops.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/public/session.h"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace tensorflow;
using namespace tensorflow::ops;
using namespace cv;
string input_layer = "input:0";
string phase_train_layer = "phase_train:0";
string output_layer = "embeddings:0";
int main (int argc, char *argv[]) {
tensorflow::GraphDef graphDef;
tensorflow::ReadBinaryProto(tensorflow::Env::Default(), "/disks/storage/downloads/20170512-110547/20170512-110547.pb", &graphDef);
tensorflow::SessionOptions options;
std::unique_ptr<tensorflow::Session> session(tensorflow::NewSession(options));
tensorflow::Status sessionCreateStatus = session->Create(graphDef);
// allocate a Tensor
Tensor input_tensor(DT_FLOAT, TensorShape({1,160,160,3}));
// get pointer to memory for that Tensor
float *p = input_tensor.flat<float>().data();
// create a "fake" cv::Mat from it
cv::Mat cameraImg(160, 160, CV_32FC3, p);
// use it here as a destination
cv::Mat imagePixels = imread (argv[1]); // get data from your video pipeline
cout << "Image Pixels: " << imagePixels.empty() << " " << imagePixels.cols << "x" << imagePixels.rows << endl;
imagePixels.convertTo(cameraImg, CV_32FC3);
tensorflow::Tensor phase_tensor(tensorflow::DT_BOOL, tensorflow::TensorShape());
phase_tensor.scalar<bool>()() = false;
cout << phase_tensor.DebugString() << endl;
cout << input_tensor.DebugString() << endl;
std::vector<tensorflow::Tensor> outputs;
std::vector<std::pair<string, tensorflow::Tensor>> feed_dict = {
{input_layer, input_tensor},
{phase_train_layer, phase_tensor},
};
Status run_status = session->Run(feed_dict,
{output_layer}, {} , &outputs);
if (!run_status.ok()) {
LOG(ERROR) << "\tRunning model failed: " << run_status << "\n";
return -1;
}
cout << outputs[0].DebugString() << endl;
float *q = outputs[0].flat<float>().data();
for (int i = 0; i < 128; i++)
cout << q[i] << " ";
cout << endl;
cout << "TensorFlow End" << endl;
return 0;
}
@giddyyupp
Copy link

hi,
i got different vectors from yours and david's, although layers and models are the same. do you think any reasons for that. thanks for the gist.

https://github.com/davidsandberg/facenet/blob/master/contributed/export_embeddings.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment