Nekshay commited on
Commit
04e6b4c
·
verified ·
1 Parent(s): 89eaa03

Create convert_tflite_2_onnx.py

Browse files
Files changed (1) hide show
  1. convert_tflite_2_onnx.py +42 -0
convert_tflite_2_onnx.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+
3
+ # Load the TFLite model
4
+ tflite_model_path = 'model.tflite'
5
+ interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
6
+ interpreter.allocate_tensors()
7
+
8
+ # Export the TFLite model back to a TensorFlow SavedModel
9
+ saved_model_dir = 'saved_model'
10
+
11
+ # Convert the TFLite model back to a TensorFlow model
12
+ converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
13
+ tf.saved_model.save(interpreter, saved_model_dir)
14
+
15
+
16
+ pip install tf2onnx
17
+ pip install onnx_runtime
18
+ python -m tf2onnx.convert --saved-model saved_model --output model.onnx --opset 11
19
+
20
+
21
+ import onnxruntime as ort
22
+ import numpy as np
23
+ from PIL import Image
24
+
25
+ # Load ONNX model
26
+ onnx_model_path = 'model.onnx'
27
+ session = ort.InferenceSession(onnx_model_path)
28
+
29
+ # Load image and preprocess (resize, normalize)
30
+ image_path = 'image.jpg'
31
+ image = Image.open(image_path).resize((320, 320)) # Assuming 320x320 model input size
32
+ image_data = np.array(image).astype('float32')
33
+ image_data = np.expand_dims(image_data, axis=0) # Add batch dimension
34
+
35
+ # Run inference
36
+ input_name = session.get_inputs()[0].name
37
+ output = session.run(None, {input_name: image_data})
38
+
39
+ # Output contains predictions, including bounding boxes and class labels
40
+ print(output)
41
+
42
+