AlexanderLutsenko / nobuco

Pytorch to Keras/Tensorflow/TFLite conversion made intuitive
MIT License
263 stars 17 forks source link

ValueError from adding tf.Tensor and None when exporting model with dynamic shapes #36

Open NathanielB123 opened 6 months ago

NathanielB123 commented 6 months ago

When exporting a PyTorch model with dynamic shapes I hit

Traceback (most recent call last):
  File "/home/nathaniel/dev/hood/./TFExport.py", line 106, in <module>
    tf_model = nobuco.pytorch_to_keras(
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/convert.py", line 340, in pytorch_to_keras
    outputs_tf = keras_op(*args_tf, **kwargs_tf)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/layers/container.py", line 69, in __call__
    outputs = op(*args, **kwargs)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/layers/container.py", line 69, in __call__
    outputs = op(*args, **kwargs)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/layers/channel_order.py", line 27, in __call__
    outputs = self.func(*args, **kwargs)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/layers/channel_order.py", line 59, in __call__
    outputs = self.func(*args, **kwargs)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/node_converters/slice.py", line 203, in func
    return slice_assign(sliced_tensor, slice_args, assigned_tensor)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/nobuco/node_converters/slice.py", line 101, in slice_assign
    idx = tf.where(idx < 0, idx + shape[real_index], idx)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/keras/src/layers/core/tf_op_layer.py", line 119, in handle
    return TFOpLambda(op)(*args, **kwargs)
  File "/home/nathaniel/miniconda3/envs/hood/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
ValueError: Exception encountered when calling layer "tf.__operators__.add_18" (type TFOpLambda).

None values not supported.

Call arguments received by layer "tf.__operators__.add_18" (type TFOpLambda):
  • x=tf.Tensor(shape=(None,), dtype=int64)
  • y=None
  • name=None

The offending addition occurs in nobuco/node_converters/slice.py

...
idx = tf.convert_to_tensor(slice_spec)
idx = tf.where(idx < 0, idx + shape[real_index], idx) # Addition in second argument to `tf.where` raises the exception
corresponding_range = tf.cast(idx, dtype=tf.int32)
...

where shape[real_index] can be None if the shape of sliced_tensor is dynamic.

As far as I can tell, this line is only relevant if there are negative indices. In my model, the indices are always positive, so I just commented out this line and the export went smoothly, but a proper fix to take dynamic shapes into account here would probably be ideal.