| import tensorflow as tf |
|
|
| |
| tflite_model_path = 'model.tflite' |
| interpreter = tf.lite.Interpreter(model_path=tflite_model_path) |
| interpreter.allocate_tensors() |
|
|
| |
| saved_model_dir = 'saved_model' |
|
|
| |
| converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) |
| tf.saved_model.save(interpreter, saved_model_dir) |
|
|
|
|
| pip install tf2onnx |
| pip install onnx_runtime |
| python -m tf2onnx.convert --saved-model saved_model --output model.onnx --opset 11 |
|
|
|
|
| import onnxruntime as ort |
| import numpy as np |
| from PIL import Image |
|
|
| |
| onnx_model_path = 'model.onnx' |
| session = ort.InferenceSession(onnx_model_path) |
|
|
| |
| image_path = 'image.jpg' |
| image = Image.open(image_path).resize((320, 320)) |
| image_data = np.array(image).astype('float32') |
| image_data = np.expand_dims(image_data, axis=0) |
|
|
| |
| input_name = session.get_inputs()[0].name |
| output = session.run(None, {input_name: image_data}) |
|
|
| |
| print(output) |
|
|
|
|
| import onnxruntime as ort |
| import numpy as np |
| from PIL import Image |
|
|
| |
| onnx_model_path = 'model.onnx' |
| session = ort.InferenceSession(onnx_model_path) |
|
|
| |
| def preprocess_image(image_path, input_size=(320, 320)): |
| image = Image.open(image_path).resize(input_size) |
| image_data = np.array(image).astype('float32') |
| image_data = np.expand_dims(image_data, axis=0) |
| return image_data |
|
|
| |
| image_paths = ['image1.jpg', 'image2.jpg', 'image3.jpg'] |
| batch_size = len(image_paths) |
|
|
| |
| batch_images = np.vstack([preprocess_image(image_path) for image_path in image_paths]) |
|
|
| |
| input_name = session.get_inputs()[0].name |
|
|
| |
| outputs = session.run(None, {input_name: batch_images}) |
|
|
| |
| scores_batch, bboxes_batch, labels_batch = outputs[0], outputs[1], outputs[2] |
|
|
| |
| score_threshold = 0.5 |
|
|
| for i in range(batch_size): |
| scores = scores_batch[i] |
| bboxes = bboxes_batch[i] |
| labels = labels_batch[i] |
|
|
| |
| valid_indices = np.where(scores > score_threshold) |
|
|
| |
| filtered_scores = scores[valid_indices] |
| filtered_bboxes = bboxes[valid_indices] |
| filtered_labels = labels[valid_indices] |
|
|
| print(f"Image {i+1}:") |
| print("Filtered Scores:", filtered_scores) |
| print("Filtered Bounding Boxes:", filtered_bboxes) |
| print("Filtered Labels:", filtered_labels) |
| print('---') |
|
|
|
|
|
|