-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from zkmkarlsruhe/master
Frozen graph support
- Loading branch information
Showing
5 changed files
with
129 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(example) | ||
|
||
find_library(TENSORFLOW_LIB tensorflow HINT $ENV{HOME}/libtensorflow2/lib) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer") | ||
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan") | ||
|
||
add_executable(example main.cpp) | ||
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include) | ||
target_link_libraries (example "${TENSORFLOW_LIB}") |
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,22 @@ | ||
import tensorflow as tf | ||
from tensorflow.python.framework.convert_to_constants import ( | ||
convert_variables_to_constants_v2, | ||
) | ||
|
||
input = tf.keras.Input(shape=(5,)) | ||
output = tf.keras.layers.Dense(5, activation=tf.nn.relu)(input) | ||
output = tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)(output) | ||
model = tf.keras.Model(inputs=input, outputs=output) | ||
|
||
# Create frozen graph | ||
x = tf.TensorSpec(model.input_shape, tf.float32, name="x") | ||
concrete_function = tf.function(lambda x: model(x)).get_concrete_function(x) | ||
frozen_model = convert_variables_to_constants_v2(concrete_function) | ||
|
||
# Check input/output node name | ||
print(f"{frozen_model.inputs=}") | ||
print(f"{frozen_model.outputs=}") | ||
|
||
# Save the graph as protobuf format | ||
directory = "." | ||
tf.io.write_graph(frozen_model.graph, directory, "model.pb", as_text=False) |
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,22 @@ | ||
#include <iostream> | ||
|
||
#include "cppflow/ops.h" | ||
#include "cppflow/model.h" | ||
|
||
|
||
int main() { | ||
|
||
auto input = cppflow::fill({10, 5}, 1.0f); | ||
std::cout << "start" << std::endl; | ||
cppflow::model model("../model.pb", cppflow::model::FROZEN_GRAPH); | ||
auto output = model({{"x:0", input}}, {{"Identity:0"}})[0]; | ||
|
||
std::cout << output << std::endl; | ||
|
||
auto values = output.get_data<float>(); | ||
|
||
for (auto v : values) { | ||
std::cout << v << std::endl; | ||
} | ||
return 0; | ||
} |
Binary file not shown.
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