Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Solved] Errors in converting Tensorflow Model to Tensorflow Lite Model #285

Closed
aparico opened this issue Dec 22, 2020 · 18 comments
Closed

Comments

@aparico
Copy link

aparico commented Dec 22, 2020

I wanted to use yolov4-tiny in the Tensorflow lite framework.

I converted my darknet weights trained from @AlexeyAB using these commands:

python save_model.py --weights yolov4-tiny.weights --output ./checkpoints/yolov4-tiny-608-tf --input_size 608 --model yolov4 --tiny --framework tflite
python convert_tflite.py --weights ./checkpoints/yolov4-tiny-608-tf --output ./checkpoints/yolov4-tiny-608.tflite

The first command is successful using numpy==1.19.0. However, the second one shows these errors.


loc("batch_normalization/moving_mean"): error: is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable
Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 213, in toco_convert_protos
    enable_mlir_converter)
  File "C:\Python37\lib\site-packages\tensorflow\lite\python\wrap_toco.py", line 38, in wrapped_toco_convert
    enable_mlir_converter)
Exception: <unknown>:0: error: loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "convert_tflite.py", line 76, in <module>
    app.run(main)
  File "C:\Python37\lib\site-packages\absl\app.py", line 303, in run
    _run_main(main, args)
  File "C:\Python37\lib\site-packages\absl\app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "convert_tflite.py", line 71, in main
    save_tflite()
  File "convert_tflite.py", line 45, in save_tflite
    tflite_model = converter.convert()
  File "C:\Python37\lib\site-packages\tensorflow\lite\python\lite.py", line 762, in convert
    result = _convert_saved_model(**converter_kwargs)
  File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 648, in convert_saved_model
    enable_mlir_converter=True)
  File "C:\Python37\lib\site-packages\tensorflow\lite\python\convert.py", line 216, in toco_convert_protos
    raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: <unknown>:0: error: loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable

I have tried other versions of Tensorflow (2.2, 2.3, 2.4 with numpy==1.19.0) but I had no luck. What should I do?

There is a similar issue raised here: Tensorflow Issue 44790

System information:
Windows 10, x64
GeForce GTX 1060
NVIDIA Driver 460.89
CUDA 11.0.3
CuDNN 8.0.5.39
Python 3.7.2

@Adithia99
Copy link

yeah same issue , i work on google colab and get same error

loc("batch_normalization/moving_mean"): is not immutable, try running tf-saved-model-optimize-global-tensors to prove tensors are immutable

@Marouaneghoulami
Copy link

Have anyone found the solution for this yet ?

@Marouaneghoulami
Copy link

Maybe try training again your model with :

tensorflow-gpu==2.3.0rc0 opencv-python==4.1.1.26

@rahulzag
Copy link

same issue, please fix

@rahulzag
Copy link

rahulzag commented Dec 31, 2020

well, I had done this before on a dataset and it had worked but now I used another dataset and tweaked the batches and subdivisions does it have something to do with this error?

@Adithia99
Copy link

Adithia99 commented Dec 31, 2020 via email

@Marouaneghoulami
Copy link

Can u show me the collab and the dataset all od your data?

On Wed, 30 Dec 2020, 21:56 Marouaneghoulami, @.***> wrote: Maybe try training again your model with : tensorflow-gpu==2.3.0rc0 opencv-python==4.1.1.26 — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#285 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANKLHMUBCYBQRSBCFXAVKU3SXNERNANCNFSM4VFHFU3A .

I'm afraid its confidential, but i faced too the same problem, it was because tensorflow has a new release 2.4.0
but when i uninstalled it and re installed the version as mention above, that seem to solve the problem for me.

@rahulzag
Copy link

rahulzag commented Jan 1, 2021

can someone please convert the model to tflite and send me it would be a great help this is the model file -https://drive.google.com/drive/folders/1-1jAHa_N2YoA28sLCa2a0xJFkEfJu4Td?usp=sharing

@gouliang1997
Copy link

try tf-nightly

@mikeDonahoe
Copy link

In case it helps. In google colab I had this issue if I used the default TF version which was 2.4.0. Running !pip install tensorflow==2.3.0 and restarting the runtime, then converting corrected the issue.

@leosapucaia
Copy link

In case it helps. In google colab I had this issue if I used the default TF version which was 2.4.0. Running !pip install tensorflow==2.3.0 and restarting the runtime, then converting corrected the issue.

Thanks! This solution works nicely!

@aparico
Copy link
Author

aparico commented Feb 2, 2021

In case it helps. In google colab I had this issue if I used the default TF version which was 2.4.0. Running !pip install tensorflow==2.3.0 and restarting the runtime, then converting corrected the issue.

Thanks for this! I will close this issue now.

@aparico aparico closed this as completed Feb 2, 2021
@aparico aparico changed the title Errors in converting Tensorflow Model to Tensorflow Lite Model [Solved] Errors in converting Tensorflow Model to Tensorflow Lite Model Feb 2, 2021
@fmigone
Copy link

fmigone commented May 13, 2021

It works for me. Thanks a lot.

@khg2478
Copy link

khg2478 commented Aug 11, 2021

@leosapucaia @mikeDonahoe @fmigone @Marouaneghoulami @aparico

Hello all, I've changed the package as mentioned above, tensorflow-gpu==2.3.0rc0 opencv-python==4.1.1.26, but they don't help resolving the issue still. I have uploaded the results. I have my custom model that slightly changes configurations such as resolution. Please help.

tflite
darknet

@Marouaneghoulami
Copy link

@khg2478 The solution mentioned above is related to conversion from SavedModel to Tflite.

As for your problem, you should check the code you used for rescaling.

@khg2478
Copy link

khg2478 commented Aug 11, 2021

@khg2478 The solution mentioned above is related to conversion from SavedModel to Tflite.

As for your problem, you should check the code you used for rescaling.

Thanks for your reply. Perhaps, it's because of the rescaling problem. Could you tell me how I can fix this then? I did change the size parameter in detect.py as my trained model, 960x960 but still the problem remains. I thought it was caused by the model conversion issue.

@Marouaneghoulami
Copy link

@khg2478 i think u should just check you rescaling code, perhaps you missed up width and height when drawing boxes.

to check whether you converted the model successfully or not please use this site : https://netron.app/ and see output and input sizes of the model

@khg2478
Copy link

khg2478 commented Aug 11, 2021

@khg2478 i think u should just check you rescaling code, perhaps you missed up width and height when drawing boxes.

to check whether you converted the model successfully or not please use this site : https://netron.app/ and see output and input sizes of the model

@Marouaneghoulami Thanks for your reply. I tried my own scaling to fix the problem. After obtaining boxes, I ran np.amax function to check the largest value and found out that it is 1.
So, I resized the value as below since I figured out that it multiplies image's shape in utils.draw_bbox function.
boxes[:,:,0::2] = boxes[:,:,0::2] / 960 (model input shape)
boxes[:,:,1::2] = boxes[:,:,1::2] / 960
However, the problem still remains.. The model works fine in darknet but the converted tflite doesn't work properly.. I think I need guidance to fix the issue. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants