Skip to content

Commit

Permalink
Init c++ example for simple net
Browse files Browse the repository at this point in the history
  • Loading branch information
skylook committed Sep 19, 2018
1 parent 7f43830 commit 42cbc88
Show file tree
Hide file tree
Showing 1,618 changed files with 319,335 additions and 0 deletions.
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.5)
project(tensorflow_cpp)

set(CMAKE_CXX_STANDARD 11)

find_package(OpenCV 3.0 QUIET)
if(NOT OpenCV_FOUND)
find_package(OpenCV 2.4.3 QUIET)
if(NOT OpenCV_FOUND)
message(FATAL_ERROR "OpenCV > 2.4.3 not found.")
endif()
endif()

set(TENSORFLOW_INCLUDES
/usr/local/include/tf/
/usr/local/include/tf/bazel-genfiles
/usr/local/include/tf/tensorflow/
/usr/local/include/tf/tensorflow/third-party)

set(TENSORFLOW_LIBS
/usr/local/lib/libtensorflow_cc.so
/usr/local/lib//libtensorflow_framework.so)


include_directories(
${TENSORFLOW_INCLUDES}
${PROJECT_SOURCE_DIR}/third_party/eigen3
)

add_executable(load_simple_net simple/load_simple_net.cpp)
target_link_libraries(load_simple_net
${TENSORFLOW_LIBS}
${OpenCV_LIBS}
)
93 changes: 93 additions & 0 deletions simple/load_simple_net.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"

using namespace tensorflow;

/**
* @brief simple model for click through rate prediction
* @details [long description]
*
* @param argv[1] graph protobuf
*
* @return [description]
*/
int main(int argc, char* argv[]) {
// Initialize a tensorflow session
Session* session;
Status status = NewSession(SessionOptions(), &session);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Session created successfully" << std::endl;
}

if (argc != 2)
{
std::cerr << std::endl << "Usage: ./project path_to_graph.pb" << std::endl;
return 1;
}

// Load the protobuf graph
GraphDef graph_def;
std::string graph_path = argv[1];
status = ReadBinaryProto(Env::Default(), graph_path, &graph_def);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Load graph protobuf successfully" << std::endl;
}

// Add the graph to the session
status = session->Create(graph_def);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Add graph to session successfully" << std::endl;
}

// Setup inputs and outputs:

// Our graph doesn't require any inputs, since it specifies default values,
// but we'll change an input to demonstrate.
Tensor a(DT_FLOAT, TensorShape());
a.scalar<float>()() = 2.0;

Tensor b(DT_FLOAT, TensorShape());
b.scalar<float>()() = 3.0;

std::vector<std::pair<string, tensorflow::Tensor>> inputs = {
{ "a:0", a },
{ "b:0", b },
};

// The session will initialize the outputs
std::vector<tensorflow::Tensor> outputs;

// Run the session, evaluating our "c" operation from the graph
status = session->Run(inputs, {"c:0"}, {}, &outputs);
if (!status.ok()) {
std::cerr << status.ToString() << std::endl;
return 1;
} else {
std::cout << "Run session successfully" << std::endl;
}

// Grab the first output (we only evaluated one graph node: "c")
// and convert the node to a scalar representation.
auto output_c = outputs[0].scalar<float>();

// (There are similar methods for vectors and matrices here:
// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/public/tensor.h)

// Print the results
std::cout << outputs[0].DebugString() << std::endl; // Tensor<type: float shape: [] values: 30>
std::cout << "output value: " << output_c() << std::endl; // 30

// Free any resources used by the session
session->Close();

return 0;
}
29 changes: 29 additions & 0 deletions simple/load_simple_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python

import tensorflow as tf
import numpy as np

import numpy as np
from tensorflow.python.platform import gfile

# Initialize a tensorflow session
with tf.Session() as sess:
# Load the protobuf graph
with gfile.FastGFile("model/simple.pb",'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# Add the graph to the session
tf.import_graph_def(graph_def, name='')

# Get graph
graph = tf.get_default_graph()

# Get tensor from graph
c = graph.get_tensor_by_name("c:0")

# Run the session, evaluating our "c" operation from the graph
res = sess.run(c, feed_dict={'a:0': 2.0, 'b:0': 3.0})

print("res = ", res)


Binary file added simple/log/.../events.out.tfevents.1537345394.slam
Binary file not shown.
Binary file added simple/log/.../events.out.tfevents.1537345428.slam
Binary file not shown.
17 changes: 17 additions & 0 deletions simple/simple_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python

import tensorflow as tf
import numpy as np

with tf.Session() as sess:

a=tf.placeholder(tf.float32,shape=None, name='a')
b=tf.placeholder(tf.float32,shape=None, name='b')
c = tf.multiply(a, b, name='c')

sess.run(tf.global_variables_initializer())

tf.train.write_graph(sess.graph_def, 'model/', 'simple.pb', as_text=False)

res = sess.run(c, feed_dict={'a:0': 2.0, 'b:0': 3.0})
print("res = ", res)
11 changes: 11 additions & 0 deletions third_party/eigen3/.hgeol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[patterns]
*.sh = LF
*.MINPACK = CRLF
scripts/*.in = LF
debug/msvc/*.dat = CRLF
debug/msvc/*.natvis = CRLF
unsupported/test/mpreal/*.* = CRLF
** = native

[repository]
native = LF
34 changes: 34 additions & 0 deletions third_party/eigen3/.hgignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
syntax: glob
qrc_*cxx
*.orig
*.pyc
*.diff
diff
*.save
save
*.old
*.gmo
*.qm
core
core.*
*.bak
*~
*build*
*.moc.*
*.moc
ui_*
CMakeCache.txt
tags
.*.swp
activity.png
*.out
*.php*
*.log
*.orig
*.rej
log
patch
a
a.*
lapack/testing
lapack/reference
34 changes: 34 additions & 0 deletions third_party/eigen3/.hgtags
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
2db9468678c6480c9633b6272ff0e3599d1e11a3 2.0-beta3
375224817dce669b6fa31d920d4c895a63fabf32 2.0-beta1
3b8120f077865e2a072e10f5be33e1d942b83a06 2.0-rc1
19dfc0e7666bcee26f7a49eb42f39a0280a3485e 2.0-beta5
7a7d8a9526f003ffa2430dfb0c2c535b5add3023 2.0-beta4
7d14ad088ac23769c349518762704f0257f6a39b 2.0.1
b9d48561579fd7d4c05b2aa42235dc9de6484bf2 2.0-beta6
e17630a40408243cb1a51ad0fe3a99beb75b7450 before-hg-migration
eda654d4cda2210ce80719addcf854773e6dec5a 2.0.0
ee9a7c468a9e73fab12f38f02bac24b07f29ed71 2.0-beta2
d49097c25d8049e730c254a2fed725a240ce4858 after-hg-migration
655348878731bcb5d9bbe0854077b052e75e5237 actual-start-from-scratch
12a658962d4e6dfdc9a1c350fe7b69e36e70675c 3.0-beta1
5c4180ad827b3f869b13b1d82f5a6ce617d6fcee 3.0-beta2
7ae24ca6f3891d5ac58ddc7db60ad413c8d6ec35 3.0-beta3
c40708b9088d622567fecc9208ad4a426621d364 3.0-beta4
b6456624eae74f49ae8683d8e7b2882a2ca0342a 3.0-rc1
a810d5dbab47acfe65b3350236efdd98f67d4d8a 3.1.0-alpha1
304c88ca3affc16dd0b008b1104873986edd77af 3.1.0-alpha2
920fc730b5930daae0a6dbe296d60ce2e3808215 3.1.0-beta1
8383e883ebcc6f14695ff0b5e20bb631abab43fb 3.1.0-rc1
bf4cb8c934fa3a79f45f1e629610f0225e93e493 3.1.0-rc2
da195914abcc1d739027cbee7c52077aab30b336 3.2-beta1
a8e0d153fc5e239ef8b06e3665f1f9e8cb8d49c8 before-evaluators
09a8e21866106b49c5dec1d6d543e5794e82efa0 3.3-alpha1
ce5a455b34c0a0ac3545a1497cb4a16c38ed90e8 3.3-beta1
69d418c0699907bcd0bf9e0b3ba0a112ed091d85 3.3-beta2
bef509908b9da05d0d07ffc0da105e2c8c6d3996 3.3-rc1
04ab5fa4b241754afcf631117572276444c67239 3.3-rc2
26667be4f70baf4f0d39e96f330714c87b399090 3.3.0
f562a193118d4f40514e2f4a0ace6e974926ef06 3.3.1
da9b4e14c2550e0d11078a3c39e6d56eba9905df 3.3.2
67e894c6cd8f5f1f604b27d37ed47fdf012674ff 3.3.3
5a0156e40feb7c4136680b493c6e433d91a6f355 3.3.4
Loading

0 comments on commit 42cbc88

Please sign in to comment.