Skip to content

Commit a172845

Browse files
dskkatobytosaur
authored andcommitted
Add frozen graph example
1 parent 4a291f4 commit a172845

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
project(example)
3+
4+
find_library(TENSORFLOW_LIB tensorflow HINT $ENV{HOME}/libtensorflow2/lib)
5+
6+
set(CMAKE_CXX_STANDARD 17)
7+
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer")
9+
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan")
10+
11+
add_executable(example main.cpp)
12+
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include)
13+
target_link_libraries (example "${TENSORFLOW_LIB}")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import tensorflow as tf
2+
from tensorflow.python.framework.convert_to_constants import (
3+
convert_variables_to_constants_v2,
4+
)
5+
6+
input = tf.keras.Input(shape=(5,))
7+
output = tf.keras.layers.Dense(5, activation=tf.nn.relu)(input)
8+
output = tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)(output)
9+
model = tf.keras.Model(inputs=input, outputs=output)
10+
11+
# Create frozen graph
12+
x = tf.TensorSpec(model.input_shape, tf.float32, name="x")
13+
concrete_function = tf.function(lambda x: model(x)).get_concrete_function(x)
14+
frozen_model = convert_variables_to_constants_v2(concrete_function)
15+
16+
# Check input/output node name
17+
print(f"{frozen_model.inputs=}")
18+
print(f"{frozen_model.outputs=}")
19+
20+
# Save the graph as protobuf format
21+
directory = "."
22+
tf.io.write_graph(frozen_model.graph, directory, "model.pb", as_text=False)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
3+
#include "cppflow/ops.h"
4+
#include "cppflow/model.h"
5+
6+
7+
int main() {
8+
9+
auto input = cppflow::fill({10, 5}, 1.0f);
10+
std::cout << "start" << std::endl;
11+
cppflow::model model("../model.pb", {});
12+
auto output = model({{"x:0", input}}, {{"Identity:0"}})[0];
13+
14+
std::cout << output << std::endl;
15+
16+
auto values = output.get_data<float>();
17+
18+
for (auto v : values) {
19+
std::cout << v << std::endl;
20+
}
21+
return 0;
22+
}
1.77 KB
Binary file not shown.

0 commit comments

Comments
 (0)