-
-
Notifications
You must be signed in to change notification settings - Fork 15.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tflite inference with tflite_runtime #11395
Comments
👋 Hello @Vikram12301, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution. If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it. If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results. RequirementsPython>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started: git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on MacOS, Windows, and Ubuntu every 24 hours and on every commit. Introducing YOLOv8 🚀We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀! Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects. Check out our YOLOv8 Docs for details and get started with: pip install ultralytics |
@Vikram12301 based on the code you've provided, it appears that you are trying to run inference on a TensorFlow Lite model using the It is recommended to check if your TensorFlow Lite model was exported with the same input and output dimensions and data types as the PyTorch model you're comparing it to. Additionally, you could use the If you still face issues, you can share more details such as the TensorFlow Lite model file, and we will try our best to help you. |
@glenn-jocher The output for 'interpreter.get_output_details()' is [{'name': 'StatefulPartitionedCall:0', But the output for the below code
Speed: 8.6ms pre-process, 577.2ms inference, 2.8ms NMS per image at shape (1, 3, 640, 640) The same .tflite is working when using torch.hub and not working with tf.lite.Interpreter |
@Vikram12301 it seems like the difference in output between your TensorFlow Lite model and the PyTorch model could be caused by differences in the graphic libraries used between your two models (especially if they use different backends). To further investigate this, I recommend checking the output shape, executing the inference with Here is an example of how you can get the output tensor shape for the same image
Please let me know if you have any additional questions. |
@glenn-jocher I followed the above-mentioned steps(I am using Google Colab for all the experiments)
I got this as the output: So how do I rectify this to get the right output with tflite using tf.lite.Interpreter |
@Vikram12301 Great! It looks like the shape of the output tensor from the PyTorch model is Here's some sample code that you can substitute in your notebook to transform the output of your TensorFlow Lite model:
I hope this helps. Let me know if you have any other questions! |
@glenn-jocher I actually want the output of model(image) and not the output of model.forward(image), The expected output is (xmin,xmax,ymin,ymax,conf,class) as in |
@Vikram12301 If you want to get the output of
This should give you the expected output shape of |
But why do I get an output of shape (1, 25200, 6), when model(image) gives me a tensor like this tensor([[5.01359e+02, 1.09255e+03, 6.92128e+02, 1.11753e+03, 9.24892e-01, 0.00000e+00]]) What does 25200 rows contain, in the above result and how to remove the rows which are not required to get a single row output like above?? |
Also the output for the code which you mentioned is giving the below result [[-9.8392916e-01 6.0772041e+01 1.3479447e+01 9.9844486e-01 How can class be floating? and how do I remove the other rows and get a clean output as I get in model(image)?? |
@Vikram12301, I apologize for the confusion earlier. If your expected output has a single row like
This code will give you an output of the form |
The above code gave me an output and not Can you please check from your side? |
@glenn-jocher Also, there are some rows, which has class = 3 in the above output, The trained model does not have those many classes, So there is something which is completely being missed |
@glenn-jocher Also, in this issue, you have mentioned not to use tensors during inference, That can be managed in torch.hub by passing the cv2 or PIL image, But how can this be handled while using tflite, Because, in that case we have to use a tensor |
@Vikram12301 I apologize for the confusion earlier. I made an error in my previous response. The expected output format of
To handle the use of tensors during inference, you can preprocess the image and run inference on the tensor, then post-process the output to get the desired format. Regarding the issue of unexpected classes in the output, you are correct that this indicates that there is something unexpected happening during inference. I suggest you take a closer look at the trained model and the data to determine why this is happening. |
How to apply post-processing to the output? Is there some simple way to do it? |
@Vikram12301 post-processing is the process of refining the output of the model to obtain the final detections. Here are some common post-processing steps used for object detection:
In your case, since you are using a TensorFlow Lite model, you can do the post-processing using NMS and filtering in TensorFlow Lite. Here's how you can do it:
This code applies NMS with an intersection over union (IOU) threshold of 0.45 and a confidence score threshold of |
@glenn-jocher Assuming that the above code works, yet there are multiple integers for the class which the object belongs to, but the model which I trained, has only one target class. So how to identify the classes which do not belong to my target class? |
👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help. For additional resources and information, please see the links below:
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed! Thank you for your contributions to YOLO 🚀 and Vision AI ⭐ |
I converted yolov8n to tflite.
Can you help, how to solve this ? |
@naseemap47 hi! Based on the output image you provided, it looks like the bounding boxes are not correctly aligned with the objects in the image. Here are a few things you can try:
I hope these suggestions help! Good luck with your project. |
heres a full example for a model that has 2 classes `import cv2 model_path = "/Users/brett/Desktop/last-fp16.tflite" def save_tensor_as_numpy(tensor, filename):
def non_max_suppression(
def from_numpy(x): def xywh2xyxy(x): def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
def box_iou(box1, box2, eps=1e-7): def fwd(interpreter, im):
Load TFLite model and allocate tensors.interpreter = tflite.Interpreter(model_path=model_path) Get input and output tensors.input_details = interpreter.get_input_details() Load the image and preprocess itimage = cv2.imread(img_path) Resize the image to 640x640image_resized, _, _ = letterbox(image, new_shape=640, auto=False, stride=32) Preprocess the image for the modelinput_shape = input_details[0]['shape'] interpreter.set_tensor(input_details[0]['index'], input_data) Run inference.output_data = fwd(interpreter, input_data) Apply NMSnms_preds = non_max_suppression(predictions, max_det=1000) print(nms_preds) Print the result and plot bounding boxesclasses = ['ball', 'hoop'] Show the imagecv2.imshow('image', image_resized) ` |
@bdytx5 it seems that you are implementing custom post-processing and visualization of object detection results for a TensorFlow Lite model. The code you provided performs the following steps:
The code is utilizing PyTorch and OpenCV for custom NMS and visualization, which is not the standard practice for TensorFlow Lite models. Your implementation combines elements from different frameworks (TFLite, PyTorch, OpenCV) for inference, post-processing, and visualization. It's important to ensure compatibility and consistency across these different components. Double-check that the bounding box coordinates and the class indices align correctly with the model's output format. If you encounter issues with the bounding box visualization, I would suggest verifying that the detected bounding box coordinates are correctly transformed and mapped onto the original image. Also, make sure that the class indices align with your expected classes. Verify that the classes are correctly mapped to the class names and that the output classes correspond to the classes expected from the trained model. Finally, consider using the TFLite Interpreter's built-in post-processing and visualization methods, if available, for a more streamlined and consistent workflow. If you encounter specific issues or have further questions, feel free to provide additional details, and I'd be happy to assist further. |
Search before asking
Question
Using the above code for inference. But getting a random array as output. But I am getting the expected answer as output when inferencing with torch.hub
Expected result:
tensor([[5.01359e+02, 1.09255e+03, 6.92128e+02, 1.11753e+03, 9.24892e-01, 0.00000e+00]])
Result I got for the above code:
[[[ 0.0053059 0.004525 0.014499 0.011126 3.7081e-05 0.99995]
[ 0.0052993 0.0029876 0.021002 0.013376 4.0981e-05 0.99999]
[ 0.0054641 0.005734 0.0095944 0.034277 8.8067e-06 0.99998]
...
[ 0.97123 0.96511 0.026191 0.075059 2.2332e-07 0.99999]
[ 0.96813 0.95563 0.0322 0.20285 2.2449e-07 0.99999]
[ 0.94753 0.98328 0.17076 0.02539 2.7026e-06 0.99998]]]
Additional
No response
The text was updated successfully, but these errors were encountered: