Comments (8)
Hi Kris,
Thank you for reaching out and providing the details of your issue. Let's work together to resolve this.
Firstly, could you please confirm that you are using the latest versions of torch
and ultralytics
? You can update them using the following commands:
pip install --upgrade torch ultralytics
Next, to better understand and reproduce the issue, could you provide a minimum reproducible code example? This will help us investigate the problem more effectively. You can refer to our guide on creating a minimum reproducible example here: Minimum Reproducible Example.
Additionally, ensure that your hand_keypoint.yaml
file is correctly configured for the dataset you are using. Sometimes, configuration mismatches can lead to conversion errors.
Hereโs a refined version of your code snippet to ensure all parameters are correctly set:
import cv2
from ultralytics import YOLO
# Load the pretrained model
model = YOLO("model/best.pt")
# Define the image size
image_size = 224
# Export the model to INT8 TFLite format
model.export(format="tflite", imgsz=image_size, int8=True, data="hand_keypoint.yaml")
If the issue persists, please share the exact error message or any additional logs you receive. This information will be crucial for diagnosing the problem.
Looking forward to your response so we can get this resolved! ๐
from ultralytics.
Hi @glenn-jocher ,
I change to latest torch and ultralytics, and converting is successful.
Thanks,
Kris
from ultralytics.
Hi @glenn-jocher ,
I want to use the int8 tflite model to do the inference to see the result, but face the error.
How can I use API to inference the hand-pose int8 tflite model which input size is 224?
import cv2
from ultralytics import YOLO
import os
tflite_model = "model/best_saved_model_yolov8n_hand_pose_imgz_224_tflite/best_full_integer_quant.tflite"
model = YOLO(tflite_model)
# cap = cv2.VideoCapture("sample.mp4")
image_size = 224
folder = "./test_image_hand"
for filename in os.listdir(folder):
frame = cv2.imread(os.path.join(folder,filename))
# frame = cv2.resize(frame, (320, 240))
frame = cv2.resize(frame, (image_size, image_size))
results = model(frame)
annotated_frame = results[0].plot()
cv2.namedWindow("YOLOv8 Inference", cv2.WINDOW_NORMAL)
cv2.imshow("YOLOv8 Inference", annotated_frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
Thanks,
Kris
from ultralytics.
@kris-himax hi Kris,
Thank you for reaching out and providing the details of your issue. It's great to hear that you successfully converted your model to INT8 TFLite format! Let's address the inference part now.
To perform inference using a TFLite model, you need to use the TensorFlow Lite Interpreter. Here's an example of how you can modify your code to load and run inference with a TFLite model:
import cv2
import os
import numpy as np
import tensorflow as tf
# Load the TFLite model
tflite_model_path = "model/best_saved_model_yolov8n_hand_pose_imgz_224_tflite/best_full_integer_quant.tflite"
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define the image size
image_size = 224
# Function to preprocess the image
def preprocess_image(image_path):
image = cv2.imread(image_path)
image = cv2.resize(image, (image_size, image_size))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = np.expand_dims(image, axis=0).astype(np.float32)
return image
# Function to run inference
def run_inference(image):
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
return output_data
# Folder containing test images
folder = "./test_image_hand"
for filename in os.listdir(folder):
image_path = os.path.join(folder, filename)
image = preprocess_image(image_path)
# Run inference
results = run_inference(image)
# Process results (this part will depend on your specific model's output format)
# For demonstration, let's assume the results contain bounding box coordinates
# and you need to draw them on the image
for result in results:
# Assuming result contains [x, y, w, h]
x, y, w, h = result
x1, y1 = int(x - w / 2), int(y - h / 2)
x2, y2 = int(x + w / 2), int(y + h / 2)
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display the image with annotations
cv2.imshow("YOLOv8 Inference", image[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
This code snippet demonstrates how to load a TFLite model, preprocess images, run inference, and display the results. Please adjust the result processing part according to your specific model's output format.
If you encounter any further issues or have additional questions, feel free to ask. We're here to help! ๐
from ultralytics.
Hi @glenn-jocher ,
Thank you. It works fine.
from ultralytics.
@kris-himax hi Kris,
That's fantastic to hear! ๐ If you have any more questions or run into any other issues, feel free to reach out. We're here to help!
from ultralytics.
i ran the code that you provided
import cv2
import os
import numpy as np
import tensorflow as tf
# Load the TFLite model
tflite_model_path = <path_to_model>
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define the image size
image_size = 224
# Function to preprocess the image
def preprocess_image(image_path):
image = cv2.imread(image_path)
image = cv2.resize(image, (image_size, image_size))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = np.expand_dims(image, axis=0).astype(np.float32)
return image
# Function to run inference
def run_inference(image):
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
return output_data
# Folder containing test images
folder = <folder>
for filename in os.listdir(folder):
image_path = os.path.join(folder, filename)
image = preprocess_image(image_path)
# Run inference
results = run_inference(image)
# Process results (this part will depend on your specific model's output format)
# For demonstration, let's assume the results contain bounding box coordinates
# and you need to draw them on the image
for result in results:
# Assuming result contains [x, y, w, h]
x, y, w, h = result
x1, y1 = int(x - w / 2), int(y - h / 2)
x2, y2 = int(x + w / 2), int(y + h / 2)
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display the image with annotations
cv2.imshow("YOLOv8 Inference", image[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
and i am facing this error
{
"name": "ValueError",
"message": "too many values to unpack (expected 4)",
"stack": "---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[129], line 48
46 for result in results:
47 # Assuming result contains [x, y, w, h]
---> 48 x, y, w, h = result
49 x1, y1 = int(x - w / 2), int(y - h / 2)
50 x2, y2 = int(x + w / 2), int(y + h / 2)
ValueError: too many values to unpack (expected 4)"
}
for my converted tflite model, the output shape is (1, 5, 8400), i have no idea where to retrieve the bounding box coordinate nor the bounding box confidence score given with this output shape
can you kindly assist on this? thanks in advance
from ultralytics.
Hi @JellyJ98,
Thank you for providing the detailed error message and the code snippet. It looks like the output shape of your TFLite model is (1, 5, 8400)
, which indicates that the model is outputting a tensor with 5 values for each of the 8400 anchor points. These values typically include the bounding box coordinates, confidence score, and class probabilities.
To properly parse the output, you need to reshape and interpret these values correctly. Hereโs an updated version of your code to handle the output format:
import cv2
import os
import numpy as np
import tensorflow as tf
# Load the TFLite model
tflite_model_path = "model/best_saved_model_yolov8n_hand_pose_imgz_224_tflite/best_full_integer_quant.tflite"
interpreter = tf.lite.Interpreter(model_path=tflite_model_path)
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define the image size
image_size = 224
# Function to preprocess the image
def preprocess_image(image_path):
image = cv2.imread(image_path)
image = cv2.resize(image, (image_size, image_size))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = np.expand_dims(image, axis=0).astype(np.float32)
return image
# Function to run inference
def run_inference(image):
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
return output_data
# Function to process the output
def process_output(output_data, threshold=0.5):
output_data = np.squeeze(output_data)
boxes = []
for i in range(output_data.shape[1]):
x, y, w, h, conf = output_data[:, i]
if conf > threshold:
x1, y1 = int((x - w / 2) * image_size), int((y - h / 2) * image_size)
x2, y2 = int((x + w / 2) * image_size), int((y + h / 2) * image_size)
boxes.append((x1, y1, x2, y2, conf))
return boxes
# Folder containing test images
folder = "./test_image_hand"
for filename in os.listdir(folder):
image_path = os.path.join(folder, filename)
image = preprocess_image(image_path)
# Run inference
results = run_inference(image)
# Process results
boxes = process_output(results)
# Draw bounding boxes on the image
for (x1, y1, x2, y2, conf) in boxes:
cv2.rectangle(image[0], (x1, y1), (x2, y2), (0, 255, 0), 2)
# Display the image with annotations
cv2.imshow("YOLOv8 Inference", image[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
This code snippet includes a process_output
function that interprets the model's output and extracts bounding box coordinates and confidence scores. Adjust the threshold
parameter as needed to filter out low-confidence detections.
If you encounter any further issues or have additional questions, feel free to ask. We're here to help! ๐
from ultralytics.
Related Issues (20)
- yolov8 exported to openvino lacks .mapping file HOT 2
- Draw a mask on the original image based on the. txt file generated by yolov8 seg HOT 4
- Training problems for RT-DETR HOT 11
- How to increase inference speed in YoloV8 HOT 3
- Training Tracker in YOLO HOT 3
- RTDETR training error reported HOT 8
- The โclassesโ method in model.predict() seems to be not applicable to the yolov10 model. HOT 5
- Criteria for defining angles in labels and predictions (90ยบ vs. 135ยบ) HOT 1
- How to access the Batch Normalization of Yolov8? HOT 5
- --cache caches validation set and training set separately HOT 3
- Detect specific object and show XY prediction of that object HOT 3
- Can I load 2 or more models into 1 GPU for inference if I have enough GPU memory? HOT 1
- parse prediction from yolo8.onnx in opencv HOT 2
- Ignoring Corrupt Image/Label Error: Non-Normalized or Out of Bounds Coordinates in DOTA Dataset HOT 2
- Parse the output of tflite model HOT 4
- As the number of training iterations increases, the bounding boxes become larger. HOT 6
- AttributeError: Can't get attribute 'v10DetectLoss' on <module 'ultralytics.utils.loss' > HOT 1
- Yolo parameters HOT 1
- Yolo v10 prediction: error with predicted classes HOT 2
- Matching tag on classify validation HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ultralytics.