KhronosGroup / NNEF-Tools

The NNEF Tools repository contains tools to generate and consume NNEF documents
https://www.khronos.org/nnef
222 stars 57 forks source link

Incorrect parsing of tf.squeeze with default values #135

Closed dvorotnev closed 3 years ago

dvorotnev commented 3 years ago

I try to convert simple python example:

import tensorflow as tf
import nnef_tools.io.tf.graphdef as graphdef

def testnet_squeeze():
    x = tf.placeholder(tf.float32, shape=[32, 1, 1, 3], name='input')
    return tf.squeeze(x)

tf.reset_default_graph()
with tf.Session() as sess:
    result = testnet_squeeze()
    sess.run(tf.global_variables_initializer())
    graphdef.save_default_graph("model.pb", session=sess, outputs={result: "output"})

with such commands:

python ./test.py
python -m nnef_tools.convert --input-format=tf --output-format=nnef --input-model=./model.pb --keep-io-names --output-model=model.nnef

The result of conversion is:

version 1.0;

graph G(input) -> (output)
{
    input = external<scalar>(shape = [32, 1, 1, 3]);
    squeeze1 = squeeze<scalar>(input, axes = []);
    output = copy(squeeze1);
}

but documented behavior of tf.squeeze with empty squeeze_dims is to remove all dimensions of size 1. So, the conversion result should be:

version 1.0;

graph G(input) -> (output)
{
    input = external<scalar>(shape = [32, 1, 1, 3]);
    squeeze1 = squeeze<scalar>(input, axes = [1, 2]);
    output = copy(squeeze1);
}
gyenesvi commented 3 years ago

Seems correct, thanks for catching that!

gyenesvi commented 3 years ago

Note however that the fix did not cover all cases, namely the input may be transposed during the conversion process, in which case the input shape is already the transposed one, and that needs to be undone to be correct, since the input transposing is also undone in this transformation. I have posted a fix.