onnx2keras icon indicating copy to clipboard operation
onnx2keras copied to clipboard

Invalid scope names when reimporting a Keras model

Open theHamsta opened this issue 4 years ago • 1 comments

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.

theHamsta avatar Jun 28 '21 07:06 theHamsta

Same problem thanks for your fix !

AlexandreBourrieau avatar Apr 19 '22 10:04 AlexandreBourrieau