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 usage of an as_numpy_dtype field in execute.py #131

Closed dvorotnev closed 3 years ago

dvorotnev commented 3 years ago

When I try to get output activations of a simple example with bool datatype tensors:

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

def testnet_not():
    x = tf.placeholder(tf.bool, shape=[6, 32, 32, 3], name='input')
    return tf.logical_not(x)

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

the script execute.py fails with exception:

Traceback (most recent call last):
  File "/nix/store/xkvjcsv75k1z7yjdwglz423wfl8biv0p-python3-3.7.9/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/nix/store/xkvjcsv75k1z7yjdwglz423wfl8biv0p-python3-3.7.9/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/nix/store/zw9kd4xk19lx26zqb3zymzmzsr1valjk-python3.7-nnef-tools-python-1.0/lib/python3.7/site-packages/nnef_tools/execute.py", line 487, in <module>
    exit(main(parser.parse_args()))
  File "/nix/store/zw9kd4xk19lx26zqb3zymzmzsr1valjk-python3.7-nnef-tools-python-1.0/lib/python3.7/site-packages/nnef_tools/execute.py", line 399, in main
    inputs = {info.name: source(info.name, info.shape, info.dtype) for info in input_info}
  File "/nix/store/zw9kd4xk19lx26zqb3zymzmzsr1valjk-python3.7-nnef-tools-python-1.0/lib/python3.7/site-packages/nnef_tools/execute.py", line 399, in <dictcomp>
    inputs = {info.name: source(info.name, info.shape, info.dtype) for info in input_info}
  File "/nix/store/zw9kd4xk19lx26zqb3zymzmzsr1valjk-python3.7-nnef-tools-python-1.0/lib/python3.7/site-packages/nnef_tools/execute.py", line 108, in __call__
    return self._distribution(shape).astype(dtype)
TypeError: data type not understood

which arises from using of the as_numpy_dtype field as a function in the TFExecutor::input_info and in the TFExecutor::output_info functions. For this reason, the dtype field of TensorInfo tuple is assigned by an element of a tensor type, but not by the type itself.

Steps to reproduce:

python3 ./test.py
python -m nnef_tools.convert --input-format=tf --output-format=nnef --input-model=./model.pb --optimize --keep-io-names --output-model=model.nnef
python -m nnef_tools.execute ./model.pb  --format=tf --random='lambda shape: np.random.uniform(0, 1, shape) < 0.5' --output-names='{"output:0": "output"}' --output-path=./
gyenesvi commented 3 years ago

Indeed, I have made that mistake a couple of times.. I wonder why it does not fail when the output is float.