-
Notifications
You must be signed in to change notification settings - Fork 429
Load from saved model support #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1e11f53
Load from saved model support
Enet4 4014131
Apply rustfmt over previous changes.
Enet4 bd6c354
Add example for loading a saved model
Enet4 91d2a39
Refactor code
Enet4 e0f4612
Merge branch 'master' into export-saved-model
adamcrume File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use tf; | ||
use libc::c_int; | ||
use libc::{c_char, c_int}; | ||
use std::ffi::CString; | ||
use std::marker; | ||
use std::path::Path; | ||
use std::ptr; | ||
use std::result::Result as StdResult; | ||
use super::Code; | ||
use super::DataType; | ||
use super::Graph; | ||
|
@@ -32,6 +35,43 @@ impl Session { | |
} | ||
} | ||
|
||
/// Loads a session from an exported model. | ||
pub fn from_saved_model<P: AsRef<Path>, Tag: AsRef<str>, Tags: IntoIterator<Item=Tag>>( | ||
options: &SessionOptions, tags: Tags, graph: &mut Graph, export_dir: P) -> Result<Self> { | ||
let mut status = Status::new(); | ||
|
||
let export_dir_cstr = try!(export_dir.as_ref().to_str() | ||
.and_then(|s| CString::new(s.as_bytes()).ok()) | ||
.ok_or_else(|| Status::new_set( | ||
Code::InvalidArgument, "Invalid export directory path").unwrap())); | ||
|
||
let tags_cstr: Vec<_> = try!(tags.into_iter() | ||
.map(|t| CString::new(t.as_ref())) | ||
.collect::<StdResult<_,_>>() | ||
.map_err(|_| Status::new_set(Code::InvalidArgument, "Invalid tag name").unwrap())); | ||
// keeping tags_cstr to retain strings in memory | ||
let tags_ptr: Vec<*const c_char> = tags_cstr.iter().map(|t| t.as_ptr()).collect(); | ||
|
||
let inner = unsafe { | ||
tf::TF_LoadSessionFromSavedModel( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting looks a bit off here, can you run it through rustfmt please? |
||
options.inner, | ||
ptr::null(), | ||
export_dir_cstr.to_bytes_with_nul().as_ptr() as *const c_char, | ||
tags_ptr.as_ptr(), | ||
tags_ptr.len() as c_int, | ||
graph.inner(), | ||
ptr::null_mut(), | ||
status.inner()) | ||
}; | ||
if inner.is_null() { | ||
Err(status) | ||
} else { | ||
Ok(Session { | ||
inner: inner, | ||
}) | ||
} | ||
} | ||
|
||
/// Closes the session. | ||
pub fn close(&mut self) -> Result<()> { | ||
let mut status = Status::new(); | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm I find this confusing, but if @adamcrume is good with it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I just needed a hint on line 56 to pluck the result out, and this approach sounded elegant to me. We could consider something else though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other places we're naming
std::result::Result
directly without importing it, like so:I'd prefer that, just for consistency. On a side note, I regret adding that type alias. I assumed it was an accepted pattern because of
std::fmt::Result
,std::io::Result
, andstd::thread::Result
, but having multiple types with the same name (just in different modules) causes no end of headaches.