Converting a YOLO model from PyTorch to TensorFlow and running inference involves several steps. While Ultralytics YOLO primarily supports PyTorch, you can use intermediate formats like ONNX or leverage experimental export options. Here's a step-by-step guide:


Step 1: Export PyTorch Model to ONNX

First, export your PyTorch model to ONNX format using Ultralytics' built-in exporter:

from ultralytics import YOLO

model = YOLO("yolo11m-pose.pt")
model.export(format="onnx")  # Exports to "yolo11m-pose.onnx"


Step 2: Convert ONNX to TensorFlow

Install onnx-tf to convert the ONNX model to TensorFlow:

pip install onnx onnx-tf

Then convert the model:

import onnx
from onnx_tf.backend import prepare

onnx_model = onnx.load("yolo11m-pose.onnx")
tf_rep = prepare(onnx_model)
tf_rep.export_graph("yolo11m-pose_tf")  # Outputs TensorFlow SavedModel


Step 3: Run Inference with TensorFlow

Load the TensorFlow model and run inference on your video:

import tensorflow as tf
import cv2
import numpy as np

# Load the TensorFlow SavedModel
model = tf.saved_model.load("yolo11m-pose_tf")
infer = model.signatures["serving_default"]

# Process video
cap = cv2.VideoCapture("real.mp4")
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Preprocess frame (resize, normalize, etc.)
    img = cv2.resize(frame, (640, 640))  # Adjust to model input size
    img = img / 255.0  # Normalize
    img = np.expand_dims(img, axis=0).astype(np.float32)

    # Run inference
    outputs = infer(tf.constant(img))

    # Post-process outputs (extract keypoints, boxes, etc.)
    # Note: Output parsing depends on your model's architecture!
    keypoints = outputs["output0"].numpy()  # Adjust "output0" to match your model

    # Visualize results (use OpenCV or other tools)
    cv2.imshow("Inference", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


Key Notes

  1. Output Parsing: The output tensor names (output0, output1, etc.) depend on the model architecture. Inspect the model's output signature using:

    print(model.signatures["serving_default"].structured_outputs)
    
    
  2. Pre/Post-Processing: You may need to replicate Ultralytics' preprocessing (e.g., letterboxing) and postprocessing (e.g., NMS, keypoint extraction) in TensorFlow.

  3. Performance: TensorFlow may not handle video inference as seamlessly as PyTorch with Ultralytics. Consider using TensorFlow Lite (model.export(format='tflite')) for edge deployment.