deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
21.92k stars 5.26k forks source link

preprocess or normalize the crop before inference, Tflite #2589

Closed fisakhan closed 3 weeks ago

fisakhan commented 3 weeks ago

How to pre-process the crop for using w600k_r50_float32.tflite? I already have uint8 112x112x3 crop. `

Load the TFLite model and allocate tensors.

interpreter = tf.lite.Interpreter(model_path="./models/tflite/w600k_r50_float32.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

ARE THE FOLLOWING 3 LINES CORRECT?

crop = (crop - 127.5) * 0.0078125
crop = np.array(crop, dtype=np.float32)
input_data = input_data = np.expand_dims(crop, axis=0)

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

output_data = interpreter.get_tensor(output_details[0]['index'])
output_data0 = output_data/np.linalg.norm(output_data)
#print("output_data: ", output_data)`
fisakhan commented 3 weeks ago

I found the solution. Replace the 3 lines by the following code

crop = cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)  
crop = (crop-127.5)/127.5
crop = np.array(crop, dtype=np.float32)
input_data = input_data = np.expand_dims(crop, axis=0)