Description
Description
Hi,
I recently started using TensorFlow.NET and was trying to load my saved Keras model, but I get an exception thrown during model deserialization.
Specifically:
- I saved my original pretrained .h5 Keras model to a .PB based SavedModel directory (following the instructions provided by @AsakusaRinne in this closed issue ) using a Python script running TF 2.15.0
- I then tried to load it in C# using:
var model = tf.keras.models.load_model("TestModel", false);
However, I get this error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Newtonsoft.Json.JsonSerializationException: Could not create an instance of type Tensorflow.Keras.IRegularizer. Type is an interface or abstract class and cannot be instantiated. Path 'bias_regularizer.class_name'.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject[T]()
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
--- End of inner exception stack trace ---
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Tensorflow.Keras.Utils.generic_utils.deserialize_keras_object(String class_name, JToken config) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Utils\generic_utils.cs:line 72
at Tensorflow.Keras.Saving.KerasObjectLoader._revive_layer_or_model_from_config(KerasMetaData metadata, Int32 node_id) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\KerasObjectLoader.cs:line 503
at Tensorflow.Keras.Saving.KerasObjectLoader._revive_from_config(String identifier, KerasMetaData metadata, Int32 node_id) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\KerasObjectLoader.cs:line 422
at Tensorflow.Keras.Saving.KerasObjectLoader._load_layer(Int32 node_id, String identifier, String metadata_json) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\KerasObjectLoader.cs:line 389
at Tensorflow.Keras.Saving.KerasObjectLoader.load_layers(Boolean compile) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\KerasObjectLoader.cs:line 79
at Tensorflow.Keras.Saving.SavedModel.KerasLoadModelUtils.load(String path, Boolean compile, LoadOptions options) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\SavedModel\load.cs:line 62
at Tensorflow.Keras.Saving.SavedModel.KerasLoadModelUtils.load_model(String filepath, IDictionary`2 custom_objects, Boolean compile, LoadOptions options) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Saving\SavedModel\load.cs:line 31
at Tensorflow.Keras.Models.ModelsApi.load_model(String filepath, Boolean compile, LoadOptions options) in C:\...\TensorFlow.NET\src\TensorFlowNET.Keras\Models\ModelsApi.cs:line 13
...etc.
Stepping through the source (cloned from the project branch at commit 8775b0b), the exception seem to be thrown on Line 40 of ConvolutionalArgs.cs
during deserialization of config for my model's bias_regularizer
object (of class L1L2
) from its second layer (of class Conv2D
):
I've tried to step further in TensorFlow.Keras and TensorFlow.Binding but I cannot find the cause.
The model I used is here:
TestModel.zip
Is there something I need to specify when I save to the SavedModel in my Python script to avoid this exception when loading in C#?
Or is this something I need to do while using the Tensorflow.NET bindings to let it ingest this model without issues?
Any help for this would be greatly appreciated.
Thank you!