TensorFlow.NET icon indicating copy to clipboard operation
TensorFlow.NET copied to clipboard

[BUG Report]: An item with the same key has already been added. Key: keras_api

Open astronomicalphenomena opened this issue 2 years ago • 6 comments

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

astronomicalphenomena avatar Apr 18 '23 14:04 astronomicalphenomena

Hi, thank you for reporting us this BUG. I'll fix it soon. :)

SanftMonster avatar Apr 18 '23 16:04 SanftMonster

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.

astronomicalphenomena avatar Apr 19 '23 17:04 astronomicalphenomena

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)

GadgetNutt avatar Apr 27 '23 14:04 GadgetNutt

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

SanftMonster avatar Apr 27 '23 19:04 SanftMonster

#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.

SanftMonster avatar May 01 '23 20:05 SanftMonster

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

romanmanasipov avatar Aug 14 '23 08:08 romanmanasipov