Views
No views yet
.pte export of ultralytics/yolo26m for CPU inference via the XNNPACK backend.yolo26m_xnnpack_q8.pte: ExecuTorch program (21.30 MB)(1, 3, 640, 640) - NCHW format, float32(1, 3, 640, 640)float32[-1.0, 1.0] or [0.0, 1.0] after preprocessing(1, 300, 6) - top 300 detections with [x1, y1, x2, y2, conf, cls]1import torch
2from executorch.runtime import Runtime
3
4# Load the .pte file
5with open("yolo26m_xnnpack_q8.pte", "rb") as f:
6 pte_buffer = f.read()
7
8# Create runtime and load method
9runtime = Runtime.get()
10program = runtime.load_program(pte_buffer)
11method = program.load_method("forward")
12
13# Prepare input (NCHW float32)
14input_tensor = torch.randn(1, 3, 640, 640)
15
16# Run inference
17outputs = method.execute([input_tensor])1import cv2
2import numpy as np
3import torch
4
5def preprocess_image(image_path, input_size=(640, 640)):
6 # Load image
7 img = cv2.imread(image_path)
8 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9
10 # Resize with padding (letterbox)
11 h, w = img.shape[:2]
12 scale = min(input_size[0] / h, input_size[1] / w)
13 new_h, new_w = int(h * scale), int(w * scale)
14
15 resized = cv2.resize(img, (new_w, new_h))
16 padded = np.full((input_size[0], input_size[1], 3), 114, dtype=np.uint8)
17 pad_h = (input_size[0] - new_h) // 2
18 pad_w = (input_size[1] - new_w) // 2
19 padded[pad_h:pad_h+new_h, pad_w:pad_w+new_w] = resized
20
21 # Convert to tensor and normalize
22 tensor = torch.from_numpy(padded).permute(2, 0, 1).float() / 255.0
23 tensor = tensor.unsqueeze(0) # Add batch dimension
24
25 return tensor
26
27input_tensor = preprocess_image("image.jpg")1import numpy as np
2
3def postprocess_detections(outputs, conf_threshold=0.25, iou_threshold=0.45):
4 # Convert model outputs to bounding boxes
5 # Args: outputs (Model output tuple), conf_threshold, iou_threshold
6 # Returns: List of detections with [x1, y1, x2, y2, confidence, class_id]
7
8 # The first output contains the main detections
9 detections = outputs[0][0] # (300, 6)
10
11 # Filter by confidence
12 mask = detections[:, 4] > conf_threshold
13 filtered = detections[mask]
14
15 # Apply NMS (simplified - use cv2.dnn.NMSBoxes for full implementation)
16 return filtered.cpu().numpy()(1, 3, 640, 640). For dynamic shapes, re-export with different dimensions.1import torch
2
3# img_hwc: float32 HWC image (e.g. RGB) in [0, 1]
4x = torch.from_numpy(img_hwc).permute(2, 0, 1).unsqueeze(0) # NCHW (often non-contiguous)
5x = x.contiguous() # IMPORTANT
6
7outputs = method.execute([x])Confidence range: [0.0004, 0.2012]
Detections: 0.contiguous()):Confidence range: [0.0001, 0.9589]
Detections: 12