gap_sdk: NNTOOL import tflite model error
Dear GreenWaves Technologies,
I redesigned my network architecture in order to use Conv2D instead of Conv3D as in my previous issue, however i get the exact same error while importing the .tflite model genereted with this piece of code:
[NOT QUANTIZED]
import tensorflow as tf
from tensorflow import keras
model = keras.models.load_model('cae_2d_model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS]
tfmodel = converter.convert()
open ('cae_2d_model.tflite' , "wb") .write(tfmodel)
and when i try to open this model inside NNTOOL i get this error (i set debug true):
(NNT) open cae_2d_model.tflite
open - opening graph file cae_2d_model.tflite
new_tflite_graph_all - Importing TFLITE model version 3
Traceback (most recent call last):
File "/home/manucar/.local/lib/python3.8/site-packages/cmd2/cmd2.py", line 1661, in onecmd_plus_hooks
stop = self.onecmd(statement, add_to_history=add_to_history)
File "/home/manucar/.local/lib/python3.8/site-packages/cmd2/cmd2.py", line 2081, in onecmd
stop = func(statement)
File "/home/manucar/.local/lib/python3.8/site-packages/cmd2/decorators.py", line 223, in cmd_wrapper
return func(cmd2_app, args)
File "/home/manucar/gap_sdk/tools/nntool/interpreter/commands/open.py", line 98, in do_open
self.__open_graph(args.nnfile, args.tensor_file, args.load_quantization, args.load_dequantized)
File "/home/manucar/gap_sdk/tools/nntool/interpreter/commands/open.py", line 71, in __open_graph
G = create_graph(graph_file, opts=opts)
File "/home/manucar/gap_sdk/tools/nntool/importer/importer.py", line 51, in create_graph
graph = importer.create_graph(filename, opts)
File "/home/manucar/gap_sdk/tools/nntool/importer/tflite/new_tflite_graph_all.py", line 1756, in create_graph
self.create_subgraph(0)
File "/home/manucar/gap_sdk/tools/nntool/importer/tflite/new_tflite_graph_all.py", line 1714, in create_subgraph
self.add_operator(graph, graph_index, op, i)
File "/home/manucar/gap_sdk/tools/nntool/importer/tflite/new_tflite_graph_all.py", line 1673, in add_operator
in_node, out_node = self.SWITCH_ADD_FUNCTIONS[op_name](
File "/home/manucar/gap_sdk/tools/nntool/importer/tflite/new_tflite_graph_all.py", line 1438, in add_pack
in_shape = Dim.unnamed(self.remove_batch_dim(input_tensor.shape), is_ordered=True)
File "/home/manucar/gap_sdk/tools/nntool/importer/tflite/new_tflite_graph_all.py", line 444, in remove_batch_dim
if dim[self.batch_dimension] != 1:
IndexError: index 0 is out of bounds for axis 0 with size 0
EXCEPTION of type 'IndexError' occurred with message: 'index 0 is out of bounds for axis 0 with size 0'
This is my .h5 model cae_2d_model.zip and this my .tflite converted model cae_2d_model_tflite.zip.
I am on a Ubuntu 20.04 machine with the latest sdk (master).
I don’t see any unsupported type of operations/activations in my .tflite model, so i’m wondering what could be wrong in my implementation. If you have any suggestions on this please let me know.
Best regards,
Manuel
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 16 (8 by maintainers)
Sigmoid is supported. I think you were hitting the same regression that you hit with tanh. That graph converts, adjusts, fuses and generates fine now. The fix will be pushed to github tonight I think. I will check.
Acknowledged. It’s a regression introduced by constant folding in the latest release. We will resolve and push a fix.
OK. It should be fixed in the 3.8.0_dev branch. It turned out to be a too general gitignore rule in the sdk root so all regressions tests pass and the sdk integration tests had no network with a sigmoid in it! Go figure. Sorry for the delay.
Hi. We appear to have a very weird git issue between myself and the person looking after Github updates. There is a file missing. We are checking what is going on.
Hi @sousoux, I downloaded the last release but I get the same error. Do you have any idea why?
Tanh was a temporary solution to replace sigmoid because when I tried to import my .tflite model cae_q.zip into NNTOOL, it gives me this error:
So I thought sigmoid activation function was not yet supported, am I wrong?
I’ve fixed that problem and will get it pushed to Github shortly but there is an outstanding issue. We do not have tanh implemented in the fused convolution generator. Tanh is a pretty inefficient activation if you want something very accurate. A min(1, max(-1, v)) approximates it pretty well but obviously not perfectly. We call that a htanh. We could add support for htanh fairly easily or you could experiment with another activation.
For the moment i have switched to tanh activation function and it does load into NNTOOL, i have also quantized the .tflite model to fully 8 bit integer (except the input and output) and i get this other error while giving “fusions --scale8” command:
This is my quantized .tflite model cae_2d_tanh_quant.zip.
I have also tried generating :
a full 8 bit integer quantized model (also input and output layer) cae_2d_tanh_quant_int8.zip
eliminating all the batch normalization layers nobatch_tanh_quant_int8.zip
but i get the same error as above.
Thank you again,
Manuel
Filter larger than input …
The batch norm position looks good. Must be a converter problem. Try getting rid of tf.lite.OpsSet.SELECT_TF_OPS in the TOCO options. You should check the resulting TFLITE with something like netron and make sure you get rid of those mul/adds. The mul we might be able to fuse through the relu but the add can’t be.
This was a missing feature for constant folding on our side. It will be supported in the next release. Should go out sometime next week. There are still strange things in your graph:
Here you have a filter that is larger than the input. Perhaps a bug on your side. Definitely not optimal.
Also are you placing batch normalization correctly. It is getting converted as mul / adds. It’s probably in the wrong place in your original graph.