1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| """ SNPE DMS 推理
在高通平台上运行 DMS 模型 """
import numpy as np import snpe from snpe import SnpeContext, SnpeRuntime
class SnapdragonDMS: """ 高通 Snapdragon DMS 基于 SNPE 框架 """ def __init__(self, dlc_path: str, runtime: str = 'GPU'): """ 初始化 Args: dlc_path: DLC 模型路径 runtime: 运行时 ('CPU', 'GPU', 'DSP') """ self.context = SnpeContext(dlc_path) runtime_map = { 'CPU': SnpeRuntime.CPU, 'GPU': SnpeRuntime.GPU, 'DSP': SnpeRuntime.DSP } self.runtime = runtime_map.get(runtime, SnpeRuntime.GPU) self.input_name = 'input' self.output_names = ['landmarks', 'eye_openness', 'gaze', 'state'] def infer(self, image: np.ndarray) -> dict: """ 推理 Args: image: 输入图像 (H, W, C), RGB格式 Returns: result: { 'landmarks': np.ndarray, 'eye_openness': np.ndarray, 'gaze': np.ndarray, 'state': np.ndarray } """ input_tensor = self._preprocess(image) inputs = {self.input_name: input_tensor} outputs = self.context.execute(inputs, self.runtime) result = self._postprocess(outputs) return result def _preprocess(self, image: np.ndarray) -> np.ndarray: """ 预处理 Args: image: (H, W, C), uint8, RGB Returns: tensor: (1, C, H, W), float32 """ import cv2 image = cv2.resize(image, (224, 224)) image = image.astype(np.float32) / 255.0 image = image.transpose(2, 0, 1) image = np.expand_dims(image, 0) return image def _postprocess(self, outputs: dict) -> dict: """后处理""" result = {} landmarks = outputs['landmarks'].squeeze() result['landmarks'] = landmarks.reshape(-1, 2) result['eye_openness'] = outputs['eye_openness'].squeeze() result['gaze'] = outputs['gaze'].squeeze() result['state'] = outputs['state'].squeeze() return result def benchmark(self, num_iterations: int = 100) -> dict: """ 性能基准测试 Returns: stats: { 'mean_latency_ms': float, 'std_latency_ms': float, 'fps': float } """ import time dummy_input = np.random.randn(1, 3, 224, 224).astype(np.float32) for _ in range(10): self.context.execute({self.input_name: dummy_input}, self.runtime) latencies = [] for _ in range(num_iterations): start = time.time() self.context.execute({self.input_name: dummy_input}, self.runtime) latencies.append((time.time() - start) * 1000) return { 'mean_latency_ms': np.mean(latencies), 'std_latency_ms': np.std(latencies), 'p99_latency_ms': np.percentile(latencies, 99), 'fps': 1000 / np.mean(latencies) }
if __name__ == "__main__": dms = SnapdragonDMS('dms_quantized.dlc', runtime='GPU') image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) result = dms.infer(image) print(f"关键点形状: {result['landmarks'].shape}") print(f"眼睑开度: {result['eye_openness']}") print(f"视线方向: {result['gaze']}") stats = dms.benchmark(100) print(f"平均延迟: {stats['mean_latency_ms']:.2f} ms") print(f"FPS: {stats['fps']:.1f}")
|