-
Notifications
You must be signed in to change notification settings - Fork 538
Support loading of SavedModel format #989
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 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62309fc
Add CheckpointReader and corresponding C APIs.
AsakusaRinne 5df6e5d
Add essential components of SavedModel format loading.
AsakusaRinne 8f7a594
Add checkpoint reading for SavedModel format loading.
AsakusaRinne 01e88bb
Revise customized json converters.
AsakusaRinne 3972989
Add support for loading models from python.
AsakusaRinne 94751b1
Fix the duplicated weights in Keras.Model.
AsakusaRinne d8a1640
Add alexnet loading test and check for loaded weights.
AsakusaRinne 3f46862
Fix ci error caused by branch merge.
AsakusaRinne 0060039
Resolve the comments and errors.
AsakusaRinne 1875d41
Fix the stucking of training when loading model.
AsakusaRinne 3dafbf0
Fix the stucking of training when loading model.
AsakusaRinne 18f0fed
merge
Oceania2018 32e61de
fix intptr.
Oceania2018 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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace Tensorflow.Checkpoint | ||
{ | ||
public class CheckpointReader : IDisposable | ||
{ | ||
private IntPtr _reader; | ||
public Dictionary<string, TF_DataType> VariableToDataTypeMap { get; set; } | ||
public Dictionary<string, Shape> VariableToShapeMap { get; set; } | ||
|
||
public CheckpointReader(string filename) | ||
{ | ||
Status status = new Status(); | ||
_reader = c_api.TF_NewCheckpointReader(filename, status.Handle); | ||
status.Check(true); | ||
ReadAllShapeAndType(); | ||
} | ||
|
||
public int HasTensor(string name) | ||
{ | ||
return c_api.TF_CheckpointReaderHasTensor(_reader, name); | ||
} | ||
|
||
/// <summary> | ||
/// Get the variable name. | ||
/// </summary> | ||
/// <param name="index"></param> | ||
/// <returns></returns> | ||
public string GetVariable(int index) | ||
{ | ||
return c_api.TF_CheckpointReaderGetVariable(_reader, index); | ||
} | ||
|
||
public int Size() | ||
{ | ||
return c_api.TF_CheckpointReaderSize(_reader); | ||
} | ||
|
||
public TF_DataType GetVariableDataType(string name) | ||
{ | ||
return c_api.TF_CheckpointReaderGetVariableDataType(_reader, name); | ||
} | ||
|
||
public Shape GetVariableShape(string name) | ||
{ | ||
// TODO(Rinne): Change it to a constant. | ||
int num_dims = GetVariableNumDims(name); | ||
long[] dims = new long[num_dims]; | ||
Status status = new Status(); | ||
c_api.TF_CheckpointReaderGetVariableShape(_reader, name, dims, num_dims, status.Handle); | ||
status.Check(true); | ||
return new Shape(dims); | ||
} | ||
|
||
public int GetVariableNumDims(string name) | ||
{ | ||
return c_api.TF_CheckpointReaderGetVariableNumDims(_reader, name); | ||
} | ||
|
||
public unsafe Tensor GetTensor(string name, TF_DataType dtype = TF_DataType.DtInvalid) | ||
{ | ||
Status status = new Status(); | ||
var tensor = c_api.TF_CheckpointReaderGetTensor(_reader, name, status.Handle); | ||
status.Check(true); | ||
var shape = GetVariableShape(name); | ||
if(dtype == TF_DataType.DtInvalid) | ||
{ | ||
dtype = GetVariableDataType(name); | ||
} | ||
return new Tensor(tensor); | ||
} | ||
|
||
private void ReadAllShapeAndType() | ||
{ | ||
VariableToDataTypeMap = new Dictionary<string, TF_DataType>(); | ||
VariableToShapeMap = new Dictionary<string, Shape>(); | ||
int size = Size(); | ||
for(int i = 0; i < size; i++) | ||
{ | ||
var name = GetVariable(i); | ||
var shape = GetVariableShape(name); | ||
var dtype = GetVariableDataType(name); | ||
VariableToDataTypeMap[name] = dtype; | ||
VariableToShapeMap[name] = shape; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
c_api.TF_DeleteCheckpointReader(_reader); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Tensorflow | ||
{ | ||
public unsafe partial class c_api | ||
{ | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern IntPtr TF_NewCheckpointReader(string filename, SafeStatusHandle status); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern void TF_DeleteCheckpointReader(IntPtr reader); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern int TF_CheckpointReaderHasTensor(IntPtr reader, string name); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern string TF_CheckpointReaderGetVariable(IntPtr reader, int index); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern int TF_CheckpointReaderSize(IntPtr reader); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern TF_DataType TF_CheckpointReaderGetVariableDataType(IntPtr reader, string name); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern void TF_CheckpointReaderGetVariableShape(IntPtr reader, string name, long[] dims, int num_dims, SafeStatusHandle status); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern int TF_CheckpointReaderGetVariableNumDims(IntPtr reader, string name); | ||
[DllImport(TensorFlowLibName)] | ||
internal static extern SafeTensorHandle TF_CheckpointReaderGetTensor(IntPtr reader, string name, SafeStatusHandle status); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.