yamhoresh/monodepth2-TensorFlow2

Inference on Tflite Model

Closed this issue · 1 comments

Is there any example code to test inference on the Tflite model?

Hey @TranATT ,

Sorry for the late reply.
Yes, here is some code to test the model on a computer, before trying it on an edge device:

import numpy as np
import tensorflow as tf
from PIL import Image
import time

I = Image.open('test.jpeg').resize((640,192), Image.BILINEAR)
im = np.asarray(I).astype('float32').reshape(1,192,640,3)/255.0

interpreter = tf.lite.Interpreter(model_path='model.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_data = im

start = time.time()
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print(time.time()-start)
im_final = Image.fromarray((output_data.reshape(192,640) * 255).astype(np.uint8))
im_final.save('depth.jpeg')