Skip to content

Commit b559b04

Browse files
committed
use Keras's default signature
1 parent b54351e commit b559b04

File tree

6 files changed

+8
-15
lines changed

6 files changed

+8
-15
lines changed

examples/mnist_savedmodel.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ use tensorflow::Tensor;
1212
use image::io::Reader as ImageReader;
1313
use image::GenericImageView;
1414

15-
#[cfg_attr(feature = "examples_system_alloc", global_allocator)]
16-
#[cfg(feature = "examples_system_alloc")]
17-
static ALLOCATOR: std::alloc::System = std::alloc::System;
18-
1915
fn main() -> Result<(), Box<dyn Error>> {
2016
let export_dir = "examples/mnist_savedmodel"; // y = w * x + b
2117
if !Path::new(export_dir).exists() {
@@ -43,7 +39,7 @@ fn main() -> Result<(), Box<dyn Error>> {
4339
let mut graph = Graph::new();
4440
let session =
4541
SavedModelBundle::load(&SessionOptions::new(), &["serve"], &mut graph, export_dir)?.session;
46-
let op_x = graph.operation_by_name_required("predict_x")?;
42+
let op_x = graph.operation_by_name_required("serving_default_sequential_input")?;
4743
let op_predict = graph.operation_by_name_required("StatefulPartitionedCall")?;
4844

4945
// Train the model (e.g. for fine tuning).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.9463042e-06, 1.12869834e-07, 0.00037566078, 0.00029264975, 2.2329447e-08, 1.9161714e-06, 1.8248756e-09, 0.999298, 2.3003201e-06, 2.6422202e-05
1+
4.0663176e-06, 1.4199884e-07, 9.556003e-05, 0.00065914105, 2.260991e-07, 4.076631e-06, 2.5459945e-09, 0.99904054, 1.5654963e-05, 0.00018059688

examples/mnist_savedmodel/mnist_savedmodel.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,20 @@
3131
model.fit(x_train, y_train, epochs=1)
3232

3333
# convert output type through softmax so that it can be interpreted as probability
34-
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
35-
36-
# convert keras model to TF2 function to get a computation graph
37-
x = tf.TensorSpec((None, 28, 28), tf.float32)
38-
predict = tf.function(lambda x: probability_model(x)).get_concrete_function(x=x)
34+
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax(name="output")])
3935

4036
# dump expected values to compare Rust's outputs
4137
with open("examples/mnist_savedmodel/expected_values.txt", "w") as f:
42-
values = predict(x_test[:1, :, :]).numpy()
38+
values = probability_model(x_test[:1, :, :])[0].numpy()
4339
print(*values, sep=", ", file=f)
4440

4541
directory = "examples/mnist_savedmodel"
46-
signatures = {"predict": predict}
47-
tf.saved_model.save(model, directory, signatures=signatures)
42+
tf.saved_model.save(probability_model, directory)
4843

4944
# export graph info to TensorBoard
5045
logdir = "logs/mnist_savedmodel"
5146
writer = tf.summary.create_file_writer(logdir)
47+
tf.summary.trace_on()
48+
values = probability_model(x_test[:1, :, :])
5249
with writer.as_default():
53-
tf.summary.graph(predict.graph)
50+
tf.summary.trace_export("Default", step=0)
27.7 KB
Binary file not shown.
Binary file not shown.
126 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)