Skip to content

Commit de082e1

Browse files
committed
tf.train.import_meta_graph passed.
1 parent 363b58b commit de082e1

8 files changed

Lines changed: 79 additions & 14 deletions

File tree

‎src/TensorFlowNET.Core/Framework/meta_graph.py.cs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static MetaGraphDef read_meta_graph_file(string filename)
1818
return meta_graph_def;
1919
}
2020

21-
public static (RefVariable[], string[]) import_scoped_meta_graph_with_return_elements(MetaGraphDef meta_graph_or_file,
21+
public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_scoped_meta_graph_with_return_elements(MetaGraphDef meta_graph_or_file,
2222
bool clear_devices = false,
2323
string import_scope = "",
2424
Dictionary<string, Tensor> input_map = null,
@@ -89,7 +89,7 @@ public static (RefVariable[], string[]) import_scoped_meta_graph_with_return_ele
8989
variable = new RefVariable(variable_def: proto, import_scope: scope_to_prepend_to_names);
9090
variable_objects[value] = variable;
9191
}
92-
92+
variable = variable_objects[value];
9393
graph.add_to_collection(col.Key, variable);
9494
}
9595
}
@@ -102,7 +102,12 @@ public static (RefVariable[], string[]) import_scoped_meta_graph_with_return_ele
102102
}
103103
}
104104

105-
return (null, null);
105+
var variables = graph.get_collection(ops.GraphKeys.GLOBAL_VARIABLES,
106+
scope: scope_to_prepend_to_names) as List<RefVariable>;
107+
var var_list = new Dictionary<string, RefVariable>();
108+
variables.ForEach(v => var_list[ops.strip_name_scope(v.name, scope_to_prepend_to_names)] = v);
109+
110+
return (var_list, imported_return_elements);
106111
}
107112

108113
/// <summary>

‎src/TensorFlowNET.Core/Graphs/Graph.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public partial class Graph : IDisposable
2626
public string _graph_key;
2727
public Status Status { get; }
2828

29+
/// <summary>
30+
/// True if the graph is considered "finalized". In that case no
31+
/// new operations can be added.
32+
/// </summary>
33+
private bool _finalized = false;
34+
2935
/// <summary>
3036
/// Arbitrary collections of objects.
3137
/// </summary>
@@ -126,6 +132,7 @@ private ITensorOrOperation _as_graph_element_locked(object obj, bool allow_tenso
126132

127133
public void add_to_collection<T>(string name, T value)
128134
{
135+
_check_not_finalized();
129136
if (_collections.ContainsKey(name))
130137
(_collections[name] as List<T>).Add(value);
131138
else
@@ -138,6 +145,12 @@ public void add_to_collections<T>(List<string> names, T value)
138145
add_to_collection(name, value);
139146
}
140147

148+
private void _check_not_finalized()
149+
{
150+
if (_finalized)
151+
throw new RuntimeError("Graph is finalized and cannot be modified.");
152+
}
153+
141154
public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes,
142155
TF_DataType[] input_types = null, string name = "",
143156
Dictionary<string, AttrValue> attrs = null, OpDef op_def = null)

‎src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
8585

8686
// Add a placeholder string tensor for the filename.
8787
var filename_tensor = array_ops.placeholder_with_default(string.IsNullOrEmpty(filename) ? "model" : filename, shape: new int[0], name: "filename");
88-
filename_tensor = gen_array_ops.placeholder_with_default(filename_tensor, shape: new int[0], name: "Const");
8988
// Keep the name "Const" for backwards compatibility.
89+
filename_tensor = gen_array_ops.placeholder_with_default(filename_tensor, shape: new int[0], name: "Const");
9090

9191
// Add the save ops.
9292
if (sharded)
@@ -106,10 +106,19 @@ public virtual SaverDef _build_internal(RefVariable[] names_to_saveables,
106106
var check_collection_list = graph.get_all_collection_keys();
107107
foreach (var collection_type in check_collection_list)
108108
{
109-
foreach (var element in graph.get_collection(collection_type) as IList<RefVariable>)
109+
var cols = graph.get_collection(collection_type);
110+
switch (cols)
110111
{
111-
112+
case List<RefVariable> values:
113+
foreach (var element in values) ;
114+
break;
115+
case List<ITensorOrOperation> values:
116+
foreach (var element in values) ;
117+
break;
118+
default:
119+
throw new NotImplementedException("_build_internal.check_collection_list");
112120
}
121+
113122
}
114123

115124
return new SaverDef()

‎src/TensorFlowNET.Core/Train/Saving/Saver.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public string save(Session sess,
193193
return _is_empty ? string.Empty : model_checkpoint_path;
194194
}
195195

196-
public Saver import_meta_graph(string meta_graph_or_file,
196+
public (Saver, object) import_meta_graph(string meta_graph_or_file,
197197
bool clear_devices = false,
198198
string import_scope = "")
199199
{

‎src/TensorFlowNET.Core/Train/Saving/saver.py.cs‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,48 @@ namespace Tensorflow
66
{
77
public class saver
88
{
9-
public static Saver _import_meta_graph_with_return_elements(string meta_graph_or_file,
9+
public static (Saver, object) _import_meta_graph_with_return_elements(string meta_graph_or_file,
1010
bool clear_devices = false,
1111
string import_scope = "",
1212
string[] return_elements = null)
1313
{
1414
var meta_graph_def = meta_graph.read_meta_graph_file(meta_graph_or_file);
1515

16-
meta_graph.import_scoped_meta_graph_with_return_elements(
16+
var imported_vars = meta_graph.import_scoped_meta_graph_with_return_elements(
1717
meta_graph_def,
1818
clear_devices: clear_devices,
1919
import_scope: import_scope,
2020
return_elements: return_elements);
2121

22-
return null;
23-
/*var (imported_vars, imported_return_elements) = (
24-
, false);*/
22+
var saver = _create_saver_from_imported_meta_graph(
23+
meta_graph_def, import_scope, imported_vars);
24+
25+
return (saver, null);
26+
}
27+
28+
public static Saver _create_saver_from_imported_meta_graph(MetaGraphDef meta_graph_def,
29+
string import_scope,
30+
(Dictionary<string, RefVariable>, ITensorOrOperation[]) imported_vars)
31+
{
32+
if(meta_graph_def.SaverDef != null)
33+
{
34+
throw new NotImplementedException("_create_saver_from_imported_meta_graph");
35+
}
36+
else
37+
{
38+
if(variables._all_saveable_objects(scope: import_scope).Length > 0)
39+
{
40+
// Return the default saver instance for all graph variables.
41+
return new Saver();
42+
}
43+
else
44+
{
45+
// If no graph variables exist, then a Saver cannot be constructed.
46+
Console.WriteLine("Saver not created because there are no variables in the" +
47+
" graph to restore");
48+
return null;
49+
}
50+
}
2551
}
2652
}
2753
}

‎src/TensorFlowNET.Core/Train/tf.optimizers.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Saver import_meta_graph(string meta_graph_or_file,
1919
bool clear_devices = false,
2020
string import_scope = "") => saver._import_meta_graph_with_return_elements(meta_graph_or_file,
2121
clear_devices,
22-
import_scope);
22+
import_scope).Item1;
2323
}
2424
}
2525
}

‎src/TensorFlowNET.Core/ops.GraphKeys.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class GraphKeys
2828
/// </summary>
2929
public static string GLOBAL_VARIABLES = "variables";
3030

31-
public static string[] _VARIABLE_COLLECTIONS = new string[] { "trainable_variables" };
31+
public static string[] _VARIABLE_COLLECTIONS = new string[] { "variables", "trainable_variables" };
3232
/// <summary>
3333
/// Key to collect BaseSaverBuilder.SaveableObject instances for checkpointing.
3434
/// </summary>

‎src/TensorFlowNET.Core/ops.py.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,17 @@ public static Tensor internal_convert_to_tensor(object value, TF_DataType dtype
440440
throw new NotImplementedException($"internal_convert_to_tensor: Can't convert {value.GetType().Name} to Tensor");
441441
}
442442
}
443+
444+
public static string strip_name_scope(string name, string export_scope = "")
445+
{
446+
if (!string.IsNullOrEmpty(export_scope))
447+
{
448+
throw new NotImplementedException("ops.strip_name_scope");
449+
}
450+
else
451+
{
452+
return name;
453+
}
454+
}
443455
}
444456
}

0 commit comments

Comments
 (0)