migraphx_infer.py 11.5 KB
Newer Older
zk's avatar
zk committed
1
2
3
4
5
import cv2
import numpy as np
import torch
import time
import os
zk's avatar
zk committed
6
import bisect
zk's avatar
zk committed
7
import migraphx
zk's avatar
zk committed
8
9
10
from typing import Tuple, List, Dict
import groundingdino.datasets.transforms as T
from PIL import Image
zk's avatar
zk committed
11
12

# =========================
zk's avatar
zk committed
13
# 预处理
zk's avatar
zk committed
14
# =========================
zk's avatar
zk committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def load_image(image_path: str) -> Tuple[np.array, torch.Tensor]:
    transform = T.Compose(
        [
            T.RandomResize([800], max_size=1333),
            T.ToTensor(),
            T.Normalize([0.485, 0.456, 0.406],
                        [0.229, 0.224, 0.225]),
        ]
    )

    image_source = Image.open(image_path).convert("RGB")
    image = np.asarray(image_source)
    image_transformed, _ = transform(image_source, None)

    return image, image_transformed


zk's avatar
zk committed
32
33
34
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

zk's avatar
zk committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

# =========================
# 文本标签还原逻辑 (移除 Tokenizer 依赖)
# =========================
def get_phrases_from_posmap(
    posmap: np.ndarray, tokens: List[str], left_idx: int = 0, right_idx: int = 255
):
    """
    直接用字符串列表映射,抛弃沉重的 Tokenizer
    """
    assert isinstance(posmap, np.ndarray), "posmap must be np.ndarray"
    if posmap.ndim == 1:
        # 将指定范围内的元素设为 False
        posmap[:left_idx + 1] = False
        posmap[right_idx:] = False

        # 获取非零元素的索引
        non_zero_idx = np.nonzero(posmap)[0]
        # 提取被激活的单词,并自动过滤掉特殊占位符
        words = [tokens[i] for i in non_zero_idx if tokens[i] not in ["[CLS]", "[SEP]", "."]]
        return " ".join(words).strip()
zk's avatar
zk committed
56
    else:
zk's avatar
zk committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        raise NotImplementedError("posmap must be 1-dim")


# =========================
# 分配输出 GPU 内存 (offload_copy=False 必须)
# =========================
def allocate_output_memory(model):
    output_data = {}
    for key in model.get_outputs().keys():
        output_data[key] = migraphx.allocate_gpu(
            s=model.get_outputs()[key]
        )
    return output_data

zk's avatar
zk committed
71
72

# =========================
zk's avatar
zk committed
73
# MIGraphX 模型类 
zk's avatar
zk committed
74
75
# =========================
class MIGraphXModel:
zk's avatar
zk committed
76
77
78
79
80
81
    def __init__(self,
                 onnx_path,
                 cache_path="../weights/ground_opt_0430.mxr",
                 device_id=3,
                 force_recompile=False):

zk's avatar
zk committed
82
83
84
        self.cache_path = cache_path

        if os.path.exists(cache_path) and not force_recompile:
zk's avatar
zk committed
85
            print(f"⚡ 直接加载缓存模型: {cache_path}")
zk's avatar
zk committed
86
87
            self.model = migraphx.load(cache_path)
        else:
zk's avatar
zk committed
88
89
90
91
92
93
94
95
96
97
98
99
            print("🔍 从 ONNX 构建模型")
            self.model = migraphx.parse_onnx(onnx_path)

            print("\n=== 输入信息 ===")
            for k, v in self.model.get_inputs().items():
                print(f"{k}: {v}")

            print("\n=== 输出信息 ===")
            for k, v in self.model.get_outputs().items():
                print(f"{k}: {v}")

            print("\n⚙️ 编译模型(GPU + offload=false)")
zk's avatar
zk committed
100
            self.model.compile(
zk's avatar
zk committed
101
102
103
                t=migraphx.get_target("gpu"),
                offload_copy=False,
                device_id=device_id
zk's avatar
zk committed
104
105
            )

zk's avatar
zk committed
106
            print(f"💾 保存 mxr: {cache_path}")
zk's avatar
zk committed
107
108
            migraphx.save(self.model, cache_path)

zk's avatar
zk committed
109
110
        self.inputs = self.model.get_inputs()
        self.outputs = self.model.get_outputs()
zk's avatar
zk committed
111
        self.param_names = self.model.get_parameter_names()
zk's avatar
zk committed
112
        
zk's avatar
zk committed
113
        print("✅ param_names:", self.param_names)
zk's avatar
zk committed
114
115
116
117
118
        print("✅ input_shape:", self.inputs)
        print("✅ output_shapes keys:", list(self.outputs.keys()))

        self.output_gpu = allocate_output_memory(self.model)
        print("✅ 模型初始化完成")
zk's avatar
zk committed
119
120

    def infer(self, input_dict):
zk's avatar
zk committed
121
122
123
124
125
126
127
        mgx_data = self.output_gpu.copy()

        for name in self.inputs.keys():
            data = input_dict[name]
            if data.dtype == np.float64:
                data = data.astype(np.float32)
            mgx_data[name] = migraphx.to_gpu(migraphx.argument(data))
zk's avatar
zk committed
128
129

        start = time.time()
zk's avatar
zk committed
130
        results = self.model.run(mgx_data)
zk's avatar
zk committed
131
132
        infer_time = time.time() - start

zk's avatar
zk committed
133
134
135
136
137
        outputs = [
            np.array(migraphx.from_gpu(r))
            for r in results
        ]

zk's avatar
zk committed
138
139
140
141
        return outputs, infer_time


# =========================
zk's avatar
zk committed
142
# 推理逻辑 (引入真正的后处理还原)
zk's avatar
zk committed
143
144
145
146
# =========================
def predict(
        model,
        image,
zk's avatar
zk committed
147
        text_cache,
zk's avatar
zk committed
148
149
        box_threshold,
        text_threshold,
zk's avatar
zk committed
150
        remove_combined=False,
zk's avatar
zk committed
151
        is_benchmark=False
zk's avatar
zk committed
152
153
154
) -> Tuple[np.ndarray, np.ndarray, List[str]]:
    
    # 使用传入的 text_cache 替代硬编码
zk's avatar
zk committed
155
156
    input_dict = {
        "img": np.expand_dims(np.asarray(image), axis=0).astype(np.float32),
zk's avatar
zk committed
157
158
159
160
161
        "input_ids": text_cache['input_ids'],
        "attention_mask": text_cache['attention_mask'],
        "position_ids": text_cache['position_ids'],
        "token_type_ids": text_cache['token_type_ids'],
        "text_token_mask": text_cache['text_token_mask']
zk's avatar
zk committed
162
163
164
165
166
    }

    outputs, infer_time = model.infer(input_dict)

    if not is_benchmark:
zk's avatar
zk committed
167
        print(f"Inference time: {infer_time:.3f}s")
zk's avatar
zk committed
168

zk's avatar
zk committed
169
170
171
172
    t0 = time.time()
    prediction_logits = sigmoid(outputs[0][0])
    prediction_boxes = outputs[1][0]
    post_time = time.time() - t0
zk's avatar
zk committed
173

zk's avatar
zk committed
174
175
176
177
178
179
180
181
182
183
    if not is_benchmark:
        print(f"post time: {post_time:.3f}s")
        print(f"\n=== Debug Info ===")
        print(f"Prediction logits shape: {prediction_logits.shape}")
        print(f"Prediction boxes shape: {prediction_boxes.shape}")
        print(f"Max logit value: {np.max(prediction_logits):.4f}")
        print(f"Mean logit value: {np.mean(prediction_logits):.4f}")

    # 1. 框过滤
    max_values = np.max(prediction_logits, axis=1)
zk's avatar
zk committed
184
185
    mask = max_values > box_threshold

zk's avatar
zk committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    logits = prediction_logits[mask]
    boxes = prediction_boxes[mask]

    tokens = text_cache['tokens']
    input_ids = text_cache['input_ids'][0].tolist()

    if remove_combined:
        sep_idx = [i for i in range(len(input_ids)) if input_ids[i] in [101, 102, 1012]]
        phrases = []
        for logit in logits:
            max_idx = logit.argmax()
            insert_idx = bisect.bisect_left(sep_idx, max_idx)
            right_idx = sep_idx[insert_idx]
            left_idx = sep_idx[insert_idx - 1]
            phrases.append(
                get_phrases_from_posmap(logit > text_threshold, tokens, left_idx, right_idx)
            )
    else:
        phrases = [
            get_phrases_from_posmap(logit > text_threshold, tokens)
            for logit in logits
        ]
zk's avatar
zk committed
208
209
210
211
212
213
214

    return boxes, np.max(logits, axis=1), phrases


# =========================
# Benchmark
# =========================
zk's avatar
zk committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def benchmark_performance(
    model, image, text_cache, box_threshold, text_threshold,
    warmup_runs=5, test_runs=10
):
    print("="*60)
    print("📊 开始性能测试(包含预热+实际推理)")
    print("="*60)

    print(f"\n🔥 预热阶段({warmup_runs} 次)- 不计入性能统计")
    warmup_start = time.time()
    for i in range(warmup_runs):
        t0 = time.time()
        predict(model, image, text_cache, box_threshold, text_threshold, is_benchmark=True)
        warmup_time = time.time() - t0
        print(f"预热 {i+1}/{warmup_runs} - 耗时: {warmup_time*1000:.2f} ms")
    total_warmup_time = time.time() - warmup_start
    print(f"\n预热完成 - 总耗时: {total_warmup_time:.3f} s, 平均每次: {total_warmup_time/warmup_runs*1000:.2f} ms")

    print(f"\n🚀 实际推理测试阶段({test_runs} 次)- 统计性能指标")
    test_start = time.time()
    infer_times = []  

    for i in range(test_runs):
        t0 = time.time()
        predict(model, image, text_cache, box_threshold, text_threshold, is_benchmark=True)
        infer_time = time.time() - t0
        infer_times.append(infer_time)
        print(f"实际推理 {i+1}/{test_runs} - 耗时: {infer_time*1000:.2f} ms")

    total_test_time = time.time() - test_start
    avg_infer_time = np.mean(infer_times)
    std_infer_time = np.std(infer_times)
    max_infer_time = np.max(infer_times)
    min_infer_time = np.min(infer_times)
    fps = test_runs / total_test_time

    print("\n" + "="*60)
    print("📈 性能测试报告(仅实际推理阶段)")
    print("="*60)
    print(f"测试次数: {test_runs} 次")
    print(f"总推理耗时: {total_test_time:.3f} s")
    print(f"平均推理耗时: {avg_infer_time*1000:.2f} ms (±{std_infer_time*1000:.2f} ms)")
    print(f"最大推理耗时: {max_infer_time*1000:.2f} ms")
    print(f"最小推理耗时: {min_infer_time*1000:.2f} ms")
    print(f"平均FPS: {fps:.2f} 帧/秒")
    print("="*60)

    return {
        "warmup_runs": warmup_runs,
        "test_runs": test_runs,
        "avg_infer_time_ms": avg_infer_time*1000,
        "std_infer_time_ms": std_infer_time*1000,
        "max_infer_time_ms": max_infer_time*1000,
        "min_infer_time_ms": min_infer_time*1000,
        "fps": fps
    }
zk's avatar
zk committed
271
272
273
274
275
276
277


# =========================
# 主函数
# =========================
if __name__ == "__main__":

zk's avatar
zk committed
278
279
    model_path = "../weights/ground_opt_0601.onnx"
    cache_path = "../weights/ground_opt_0601.mxr"
zk's avatar
zk committed
280
    img_path = "../images/in/car_1.jpg"
zk's avatar
zk committed
281
282
283

    BOX_TRESHOLD = 0.35
    TEXT_TRESHOLD = 0.25
zk's avatar
zk committed
284
285
286
    
    WARMUP_RUNS = 5
    TEST_RUNS = 10
zk's avatar
zk committed
287
288
289
290

    model = MIGraphXModel(
        model_path,
        cache_path=cache_path,
zk's avatar
zk committed
291
        device_id=2,
zk's avatar
zk committed
292
        force_recompile=False 
zk's avatar
zk committed
293
294
295
296
    )

    image_source, image = load_image(img_path)

zk's avatar
zk committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
    # =========================
    # 提前计算得到的 Text Cache
    # =========================
    TEXT_CACHE = {
        'input_ids': np.array([[ 101, 2482, 1012,  102]], dtype=np.int64),
        'attention_mask': np.array([[ True,  True,  True,  True]], dtype=np.bool_),
        'position_ids': np.array([[0, 0, 1, 0]], dtype=np.int64),
        'token_type_ids': np.array([[0, 0, 0, 0]], dtype=np.int64),
        'text_token_mask': np.array([[[ True, False, False, False],
                                      [False,  True,  True, False],
                                      [False,  True,  True, False],
                                      [False, False, False,  True]]], dtype=np.bool_),
        # 存放 ID 对应的单词,用于快速 decode
        'tokens': ["[CLS]", "car", ".", "[SEP]"]
    }

    benchmark_performance(
        model, image, TEXT_CACHE, 
        BOX_TRESHOLD, TEXT_TRESHOLD,
        WARMUP_RUNS, TEST_RUNS
    )
zk's avatar
zk committed
318

zk's avatar
zk committed
319
320
321
322
323
    print("\n" + "="*60)
    print("🎯 执行最终推理(带详细日志+保存结果)")
    print("="*60)
    
    # 传入 TEXT_CACHE
zk's avatar
zk committed
324
    boxes, confs, phrases = predict(
zk's avatar
zk committed
325
326
        model, image, TEXT_CACHE,
        BOX_TRESHOLD, TEXT_TRESHOLD
zk's avatar
zk committed
327
328
    )

zk's avatar
zk committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    print("\n🎯 执行最终推理并保存结果图")
    ori_img = cv2.imread(img_path)
    img_h = ori_img.shape[0]
    img_w = ori_img.shape[1]
    
    for i in range(len(boxes)):
        one_box = boxes[i]
        one_conf = confs[i]
        one_cls = phrases[i]
        
        x1 = int((one_box[0] - one_box[2] / 2) * img_w)
        y1 = int((one_box[1] - one_box[3] / 2) * img_h)
        x2 = int((one_box[0] + one_box[2] / 2) * img_w)
        y2 = int((one_box[1] + one_box[3] / 2) * img_h)
        
        cv2.rectangle(ori_img, (x1, y1), (x2, y2), (0, 0, 255), 2)
        
        # 此时打印的 one_cls 将是真实的类别名称(如 "car")
        cv2.putText(
            ori_img, f'{one_cls} {one_conf:.2f}',  
            (x1-15, y1-15), 
            fontFace=cv2.FONT_HERSHEY_SIMPLEX, 
            color=(255, 255, 255), 
            fontScale=1.5, 
            thickness=3
        )

    cv2.imwrite('../weights/result_migraphx.jpg', ori_img)
    print(f"\n✅ 结果已保存至: ../weights/result_migraphx.jpg")
    print(f"✅ 检测到目标: {phrases} (共 {len(boxes)} 个)")