# How to convert tflite to model.json + weights?

**URL:** <https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511>\
**Category:** General Discussion\
**Tags:** tfjs\
**Created:** [May 30, 2023, 2:25am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511 "2023-05-30T02:25:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [May 30, 2023, 2:25am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/1 "2023-05-30T02:25:34Z")

</div>

Hi there 👋,  
I’m developing a React Native mobile app which should detect and recognize (using a TensorCamera) text from live stream.

I’m totally new and, reading [documentation](https://js.tensorflow.org/api_react_native/0.2.1/#bundleResourceIO), my code would do something like that:

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/8/8b6d1a7f91005495cc99e03295f8a07970f592b2.png)

I found in the hub this [models](https://tfhub.dev/s?deployment-format=lite&module-type=image-text-detection,image-text-recognition):

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/8/8f5565abfdc715f702a0b72154bb6e29ec53cc02.png)

They are all .tflite!!!  
How can I use in my code ?

Thank you 🙏

**PS:**  
I’ve tried to convert using:

`tensorflowjs_converter --input_format=tf_hub 'https://tfhub.dev/tulasiram58827/lite-model/keras-ocr/float16/2' path/my/folder`

but it raises a lot of errors:

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/5/5fbb4aec140797edb0fddda9ea8fa956962f13f3.jpeg)

---

<div class="post-metadata">

**Author:** ![PINTO0309](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pinto0309/32/12651_2.png) [@PINTO0309](https://discuss.ai.google.dev/u/PINTO0309)\
**Post date:** [May 30, 2023, 4:31pm UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/3 "2023-05-30T16:31:17Z")

</div>

If you want to generate a TensorFlow.js model, you can use the following procedure to inverse-quantify tflite to generate onnx, then generate TensorFlow saved\_model again, and convert from saved\_model to TFJS.

```bash
pip install -U tf2onnx

python -m tf2onnx.convert \
--opset 11 \
--tflite lite-model_craft-text-detector_dr_1.tflite \
--dequantize \
--output lite-model_craft-text-detector_dr_1.onnx

pip install onnx2tf

onnx2tf \
-i lite-model_craft-text-detector_dr_1.onnx \
-ois input:1,3,800,600 -osd

pip install protobuf==3.20

tensorflowjs_converter \
--input_format tf_saved_model \
--output_format tfjs_graph_model \
saved_model \
tfjs_model_float32

tensorflowjs_converter \
--input_format tf_saved_model \
--output_format tfjs_graph_model \
--quantize_float16 "*" \
saved_model \
tfjs_model_float16

```

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/6/68d44be600a217b64fb6d98758900620002a03f0.png)

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [May 30, 2023, 7:42pm UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/4 "2023-05-30T19:42:50Z")

</div>

Thank you so much for your kind and very detailed response! 🙏

I’ve tried it but, unfortunately, on running this command:

```auto
onnx2tf \
-i lite-model_craft-text-detector_dr_1.onnx \
-ois input:1,3,800,600 -osd

```

I get these errors:

```auto
TypeError: argument of type 'NoneType' is not iterable
ERROR: input_onnx_file_path: /Users/someuser/Downloads/myModel/lite-model_craft-text-detector_dr_1.onnx
ERROR: onnx_op_name: onnx_tf_prefix_MatMul_84;StatefulPartitionedCall/onnx_tf_prefix_MatMul_845
ERROR: Read this and deal with it. https://github.com/PINTO0309/onnx2tf#parameter-replacement
ERROR: Alternatively, if the input OP has a dynamic dimension, use the -b or -ois option to rewrite it to a static shape and try again.
ERROR: If the input OP of ONNX before conversion is NHWC or an irregular channel arrangement other than NCHW, use the -kt or -kat option.
ERROR: Also, for models that include NonMaxSuppression in the post-processing, try the -onwdt option.

```

Any tips?

---

<div class="post-metadata">

**Author:** ![PINTO0309](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pinto0309/32/12651_2.png) [@PINTO0309](https://discuss.ai.google.dev/u/PINTO0309)\
**Post date:** [May 31, 2023, 12:05am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/5 "2023-05-31T00:05:39Z")

</div>

I performed the same procedure again, but unfortunately no such error occurs in my environment.

```bash
 tf.nn.convolution_25 (TFOpLambda) (1, 400, 300, 2) 0 ['tf.split_26[0][0]']                          
                                                                                                                                            
 tf.math.add_46 (TFOpLambda) (1, 400, 300, 2) 0 ['tf.nn.convolution_25[0][0]']                 
                                                                                                                                            
 Identity (TFOpLambda) (1, 400, 300, 2) 0 ['tf.math.add_46[0][0]']                       
                                                                                                                                            
============================================================================================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
____________________________________________________________________________________________________________________________________________

saved_model output started ==========================================================
saved_model output complete!
Float32 tflite output complete!
Float16 tflite output complete!

```

I expect that this is probably due to an outdated version of some package used in the back end, etc. Therefore, I recommend trying the following. If you are a Windows user, running the commands inside WSL2 Ubuntu should go very smoothly.

```bash
docker login ghcr.io
Username (xxxx): {Enter}
Password: {Personal Access Token}
Login Succeeded

docker run --rm -it \
-v `pwd`:/workdir \
-w /workdir \
ghcr.io/pinto0309/onnx2tf:1.13.3

wget -O lite-model_craft-text-detector_dr_1.tflite https://tfhub.dev/tulasiram58827/lite-model/craft-text-detector/dr/1?lite-format=tflite

pip install -U tf2onnx

python -m tf2onnx.convert \
--opset 11 \
--tflite lite-model_craft-text-detector_dr_1.tflite \
--dequantize \
--output lite-model_craft-text-detector_dr_1.onnx

onnx2tf \
-i lite-model_craft-text-detector_dr_1.onnx \
-ois input:1,3,800,600 -osd

pip install tensorflowjs 

tensorflowjs_converter \
--input_format tf_saved_model \
--output_format tfjs_graph_model \
saved_model \
tfjs_model_float32

tensorflowjs_converter \
--input_format tf_saved_model \
--output_format tfjs_graph_model \
--quantize_float16 "*" \
saved_model \
tfjs_model_float16

```

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [May 31, 2023, 7:35am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/7 "2023-05-31T07:35:18Z")

</div>

> [@PINTO0309](#):
>
> ```auto
> onnx2tf \
> -i lite-model_craft-text-detector_dr_1.onnx \
> -ois input:1,3,800,600 -osd
> 
> ```

Now it kills himself 🫤

```auto
...

Model optimizing complete!

Automatic generation of each OP name started ========================================
Automatic generation of each OP name complete!

Model loaded ========================================================================

Model convertion started ============================================================
INFO: input_op_name: input shape: [1, 3, 800, 600] dtype: float32
Killed

```

I used the docker approach you suggest above.  
My computer is a Macbook (13.3.1 (a) )

---

<div class="post-metadata">

**Author:** ![PINTO0309](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pinto0309/32/12651_2.png) [@PINTO0309](https://discuss.ai.google.dev/u/PINTO0309)\
**Post date:** [May 31, 2023, 8:17am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/8 "2023-05-31T08:17:32Z")

</div>

I am not going to advise you on issues specific to your environment. `Killed` is probably due to lack of RAM or something. TensorFlow unnecessarily consumes a lot of RAM, not an onnx2tf problem.

> **[Google Colab](https://colab.research.google.com/drive/1_aZLb2Su48Nff8spd59Anz8lslbjBdhR?usp=sharing)**

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/e/e9a3aa5c38b3f73e1d1220f9552b149aed4e3f50.png)

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [May 31, 2023, 10:28am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/9 "2023-05-31T10:28:44Z")

</div>

Thank you again, you are very kind and patient with me. I’m learning a lot with your help! 😊

With Colab it works and I was able to convert `lite-model_craft-text-detector_dr_1.onnx` easy.

I was trying with another model from hub, [rosetta](https://tfhub.dev/tulasiram58827/lite-model/rosetta/dr/1), but in the command:

`!onnx2tf -i lite-model_rosetta_dr_1.onnx -ois input:1,3,800,600 -osd`

I get following errors:

```auto
ERROR: input_onnx_file_path: lite-model_rosetta_dr_1.onnx
ERROR: onnx_op_name: transpose_1;StatefulPartitionedCall/transpose_11
ERROR: Read this and deal with it. https://github.com/PINTO0309/onnx2tf#parameter-replacement
ERROR: Alternatively, if the input OP has a dynamic dimension, use the -b or -ois option to rewrite it to a static shape and try again.
ERROR: If the input OP of ONNX before conversion is NHWC or an irregular channel arrangement other than NCHW, use the -kt or -kat option.
ERROR: Also, for models that include NonMaxSuppression in the post-processing, try the -onwdt option.

```

Could you please suggest me a way to fix it?  
Thank you in advance 🙏

⚠ PS: I’ve tried to use in my code the model exported as json+bin but it gave me following error:

```auto
Error: layer: Improper config format:

{
... <- all json content here
}

'className' and 'config' must set.

```

---

<div class="post-metadata">

**Author:** ![PINTO0309](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pinto0309/32/12651_2.png) [@PINTO0309](https://discuss.ai.google.dev/u/PINTO0309)\
**Post date:** [May 31, 2023, 1:51pm UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/11 "2023-05-31T13:51:25Z")

</div>

There was an omission in the conversion process of `Slice` in onnx2tf, which will be fixed and released later.

[https://github.com/PINTO0309/onnx2tf/pull/377](https://github.com/PINTO0309/onnx2tf/pull/377)

---

<div class="post-metadata">

**Author:** ![PINTO0309](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/pinto0309/32/12651_2.png) [@PINTO0309](https://discuss.ai.google.dev/u/PINTO0309)\
**Post date:** [May 31, 2023, 11:27pm UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/13 "2023-05-31T23:27:44Z")

</div>

I fixed it. From today rosetta can also be converted. Note that I do not know how to implement React. You’ll have to rely on other engineers.

> **[Google Colab](https://colab.research.google.com/drive/1_aZLb2Su48Nff8spd59Anz8lslbjBdhR?usp=sharing)**

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/1/1e10a6fe978ec47da46ae5f8319418b185ddb2c5.png)

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [June 1, 2023, 7:12am UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/15 "2023-06-01T07:12:16Z")

</div>

Your work is amazing!  
Thank you so much. I’m going to try it soon 😊

I will definitely ask in another thread about other application’s errors.

---

<div class="post-metadata">

**Author:** ![Giuseppe\_Ravida](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/giuseppe_ravida/32/16139_2.png) [@Giuseppe\_Ravida](https://discuss.ai.google.dev/u/Giuseppe_Ravida)\
**Post date:** [June 9, 2023, 9:01pm UTC](https://discuss.ai.google.dev/t/how-to-convert-tflite-to-model-json-weights/23511/17 "2023-06-09T21:01:37Z")

</div>

Hello 👋,  
I was trying to convert [keras-ocr tflite](https://tfhub.dev/tulasiram58827/lite-model/keras-ocr/float16/2) using previous Colab but I crash with this error:

 ![image](https://d3qe71uytubmmx.cloudfront.net/original/2X/6/67155baa469fc505ea8d65872be3c7a220460068.png)

```auto
ERROR - rewriter <function rewrite_cond at 0x7f207905cdc0>: exception make_sure failure: Cannot find node with output 'std.constant107' in graph 'main'
Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main

```

Any suggestions?
