|
1 | | -using Tensorflow.Util; |
| 1 | +namespace Tensorflow.Checkpoint; |
2 | 2 |
|
3 | | -namespace Tensorflow.Checkpoint |
| 3 | +public class CheckpointReader |
4 | 4 | { |
5 | | - sealed class SafeCheckpointReaderHandle : SafeTensorflowHandle |
6 | | - { |
7 | | - public SafeCheckpointReaderHandle(): base() |
8 | | - { |
9 | | - |
10 | | - } |
11 | | - public SafeCheckpointReaderHandle(IntPtr handle): base(handle) |
12 | | - { |
| 5 | + private SafeCheckpointReaderHandle _handle; |
| 6 | + public Dictionary<string, TF_DataType> VariableToDataTypeMap { get; set; } |
| 7 | + public Dictionary<string, Shape> VariableToShapeMap { get; set; } |
13 | 8 |
|
14 | | - } |
15 | | - |
16 | | - protected override bool ReleaseHandle() |
17 | | - { |
18 | | - c_api.TF_DeleteCheckpointReader(handle); |
19 | | - SetHandle(IntPtr.Zero); |
20 | | - return true; |
21 | | - } |
22 | | - } |
23 | | - public class CheckpointReader |
| 9 | + public CheckpointReader(string filename) |
24 | 10 | { |
25 | | - private SafeCheckpointReaderHandle _handle; |
26 | | - public Dictionary<string, TF_DataType> VariableToDataTypeMap { get; set; } |
27 | | - public Dictionary<string, Shape> VariableToShapeMap { get; set; } |
28 | | - |
29 | | - public CheckpointReader(string filename) |
30 | | - { |
31 | | - Status status = new Status(); |
32 | | - _handle = c_api.TF_NewCheckpointReader(filename, status.Handle); |
33 | | - status.Check(true); |
34 | | - ReadAllShapeAndType(); |
35 | | - } |
| 11 | + Status status = new Status(); |
| 12 | + VariableToDataTypeMap = new Dictionary<string, TF_DataType>(); |
| 13 | + VariableToShapeMap = new Dictionary<string, Shape>(); |
| 14 | + _handle = c_api.TF_NewCheckpointReader(filename, status.Handle); |
| 15 | + status.Check(true); |
| 16 | + ReadAllShapeAndType(); |
| 17 | + } |
36 | 18 |
|
37 | | - public int HasTensor(string name) |
38 | | - { |
39 | | - return c_api.TF_CheckpointReaderHasTensor(_handle, name); |
40 | | - } |
| 19 | + public int HasTensor(string name) |
| 20 | + => c_api.TF_CheckpointReaderHasTensor(_handle, name); |
41 | 21 |
|
42 | | - /// <summary> |
43 | | - /// Get the variable name. |
44 | | - /// </summary> |
45 | | - /// <param name="index"></param> |
46 | | - /// <returns></returns> |
47 | | - public string GetVariable(int index) |
48 | | - { |
49 | | - return c_api.StringPiece(c_api.TF_CheckpointReaderGetVariable(_handle, index)); |
50 | | - } |
| 22 | + /// <summary> |
| 23 | + /// Get the variable name. |
| 24 | + /// </summary> |
| 25 | + /// <param name="index"></param> |
| 26 | + /// <returns></returns> |
| 27 | + public string GetVariable(int index) |
| 28 | + => c_api.StringPiece(c_api.TF_CheckpointReaderGetVariable(_handle, index)); |
51 | 29 |
|
52 | | - public int Size() |
53 | | - { |
54 | | - return c_api.TF_CheckpointReaderSize(_handle); |
55 | | - } |
| 30 | + public int Size() |
| 31 | + => c_api.TF_CheckpointReaderSize(_handle); |
56 | 32 |
|
57 | | - public TF_DataType GetVariableDataType(string name) |
58 | | - { |
59 | | - return c_api.TF_CheckpointReaderGetVariableDataType(_handle, name); |
60 | | - } |
| 33 | + public TF_DataType GetVariableDataType(string name) |
| 34 | + => c_api.TF_CheckpointReaderGetVariableDataType(_handle, name); |
61 | 35 |
|
62 | | - public Shape GetVariableShape(string name) |
63 | | - { |
64 | | - int num_dims = GetVariableNumDims(name); |
65 | | - long[] dims = new long[num_dims]; |
66 | | - Status status = new Status(); |
67 | | - c_api.TF_CheckpointReaderGetVariableShape(_handle, name, dims, num_dims, status.Handle); |
68 | | - status.Check(true); |
69 | | - return new Shape(dims); |
70 | | - } |
| 36 | + public Shape GetVariableShape(string name) |
| 37 | + { |
| 38 | + int num_dims = GetVariableNumDims(name); |
| 39 | + long[] dims = new long[num_dims]; |
| 40 | + Status status = new Status(); |
| 41 | + c_api.TF_CheckpointReaderGetVariableShape(_handle, name, dims, num_dims, status.Handle); |
| 42 | + status.Check(true); |
| 43 | + return new Shape(dims); |
| 44 | + } |
71 | 45 |
|
72 | | - public int GetVariableNumDims(string name) |
73 | | - { |
74 | | - return c_api.TF_CheckpointReaderGetVariableNumDims(_handle, name); |
75 | | - } |
| 46 | + public int GetVariableNumDims(string name) |
| 47 | + => c_api.TF_CheckpointReaderGetVariableNumDims(_handle, name); |
76 | 48 |
|
77 | | - public unsafe Tensor GetTensor(string name, TF_DataType dtype = TF_DataType.DtInvalid) |
78 | | - { |
79 | | - Status status = new Status(); |
80 | | - var tensor = c_api.TF_CheckpointReaderGetTensor(_handle, name, status.Handle); |
81 | | - status.Check(true); |
82 | | - return new Tensor(tensor); |
83 | | - } |
| 49 | + public unsafe Tensor GetTensor(string name, TF_DataType dtype = TF_DataType.DtInvalid) |
| 50 | + { |
| 51 | + Status status = new Status(); |
| 52 | + var tensor = c_api.TF_CheckpointReaderGetTensor(_handle, name, status.Handle); |
| 53 | + status.Check(true); |
| 54 | + return new Tensor(tensor); |
| 55 | + } |
84 | 56 |
|
85 | | - private void ReadAllShapeAndType() |
| 57 | + private void ReadAllShapeAndType() |
| 58 | + { |
| 59 | + int size = Size(); |
| 60 | + for(int i = 0; i < size; i++) |
86 | 61 | { |
87 | | - VariableToDataTypeMap = new Dictionary<string, TF_DataType>(); |
88 | | - VariableToShapeMap = new Dictionary<string, Shape>(); |
89 | | - int size = Size(); |
90 | | - for(int i = 0; i < size; i++) |
91 | | - { |
92 | | - var name = GetVariable(i); |
93 | | - var shape = GetVariableShape(name); |
94 | | - var dtype = GetVariableDataType(name); |
95 | | - VariableToDataTypeMap[name] = dtype; |
96 | | - VariableToShapeMap[name] = shape; |
97 | | - } |
| 62 | + var name = GetVariable(i); |
| 63 | + var shape = GetVariableShape(name); |
| 64 | + var dtype = GetVariableDataType(name); |
| 65 | + VariableToDataTypeMap[name] = dtype; |
| 66 | + VariableToShapeMap[name] = shape; |
98 | 67 | } |
99 | 68 | } |
100 | 69 | } |
0 commit comments