gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
193 stars 115 forks source link

Issues when parameter "change_ordering" is "True" #113

Open bwery opened 3 years ago

bwery commented 3 years ago

I know this is an experimental feature, but as I need it, I have performed some tests and identified the two following issues, for which I can propose a correction.

First is an error with the batch normalization layers. Transposition is attempted on the weight tensor as if its dimension is 4, while it is 1.

I propose the following patch, in file "converter.py", replacing lines 280 and 281

        if conf['config'] and 'shared_axes' in conf['config']:
            W[0] = W[0].transpose(1, 2, 0)

with

        if (W[0].ndim >= 3)
            if conf['config'] and 'shared_axes' in conf['config']:
                W[0] = W[0].transpose(1, 2, 0)

Second one is a problem with the "Flatten" layers for which a similar error occur:s: a transposition considering a 4D tensor is applied, while data is a 2D tensor that does not need transposition.

I propose to replace line 257 in file “reshape layers.py”

        x = tf.transpose(x, [0, 3, 1, 2])

with

        if (len(x.shape) == 4):
            x = tf.transpose(x, [0, 3, 1, 2])
        if (len(x.shape) == 3):
            x = tf.transpose(x, [0, 2, 1])              

Anyway, thank you a lot for this valuable piece of software !

Andredance commented 3 years ago

Hi, @bwery ! I can't say for sure (since it wasn't me to implement this feature) but I think that this was done because 4d input in most cases is [B, C, H, W] (and its permutations), while 3d input is quite tricky and depends a lot on the idea behind the model. In any way, thank you for your interest, we will think more about it and maybe will add this functionality with some warning message or something else. Also, if you want, you can make your own PR for this but we should cover it with some tests. Thank you for your interest in this project!

bwery commented 3 years ago

Hello, I am not sure on my side what is the best action to perform when dimension 3 input is present. I agree with the fact that this may be context dependent. And it was not the case in the example in which I have detected the issue, where the tensor was 2D. I have written this thinking to 1D data.