Giter VIP home page Giter VIP logo

Comments (8)

glenn-jocher avatar glenn-jocher commented on June 25, 2024

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.

kris-himax avatar kris-himax commented on June 25, 2024

Hi @glenn-jocher ,

I change to latest torch and ultralytics, and converting is successful.

Thanks,
Kris

from ultralytics.

kris-himax avatar kris-himax commented on June 25, 2024

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?
Screenshot from 2024-06-12 17-19-07

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.

glenn-jocher avatar glenn-jocher commented on June 25, 2024

@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.

kris-himax avatar kris-himax commented on June 25, 2024

Hi @glenn-jocher ,

Thank you. It works fine.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on June 25, 2024

@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.

JellyJ98 avatar JellyJ98 commented on June 25, 2024

hi @glenn-jocher

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.

glenn-jocher avatar glenn-jocher commented on June 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.