|
1 |
| -using Google.Protobuf; |
2 |
| -using System; |
3 |
| -using System.Collections.Generic; |
4 |
| -using System.IO; |
5 |
| -using System.Text; |
6 |
| -using Tensorflow.Keras.Engine; |
| 1 | +using System.IO; |
7 | 2 | using Tensorflow.Train;
|
8 | 3 | using ThirdParty.Tensorflow.Python.Keras.Protobuf;
|
9 |
| -using static Tensorflow.Binding; |
10 |
| -using static Tensorflow.KerasApi; |
11 | 4 |
|
12 |
| -namespace Tensorflow.Keras.Saving.SavedModel |
| 5 | +namespace Tensorflow.Keras.Saving.SavedModel; |
| 6 | + |
| 7 | +public class KerasLoadModelUtils |
13 | 8 | {
|
14 |
| - public class KerasLoadModelUtils |
| 9 | + /// <summary> |
| 10 | + /// Corresponding to keras/saving/save.py/load_model |
| 11 | + /// </summary> |
| 12 | + /// <param name="filepath"></param> |
| 13 | + /// <param name="custom_objects"></param> |
| 14 | + /// <param name="compile"></param> |
| 15 | + /// <param name="options"></param> |
| 16 | + /// <returns></returns> |
| 17 | + public static Trackable load_model(string filepath, IDictionary<string, object>? custom_objects = null, |
| 18 | + bool compile = true, LoadOptions? options = null) |
15 | 19 | {
|
16 |
| - /// <summary> |
17 |
| - /// Corresponding to keras/saving/save.py/load_model |
18 |
| - /// </summary> |
19 |
| - /// <param name="filepath"></param> |
20 |
| - /// <param name="custom_objects"></param> |
21 |
| - /// <param name="compile"></param> |
22 |
| - /// <param name="options"></param> |
23 |
| - /// <returns></returns> |
24 |
| - public static Trackable load_model(string filepath, IDictionary<string, object>? custom_objects = null, |
25 |
| - bool compile = true, LoadOptions? options = null) |
| 20 | + using var savingScope = SharedObjectSavingScope.Enter(); |
| 21 | + |
| 22 | + using var ctx = LoadContext.load_context(options); |
| 23 | + |
| 24 | + if (!File.Exists(filepath) && !Directory.Exists(filepath)) |
26 | 25 | {
|
27 |
| - using (SharedObjectSavingScope.Enter()) |
28 |
| - { |
29 |
| - using (LoadContext.load_context(options)) |
30 |
| - { |
31 |
| - if (!File.Exists(filepath) && !Directory.Exists(filepath)) |
32 |
| - { |
33 |
| - throw new IOException($"No file or directory found at {filepath}."); |
34 |
| - } |
35 |
| - if (Directory.Exists(filepath)) |
36 |
| - { |
37 |
| - return load(filepath, compile, options); |
38 |
| - } |
39 |
| - else |
40 |
| - { |
41 |
| - throw new NotImplementedException("Model load of h5 format has not been supported. Please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues if it's needed."); |
42 |
| - } |
43 |
| - } |
44 |
| - } |
| 26 | + throw new IOException($"No file or directory found at {filepath}."); |
45 | 27 | }
|
46 | 28 |
|
47 |
| - private static Trackable load(string path, bool compile = true, LoadOptions? options = null) |
| 29 | + if (Directory.Exists(filepath)) |
| 30 | + { |
| 31 | + return load(filepath, compile, options); |
| 32 | + } |
| 33 | + else |
48 | 34 | {
|
49 |
| - SavedMetadata metadata = new SavedMetadata(); |
50 |
| - var meta_graph_def = Loader.parse_saved_model(path).MetaGraphs[0]; |
51 |
| - var object_graph_def = meta_graph_def.ObjectGraphDef; |
52 |
| - string path_to_metadata_pb = Path.Combine(path, Constants.SAVED_METADATA_PATH); |
53 |
| - if (File.Exists(path_to_metadata_pb)) |
54 |
| - { |
55 |
| - metadata.MergeFrom(new FileStream(path_to_metadata_pb, FileMode.Open, FileAccess.Read)); |
56 |
| - } |
57 |
| - else |
58 |
| - { |
59 |
| - throw new NotImplementedException("SavedModel saved prior to TF 2.5 detected when loading Keras model, please" + |
60 |
| - " use higher version or submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues. to let us know you need it."); |
61 |
| - } |
| 35 | + throw new NotImplementedException("Model load of h5 format has not been supported. Please submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues if it's needed."); |
| 36 | + } |
| 37 | + } |
62 | 38 |
|
63 |
| - if (metadata.Nodes is null || metadata.Nodes.Count == 0) |
64 |
| - { |
65 |
| - return Loader.load(path, options: options) as Model; |
66 |
| - } |
| 39 | + private static Trackable load(string path, bool compile = true, LoadOptions? options = null) |
| 40 | + { |
| 41 | + SavedMetadata metadata; |
| 42 | + var meta_graph_def = Loader.parse_saved_model(path).MetaGraphs[0]; |
| 43 | + var object_graph_def = meta_graph_def.ObjectGraphDef; |
| 44 | + string path_to_metadata_pb = Path.Combine(path, Constants.SAVED_METADATA_PATH); |
| 45 | + if (File.Exists(path_to_metadata_pb)) |
| 46 | + { |
| 47 | + using var stream = new FileStream(path_to_metadata_pb, FileMode.Open, FileAccess.Read); |
| 48 | + metadata = SavedMetadata.Parser.ParseFrom(stream); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + throw new NotImplementedException("SavedModel saved prior to TF 2.5 detected when loading Keras model, please" + |
| 53 | + " use higher version or submit an issue to https://github.com/SciSharp/TensorFlow.NET/issues. to let us know you need it."); |
| 54 | + } |
67 | 55 |
|
68 |
| - var keras_loader = new KerasObjectLoader(metadata, object_graph_def); |
69 |
| - keras_loader.load_layers(compile: compile); |
| 56 | + if (metadata.Nodes is null || metadata.Nodes.Count == 0) |
| 57 | + { |
| 58 | + return Loader.load(path, options: options) as Model; |
| 59 | + } |
70 | 60 |
|
71 |
| - Dictionary<string, (Trackable, Action<object, object, object>)> nodes_to_load = new(); |
72 |
| - nodes_to_load["root"] = (null, null); |
73 |
| - foreach(var item in keras_loader.LoadedNodes) |
74 |
| - { |
75 |
| - nodes_to_load[keras_loader.get_path(item.Key)] = item.Value; |
76 |
| - } |
77 |
| - var loaded = Loader.load_partial(path, nodes_to_load, options); |
| 61 | + var keras_loader = new KerasObjectLoader(metadata, object_graph_def); |
| 62 | + keras_loader.load_layers(compile: compile); |
78 | 63 |
|
79 |
| - keras_loader.finalize_objects(); |
80 |
| - keras_loader.del_tracking(); |
| 64 | + Dictionary<string, (Trackable, Action<object, object, object>)> nodes_to_load = new(); |
| 65 | + nodes_to_load["root"] = (null, null); |
| 66 | + foreach(var item in keras_loader.LoadedNodes) |
| 67 | + { |
| 68 | + nodes_to_load[keras_loader.get_path(item.Key)] = item.Value; |
| 69 | + } |
| 70 | + var loaded = Loader.load_partial(path, nodes_to_load, options); |
81 | 71 |
|
82 |
| - var model = loaded["root"]; |
| 72 | + keras_loader.finalize_objects(); |
| 73 | + keras_loader.del_tracking(); |
83 | 74 |
|
84 |
| - if(model is Model && compile) |
85 |
| - { |
86 |
| - // TODO(Rinne): implement it. |
87 |
| - } |
| 75 | + var model = loaded["root"]; |
88 | 76 |
|
89 |
| - if (!tf.Context.executing_eagerly()) |
90 |
| - { |
91 |
| - // TODO(Rinne): implement it. |
92 |
| - } |
| 77 | + if (model is Model && compile) |
| 78 | + { |
| 79 | + // TODO(Rinne): implement it. |
| 80 | + } |
93 | 81 |
|
94 |
| - return model; |
| 82 | + if (!tf.Context.executing_eagerly()) |
| 83 | + { |
| 84 | + // TODO(Rinne): implement it. |
95 | 85 | }
|
| 86 | + |
| 87 | + return model; |
96 | 88 | }
|
97 | 89 | }
|
0 commit comments