[BUG Report]: An item with the same key has already been added. Key: keras_api
Description
When saving model, this error happened. By reading Linq code, it seems like happening in TensorFlowNET.Keras.Engine.Layer.Serialize._trackable_children when concatenating model trackable with another trackable while they both have Key: keras_api
model trackable keys: layer-0 layer-1 layer_with_weights-0 layer-2 keras_api
children keys: non_trainable_variables layers variables trainable_variables keras_api
Reproduction Steps
load a keras pb model saved by python and save it immediately:
var model = keras.models.load_model(@"path"); model.summary(); model.save(@"path");
Known Workarounds
saveweght is fine
Configuration and Other Information
python tensorflow version: 2.10.0-GPU TensorFlow.Redist version:2.10.0-CPU Tensorflow.NET: 0.100.4 Tensorflow.Keras :0.10.4 .NET: 7.0
Hi, thank you for reporting us this BUG. I'll fix it soon. :)
What's more, UWP will break down in clrcore while using V1 LoadSession function under new version, but not very sure, need more test. I will abandon UWP.
I have also experienced this error setting up a Conv1D layer: Dim denseLayer5 = layers.Conv1D(64, kernel_size:=3, activation:="relu") Dim denseOutput5 = denseLayer3.Apply(denseOutput4)
Here is a possible work around https://github.com/SciSharp/TensorFlow.NET/pull/976#issuecomment-1490567800. I'll fix this BUG this weekend. Recently a little busy and forgot it🤣Sorry for that
#1049 partially fixed this problem. Note that tf.net support loading models with functions/concrete functions now but hasn't supported saving models with functions/concrete functions. The complete support for model saving would be added in 2 weeks. It's recommended to use model saving after loading (from model saved in python) after that.
Here is a possible work around #976 (comment). I'll fix this BUG this weekend. Recently a little busy and forgot it🤣Sorry for that
Can the mentioned workaround be added any time soon? The issue still persists when loading and saving keras model for the 2nd time, it throws: System.ArgumentException: An item with the same key has already been added. Key: non_trainable_variables
can be reproduced e.g. using avaliable unit tests:
// build and save model AlexnetFromSequential(); // load and save again var model = tf.keras.models.load_model(@"./alexnet_from_sequential"); model.summary(); model.save("./saved_alexnet_from_sequential_after_loading"); // <- the ERROR