File size: 11,344 Bytes
d8c26e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
359
#!/bin/bash
# TRT 加速检测器性能对比实验 - 一键运行脚本
# 对比 DeCLIP (csa模式) vs CLIP (vanilla模式) 在 TRT 加速前后的速度和精度

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# ============================================================
# 配置
# ============================================================
DECLIP_ROOT="$(dirname "$SCRIPT_DIR")"
CHECKPOINT_DIR="/mnt/SSD8T/home/wjj/code/my_CLIPSelf/checkpoints"
COCO_ROOT="/mnt/SSD8T/home/wjj/dataset/standard_coco"

# 模型检查点
EVACLIP_B16="${CHECKPOINT_DIR}/EVA02_CLIP_B_psz16_s8B.pt"
EVACLIP_L14="${CHECKPOINT_DIR}/EVA02_CLIP_L_psz14_336_s8B.pt"

# 测试配置
IMAGE_SIZE=560
BATCH_SIZE=1
WARMUP=10
ITERATIONS=100

# 输出目录
OUTPUT_DIR="results"
ENGINE_DIR="engines"

# ============================================================
# 函数
# ============================================================
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

check_dependencies() {
    log "Checking dependencies..."
    
    python3 -c "import torch; print(f'PyTorch: {torch.__version__}')"
    python3 -c "import tensorrt; print(f'TensorRT: {tensorrt.__version__}')" 2>/dev/null || echo "Warning: TensorRT not installed"
    python3 -c "import onnx; print(f'ONNX: {onnx.__version__}')" 2>/dev/null || echo "Warning: ONNX not installed"
    python3 -c "import onnxruntime; print(f'ONNX Runtime: {onnxruntime.__version__}')" 2>/dev/null || echo "Warning: ONNX Runtime not installed"
    
    # 检查 CUDA
    if command -v nvidia-smi &> /dev/null; then
        nvidia-smi --query-gpu=name,memory.total,driver_version --format=csv,noheader | head -1
    fi
}

convert_models() {
    log "Converting models to TensorRT..."
    
    mkdir -p "$ENGINE_DIR"
    
    # CLIP (vanilla mode)
    log "Converting CLIP (vanilla mode)..."
    python3 convert_model.py \
        --model-type clip \
        --mode vanilla \
        --model-name EVA02-CLIP-B-16 \
        --checkpoint "$EVACLIP_B16" \
        --image-size $IMAGE_SIZE \
        --output-dir "$ENGINE_DIR" \
        --output-name clip_vanilla_${IMAGE_SIZE} \
        --fp16 \
        --dynamic \
        --verify || log "Warning: CLIP conversion failed"
    
    # DeCLIP (csa mode)
    log "Converting DeCLIP (csa mode)..."
    python3 convert_model.py \
        --model-type declip \
        --mode csa \
        --model-name EVA02-CLIP-B-16 \
        --checkpoint "$EVACLIP_B16" \
        --image-size $IMAGE_SIZE \
        --output-dir "$ENGINE_DIR" \
        --output-name declip_csa_${IMAGE_SIZE} \
        --fp16 \
        --dynamic \
        --verify || log "Warning: DeCLIP conversion failed"
}

benchmark_speed() {
    log "Running speed benchmarks..."
    
    mkdir -p "$OUTPUT_DIR"
    
    # PyTorch benchmarks
    log "Benchmarking PyTorch (vanilla mode)..."
    python3 benchmark_speed.py \
        --pytorch-model "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode vanilla \
        --image-size $IMAGE_SIZE $IMAGE_SIZE \
        --batch-size $BATCH_SIZE \
        --warmup $WARMUP \
        --iterations $ITERATIONS \
        --output "$OUTPUT_DIR/speed_pytorch_vanilla.json" || true
    
    log "Benchmarking PyTorch (csa mode)..."
    python3 benchmark_speed.py \
        --pytorch-model "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode csa \
        --image-size $IMAGE_SIZE $IMAGE_SIZE \
        --batch-size $BATCH_SIZE \
        --warmup $WARMUP \
        --iterations $ITERATIONS \
        --output "$OUTPUT_DIR/speed_pytorch_csa.json" || true
    
    log "Benchmarking PyTorch FP16 (vanilla mode)..."
    python3 benchmark_speed.py \
        --pytorch-model "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode vanilla \
        --image-size $IMAGE_SIZE $IMAGE_SIZE \
        --batch-size $BATCH_SIZE \
        --warmup $WARMUP \
        --iterations $ITERATIONS \
        --fp16 \
        --output "$OUTPUT_DIR/speed_pytorch_vanilla_fp16.json" || true
    
    log "Benchmarking PyTorch FP16 (csa mode)..."
    python3 benchmark_speed.py \
        --pytorch-model "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode csa \
        --image-size $IMAGE_SIZE $IMAGE_SIZE \
        --batch-size $BATCH_SIZE \
        --warmup $WARMUP \
        --iterations $ITERATIONS \
        --fp16 \
        --output "$OUTPUT_DIR/speed_pytorch_csa_fp16.json" || true
    
    # TensorRT benchmarks
    if [ -f "$ENGINE_DIR/clip_vanilla_${IMAGE_SIZE}_fp16.engine" ]; then
        log "Benchmarking TensorRT (vanilla mode)..."
        python3 benchmark_speed.py \
            --trt-engine "$ENGINE_DIR/clip_vanilla_${IMAGE_SIZE}_fp16.engine" \
            --mode vanilla \
            --image-size $IMAGE_SIZE $IMAGE_SIZE \
            --batch-size $BATCH_SIZE \
            --warmup $WARMUP \
            --iterations $ITERATIONS \
            --output "$OUTPUT_DIR/speed_trt_vanilla_fp16.json" || true
    fi
    
    if [ -f "$ENGINE_DIR/declip_csa_${IMAGE_SIZE}_fp16.engine" ]; then
        log "Benchmarking TensorRT (csa mode)..."
        python3 benchmark_speed.py \
            --trt-engine "$ENGINE_DIR/declip_csa_${IMAGE_SIZE}_fp16.engine" \
            --mode csa \
            --image-size $IMAGE_SIZE $IMAGE_SIZE \
            --batch-size $BATCH_SIZE \
            --warmup $WARMUP \
            --iterations $ITERATIONS \
            --output "$OUTPUT_DIR/speed_trt_csa_fp16.json" || true
    fi
}

eval_accuracy() {
    log "Running accuracy evaluations..."
    
    mkdir -p "$OUTPUT_DIR"
    
    # PyTorch evaluations
    log "Evaluating PyTorch (vanilla mode)..."
    python3 eval_panoptic.py \
        --backend pytorch \
        --checkpoint "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode vanilla \
        --coco-root "$COCO_ROOT" \
        --image-size $IMAGE_SIZE \
        --output "$OUTPUT_DIR/accuracy_pytorch_vanilla.json" || true
    
    log "Evaluating PyTorch (csa mode)..."
    python3 eval_panoptic.py \
        --backend pytorch \
        --checkpoint "$EVACLIP_B16" \
        --model-name EVA02-CLIP-B-16 \
        --mode csa \
        --coco-root "$COCO_ROOT" \
        --image-size $IMAGE_SIZE \
        --output "$OUTPUT_DIR/accuracy_pytorch_csa.json" || true
    
    # TensorRT evaluations
    if [ -f "$ENGINE_DIR/clip_vanilla_${IMAGE_SIZE}_fp16.engine" ]; then
        log "Evaluating TensorRT (vanilla mode)..."
        python3 eval_panoptic.py \
            --backend tensorrt \
            --engine "$ENGINE_DIR/clip_vanilla_${IMAGE_SIZE}_fp16.engine" \
            --mode vanilla \
            --coco-root "$COCO_ROOT" \
            --image-size $IMAGE_SIZE \
            --output "$OUTPUT_DIR/accuracy_trt_vanilla.json" || true
    fi
    
    if [ -f "$ENGINE_DIR/declip_csa_${IMAGE_SIZE}_fp16.engine" ]; then
        log "Evaluating TensorRT (csa mode)..."
        python3 eval_panoptic.py \
            --backend tensorrt \
            --engine "$ENGINE_DIR/declip_csa_${IMAGE_SIZE}_fp16.engine" \
            --mode csa \
            --coco-root "$COCO_ROOT" \
            --image-size $IMAGE_SIZE \
            --output "$OUTPUT_DIR/accuracy_trt_csa.json" || true
    fi
}

summarize_results() {
    log "Summarizing results..."
    
    python3 << 'EOF'
import json
import os
from pathlib import Path

results_dir = Path("results")
if not results_dir.exists():
    print("No results found")
    exit(0)

print("\n" + "="*80)
print("EXPERIMENT SUMMARY")
print("="*80)

# Speed results
print("\n## Speed Benchmark Results")
print("-"*80)
print(f"{'Model':<30} {'Backend':<12} {'Precision':<10} {'Latency (ms)':<15} {'FPS':<10}")
print("-"*80)

for json_file in sorted(results_dir.glob("speed_*.json")):
    try:
        with open(json_file) as f:
            data = json.load(f)
            if isinstance(data, list):
                data = data[0]
            print(f"{data.get('model_name', 'N/A')[:30]:<30} "
                  f"{data.get('backend', 'N/A'):<12} "
                  f"{data.get('precision', 'N/A'):<10} "
                  f"{data.get('latency_mean_ms', 0):.2f}±{data.get('latency_std_ms', 0):.2f}{'':>4} "
                  f"{data.get('throughput_fps', 0):.1f}")
    except Exception as e:
        print(f"Error reading {json_file}: {e}")

# Accuracy results
print("\n## Accuracy Evaluation Results (COCO Panoptic)")
print("-"*80)
print(f"{'Model':<30} {'Backend':<12} {'Mode':<8} {'Thing mAcc@1':<12} {'Stuff mAcc@1':<12}")
print("-"*80)

for json_file in sorted(results_dir.glob("accuracy_*.json")):
    try:
        with open(json_file) as f:
            data = json.load(f)
            print(f"{data.get('model_name', 'N/A')[:30]:<30} "
                  f"{data.get('backend', 'N/A'):<12} "
                  f"{data.get('mode', 'N/A'):<8} "
                  f"{data.get('rois_thing_macc1', 0)*100:.2f}%{'':>6} "
                  f"{data.get('rois_stuff_macc1', 0)*100:.2f}%")
    except Exception as e:
        print(f"Error reading {json_file}: {e}")

print("="*80)
EOF
}

# ============================================================
# 主流程
# ============================================================
main() {
    log "Starting TRT Benchmark Experiment"
    log "=============================="
    
    # 解析参数
    SKIP_CONVERT=false
    SKIP_SPEED=false
    SKIP_ACCURACY=false
    
    while [[ $# -gt 0 ]]; do
        case $1 in
            --skip-convert)
                SKIP_CONVERT=true
                shift
                ;;
            --skip-speed)
                SKIP_SPEED=true
                shift
                ;;
            --skip-accuracy)
                SKIP_ACCURACY=true
                shift
                ;;
            --convert-only)
                SKIP_SPEED=true
                SKIP_ACCURACY=true
                shift
                ;;
            --speed-only)
                SKIP_CONVERT=true
                SKIP_ACCURACY=true
                shift
                ;;
            --accuracy-only)
                SKIP_CONVERT=true
                SKIP_SPEED=true
                shift
                ;;
            --help)
                echo "Usage: $0 [options]"
                echo ""
                echo "Options:"
                echo "  --skip-convert    Skip model conversion"
                echo "  --skip-speed      Skip speed benchmark"
                echo "  --skip-accuracy   Skip accuracy evaluation"
                echo "  --convert-only    Only run model conversion"
                echo "  --speed-only      Only run speed benchmark"
                echo "  --accuracy-only   Only run accuracy evaluation"
                echo "  --help            Show this help"
                exit 0
                ;;
            *)
                echo "Unknown option: $1"
                exit 1
                ;;
        esac
    done
    
    # 检查依赖
    check_dependencies
    
    # 执行步骤
    if [ "$SKIP_CONVERT" = false ]; then
        convert_models
    fi
    
    if [ "$SKIP_SPEED" = false ]; then
        benchmark_speed
    fi
    
    if [ "$SKIP_ACCURACY" = false ]; then
        eval_accuracy
    fi
    
    # 汇总结果
    summarize_results
    
    log "Experiment completed!"
    log "Results saved to: $OUTPUT_DIR/"
}

# 运行
main "$@"