Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

Commit 55a41e1

Browse files
committed
Improve SavedModelBundle support
- Give explicit name to an operation in regression_savedmodel script - Add test_resources with a saved model - Add test for saved model bundle - Fix typo - Wipe try! macros
1 parent 43f6bf9 commit 55a41e1

5 files changed

Lines changed: 49 additions & 15 deletions

File tree

examples/regression_savedmodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
w = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='w')
1212
b = tf.Variable(tf.zeros([1]), name='b')
13-
y_hat = w * x + b
13+
y_hat = tf.add(w * x, b, name="y_hat")
1414

1515
loss = tf.reduce_mean(tf.square(y_hat - y))
1616
optimizer = tf.train.GradientDescentOptimizer(0.5)

src/session.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::TensorType;
2222
pub struct SavedModelBundle {
2323
/// The loaded session.
2424
pub session: Session,
25-
/// A meta graph defition as raw protocol buffer.
25+
/// A meta graph definition as raw protocol buffer.
2626
pub meta_graph_def: Vec<u8>,
2727
}
2828

@@ -38,15 +38,15 @@ impl SavedModelBundle {
3838
let mut status = Status::new();
3939

4040
let export_dir_cstr =
41-
try!(export_dir.as_ref()
41+
export_dir.as_ref()
4242
.to_str()
4343
.and_then(|s| CString::new(s.as_bytes()).ok())
44-
.ok_or_else(|| invalid_arg!("Invalid export directory path")));
44+
.ok_or_else(|| invalid_arg!("Invalid export directory path"))?;
4545

46-
let tags_cstr: Vec<_> = try!(tags.into_iter()
47-
.map(|t| CString::new(t.as_ref()))
48-
.collect::<::std::result::Result<_, _>>()
49-
.map_err(|_| invalid_arg!("Invalid tag name")));
46+
let tags_cstr: Vec<_> = tags.into_iter()
47+
.map(|t| CString::new(t.as_ref()))
48+
.collect::<::std::result::Result<_, _>>()
49+
.map_err(|_| invalid_arg!("Invalid tag name"))?;
5050
let tags_ptr: Vec<*const c_char> = tags_cstr.iter().map(|t| t.as_ptr()).collect();
5151

5252
// The empty TF_Buffer will be filled by LoadSessionFromSavedModel
@@ -102,16 +102,15 @@ impl Session {
102102
-> Result<Self> {
103103
let mut status = Status::new();
104104

105-
let export_dir_cstr =
106-
try!(export_dir.as_ref()
105+
let export_dir_cstr = export_dir.as_ref()
107106
.to_str()
108107
.and_then(|s| CString::new(s.as_bytes()).ok())
109-
.ok_or_else(|| invalid_arg!("Invalid export directory path")));
108+
.ok_or_else(|| invalid_arg!("Invalid export directory path"))?;
110109

111-
let tags_cstr: Vec<_> = try!(tags.into_iter()
112-
.map(|t| CString::new(t.as_ref()))
113-
.collect::<::std::result::Result<_, _>>()
114-
.map_err(|_| invalid_arg!("Invalid tag name")));
110+
let tags_cstr: Vec<_> = tags.into_iter()
111+
.map(|t| CString::new(t.as_ref()))
112+
.collect::<::std::result::Result<_, _>>()
113+
.map_err(|_| invalid_arg!("Invalid tag name"))?;
115114
// keeping tags_cstr to retain strings in memory
116115
let tags_ptr: Vec<*const c_char> = tags_cstr.iter().map(|t| t.as_ptr()).collect();
117116

@@ -385,4 +384,39 @@ mod tests {
385384
assert_eq!(output_tensor[0], 4.0);
386385
assert_eq!(output_tensor[1], 6.0);
387386
}
387+
388+
#[test]
389+
fn test_savedmodelbundle() {
390+
let mut graph = Graph::new();
391+
let bundle = SavedModelBundle::load(
392+
&SessionOptions::new(),
393+
&["train", "serve"],
394+
&mut graph,
395+
"test_resources/regression-model",
396+
).unwrap();
397+
398+
let x_op = graph.operation_by_name_required("x").unwrap();
399+
let y_op = graph.operation_by_name_required("y").unwrap();
400+
let y_hat_op = graph.operation_by_name_required("y_hat").unwrap();
401+
let _train_op = graph.operation_by_name_required("train").unwrap();
402+
403+
let SavedModelBundle {
404+
mut session,
405+
meta_graph_def,
406+
} = bundle;
407+
408+
assert!(!meta_graph_def.is_empty());
409+
410+
let mut x = <Tensor<f32>>::new(&[1]);
411+
x[0] = 2.0;
412+
let mut y = <Tensor<f32>>::new(&[1]);
413+
y[0] = 4.0;
414+
let mut step = StepWithGraph::new();
415+
step.add_input(&x_op, 0, &x);
416+
step.add_input(&y_op, 0, &y);
417+
let output_token = step.request_output(&y_hat_op, 0);
418+
session.run(&mut step).unwrap();
419+
let output_tensor = step.take_output::<f32>(output_token).unwrap();
420+
assert_eq!(output_tensor.len(), 1);
421+
}
388422
}
17.6 KB
Binary file not shown.
Binary file not shown.
142 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)