gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
195 stars 116 forks source link

Invalid scope names when reimporting a Keras model #125

Open theHamsta opened 3 years ago

theHamsta commented 3 years ago

When I try to re-import a Keras model. I get an error about invalid scope names in Tensorflow. The mentioned scope had a : in it's name.

        model = tf.keras.Sequential([
            hub.KerasLayer(
                "https://tfhub.dev/google/imagenet/resnet_v2_50/classification/5", dtype=tf.float32)
        ])
        model.build([None, 224, 224, 3])
        tf_net = convert_variables_to_constants_v2(
            tf.function(model, jit_compile=args.jit_compile == 'true').get_concrete_function(
                tf.TensorSpec(model.inputs[0].shape, dtype)))
        tf_net.save('some path')
        # then convert to onnx with tensorflow-onnx2 CLI

I could solve the bug by replacing all : by _ for keras_names in this line https://github.com/gmalivenko/onnx2keras/blob/45c81f221bb4228751abb061cb24d473bb74a8e8/onnx2keras/converter.py#L182

            [k.replace(':', '_') for k in keras_names]
            if isinstance(keras_names, list) else keras_names.replace(':', '_')

I'm now wondering whether this is a proper fix or just a work around. Maybe there could be a validation/coercion step where all invalid Tensorflow names could be replaced by fill character.

AlexandreBourrieau commented 2 years ago

Same problem thanks for your fix !