forked from masonhuang/cluster4npu
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0919e6ae14 | |||
| be4bd617c3 | |||
| 6e9885404c | |||
| d2fdbf85ee | |||
| 55040733fe | |||
| 5aa374625f | |||
| ccd7cdd6b9 | |||
| bfac50f066 | |||
| 1781a05269 | |||
|
|
d90d9d6783 | ||
| c4090b2420 | |||
| 2fea1eceec | |||
|
|
ec940c3f2f | ||
| 48acae9c74 |
141
.autoflow/00-onboarding/health-check.md
Normal file
141
.autoflow/00-onboarding/health-check.md
Normal file
@ -0,0 +1,141 @@
|
||||
# 專案健檢報告
|
||||
|
||||
## 基本資訊
|
||||
|
||||
- **專案名稱**:Cluster4NPU UI — Visual Pipeline Designer
|
||||
- **版本**:v0.0.3
|
||||
- **程式碼來源**:本地路徑 `C:\Users\sungs\Documents\abin\temp\cluster4npu`
|
||||
- **Git 分支**:developer(主分支為 main)
|
||||
- **最後 commit**:feat: Reorganize test scripts and improve YOLOv5 postprocessing
|
||||
- **健檢日期**:2026-04-05
|
||||
|
||||
---
|
||||
|
||||
## 技術堆疊
|
||||
|
||||
| 層級 | 技術 | 版本 |
|
||||
|------|------|------|
|
||||
| 語言 | Python | >=3.9, <3.12 |
|
||||
| GUI 框架 | PyQt5 | >=5.15.11 |
|
||||
| 視覺節點編輯器 | NodeGraphQt | >=0.6.40 |
|
||||
| 影像處理 | OpenCV | (runtime dependency) |
|
||||
| 數值運算 | NumPy | (runtime dependency) |
|
||||
| 硬體 SDK | Kneron KP SDK | (runtime, NPU dongle 驅動) |
|
||||
| 套件管理 | uv | — |
|
||||
| 打包 | PyInstaller (main.spec) | — |
|
||||
|
||||
**支援硬體:** Kneron NPU dongles — KL520、KL720、KL1080
|
||||
|
||||
---
|
||||
|
||||
## 專案結構概覽
|
||||
|
||||
```
|
||||
cluster4npu/
|
||||
├── main.py # 應用程式入口點
|
||||
├── config/ # 設定與主題 (settings.py, theme.py)
|
||||
├── core/
|
||||
│ ├── pipeline.py # Pipeline 分析、stage 偵測、驗證
|
||||
│ ├── functions/
|
||||
│ │ ├── InferencePipeline.py # 多 stage pipeline 執行引擎(多執行緒)
|
||||
│ │ ├── Multidongle.py # NPU dongle 管理與自動偵測
|
||||
│ │ ├── camera_source.py # 相機輸入來源
|
||||
│ │ ├── video_source.py # 影片輸入來源
|
||||
│ │ ├── result_handler.py # 推論結果處理
|
||||
│ │ ├── workflow_orchestrator.py
|
||||
│ │ ├── mflow_converter.py # .mflow 格式轉換
|
||||
│ │ └── yolo_v5_postprocess_reference.py
|
||||
│ └── nodes/ # 節點定義(5 種類型)
|
||||
│ ├── base_node.py
|
||||
│ ├── input_node.py
|
||||
│ ├── model_node.py
|
||||
│ ├── preprocess_node.py
|
||||
│ ├── postprocess_node.py
|
||||
│ ├── output_node.py
|
||||
│ ├── simple_input_node.py
|
||||
│ └── exact_nodes.py
|
||||
├── ui/
|
||||
│ ├── windows/ # 主視窗(login.py, dashboard.py, pipeline_editor.py)
|
||||
│ ├── components/ # 可重用元件(node_palette, properties_widget, common_widgets)
|
||||
│ └── dialogs/ # 對話框(deployment, performance, stage_config 等)
|
||||
├── utils/ # 工具函式(file_utils, folder_dialog, ui_utils)
|
||||
├── example_utils/ # 範例後處理工具(ByteTrack 等)
|
||||
├── tests/ # 測試腳本(42 個,多為腳本式,非正式 test suite)
|
||||
├── resources/ # 資源檔案
|
||||
└── output/ # 推論輸出結果
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件完整度
|
||||
|
||||
| 文件類型 | 狀態 | 位置 | 備註 |
|
||||
|---------|------|------|------|
|
||||
| README | ✅ 有 | `README.md` | 詳細,含安裝、架構說明 |
|
||||
| 產品需求 / PRD | ⚠️ 部分 | `PROJECT_SUMMARY.md` | 有願景與待開發功能,但非正式 PRD 格式 |
|
||||
| 開發路線圖 | ✅ 有 | `DEVELOPMENT_ROADMAP.md` | 四個 Phase,有具體目標 |
|
||||
| 架構設計文件 | ❌ 無 | — | README 內有簡介,但無正式 Design Doc |
|
||||
| API 文件 | ❌ 無 | — | 無正式 API 文件 |
|
||||
| 設計稿 | ❌ 無 | 只有 `Flowchart.jpg` | 無 Wireframe 或 UI 規格 |
|
||||
| 技術設計文件 (TDD) | ❌ 無 | — | 無 |
|
||||
| 測試計畫 | ❌ 無 | — | 有測試腳本但無正式測試計畫 |
|
||||
| 部署文件 | ⚠️ 部分 | README 內 | 有基本步驟,無完整部署文件 |
|
||||
| Release Notes | ✅ 有 | `release_note.md` | 目前到 v0.0.2 |
|
||||
|
||||
---
|
||||
|
||||
## 程式碼健康度
|
||||
|
||||
- **測試覆蓋率**:⚠️ 部分測試 — `tests/` 下有 42 個腳本,但多為情境測試腳本(非 pytest 單元測試),缺乏系統性覆蓋
|
||||
- **程式碼品質**:中等 — 有明確的模組分離;部分根目錄腳本(debug_*.py, force_cleanup.py 等)為開發過程遺留,結構略混亂
|
||||
- **安全性**:低風險(本地桌面應用,無網路 API)
|
||||
- **技術債**:
|
||||
- 根目錄有多個 debug/cleanup 腳本未整理
|
||||
- tests/ 下腳本命名與分類混亂(部分非 test_ 開頭)
|
||||
- 缺乏正式的 pytest 測試架構
|
||||
|
||||
---
|
||||
|
||||
## 現有功能清單
|
||||
|
||||
| 功能 | 描述 | 狀態 |
|
||||
|------|------|------|
|
||||
| 視覺化 Pipeline 編輯器 | 拖拽節點建立 Pipeline(NodeGraphQt) | ✅ 完成 |
|
||||
| 5 種節點類型 | Input / Preprocess / Model / Postprocess / Output | ✅ 完成 |
|
||||
| Pipeline 驗證 | 即時 stage 偵測與錯誤標示 | ✅ 完成 |
|
||||
| .mflow 檔案格式 | Pipeline 儲存與載入(JSON) | ✅ 完成 |
|
||||
| 多 NPU Dongle 支援 | KL520 / KL720 / KL1080 自動偵測 | ✅ 完成 |
|
||||
| 多 stage 推論引擎 | 多執行緒 Pipeline 執行 | ✅ 完成 |
|
||||
| 效能監控 | FPS、延遲即時顯示 | ✅ 完成(有 known bugs) |
|
||||
| 相機 / 影片 / 圖片輸入 | 多種輸入來源 | ✅ 完成 |
|
||||
| 專案管理 | 登入畫面、最近專案、新增/載入 Pipeline | ✅ 完成 |
|
||||
| YOLOv5 後處理 | 偵測結果格式化 | ✅ 完成(最近改善) |
|
||||
| ByteTrack 追蹤 | 物件追蹤後處理 | ✅ 完成(example_utils) |
|
||||
|
||||
---
|
||||
|
||||
## 缺失項目摘要(待開發)
|
||||
|
||||
根據 `PROJECT_SUMMARY.md` 與 `DEVELOPMENT_ROADMAP.md`:
|
||||
|
||||
1. **效能視覺化**:並行 vs 循序執行比較、Speedup 指標顯示(Phase 1)
|
||||
2. **Benchmarking 系統**:自動化效能測試、圖表比較(Phase 1)
|
||||
3. **裝置管理介面**:視覺化裝置分配、負載平衡(Phase 2)
|
||||
4. **即時監控 Dashboard**:FPS/延遲圖表、資源使用率(Phase 2)
|
||||
5. **優化引擎**:自動化建議、效能預測(Phase 3)
|
||||
|
||||
已知 Bug:
|
||||
- 節點屬性顯示問題
|
||||
- 輸出視覺化(含後處理)
|
||||
|
||||
---
|
||||
|
||||
## CI/CD 與基礎設施
|
||||
|
||||
| 項目 | 狀態 |
|
||||
|------|------|
|
||||
| Docker | ❌ 無 |
|
||||
| CI/CD | ❌ 無 |
|
||||
| 部署設定 | ❌ 無(本地桌面應用,有 PyInstaller spec) |
|
||||
| 環境變數管理 | ❌ 無 |
|
||||
| 版本控制 | ✅ Git(GitHub 遠端) |
|
||||
344
.autoflow/02-prd/PRD.md
Normal file
344
.autoflow/02-prd/PRD.md
Normal file
@ -0,0 +1,344 @@
|
||||
# PRD — Cluster4NPU UI
|
||||
|
||||
> 此 PRD 為從既有程式碼與文件反向整理,反映截至 2026-04-05 的實際狀況。
|
||||
> 版本:v0.0.3(developer 分支)
|
||||
|
||||
---
|
||||
|
||||
## 1. 產品概覽
|
||||
|
||||
### 1.1 產品願景
|
||||
|
||||
Cluster4NPU UI 的目標是讓任何人(不需要寫程式)都能夠透過直覺的視覺化拖拽介面,設計並執行平行 AI 推論 Pipeline,充分發揮 Kneron NPU Dongle 的硬體效能,並清楚看見平行處理帶來的效能提升。
|
||||
|
||||
**一句話描述**:「用拖拽的方式設計 AI Pipeline,不需要一行程式碼,就能讓多個 NPU Dongle 平行加速你的 AI 推論工作。」
|
||||
|
||||
### 1.2 目標用戶
|
||||
|
||||
**主要用戶:AI 應用整合工程師 / 系統整合商**
|
||||
|
||||
- 具備 AI 模型使用知識,但未必熟悉底層 NPU 程式設計
|
||||
- 需要快速驗證多模型串接 Pipeline 的效能
|
||||
- 希望在不修改程式碼的情況下調整 Pipeline 設定與硬體分配
|
||||
|
||||
**次要用戶:AI 研究員 / 技術評估人員**
|
||||
|
||||
- 需要比較不同 Pipeline 配置下的效能表現
|
||||
- 希望有可視化的數據佐證平行處理的效益(用於提案或報告)
|
||||
|
||||
**潛在用戶:Kneron 硬體銷售團隊**
|
||||
|
||||
- 需要 Demo 工具,向潛在客戶展示 Kneron NPU 的效能優勢
|
||||
|
||||
### 1.3 核心價值主張
|
||||
|
||||
1. **無程式碼 Pipeline 設計**:拖拽介面即可建立複雜多模型 AI Pipeline
|
||||
2. **平行效能可視化**:清楚顯示平行 vs 循序處理的效能差異(2x、3x、4x 加速)
|
||||
3. **硬體自動管理**:自動偵測並最佳化 NPU Dongle 分配,降低使用門檻
|
||||
4. **專業監控工具**:即時 FPS、延遲、吞吐量監控,滿足工程師級的分析需求
|
||||
|
||||
---
|
||||
|
||||
## 2. 市場背景
|
||||
|
||||
### 2.1 問題陳述
|
||||
|
||||
隨著 Edge AI 應用普及,使用者面臨以下問題:
|
||||
|
||||
1. **設定複雜**:在多個 NPU Dongle 上執行平行 AI 推論需要撰寫大量底層程式碼
|
||||
2. **效能不透明**:難以量化平行處理帶來的效能增益,缺乏說服力
|
||||
3. **Pipeline 設計困難**:多模型串接(如 偵測 → 追蹤 → 分類)需要手動處理資料流
|
||||
4. **硬體管理負擔**:多個 NPU Dongle 的分配、監控、除錯缺乏統一工具
|
||||
|
||||
### 2.2 目標市場
|
||||
|
||||
- **主要市場**:使用 Kneron NPU 硬體(KL520、KL720、KL1080)的系統整合商與企業用戶
|
||||
- **市場範圍**:Edge AI 推論領域,偏向工業視覺、安全監控、智慧零售等應用場景
|
||||
- **地理範圍**:目前以繁體中文、英文環境為主(台灣、亞太地區)
|
||||
|
||||
---
|
||||
|
||||
## 3. 用戶故事
|
||||
|
||||
以下用戶故事基於現有功能與規劃功能:
|
||||
|
||||
**已實現的用戶故事:**
|
||||
|
||||
- As a system integrator, I want to design an AI inference pipeline by dragging and dropping nodes, so that I can build complex multi-model workflows without writing code.
|
||||
- As a developer, I want to see real-time pipeline validation errors, so that I can fix configuration issues before deployment.
|
||||
- As a user, I want to save my pipeline configuration to a file (.mflow), so that I can reuse and share it with teammates.
|
||||
- As an engineer, I want to see live FPS and latency metrics during inference, so that I can monitor pipeline performance in real time.
|
||||
- As a hardware manager, I want the application to automatically detect available NPU dongles, so that I don't need to manually configure device connections.
|
||||
- As a user, I want to load video files, camera streams, or images as pipeline inputs, so that I can test my pipeline with different data sources.
|
||||
|
||||
**待開發的用戶故事:**
|
||||
|
||||
- As a user, I want to compare parallel vs sequential inference performance side by side, so that I can clearly see the speedup benefit of using multiple NPU dongles.
|
||||
- As an engineer, I want to run automated benchmarks with one click, so that I can measure performance without manual testing.
|
||||
- As a hardware manager, I want to visually assign NPU dongles to specific pipeline stages, so that I have fine-grained control over device allocation.
|
||||
- As a user, I want to see live performance graphs (FPS, latency over time), so that I can identify bottlenecks during pipeline execution.
|
||||
- As an engineer, I want to receive automated optimization suggestions, so that I can improve pipeline performance without deep NPU expertise.
|
||||
- As a sales engineer, I want to generate a performance report showing speedup metrics, so that I can present the ROI of parallel NPU processing to clients.
|
||||
|
||||
---
|
||||
|
||||
## 4. 功能需求
|
||||
|
||||
### 4.1 已完成功能(現有)
|
||||
|
||||
以下功能已在 v0.0.3 中實作完成(資料來源:健檢報告):
|
||||
|
||||
| 功能 | 描述 | 狀態 |
|
||||
|------|------|------|
|
||||
| 視覺化 Pipeline 編輯器 | 基於 NodeGraphQt 的拖拽節點介面 | 完成 |
|
||||
| 5 種節點類型 | Input / Preprocess / Model / Postprocess / Output | 完成 |
|
||||
| Pipeline 即時驗證 | 即時 Stage 偵測與錯誤標示 | 完成 |
|
||||
| .mflow 檔案格式 | Pipeline 儲存與載入(JSON 格式) | 完成 |
|
||||
| 三面板 UI 佈局 | 左:節點面板、中:編輯器、右:設定與監控 | 完成 |
|
||||
| 多 NPU Dongle 支援 | KL520 / KL720 / KL1080 自動偵測 | 完成 |
|
||||
| 多 Stage 推論引擎 | 基於多執行緒的平行 Pipeline 執行 | 完成 |
|
||||
| 效能基礎監控 | FPS、延遲即時顯示(有已知 Bug) | 完成(有瑕疵) |
|
||||
| 多種輸入來源 | 相機(USB)、影片(MP4/AVI/MOV)、圖片(JPG/PNG/BMP)、RTSP 串流(基本) | 完成 |
|
||||
| 專案管理 | 登入畫面、最近專案清單、新增 / 載入 Pipeline | 完成 |
|
||||
| YOLOv5 後處理 | 偵測結果格式化與邊界框處理 | 完成 |
|
||||
| ByteTrack 追蹤 | 物件追蹤後處理(example_utils) | 完成 |
|
||||
| 固件上傳支援 | upload_fw 選項與推論流程整合 | 完成(v0.0.2) |
|
||||
| PyInstaller 打包 | 獨立執行檔打包支援(main.spec) | 完成 |
|
||||
|
||||
**已知 Bug(v0.0.2 記錄):**
|
||||
|
||||
- 節點屬性顯示問題
|
||||
- 輸出視覺化(含後處理結果)異常
|
||||
|
||||
### 4.2 待開發功能(依優先級)
|
||||
|
||||
#### Phase 1:效能視覺化(第 1-2 週,優先級:P0)
|
||||
|
||||
**功能 1:平行 vs 循序效能比較**
|
||||
|
||||
- **描述**:提供並行處理與循序處理的效能對照,視覺化顯示加速倍數(如 "3.2x FASTER")
|
||||
- **驗收標準**:
|
||||
- 可選擇「單裝置 / 多裝置」模式執行同一 Pipeline
|
||||
- 顯示兩種模式的 FPS 與延遲數值
|
||||
- 以視覺指標(進度條、倍數文字)呈現加速結果
|
||||
- 比較結果可在 UI 中保留供查閱
|
||||
- **優先級**:P0
|
||||
- **所屬 Phase**:Phase 1
|
||||
|
||||
**功能 2:自動化效能 Benchmark 系統(PerformanceBenchmarker)**
|
||||
|
||||
- **描述**:一鍵啟動效能測試,自動執行單裝置與多裝置比較並記錄結果
|
||||
- **驗收標準**:
|
||||
- 提供「執行 Benchmark」按鈕
|
||||
- 自動完成測試並呈現結果圖表
|
||||
- 結果可歷史保存(追蹤效能變化)
|
||||
- 支援回歸測試(比較不同版本的效能)
|
||||
- **優先級**:P0
|
||||
- **所屬 Phase**:Phase 1
|
||||
|
||||
**功能 3:即時效能儀表板(PerformanceDashboard)**
|
||||
|
||||
- **描述**:在推論執行期間顯示即時 FPS、延遲、吞吐量折線圖
|
||||
- **驗收標準**:
|
||||
- 以圖表形式顯示 FPS 隨時間變化
|
||||
- 以圖表形式顯示延遲分佈
|
||||
- 更新頻率 >= 1 Hz
|
||||
- 不影響推論效能(CPU 使用率增加 < 5%)
|
||||
- **優先級**:P0
|
||||
- **所屬 Phase**:Phase 1
|
||||
|
||||
#### Phase 2:裝置管理(第 3-4 週,優先級:P1)
|
||||
|
||||
**功能 4:視覺化裝置管理面板(DeviceManagementPanel)**
|
||||
|
||||
- **描述**:提供 NPU Dongle 狀態總覽,包含裝置健康度、型號、當前分配狀態
|
||||
- **驗收標準**:
|
||||
- 列出所有已偵測的 NPU Dongle 及其狀態(線上/離線/繁忙)
|
||||
- 顯示每個裝置的型號(KL520/KL720/KL1080)
|
||||
- 顯示每個裝置當前分配至哪個 Pipeline Stage
|
||||
- **優先級**:P1
|
||||
- **所屬 Phase**:Phase 2
|
||||
|
||||
**功能 5:手動裝置分配介面**
|
||||
|
||||
- **描述**:允許用戶手動將特定 NPU Dongle 指定給特定 Pipeline Stage
|
||||
- **驗收標準**:
|
||||
- 可透過下拉選單或拖拽方式指定裝置
|
||||
- 指定後立即反映在 Pipeline 執行設定中
|
||||
- 無效的分配(如指定離線裝置)會有錯誤提示
|
||||
- **優先級**:P1
|
||||
- **所屬 Phase**:Phase 2
|
||||
|
||||
**功能 6:裝置效能分析(DeviceManager 強化)**
|
||||
|
||||
- **描述**:追蹤個別 NPU Dongle 的效能指標與歷史記錄
|
||||
- **驗收標準**:
|
||||
- 顯示每個裝置的推論吞吐量(Inference/sec)
|
||||
- 顯示裝置使用率百分比
|
||||
- 提供自動負載平衡建議
|
||||
- **優先級**:P1
|
||||
- **所屬 Phase**:Phase 2
|
||||
|
||||
**功能 7:瓶頸偵測與警告系統**
|
||||
|
||||
- **描述**:自動識別 Pipeline 中的效能瓶頸並發出警告
|
||||
- **驗收標準**:
|
||||
- 當某 Stage 的佇列持續積壓時觸發警告
|
||||
- 在 UI 中以視覺提示標示瓶頸 Stage
|
||||
- 提供基本的改善建議(如增加裝置數量)
|
||||
- **優先級**:P1
|
||||
- **所屬 Phase**:Phase 2
|
||||
|
||||
#### Phase 3:進階功能(第 5-6 週,優先級:P2)
|
||||
|
||||
**功能 8:自動化優化引擎(OptimizationEngine)**
|
||||
|
||||
- **描述**:分析當前 Pipeline 配置,自動產生效能優化建議
|
||||
- **驗收標準**:
|
||||
- 分析 Stage 效能差異,建議最佳裝置分配方式
|
||||
- 識別不必要的前後處理步驟並提出建議
|
||||
- 建議以卡片形式呈現,用戶可選擇採納或忽略
|
||||
- **優先級**:P2
|
||||
- **所屬 Phase**:Phase 3
|
||||
|
||||
**功能 9:Pipeline 設定範本**
|
||||
|
||||
- **描述**:提供常見使用情境的預設 Pipeline 範本(如 YOLOv5 偵測、物件追蹤)
|
||||
- **驗收標準**:
|
||||
- 提供至少 3 種常見範本
|
||||
- 範本可直接載入並修改
|
||||
- 現有 Pipeline 可儲存為自訂範本
|
||||
- **優先級**:P2
|
||||
- **所屬 Phase**:Phase 3
|
||||
|
||||
**功能 10:效能預測(執行前估算)**
|
||||
|
||||
- **描述**:在執行 Pipeline 之前,根據硬體設定預估效能表現
|
||||
- **驗收標準**:
|
||||
- 顯示預估 FPS 與延遲範圍
|
||||
- 預估值與實際值誤差 <= 20%(基於歷史資料)
|
||||
- **優先級**:P2
|
||||
- **所屬 Phase**:Phase 3
|
||||
|
||||
#### Phase 4:專業潤色(第 7-8 週,優先級:P2)
|
||||
|
||||
**功能 11:效能報告匯出**
|
||||
|
||||
- **描述**:將 Benchmark 結果匯出為可分享的報告格式
|
||||
- **驗收標準**:
|
||||
- 支援匯出為 PDF 或 CSV
|
||||
- 報告包含:Pipeline 設定、裝置配置、效能指標、加速倍數
|
||||
- **優先級**:P2
|
||||
- **所屬 Phase**:Phase 4
|
||||
|
||||
**功能 12:進階分析與趨勢圖**
|
||||
|
||||
- **描述**:追蹤效能指標的歷史趨勢,識別長期的效能退化
|
||||
- **驗收標準**:
|
||||
- 顯示多次執行的效能趨勢圖
|
||||
- 支援篩選特定時間範圍
|
||||
- **優先級**:P2
|
||||
- **所屬 Phase**:Phase 4
|
||||
|
||||
---
|
||||
|
||||
## 5. 非功能需求
|
||||
|
||||
### 5.1 效能需求
|
||||
|
||||
- UI 互動回應時間 < 200ms(節點拖拽、屬性切換)
|
||||
- Pipeline 即時驗證延遲 < 100ms
|
||||
- 效能儀表板更新不得對推論 FPS 造成超過 5% 的影響
|
||||
- 應用程式啟動時間(含硬體偵測)< 10 秒
|
||||
|
||||
### 5.2 相容性需求
|
||||
|
||||
- **作業系統**:Windows 10/11(主要);Linux(次要)
|
||||
- **Python 版本**:3.9 以上、3.12 以下
|
||||
- **硬體**:Kneron NPU Dongle(KL520、KL720、KL1080),USB 3.0 連接
|
||||
- **PyQt5 版本**:>= 5.15.11
|
||||
|
||||
### 5.3 可用性需求
|
||||
|
||||
- 首次使用者應能在 5 分鐘內完成基本 Pipeline 設計(拖拽 5 個節點並連接)
|
||||
- 節點設定面板需防止水平滾動條出現(已在 v0.0.2 修正)
|
||||
- 所有錯誤訊息應具有可讀性,避免技術術語
|
||||
|
||||
### 5.4 可靠性需求
|
||||
|
||||
- 重複執行推論不得出現錯誤(已在 v0.0.2 修正)
|
||||
- Pipeline 儲存(.mflow)需能完整還原節點設定與連接關係
|
||||
- 應用程式異常關閉後,下次啟動應能顯示最近專案清單
|
||||
|
||||
### 5.5 可維護性需求
|
||||
|
||||
- 新增節點類型需有對應的單元測試
|
||||
- 核心模組(InferencePipeline、Multidongle)需有 pytest 格式的測試覆蓋
|
||||
- 根目錄的 debug/cleanup 腳本應整理並移至 `tools/` 或 `tests/` 目錄
|
||||
|
||||
---
|
||||
|
||||
## 6. 成功指標
|
||||
|
||||
### 6.1 核心使用目標(依產品階段)
|
||||
|
||||
**Phase 1 完成標準(效能視覺化):**
|
||||
- 用戶可在 3 步以內啟動 Benchmark 並看到加速倍數比較結果
|
||||
- 儀表板更新流暢(無明顯卡頓)
|
||||
|
||||
**Phase 2 完成標準(裝置管理):**
|
||||
- 用戶可在不修改程式碼的情況下手動調整裝置分配
|
||||
- 瓶頸偵測正確識別率 > 80%(在測試情境下)
|
||||
|
||||
**Phase 3 完成標準(進階功能):**
|
||||
- OptimizationEngine 建議的裝置分配方案,實際效能提升 > 10%
|
||||
- 提供至少 3 種可直接使用的 Pipeline 範本
|
||||
|
||||
**整體產品品質標準:**
|
||||
- 已知 Bug(節點屬性顯示、輸出視覺化)全數修復
|
||||
- 完整的 pytest 測試覆蓋核心模組
|
||||
|
||||
### 6.2 使用者體驗指標
|
||||
|
||||
- Pipeline 設計完成時間(目標:首次使用 < 5 分鐘,熟悉後 < 2 分鐘)
|
||||
- Benchmark 一鍵啟動到結果呈現(目標:< 30 秒完成)
|
||||
|
||||
---
|
||||
|
||||
## 7. 超出範圍
|
||||
|
||||
以下事項明確不在 v0.0.3 至 Phase 4 的開發範圍內:
|
||||
|
||||
1. **雲端功能**:無雲端儲存、遠端執行、或 SaaS 服務
|
||||
2. **非 Kneron 硬體支援**:不支援其他廠商的 NPU(如 Hailo、Coral)
|
||||
3. **模型訓練**:本工具僅處理推論(Inference),不包含模型訓練功能
|
||||
4. **行動端 App**:僅為桌面應用(Windows / Linux)
|
||||
5. **多人協作**:不支援多人同時編輯同一 Pipeline
|
||||
6. **付費 / 授權系統**:目前無商業授權機制
|
||||
7. **自動語言切換 / 完整多語系**:目前以英文 UI 為主,無正式多語系支援
|
||||
8. **RTSP 串流完整支援**:RTSP 目前僅為基本支援,完整串流管理不在當前範圍
|
||||
|
||||
---
|
||||
|
||||
## 附錄
|
||||
|
||||
### A. 版本歷史摘要
|
||||
|
||||
| 版本 | 日期 | 主要變更 |
|
||||
|------|------|---------|
|
||||
| v0.0.1 | — | 初始版本(確切日期不明) |
|
||||
| v0.0.2 | 2025-07-31 | 自動資料清理、固件上傳支援、修復多次推論錯誤、FPS 修正 |
|
||||
| v0.0.3 | 進行中 | YOLOv5 後處理改善、測試腳本整理(developer 分支) |
|
||||
|
||||
### B. 相關文件
|
||||
|
||||
- 健檢報告:`C:\Users\sungs\Documents\abin\temp\cluster4npu\.autoflow\00-onboarding\health-check.md`
|
||||
- 開發路線圖:`C:\Users\sungs\Documents\abin\temp\cluster4npu\DEVELOPMENT_ROADMAP.md`
|
||||
- 專案摘要:`C:\Users\sungs\Documents\abin\temp\cluster4npu\PROJECT_SUMMARY.md`
|
||||
- README:`C:\Users\sungs\Documents\abin\temp\cluster4npu\README.md`
|
||||
|
||||
### C. 技術限制說明
|
||||
|
||||
- 本工具強依賴 Kneron KP SDK,SDK 版本更新可能影響硬體相容性
|
||||
- NodeGraphQt 的視覺編輯器版本(>= 0.6.40)限制了某些 UI 客製化能力
|
||||
- Python 版本限制(3.9–3.11)源自 PyQt5 與 Kneron SDK 的相容性需求
|
||||
1149
.autoflow/04-architecture/TDD.md
Normal file
1149
.autoflow/04-architecture/TDD.md
Normal file
File diff suppressed because it is too large
Load Diff
581
.autoflow/04-architecture/design-doc.md
Normal file
581
.autoflow/04-architecture/design-doc.md
Normal file
@ -0,0 +1,581 @@
|
||||
# Design Doc — Cluster4NPU UI
|
||||
|
||||
## 作者:Architect Agent
|
||||
## 狀態:Draft
|
||||
## 最後更新:2026-04-05
|
||||
## 版本對應:v0.0.3(developer 分支)
|
||||
|
||||
---
|
||||
|
||||
## 1. 背景與目標
|
||||
|
||||
### 1.1 背景
|
||||
|
||||
Cluster4NPU UI 是一個桌面應用程式,讓使用者不需要撰寫程式碼,就能透過視覺化拖拽介面設計並執行 AI 推論 Pipeline,並將工作負載分配到多個 Kneron NPU Dongle(KL520、KL720、KL1080)上平行執行。
|
||||
|
||||
現有系統已完成核心 Pipeline 設計器與推論引擎的基礎建設,但缺乏:
|
||||
- 效能視覺化(無法直觀看到平行處理的加速效果)
|
||||
- 進階裝置管理介面
|
||||
- 自動化 Benchmark 系統
|
||||
- 優化建議引擎
|
||||
|
||||
### 1.2 目標
|
||||
|
||||
1. **核心目標**:使任何 AI 應用工程師都能在 5 分鐘內完成 Pipeline 設計並看到推論結果
|
||||
2. **差異化目標**:清楚視覺化呈現多 NPU Dongle 平行處理帶來的效能加速(2x、3x、4x)
|
||||
3. **工程目標**:提供可擴展的架構,支援 Phase 1-4 的功能迭代
|
||||
|
||||
### 1.3 範圍
|
||||
|
||||
**本文件涵蓋:**
|
||||
- 現有(v0.0.3)核心架構的完整說明
|
||||
- Phase 1-3 待開發功能的架構設計方向
|
||||
|
||||
**不涵蓋:**
|
||||
- 雲端功能、非 Kneron 硬體、模型訓練、行動端
|
||||
|
||||
---
|
||||
|
||||
## 2. 系統架構總覽
|
||||
|
||||
### 2.1 整體分層架構
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ 使用者介面層(UI Layer) │
|
||||
│ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
||||
│ │ Login Window │ │ Dashboard │ │ Dialogs │ │
|
||||
│ │ (login.py) │ │(dashboard.py)│ │ (deployment, │ │
|
||||
│ └──────────────┘ └──────────────┘ │ performance)│ │
|
||||
│ │ └──────────────┘ │
|
||||
│ ┌──────────────────────────────────────────────────┐ │
|
||||
│ │ 三面板佈局(Three-Panel Layout) │ │
|
||||
│ │ ┌──────────┐ ┌──────────────┐ ┌──────────┐ │ │
|
||||
│ │ │ 左面板 │ │ 中面板 │ │ 右面板 │ │ │
|
||||
│ │ │ 節點面板 │ │ Pipeline 編輯│ │ 設定/監控│ │ │
|
||||
│ │ │(palette) │ │ (NodeGraphQt)│ │(properties│ │ │
|
||||
│ │ └──────────┘ └──────────────┘ └──────────┘ │ │
|
||||
│ └──────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ 應用程式核心層(Core Layer) │
|
||||
│ │
|
||||
│ ┌────────────────────┐ ┌──────────────────────────┐ │
|
||||
│ │ Pipeline 分析引擎 │ │ 節點系統(Nodes) │ │
|
||||
│ │ (pipeline.py) │ │ (base/input/model/ │ │
|
||||
│ │ │ │ preprocess/postprocess/ │ │
|
||||
│ │ - Stage 偵測 │ │ output nodes) │ │
|
||||
│ │ - 結構驗證 │ │ │ │
|
||||
│ │ - 路徑分析 │ │ - 業務屬性管理 │ │
|
||||
│ │ - 設定匯出 │ │ - 設定序列化 │ │
|
||||
│ └────────────────────┘ └──────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌──────────────────────────────────────────────────┐ │
|
||||
│ │ 推論執行層(Inference Execution Layer) │ │
|
||||
│ │ │ │
|
||||
│ │ ┌──────────────────────┐ ┌─────────────────┐ │ │
|
||||
│ │ │ InferencePipeline │ │ MultiDongle │ │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ │ - 多 Stage 協調 │ │ - NPU 裝置管理 │ │ │
|
||||
│ │ │ - 執行緒管理 │ │ - 非同步推論 │ │ │
|
||||
│ │ │ - 佇列管理 │ │ - 前後處理 │ │ │
|
||||
│ │ │ - FPS 計算 │ │ - 多裝置排程 │ │ │
|
||||
│ │ └──────────────────────┘ └─────────────────┘ │ │
|
||||
│ └──────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ 硬體抽象層(Hardware Abstraction Layer) │
|
||||
│ │
|
||||
│ ┌──────────────────────────────────────────────────┐ │
|
||||
│ │ Kneron KP SDK │ │
|
||||
│ │ │ │
|
||||
│ │ KL520 Dongle │ KL720 Dongle │ KL1080 Dongle │ │
|
||||
│ └──────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
### 2.2 模組間依賴關係
|
||||
|
||||
```
|
||||
main.py
|
||||
└── ui/windows/login.py (DashboardLogin)
|
||||
└── ui/windows/dashboard.py (DashboardWindow)
|
||||
├── ui/windows/pipeline_editor.py
|
||||
│ └── core/pipeline.py (PipelineAnalyzer)
|
||||
│ └── core/nodes/*.py
|
||||
├── ui/components/properties_widget.py
|
||||
│ └── core/nodes/*.py
|
||||
└── core/functions/InferencePipeline.py
|
||||
└── core/functions/Multidongle.py
|
||||
└── kp (Kneron KP SDK)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 核心元件說明
|
||||
|
||||
### 3.1 Pipeline 分析引擎(`core/pipeline.py`)
|
||||
|
||||
**職責:** 分析 NodeGraphQt 視覺圖形,識別 Pipeline 的 Stage 結構、驗證合法性、產生執行設定。
|
||||
|
||||
**關鍵類別:**
|
||||
|
||||
| 類別/函式 | 職責 |
|
||||
|---------|------|
|
||||
| `PipelineStage` | 代表一個推論 Stage,包含 ModelNode 與可選的 Pre/Postprocess Node |
|
||||
| `analyze_pipeline_stages(node_graph)` | 從視覺圖形中識別所有 Stage,依距離排序 |
|
||||
| `get_stage_count(node_graph)` | 計算 Pipeline 中的 Stage 數量(用於 UI 顯示) |
|
||||
| `validate_pipeline_structure(node_graph)` | 驗證 Pipeline 是否包含必要節點(Input、Model、Output) |
|
||||
| `get_pipeline_summary(node_graph)` | 回傳 Pipeline 統計摘要(節點數、Stage 數、驗證結果) |
|
||||
|
||||
**設計決策:**
|
||||
- 採用多重節點識別策略(`__identifier__`、`type_`、`NODE_NAME`、class 名稱、特定方法的存在)以提高相容性
|
||||
- Stage 排序依據:計算各 ModelNode 到輸入節點的最短路徑距離(BFS)
|
||||
- 所有圖遍歷方法都包含 defensive exception handling,避免 NodeGraphQt 物件狀態不一致時崩潰
|
||||
|
||||
**介面:**
|
||||
```python
|
||||
# 主要公開介面
|
||||
get_stage_count(node_graph: NodeGraph) -> int
|
||||
analyze_pipeline_stages(node_graph: NodeGraph) -> List[PipelineStage]
|
||||
validate_pipeline_structure(node_graph: NodeGraph) -> Tuple[bool, str]
|
||||
get_pipeline_summary(node_graph: NodeGraph) -> Dict[str, Any]
|
||||
```
|
||||
|
||||
### 3.2 節點系統(`core/nodes/`)
|
||||
|
||||
**職責:** 定義 Pipeline 中的各類節點,提供業務屬性管理與設定序列化能力。
|
||||
|
||||
**繼承架構:**
|
||||
```
|
||||
NodeGraphQt.BaseNode
|
||||
└── BaseNodeWithProperties(base_node.py)
|
||||
├── InputNode(input_node.py)
|
||||
├── ModelNode(model_node.py)
|
||||
├── PreprocessNode(preprocess_node.py)
|
||||
├── PostprocessNode(postprocess_node.py)
|
||||
└── OutputNode(output_node.py)
|
||||
```
|
||||
|
||||
**`BaseNodeWithProperties` 核心能力:**
|
||||
- `create_business_property(name, default, options)` — 建立帶驗證選項的業務屬性
|
||||
- `validate_property(name, value)` — 數值範圍、選項列表驗證
|
||||
- `get_node_config()` / `load_node_config(config)` — JSON 序列化/還原
|
||||
- `create_node_property_widget(node, prop_name, value, options)` — 根據屬性型別自動生成 Qt Widget
|
||||
|
||||
**ModelNode 屬性(主要節點):**
|
||||
|
||||
| 屬性 | 型別 | 說明 |
|
||||
|------|------|------|
|
||||
| `model_path` | file_path | .nef 模型檔案路徑 |
|
||||
| `dongle_series` | choice | KL520 / KL720 / KL1080 |
|
||||
| `num_dongles` | int (1-16) | 分配給此 Stage 的 Dongle 數量 |
|
||||
| `port_id` | string | USB Port ID(或 auto) |
|
||||
| `batch_size` | int (1-32) | 推論批次大小 |
|
||||
| `max_queue_size` | int (1-100) | 輸入佇列最大長度 |
|
||||
|
||||
### 3.3 推論執行引擎(`core/functions/InferencePipeline.py`)
|
||||
|
||||
**職責:** 管理多 Stage Pipeline 的生命週期、協調執行緒間資料流、計算效能指標。
|
||||
|
||||
**主要資料結構:**
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class StageConfig:
|
||||
stage_id: str
|
||||
port_ids: List[int]
|
||||
scpu_fw_path: str # SCPU 韌體路徑
|
||||
ncpu_fw_path: str # NCPU 韌體路徑
|
||||
model_path: str # .nef 模型路徑
|
||||
upload_fw: bool # 是否上傳韌體
|
||||
max_queue_size: int # 佇列大小(預設 50)
|
||||
multi_series_config: Optional[Dict] # 多系列模式設定
|
||||
input_preprocessor: Optional[PreProcessor]
|
||||
output_postprocessor: Optional[PostProcessor]
|
||||
|
||||
@dataclass
|
||||
class PipelineData:
|
||||
data: Any # 當前資料(影像、中間結果)
|
||||
metadata: Dict[str, Any] # 時間戳、處理資訊
|
||||
stage_results: Dict[str, Any] # 各 Stage 推論結果
|
||||
pipeline_id: str # 唯一識別碼
|
||||
timestamp: float
|
||||
```
|
||||
|
||||
**執行緒模型:**
|
||||
|
||||
```
|
||||
主執行緒(UI)
|
||||
│
|
||||
├── InferencePipeline.coordinator_thread(協調器)
|
||||
│ │ 從 pipeline_input_queue 取資料
|
||||
│ │ 依序分配給各 Stage
|
||||
│ └── 收集結果放入 pipeline_output_queue
|
||||
│
|
||||
├── PipelineStage[0].worker_thread(Stage 0 工作執行緒)
|
||||
│ └── 從 input_queue 取資料 → MultiDongle 推論 → 放入 output_queue
|
||||
│
|
||||
├── PipelineStage[1].worker_thread(Stage 1 工作執行緒)
|
||||
│ └── ...
|
||||
│
|
||||
└── stats_thread(效能統計回報)
|
||||
```
|
||||
|
||||
**FPS 計算方式:** 採用累積式計算(`completed_counter / elapsed_time`),與 Kneron 範例程式的計算邏輯一致,只計算真實推論結果(排除 async/processing 狀態)。
|
||||
|
||||
**佇列管理策略:**
|
||||
- 輸入佇列滿時:捨棄最舊的幀(為了即時串流的實時性)
|
||||
- 輸出佇列上限 50 筆:超出時捨棄最舊的結果,避免記憶體無限增長
|
||||
|
||||
### 3.4 硬體抽象層(`core/functions/Multidongle.py`)
|
||||
|
||||
**職責:** 封裝 Kneron KP SDK,提供統一的 NPU Dongle 管理介面,支援單裝置與多裝置(multi-series)模式。
|
||||
|
||||
**核心抽象類別:**
|
||||
|
||||
```python
|
||||
class DataProcessor(ABC):
|
||||
def process(self, data: Any, *args, **kwargs) -> Any: ...
|
||||
|
||||
class PreProcessor(DataProcessor):
|
||||
# 影像縮放(resize)+ 格式轉換(BGR → BGR565/RGB8888)
|
||||
|
||||
class PostProcessor(DataProcessor):
|
||||
# 支援 4 種後處理類型:
|
||||
# - FIRE_DETECTION(火焰分類)
|
||||
# - CLASSIFICATION(一般分類)
|
||||
# - YOLO_V3(物件偵測)
|
||||
# - YOLO_V5(物件偵測,使用參考實作)
|
||||
# - RAW_OUTPUT(原始輸出)
|
||||
```
|
||||
|
||||
**裝置規格(DongleSeriesSpec):**
|
||||
|
||||
| 系列 | Product ID | GOPS 算力 |
|
||||
|------|-----------|---------|
|
||||
| KL520 | 0x100 | 2 GOPS |
|
||||
| KL720 | 0x720 | 28 GOPS |
|
||||
| KL630 | 0x630 | 400 GOPS |
|
||||
| KL730 | 0x730 | 1600 GOPS |
|
||||
|
||||
**推論結果資料結構:**
|
||||
|
||||
```python
|
||||
@dataclass
|
||||
class ClassificationResult:
|
||||
probability: float
|
||||
class_name: str
|
||||
class_num: int
|
||||
confidence_threshold: float
|
||||
|
||||
@dataclass
|
||||
class ObjectDetectionResult:
|
||||
class_count: int
|
||||
box_count: int
|
||||
box_list: List[BoundingBox]
|
||||
# Letterbox 映射資訊(用於還原到原始影像座標)
|
||||
model_input_width, model_input_height: int
|
||||
pad_left, pad_top, pad_right, pad_bottom: int
|
||||
```
|
||||
|
||||
### 3.5 使用者介面層(`ui/`)
|
||||
|
||||
**職責:** 呈現視覺化 Pipeline 設計環境,管理節點屬性設定、效能監控顯示。
|
||||
|
||||
**主要視窗:**
|
||||
- `DashboardLogin`(`ui/windows/login.py`):啟動畫面、最近專案清單、新建/載入 Pipeline
|
||||
- `DashboardWindow`(`ui/windows/dashboard.py`):主工作介面,三面板佈局
|
||||
- `PipelineEditor`(`ui/windows/pipeline_editor.py`):內嵌 NodeGraphQt 視覺編輯器
|
||||
|
||||
**三面板配置:**
|
||||
|
||||
| 面板 | 寬度比例 | 主要內容 |
|
||||
|------|---------|---------|
|
||||
| 左面板 | 25% | 節點面板(拖拽來源)、Pipeline 操作按鈕 |
|
||||
| 中面板 | 50% | NodeGraphQt 視覺編輯器、全域狀態列 |
|
||||
| 右面板 | 25% | Properties Tab(節點設定)、Performance Tab(效能監控)、Dongles Tab(裝置管理) |
|
||||
|
||||
### 3.6 應用程式入口(`main.py`)
|
||||
|
||||
**職責:** 應用程式初始化、單一實例保護、Qt 環境設定。
|
||||
|
||||
**單一實例機制:** `SingleInstance` 類別採用雙重保護:
|
||||
1. Qt `QSharedMemory`(跨平台)
|
||||
2. 檔案鎖(Unix: fcntl / Windows: O_CREAT|O_EXCL)
|
||||
3. 自動清理 5 分鐘以上的過期鎖定檔案
|
||||
|
||||
---
|
||||
|
||||
## 4. 資料流
|
||||
|
||||
### 4.1 設計階段資料流(Design Time)
|
||||
|
||||
```
|
||||
使用者拖拽節點
|
||||
│
|
||||
▼
|
||||
NodeGraphQt 視覺圖形
|
||||
│
|
||||
▼
|
||||
core/pipeline.py
|
||||
analyze_pipeline_stages()
|
||||
│
|
||||
▼
|
||||
List[PipelineStage](邏輯 Stage 列表)
|
||||
│
|
||||
├──→ UI 顯示 Stage 數量(狀態列)
|
||||
└──→ 驗證錯誤提示(Validation Errors)
|
||||
```
|
||||
|
||||
### 4.2 執行階段資料流(Runtime)
|
||||
|
||||
```
|
||||
輸入來源(相機 / 影片 / 圖片)
|
||||
│
|
||||
▼
|
||||
camera_source.py / video_source.py
|
||||
│ numpy.ndarray(BGR 影像幀)
|
||||
▼
|
||||
InferencePipeline.put_data()
|
||||
│
|
||||
▼
|
||||
pipeline_input_queue(Queue, maxsize=100)
|
||||
│
|
||||
▼
|
||||
coordinator_thread(協調器執行緒)
|
||||
建立 PipelineData 包裝器
|
||||
│
|
||||
▼(依序通過每個 Stage)
|
||||
PipelineStage[0].input_queue
|
||||
│
|
||||
▼
|
||||
worker_thread[0]
|
||||
1. input_preprocessor(可選的 Stage 間前處理)
|
||||
2. MultiDongle.preprocess_frame()(BGR → BGR565 格式轉換)
|
||||
3. MultiDongle.put_input()(送入推論佇列)
|
||||
4. MultiDongle.get_latest_inference_result()(非阻塞取結果)
|
||||
5. 更新 PipelineData.stage_results
|
||||
│
|
||||
▼
|
||||
PipelineStage[0].output_queue
|
||||
│
|
||||
▼(下一個 Stage...)
|
||||
│
|
||||
▼
|
||||
pipeline_output_queue(Queue, maxsize=50)
|
||||
│
|
||||
├──→ result_callback(UI 更新)
|
||||
└──→ stats_callback(效能統計)
|
||||
```
|
||||
|
||||
### 4.3 .mflow 檔案格式
|
||||
|
||||
Pipeline 儲存為 JSON 格式:
|
||||
|
||||
```json
|
||||
{
|
||||
"nodes": [
|
||||
{
|
||||
"type": "ModelNode",
|
||||
"name": "Stage 1 Model",
|
||||
"properties": {
|
||||
"model_path": "/path/to/model.nef",
|
||||
"dongle_series": "720",
|
||||
"num_dongles": 2
|
||||
},
|
||||
"position": [100, 200]
|
||||
}
|
||||
],
|
||||
"connections": [
|
||||
{"from_node": "input_0", "from_port": "output", "to_node": "model_0", "to_port": "input"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. 技術決策紀錄(ADR)
|
||||
|
||||
### ADR-001:選用 PyQt5 作為 GUI 框架
|
||||
|
||||
**決策**:使用 PyQt5(>= 5.15.11)
|
||||
|
||||
**原因:**
|
||||
- NodeGraphQt 依賴 PyQt5,無法使用其他框架
|
||||
- PyQt5 在 Windows 上有成熟的支援
|
||||
- 提供豐富的 Widget 與 Signal/Slot 機制
|
||||
|
||||
**取捨:**
|
||||
- 限制 Python 版本在 3.9–3.11(PyQt5 + Kneron SDK 相容性)
|
||||
- PyQt6 不向下相容,短期不考慮遷移
|
||||
|
||||
### ADR-002:選用 NodeGraphQt 作為視覺節點編輯器
|
||||
|
||||
**決策**:使用 NodeGraphQt(>= 0.6.40)
|
||||
|
||||
**原因:**
|
||||
- 提供完整的拖拽節點圖形編輯能力,開發成本低
|
||||
- 支援節點連接、屬性面板、視覺化輸出
|
||||
|
||||
**取捨:**
|
||||
- NodeGraphQt 的 UI 客製化能力有限(如節點顏色、形狀)
|
||||
- 節點識別採用多重 fallback 機制(透過 `__identifier__`、`NODE_NAME` 等),因 NodeGraphQt 版本差異可能造成 API 不一致
|
||||
|
||||
### ADR-003:多執行緒 Pipeline 架構
|
||||
|
||||
**決策**:每個 Stage 一個 Worker Thread + 一個 Coordinator Thread
|
||||
|
||||
**原因:**
|
||||
- 推論為 CPU/硬體密集操作,多執行緒可避免 UI 阻塞
|
||||
- 各 Stage 獨立執行緒允許流水線(pipelining)並行,提升吞吐量
|
||||
|
||||
**取捨:**
|
||||
- 協調器採用循序(sequential)方式傳遞資料,並非真正平行(真正平行需要 DAG 調度器)
|
||||
- 使用 `queue.Queue` 進行執行緒間通訊,有固定的記憶體上限
|
||||
|
||||
### ADR-004:非阻塞式推論結果取得
|
||||
|
||||
**決策**:`MultiDongle.get_latest_inference_result()` 採用非阻塞模式
|
||||
|
||||
**原因:**
|
||||
- 與 Kneron 範例程式碼(example.py)的設計模式一致
|
||||
- 避免推論延遲阻塞整個 Pipeline 執行緒
|
||||
|
||||
**取捨:**
|
||||
- 結果可能為 None(尚未完成),需要 async/processing 狀態的過濾邏輯
|
||||
|
||||
### ADR-005:FPS 計算採用累積式
|
||||
|
||||
**決策**:`completed_counter / elapsed_time`(從第一個結果開始計算)
|
||||
|
||||
**原因:**
|
||||
- 與 Kneron 官方範例的計算方式一致,確保可比性
|
||||
- 排除熱機(warm-up)期間的異常低 FPS
|
||||
|
||||
**取捨:**
|
||||
- 無法反映即時的 FPS 波動(適合穩定場景,不適合延遲敏感場景)
|
||||
|
||||
### ADR-006:PyInstaller 打包
|
||||
|
||||
**決策**:使用 PyInstaller(`main.spec`)產生獨立可執行檔
|
||||
|
||||
**原因:**
|
||||
- 目標用戶(系統整合商)可能沒有 Python 環境
|
||||
- 簡化部署流程
|
||||
|
||||
**取捨:**
|
||||
- 打包後的執行檔體積較大
|
||||
- Kneron KP SDK 的動態函式庫需要正確包含在打包設定中
|
||||
|
||||
---
|
||||
|
||||
## 6. 已知限制與技術債
|
||||
|
||||
### 6.1 已知 Bug
|
||||
|
||||
| Bug | 狀態 | 影響 |
|
||||
|-----|------|------|
|
||||
| 節點屬性顯示問題 | 未修復(v0.0.2 記錄) | 右面板 Properties Tab 可能顯示錯誤 |
|
||||
| 輸出視覺化異常(含後處理結果) | 未修復(v0.0.2 記錄) | 輸出畫面可能不正確 |
|
||||
|
||||
### 6.2 技術債
|
||||
|
||||
| 項目 | 嚴重度 | 說明 |
|
||||
|------|--------|------|
|
||||
| 根目錄 debug 腳本未整理 | 低 | `debug_*.py`、`force_cleanup.py` 等應移至 `tools/` |
|
||||
| tests/ 命名混亂 | 中 | 42 個腳本缺乏系統性分類,部分非 test_ 開頭 |
|
||||
| 缺乏 pytest 測試框架 | 中 | 核心模組(InferencePipeline、MultiDongle)無 pytest 覆蓋 |
|
||||
| Coordinator 為循序設計 | 中 | 真正的 Stage 並行需要重構協調器為 DAG 模式 |
|
||||
| 節點識別多重 fallback | 低 | 可讀性差,應統一為單一識別策略 |
|
||||
| RTSP 串流僅基本支援 | 低 | 完整 RTSP 功能未在當前路線圖中 |
|
||||
|
||||
### 6.3 效能限制
|
||||
|
||||
- **協調器為循序傳遞**:目前 Coordinator 依序將資料傳給 Stage 0 → Stage 1,無真正的平行推論(真正平行需重構為流水線佇列模式)
|
||||
- **FPS 計算不反映即時波動**:累積式 FPS 在長時間執行後準確,但短期波動不可見
|
||||
- **輸出佇列上限 50**:高吞吐量場景下可能成為瓶頸
|
||||
|
||||
---
|
||||
|
||||
## 7. 未來架構演進方向
|
||||
|
||||
### Phase 1:效能視覺化(對應 DEVELOPMENT_ROADMAP Phase 1)
|
||||
|
||||
**需要新增的架構元件:**
|
||||
|
||||
```python
|
||||
# 新增模組:core/performance/
|
||||
class PerformanceBenchmarker:
|
||||
"""自動化效能測試器"""
|
||||
def run_sequential_benchmark(self, pipeline_config) -> BenchmarkResult
|
||||
def run_parallel_benchmark(self, pipeline_config) -> BenchmarkResult
|
||||
def calculate_speedup(self, seq: BenchmarkResult, par: BenchmarkResult) -> float
|
||||
|
||||
class PerformanceHistory:
|
||||
"""效能歷史記錄(本地 JSON 儲存)"""
|
||||
def record(self, result: BenchmarkResult)
|
||||
def get_history(self, limit: int) -> List[BenchmarkResult]
|
||||
```
|
||||
|
||||
**UI 層新增:**
|
||||
- `ui/components/performance_dashboard.py`:即時 FPS/延遲折線圖(使用 pyqtgraph 或 matplotlib)
|
||||
- `ui/dialogs/benchmark_dialog.py`:Benchmark 啟動與結果呈現
|
||||
|
||||
**架構考量:**
|
||||
- Benchmark 需要能控制 `InferencePipeline` 以單裝置/多裝置模式執行,需要在 `StageConfig` 層級提供模式切換介面
|
||||
- 效能圖表更新須在獨立執行緒中產生資料,透過 Qt Signal 傳遞到 UI 執行緒
|
||||
|
||||
### Phase 2:裝置管理(對應 DEVELOPMENT_ROADMAP Phase 2)
|
||||
|
||||
**需要新增的架構元件:**
|
||||
|
||||
```python
|
||||
# 強化 core/functions/Multidongle.py
|
||||
class DeviceManager:
|
||||
"""裝置管理器"""
|
||||
def scan_devices() -> List[DeviceInfo]
|
||||
def get_device_health(device_id: str) -> DeviceHealth
|
||||
def assign_device(device_id: str, stage_id: str)
|
||||
def get_load_balance_recommendation() -> Dict[str, str]
|
||||
|
||||
@dataclass
|
||||
class DeviceInfo:
|
||||
device_id: str
|
||||
series: str # KL520/KL720/KL1080
|
||||
status: str # online/offline/busy
|
||||
gops: int # 算力(來自 DongleSeriesSpec)
|
||||
assigned_stage: Optional[str]
|
||||
```
|
||||
|
||||
**UI 層新增:**
|
||||
- `ui/components/device_management_panel.py`:裝置狀態儀表板
|
||||
|
||||
### Phase 3:優化引擎(對應 DEVELOPMENT_ROADMAP Phase 3)
|
||||
|
||||
**需要新增的架構元件:**
|
||||
|
||||
```python
|
||||
# 新增模組:core/optimization/
|
||||
class OptimizationEngine:
|
||||
def analyze_pipeline(self, stats: PipelineStats) -> List[OptimizationSuggestion]
|
||||
def predict_performance(self, config: PipelineConfig) -> PerformancePrediction
|
||||
|
||||
@dataclass
|
||||
class OptimizationSuggestion:
|
||||
type: str # "rebalance_devices" | "remove_redundant_node" | ...
|
||||
description: str
|
||||
estimated_improvement: float # 預估效能提升 %
|
||||
action: Callable # 可執行的改善動作
|
||||
```
|
||||
|
||||
### 架構演進的長期考量
|
||||
|
||||
1. **Coordinator 重構**:當前循序協調器在多 Stage Pipeline 中形成瓶頸。長期應重構為流水線(pipeline)模式,讓 Stage N+1 在 Stage N 處理下一幀時就開始處理上一幀的結果。
|
||||
|
||||
2. **測試架構建立**:建立 pytest 測試框架,核心模組需達到 80% 以上覆蓋率(特別是 `InferencePipeline` 的佇列邏輯、`pipeline.py` 的 Stage 分析邏輯)。
|
||||
|
||||
3. **型別標註完善**:目前部分模組缺乏完整型別標註,建議逐步加入 mypy 靜態分析。
|
||||
39
.autoflow/progress.md
Normal file
39
.autoflow/progress.md
Normal file
@ -0,0 +1,39 @@
|
||||
# 專案進度 — Cluster4NPU UI
|
||||
|
||||
## 目的:接入既有專案 → 文件補齊 → Phase 1 開發
|
||||
## 當前階段:Phase 1 開發完成,待執行測試
|
||||
## 當前狀態:進行中
|
||||
## 最後更新:2026-04-05
|
||||
|
||||
## 進度表
|
||||
|
||||
| 階段 | 狀態 | 完成時間 | 備註 |
|
||||
|------|------|----------|------|
|
||||
| 專案接入 | ✅ 已完成 | 2026-04-05 | 本地路徑 |
|
||||
| 專案健檢 | ✅ 已完成 | 2026-04-05 | 見 00-onboarding/health-check.md |
|
||||
| PRD 產出 | ✅ 已完成 | 2026-04-05 | 02-prd/PRD.md |
|
||||
| Design Doc 產出 | ✅ 已完成 | 2026-04-05 | 04-architecture/design-doc.md |
|
||||
| TDD 產出 | ✅ 已完成 | 2026-04-05 | 04-architecture/TDD.md |
|
||||
| 交叉審閱 | ✅ 已完成 | 2026-04-05 | PM 審閱 TDD,缺口已補充 |
|
||||
| TDD 補充(Phase 4 功能 11) | ✅ 已完成 | 2026-04-05 | reportlab PDF + csv 標準庫 |
|
||||
| Phase 1 後端實作 | ✅ Review 通過 | 2026-04-05 | PerformanceBenchmarker + PerformanceHistory(31 tests) |
|
||||
| Phase 1 UI 實作 | ✅ Review 通過 | 2026-04-05 | PerformanceDashboard + BenchmarkDialog(58 tests total) |
|
||||
| Phase 1 整合到 dashboard | ✅ Review 通過 | 2026-04-05 | dashboard.py 整合完成 |
|
||||
| Phase 2 後端實作 | ✅ Review 通過 | 2026-04-05 | DeviceManager + BottleneckAlert(94 tests) |
|
||||
| Phase 2 UI 實作 | ✅ Review 通過 | 2026-04-05 | DeviceManagementPanel,已整合到 dashboard |
|
||||
| Phase 3 開發 | ✅ Review 通過 | 2026-04-06 | OptimizationEngine + TemplateManager(154 tests) |
|
||||
| Phase 4 開發 | ✅ Review 通過 | 2026-04-06 | ReportExporter + ExportReportDialog(192 tests) |
|
||||
|
||||
## 當前待辦
|
||||
|
||||
- [ ] 執行 Phase 1 整合測試確認所有元件協同運作
|
||||
- [ ] 決定是否繼續 Phase 2
|
||||
|
||||
## 未解決問題
|
||||
|
||||
- 無
|
||||
|
||||
## 重要決策紀錄
|
||||
|
||||
- 程式碼來源:本地路徑(非 GitHub)
|
||||
- 文件補齊策略:從程式碼反向整理,不補設計稿(無現有 UI 截圖或 Wireframe)
|
||||
13
.gitignore
vendored
13
.gitignore
vendored
@ -35,7 +35,6 @@ env/
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
@ -94,3 +93,15 @@ celerybeat-schedule
|
||||
# Windows
|
||||
Thumbs.db
|
||||
|
||||
# Kneron firmware/models and large artifacts
|
||||
*.nef
|
||||
fw_*.bin
|
||||
*.zip
|
||||
*.7z
|
||||
*.tar
|
||||
*.tar.gz
|
||||
*.tgz
|
||||
*.mflow
|
||||
# Autoflow Agent(由 autoflow-agent init 自動產生)
|
||||
.claude/
|
||||
.autoflow/CLAUDE.md.backup.*
|
||||
|
||||
54
AGENTS.md
Normal file
54
AGENTS.md
Normal file
@ -0,0 +1,54 @@
|
||||
# Repository Guidelines
|
||||
|
||||
## Project Structure & Module Organization
|
||||
- `main.py`: Application entry point.
|
||||
- `core/`: Engine and logic
|
||||
- `core/functions/`: inference, device, and workflow orchestration
|
||||
- `core/nodes/`: node types and base classes
|
||||
- `core/pipeline.py`: pipeline analysis/validation
|
||||
- `ui/`: PyQt5 UI (windows, dialogs, components)
|
||||
- `config/`: settings and theme
|
||||
- `resources/`: assets
|
||||
- `tests/` + root `test_*.py`: runnable test scripts
|
||||
|
||||
## Build, Test, and Development Commands
|
||||
- Environment: Python 3.9–3.11.
|
||||
- Setup (uv): `uv venv && . .venv/bin/activate` (Windows: `.venv\Scripts\activate`), then `uv pip install -e .`
|
||||
- Setup (pip): `python -m venv .venv && activate && pip install -e .`
|
||||
- Run app: `python main.py`
|
||||
- Run tests (examples):
|
||||
- `python tests/test_integration.py`
|
||||
- `python tests/test_deploy.py`
|
||||
- Many tests are direct scripts; run from repo root.
|
||||
|
||||
## Coding Style & Naming Conventions
|
||||
- Python, PEP 8, 4-space indents.
|
||||
- Names: modules/functions `snake_case`, classes `PascalCase`, constants `UPPER_SNAKE_CASE`.
|
||||
- Prefer type hints and docstrings for new/changed code.
|
||||
- Separation: keep UI in `ui/`; business logic in `core/`; avoid mixing concerns.
|
||||
|
||||
## Testing Guidelines
|
||||
- Place runnable scripts under `tests/` and name `test_*.py`.
|
||||
- Follow TDD principles in `CLAUDE.md` (small, focused tests; Red → Green → Refactor).
|
||||
- GUI tests: create a minimal `QApplication` as needed; keep long-running or hardware-dependent tests optional.
|
||||
- Example pattern: `if __name__ == "__main__": run_all_tests()` to allow direct execution.
|
||||
|
||||
## Commit & Pull Request Guidelines
|
||||
- Small, atomic commits; all tests pass before commit.
|
||||
- Message style: imperative mood; note change type e.g. `[Structural]` vs `[Behavioral]` per `CLAUDE.md`.
|
||||
- PRs include: clear description, linked issue, test plan, and screenshots/GIFs for UI changes.
|
||||
- Do not introduce unrelated refactors in feature/bugfix PRs.
|
||||
|
||||
## Security & Configuration Tips
|
||||
- Do not commit firmware (`fw_*.bin`) or model (`.nef`) files.
|
||||
- Avoid hard-coded absolute paths; use project-relative paths and config in `config/`.
|
||||
- Headless runs: set `QT_QPA_PLATFORM=offscreen` when needed.
|
||||
|
||||
## Agent-Specific Instructions
|
||||
- Scope: applies to entire repository tree.
|
||||
- Make minimal, targeted patches; do not add dependencies without discussion.
|
||||
- Prefer absolute imports from package root; keep edits consistent with existing structure and naming.
|
||||
|
||||
## TOOL to use
|
||||
- 你可以使用 「gemini -p "xxx"」來呼叫 gemini cli 這個工具做事情, gemini cli 的上下文 token 很大,你可以用它找專案裡的程式碼,上網查資料等。但禁止使用它修改或刪除檔案。以下是一個使用範例
|
||||
- Bash(gemini -p "找出專案裡使用 xAI 的地方")
|
||||
@ -74,3 +74,8 @@ When approaching a new feature:
|
||||
Follow this process precisely, always prioritizing clean, well-tested code over quick implementation.
|
||||
|
||||
Always write one test at a time, make it run, then improve structure. Always run all the tests (except long-running tests) each time.
|
||||
|
||||
|
||||
## TOOL to use
|
||||
- 你可以使用 「gemini -p "xxx"」來呼叫 gemini cli 這個工具做事情, gemini cli 的上下文 token 很大,你可以用它找專案裡的程式碼,上網查資料等。但禁止使用它修改或刪除檔案。以下是一個使用範例
|
||||
- Bash(gemini -p "找出專案裡使用 xAI 的地方")
|
||||
110
check_multi_series_config.py
Normal file
110
check_multi_series_config.py
Normal file
@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Check current multi-series configuration in saved .mflow files
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import glob
|
||||
|
||||
def check_mflow_files():
|
||||
"""Check .mflow files for multi-series configuration"""
|
||||
|
||||
# Look for .mflow files in common locations
|
||||
search_paths = [
|
||||
"*.mflow",
|
||||
"flows/*.mflow",
|
||||
"examples/*.mflow",
|
||||
"../*.mflow"
|
||||
]
|
||||
|
||||
mflow_files = []
|
||||
for pattern in search_paths:
|
||||
mflow_files.extend(glob.glob(pattern))
|
||||
|
||||
if not mflow_files:
|
||||
print("No .mflow files found in current directory")
|
||||
return
|
||||
|
||||
print(f"Found {len(mflow_files)} .mflow file(s):")
|
||||
|
||||
for mflow_file in mflow_files:
|
||||
print(f"\n=== Checking {mflow_file} ===")
|
||||
|
||||
try:
|
||||
with open(mflow_file, 'r') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Look for nodes with type "Model" or "ExactModelNode"
|
||||
nodes = data.get('nodes', [])
|
||||
model_nodes = [node for node in nodes if node.get('type') in ['Model', 'ExactModelNode']]
|
||||
|
||||
if not model_nodes:
|
||||
print(" No Model nodes found")
|
||||
continue
|
||||
|
||||
for i, node in enumerate(model_nodes):
|
||||
print(f"\n Model Node {i+1}:")
|
||||
print(f" Name: {node.get('name', 'Unnamed')}")
|
||||
|
||||
# Check both custom_properties and properties for multi-series config
|
||||
custom_properties = node.get('custom_properties', {})
|
||||
properties = node.get('properties', {})
|
||||
|
||||
# Multi-series config is typically in custom_properties
|
||||
config_props = custom_properties if custom_properties else properties
|
||||
|
||||
# Check multi-series configuration
|
||||
multi_series_mode = config_props.get('multi_series_mode', False)
|
||||
enabled_series = config_props.get('enabled_series', [])
|
||||
|
||||
print(f" multi_series_mode: {multi_series_mode}")
|
||||
print(f" enabled_series: {enabled_series}")
|
||||
|
||||
if multi_series_mode:
|
||||
print(" Multi-series port configurations:")
|
||||
for series in ['520', '720', '630', '730', '540']:
|
||||
port_ids = config_props.get(f'kl{series}_port_ids', '')
|
||||
if port_ids:
|
||||
print(f" kl{series}_port_ids: '{port_ids}'")
|
||||
|
||||
assets_folder = config_props.get('assets_folder', '')
|
||||
if assets_folder:
|
||||
print(f" assets_folder: '{assets_folder}'")
|
||||
else:
|
||||
print(" assets_folder: (not set)")
|
||||
else:
|
||||
print(" Multi-series mode is DISABLED")
|
||||
print(" Current single-series configuration:")
|
||||
port_ids = properties.get('port_ids', [])
|
||||
model_path = properties.get('model_path', '')
|
||||
print(f" port_ids: {port_ids}")
|
||||
print(f" model_path: '{model_path}'")
|
||||
|
||||
except Exception as e:
|
||||
print(f" Error reading file: {e}")
|
||||
|
||||
def print_configuration_guide():
|
||||
"""Print guide for setting up multi-series configuration"""
|
||||
print("\n" + "="*60)
|
||||
print("MULTI-SERIES CONFIGURATION GUIDE")
|
||||
print("="*60)
|
||||
print()
|
||||
print("To enable multi-series inference, set these properties in your Model Node:")
|
||||
print()
|
||||
print("1. multi_series_mode = True")
|
||||
print("2. enabled_series = ['520', '720']")
|
||||
print("3. kl520_port_ids = '28,32'")
|
||||
print("4. kl720_port_ids = '4'")
|
||||
print("5. assets_folder = (optional, for auto model/firmware detection)")
|
||||
print()
|
||||
print("Expected devices found:")
|
||||
print(" KL520 devices on ports: 28, 32")
|
||||
print(" KL720 device on port: 4")
|
||||
print()
|
||||
print("If multi_series_mode is False or not set, the system will use")
|
||||
print("single-series mode with only the first available device.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_mflow_files()
|
||||
print_configuration_guide()
|
||||
1
core/device/__init__.py
Normal file
1
core/device/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""core.device — device management subpackage."""
|
||||
32
core/device/bottleneck.py
Normal file
32
core/device/bottleneck.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""
|
||||
core/device/bottleneck.py
|
||||
|
||||
BottleneckAlert dataclass — describes a detected pipeline bottleneck.
|
||||
|
||||
Integration with InferencePipeline is deferred to a later phase.
|
||||
This module only defines the data structure.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class BottleneckAlert:
|
||||
"""Describes a detected pipeline bottleneck in a single Stage.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
stage_id:
|
||||
The pipeline Stage that is experiencing the bottleneck.
|
||||
queue_fill_rate:
|
||||
Input queue utilisation as a fraction in [0.0, 1.0].
|
||||
suggested_action:
|
||||
Human-readable suggestion (e.g. "Add more Dongles to this stage").
|
||||
severity:
|
||||
Either ``"warning"`` (fill_rate > 0.8) or ``"critical"``
|
||||
(fill_rate > 0.95).
|
||||
"""
|
||||
|
||||
stage_id: str
|
||||
queue_fill_rate: float
|
||||
suggested_action: str
|
||||
severity: str # "warning" | "critical"
|
||||
217
core/device/device_manager.py
Normal file
217
core/device/device_manager.py
Normal file
@ -0,0 +1,217 @@
|
||||
"""
|
||||
core/device/device_manager.py
|
||||
|
||||
DeviceManager — manages NPU Dongle discovery, health, and assignment.
|
||||
|
||||
Design:
|
||||
- scan_devices() calls the Kneron KP SDK but accepts an injectable kp_api
|
||||
parameter so tests can supply a Mock without real hardware.
|
||||
- DongleSeriesSpec constants are inlined here to avoid a circular import
|
||||
from core.functions.Multidongle.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# GOPS table (mirrors DongleSeriesSpec in Multidongle.py)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_PRODUCT_ID_TO_SERIES: Dict[int, str] = {
|
||||
0x100: "KL520",
|
||||
0x720: "KL720",
|
||||
0x630: "KL630",
|
||||
0x730: "KL730",
|
||||
}
|
||||
|
||||
_SERIES_GOPS: Dict[str, int] = {
|
||||
"KL520": 2,
|
||||
"KL720": 28,
|
||||
"KL630": 400,
|
||||
"KL730": 1600,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data classes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@dataclass
|
||||
class DeviceInfo:
|
||||
"""Snapshot of a single NPU Dongle's state."""
|
||||
|
||||
device_id: str # unique id, e.g. "usb-<port_id>"
|
||||
series: str # "KL520" | "KL720" | ...
|
||||
product_id: int # raw USB product ID
|
||||
status: str # "online" | "offline" | "busy"
|
||||
gops: int # compute capacity
|
||||
assigned_stage: Optional[str] # currently assigned stage ID, or None
|
||||
current_fps: float # live inference throughput
|
||||
utilization_pct: float # 0.0 – 100.0
|
||||
|
||||
|
||||
@dataclass
|
||||
class DeviceHealth:
|
||||
"""Health snapshot of a single NPU Dongle."""
|
||||
|
||||
device_id: str
|
||||
temperature_celsius: Optional[float] # None if SDK does not support it
|
||||
error_count: int
|
||||
last_error: Optional[str]
|
||||
uptime_seconds: float
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class DeviceManager:
|
||||
"""Manages NPU Dongle discovery, health queries, and stage assignment.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
kp_api:
|
||||
Kneron KP SDK module reference. Pass ``None`` to import the real
|
||||
``kp`` module at runtime, or inject a Mock in tests.
|
||||
"""
|
||||
|
||||
def __init__(self, kp_api=None) -> None:
|
||||
if kp_api is None:
|
||||
import kp as _kp # real SDK (requires hardware)
|
||||
self._kp = _kp
|
||||
else:
|
||||
self._kp = kp_api
|
||||
|
||||
# Known devices, populated by scan_devices()
|
||||
self._devices: Dict[str, DeviceInfo] = {}
|
||||
# stage assignments: {device_id: stage_id}
|
||||
self._assignments: Dict[str, str] = {}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def scan_devices(self) -> List[DeviceInfo]:
|
||||
"""Scan for connected Kneron Dongles and update internal state.
|
||||
|
||||
Returns
|
||||
-------
|
||||
List[DeviceInfo]
|
||||
All currently connected devices, each with status "online".
|
||||
"""
|
||||
try:
|
||||
descriptors = self._kp.core.scan_devices()
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
if not descriptors or descriptors.device_descriptor_number == 0:
|
||||
return []
|
||||
|
||||
found: Dict[str, DeviceInfo] = {}
|
||||
for desc in descriptors.device_descriptor_list:
|
||||
try:
|
||||
port_id = desc.usb_port_id
|
||||
product_id = desc.product_id
|
||||
device_id = f"usb-{port_id}"
|
||||
series = _PRODUCT_ID_TO_SERIES.get(product_id, "Unknown")
|
||||
gops = _SERIES_GOPS.get(series, 0)
|
||||
assigned = self._assignments.get(device_id)
|
||||
info = DeviceInfo(
|
||||
device_id=device_id,
|
||||
series=series,
|
||||
product_id=product_id,
|
||||
status="online",
|
||||
gops=gops,
|
||||
assigned_stage=assigned,
|
||||
current_fps=0.0,
|
||||
utilization_pct=0.0,
|
||||
)
|
||||
found[device_id] = info
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
self._devices = found
|
||||
return list(self._devices.values())
|
||||
|
||||
def get_device_health(self, device_id: str) -> DeviceHealth:
|
||||
"""Return a health snapshot for the given device.
|
||||
|
||||
Temperature is returned as ``None`` because the current KP SDK
|
||||
version does not expose thermal sensors.
|
||||
"""
|
||||
return DeviceHealth(
|
||||
device_id=device_id,
|
||||
temperature_celsius=None,
|
||||
error_count=0,
|
||||
last_error=None,
|
||||
uptime_seconds=0.0,
|
||||
)
|
||||
|
||||
def assign_device(self, device_id: str, stage_id: str) -> bool:
|
||||
"""Assign *device_id* to *stage_id*.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool
|
||||
``False`` if the device is unknown or already assigned to a
|
||||
different stage; ``True`` on success.
|
||||
"""
|
||||
device = self._devices.get(device_id)
|
||||
if device is None or device.status == "offline":
|
||||
return False
|
||||
existing_stage = self._assignments.get(device_id)
|
||||
if existing_stage is not None and existing_stage != stage_id:
|
||||
return False # already assigned to a different stage
|
||||
self._assignments[device_id] = stage_id
|
||||
self._devices[device_id].assigned_stage = stage_id
|
||||
return True
|
||||
|
||||
def unassign_device(self, device_id: str) -> bool:
|
||||
"""Release *device_id* from its current stage assignment.
|
||||
|
||||
Returns
|
||||
-------
|
||||
bool
|
||||
``False`` if the device is unknown; ``True`` on success.
|
||||
"""
|
||||
if device_id not in self._devices:
|
||||
return False
|
||||
self._assignments.pop(device_id, None)
|
||||
self._devices[device_id].assigned_stage = None
|
||||
return True
|
||||
|
||||
def get_load_balance_recommendation(
|
||||
self, stages: List[str]
|
||||
) -> Dict[str, str]:
|
||||
"""Recommend device-to-stage assignment by GOPS (descending).
|
||||
|
||||
Higher-GOPS devices are assigned to earlier stages. Stages with
|
||||
no available device are mapped to an empty string.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
stages:
|
||||
Ordered list of stage IDs (first stage has highest priority).
|
||||
|
||||
Returns
|
||||
-------
|
||||
Dict[str, str]
|
||||
``{stage_id: device_id}``; device_id is "" if unavailable.
|
||||
"""
|
||||
available = sorted(
|
||||
self._devices.values(),
|
||||
key=lambda d: d.gops,
|
||||
reverse=True,
|
||||
)
|
||||
recommendation: Dict[str, str] = {}
|
||||
for i, stage_id in enumerate(stages):
|
||||
if i < len(available):
|
||||
recommendation[stage_id] = available[i].device_id
|
||||
else:
|
||||
recommendation[stage_id] = ""
|
||||
return recommendation
|
||||
|
||||
def get_device_statistics(self) -> Dict[str, DeviceInfo]:
|
||||
"""Return a snapshot of all known devices keyed by device_id."""
|
||||
return dict(self._devices)
|
||||
@ -7,7 +7,7 @@ from dataclasses import dataclass
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import numpy as np
|
||||
|
||||
from Multidongle import MultiDongle, PreProcessor, PostProcessor, DataProcessor
|
||||
from .Multidongle import MultiDongle, PreProcessor, PostProcessor, DataProcessor
|
||||
|
||||
@dataclass
|
||||
class StageConfig:
|
||||
@ -19,6 +19,8 @@ class StageConfig:
|
||||
model_path: str
|
||||
upload_fw: bool
|
||||
max_queue_size: int = 50
|
||||
# Multi-series support
|
||||
multi_series_config: Optional[Dict[str, Any]] = None # For multi-series mode
|
||||
# Inter-stage processing
|
||||
input_preprocessor: Optional[PreProcessor] = None # Before this stage
|
||||
output_postprocessor: Optional[PostProcessor] = None # After this stage
|
||||
@ -43,15 +45,25 @@ class PipelineStage:
|
||||
self.stage_id = config.stage_id
|
||||
|
||||
# Initialize MultiDongle for this stage
|
||||
self.multidongle = MultiDongle(
|
||||
port_id=config.port_ids,
|
||||
scpu_fw_path=config.scpu_fw_path,
|
||||
ncpu_fw_path=config.ncpu_fw_path,
|
||||
model_path=config.model_path,
|
||||
upload_fw=config.upload_fw,
|
||||
auto_detect=config.auto_detect if hasattr(config, 'auto_detect') else False,
|
||||
max_queue_size=config.max_queue_size
|
||||
)
|
||||
if config.multi_series_config:
|
||||
# Multi-series mode
|
||||
self.multidongle = MultiDongle(
|
||||
multi_series_config=config.multi_series_config,
|
||||
max_queue_size=config.max_queue_size
|
||||
)
|
||||
print(f"[Stage {self.stage_id}] Initialized in multi-series mode with config: {list(config.multi_series_config.keys())}")
|
||||
else:
|
||||
# Single-series mode (legacy)
|
||||
self.multidongle = MultiDongle(
|
||||
port_id=config.port_ids,
|
||||
scpu_fw_path=config.scpu_fw_path,
|
||||
ncpu_fw_path=config.ncpu_fw_path,
|
||||
model_path=config.model_path,
|
||||
upload_fw=config.upload_fw,
|
||||
auto_detect=config.auto_detect if hasattr(config, 'auto_detect') else False,
|
||||
max_queue_size=config.max_queue_size
|
||||
)
|
||||
print(f"[Stage {self.stage_id}] Initialized in single-series mode")
|
||||
|
||||
# Store preprocessor and postprocessor for later use
|
||||
self.stage_preprocessor = config.stage_preprocessor
|
||||
@ -78,6 +90,13 @@ class PipelineStage:
|
||||
"""Initialize the stage"""
|
||||
print(f"[Stage {self.stage_id}] Initializing...")
|
||||
try:
|
||||
# Set postprocessor if available
|
||||
if self.stage_postprocessor:
|
||||
self.multidongle.set_postprocess_options(self.stage_postprocessor.options)
|
||||
print(f"[Stage {self.stage_id}] Applied postprocessor: {self.stage_postprocessor.options.postprocess_type.value}")
|
||||
else:
|
||||
print(f"[Stage {self.stage_id}] No postprocessor configured, using default")
|
||||
|
||||
self.multidongle.initialize()
|
||||
self.multidongle.start()
|
||||
print(f"[Stage {self.stage_id}] Initialized successfully")
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,375 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
智慧拓撲排序算法演示 (獨立版本)
|
||||
|
||||
不依賴外部模組,純粹展示拓撲排序算法的核心功能
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import List, Dict, Any, Tuple
|
||||
from collections import deque
|
||||
|
||||
class TopologyDemo:
|
||||
"""演示拓撲排序算法的類別"""
|
||||
|
||||
def __init__(self):
|
||||
self.stage_order = []
|
||||
|
||||
def analyze_pipeline(self, pipeline_data: Dict[str, Any]):
|
||||
"""分析pipeline並執行拓撲排序"""
|
||||
print("Starting intelligent pipeline topology analysis...")
|
||||
|
||||
# 提取模型節點
|
||||
model_nodes = [node for node in pipeline_data.get('nodes', [])
|
||||
if 'model' in node.get('type', '').lower()]
|
||||
connections = pipeline_data.get('connections', [])
|
||||
|
||||
if not model_nodes:
|
||||
print(" Warning: No model nodes found!")
|
||||
return []
|
||||
|
||||
# 建立依賴圖
|
||||
dependency_graph = self._build_dependency_graph(model_nodes, connections)
|
||||
|
||||
# 檢測循環
|
||||
cycles = self._detect_cycles(dependency_graph)
|
||||
if cycles:
|
||||
print(f" Warning: Found {len(cycles)} cycles!")
|
||||
dependency_graph = self._resolve_cycles(dependency_graph, cycles)
|
||||
|
||||
# 執行拓撲排序
|
||||
sorted_stages = self._topological_sort_with_optimization(dependency_graph, model_nodes)
|
||||
|
||||
# 計算指標
|
||||
metrics = self._calculate_pipeline_metrics(sorted_stages, dependency_graph)
|
||||
self._display_pipeline_analysis(sorted_stages, metrics)
|
||||
|
||||
return sorted_stages
|
||||
|
||||
def _build_dependency_graph(self, model_nodes: List[Dict], connections: List[Dict]) -> Dict[str, Dict]:
|
||||
"""建立依賴圖"""
|
||||
print(" Building dependency graph...")
|
||||
|
||||
graph = {}
|
||||
for node in model_nodes:
|
||||
graph[node['id']] = {
|
||||
'node': node,
|
||||
'dependencies': set(),
|
||||
'dependents': set(),
|
||||
'depth': 0
|
||||
}
|
||||
|
||||
# 分析連接
|
||||
for conn in connections:
|
||||
output_node_id = conn.get('output_node')
|
||||
input_node_id = conn.get('input_node')
|
||||
|
||||
if output_node_id in graph and input_node_id in graph:
|
||||
graph[input_node_id]['dependencies'].add(output_node_id)
|
||||
graph[output_node_id]['dependents'].add(input_node_id)
|
||||
|
||||
dep_count = sum(len(data['dependencies']) for data in graph.values())
|
||||
print(f" Graph built: {len(graph)} nodes, {dep_count} dependencies")
|
||||
return graph
|
||||
|
||||
def _detect_cycles(self, graph: Dict[str, Dict]) -> List[List[str]]:
|
||||
"""檢測循環"""
|
||||
print(" Checking for dependency cycles...")
|
||||
|
||||
cycles = []
|
||||
visited = set()
|
||||
rec_stack = set()
|
||||
|
||||
def dfs_cycle_detect(node_id, path):
|
||||
if node_id in rec_stack:
|
||||
cycle_start = path.index(node_id)
|
||||
cycle = path[cycle_start:] + [node_id]
|
||||
cycles.append(cycle)
|
||||
return True
|
||||
|
||||
if node_id in visited:
|
||||
return False
|
||||
|
||||
visited.add(node_id)
|
||||
rec_stack.add(node_id)
|
||||
path.append(node_id)
|
||||
|
||||
for dependent in graph[node_id]['dependents']:
|
||||
if dfs_cycle_detect(dependent, path):
|
||||
return True
|
||||
|
||||
path.pop()
|
||||
rec_stack.remove(node_id)
|
||||
return False
|
||||
|
||||
for node_id in graph:
|
||||
if node_id not in visited:
|
||||
dfs_cycle_detect(node_id, [])
|
||||
|
||||
if cycles:
|
||||
print(f" Warning: Found {len(cycles)} cycles")
|
||||
else:
|
||||
print(" No cycles detected")
|
||||
|
||||
return cycles
|
||||
|
||||
def _resolve_cycles(self, graph: Dict[str, Dict], cycles: List[List[str]]) -> Dict[str, Dict]:
|
||||
"""解決循環"""
|
||||
print(" Resolving dependency cycles...")
|
||||
|
||||
for cycle in cycles:
|
||||
node_names = [graph[nid]['node']['name'] for nid in cycle]
|
||||
print(f" Breaking cycle: {' → '.join(node_names)}")
|
||||
|
||||
if len(cycle) >= 2:
|
||||
node_to_break = cycle[-2]
|
||||
dependent_to_break = cycle[-1]
|
||||
|
||||
graph[dependent_to_break]['dependencies'].discard(node_to_break)
|
||||
graph[node_to_break]['dependents'].discard(dependent_to_break)
|
||||
|
||||
print(f" Broke dependency: {graph[node_to_break]['node']['name']} → {graph[dependent_to_break]['node']['name']}")
|
||||
|
||||
return graph
|
||||
|
||||
def _topological_sort_with_optimization(self, graph: Dict[str, Dict], model_nodes: List[Dict]) -> List[Dict]:
|
||||
"""執行優化的拓撲排序"""
|
||||
print(" Performing optimized topological sort...")
|
||||
|
||||
# 計算深度層級
|
||||
self._calculate_depth_levels(graph)
|
||||
|
||||
# 按深度分組
|
||||
depth_groups = self._group_by_depth(graph)
|
||||
|
||||
# 排序
|
||||
sorted_nodes = []
|
||||
for depth in sorted(depth_groups.keys()):
|
||||
group_nodes = depth_groups[depth]
|
||||
|
||||
group_nodes.sort(key=lambda nid: (
|
||||
len(graph[nid]['dependencies']),
|
||||
-len(graph[nid]['dependents']),
|
||||
graph[nid]['node']['name']
|
||||
))
|
||||
|
||||
for node_id in group_nodes:
|
||||
sorted_nodes.append(graph[node_id]['node'])
|
||||
|
||||
print(f" Sorted {len(sorted_nodes)} stages into {len(depth_groups)} execution levels")
|
||||
return sorted_nodes
|
||||
|
||||
def _calculate_depth_levels(self, graph: Dict[str, Dict]):
|
||||
"""計算深度層級"""
|
||||
print(" Calculating execution depth levels...")
|
||||
|
||||
no_deps = [nid for nid, data in graph.items() if not data['dependencies']]
|
||||
queue = deque([(nid, 0) for nid in no_deps])
|
||||
|
||||
while queue:
|
||||
node_id, depth = queue.popleft()
|
||||
|
||||
if graph[node_id]['depth'] < depth:
|
||||
graph[node_id]['depth'] = depth
|
||||
|
||||
for dependent in graph[node_id]['dependents']:
|
||||
queue.append((dependent, depth + 1))
|
||||
|
||||
def _group_by_depth(self, graph: Dict[str, Dict]) -> Dict[int, List[str]]:
|
||||
"""按深度分組"""
|
||||
depth_groups = {}
|
||||
|
||||
for node_id, data in graph.items():
|
||||
depth = data['depth']
|
||||
if depth not in depth_groups:
|
||||
depth_groups[depth] = []
|
||||
depth_groups[depth].append(node_id)
|
||||
|
||||
return depth_groups
|
||||
|
||||
def _calculate_pipeline_metrics(self, sorted_stages: List[Dict], graph: Dict[str, Dict]) -> Dict[str, Any]:
|
||||
"""計算指標"""
|
||||
print(" Calculating pipeline metrics...")
|
||||
|
||||
total_stages = len(sorted_stages)
|
||||
max_depth = max([data['depth'] for data in graph.values()]) + 1 if graph else 1
|
||||
|
||||
depth_distribution = {}
|
||||
for data in graph.values():
|
||||
depth = data['depth']
|
||||
depth_distribution[depth] = depth_distribution.get(depth, 0) + 1
|
||||
|
||||
max_parallel = max(depth_distribution.values()) if depth_distribution else 1
|
||||
critical_path = self._find_critical_path(graph)
|
||||
|
||||
return {
|
||||
'total_stages': total_stages,
|
||||
'pipeline_depth': max_depth,
|
||||
'max_parallel_stages': max_parallel,
|
||||
'parallelization_efficiency': (total_stages / max_depth) if max_depth > 0 else 1.0,
|
||||
'critical_path_length': len(critical_path),
|
||||
'critical_path': critical_path
|
||||
}
|
||||
|
||||
def _find_critical_path(self, graph: Dict[str, Dict]) -> List[str]:
|
||||
"""找出關鍵路徑"""
|
||||
longest_path = []
|
||||
|
||||
def dfs_longest_path(node_id, current_path):
|
||||
nonlocal longest_path
|
||||
|
||||
current_path.append(node_id)
|
||||
|
||||
if not graph[node_id]['dependents']:
|
||||
if len(current_path) > len(longest_path):
|
||||
longest_path = current_path.copy()
|
||||
else:
|
||||
for dependent in graph[node_id]['dependents']:
|
||||
dfs_longest_path(dependent, current_path)
|
||||
|
||||
current_path.pop()
|
||||
|
||||
for node_id, data in graph.items():
|
||||
if not data['dependencies']:
|
||||
dfs_longest_path(node_id, [])
|
||||
|
||||
return longest_path
|
||||
|
||||
def _display_pipeline_analysis(self, sorted_stages: List[Dict], metrics: Dict[str, Any]):
|
||||
"""顯示分析結果"""
|
||||
print("\n" + "="*60)
|
||||
print("INTELLIGENT PIPELINE TOPOLOGY ANALYSIS COMPLETE")
|
||||
print("="*60)
|
||||
|
||||
print(f"Pipeline Metrics:")
|
||||
print(f" Total Stages: {metrics['total_stages']}")
|
||||
print(f" Pipeline Depth: {metrics['pipeline_depth']} levels")
|
||||
print(f" Max Parallel Stages: {metrics['max_parallel_stages']}")
|
||||
print(f" Parallelization Efficiency: {metrics['parallelization_efficiency']:.1%}")
|
||||
|
||||
print(f"\nOptimized Execution Order:")
|
||||
for i, stage in enumerate(sorted_stages, 1):
|
||||
print(f" {i:2d}. {stage['name']} (ID: {stage['id'][:8]}...)")
|
||||
|
||||
if metrics['critical_path']:
|
||||
print(f"\nCritical Path ({metrics['critical_path_length']} stages):")
|
||||
critical_names = []
|
||||
for node_id in metrics['critical_path']:
|
||||
node_name = next((stage['name'] for stage in sorted_stages if stage['id'] == node_id), 'Unknown')
|
||||
critical_names.append(node_name)
|
||||
print(f" {' → '.join(critical_names)}")
|
||||
|
||||
print(f"\nPerformance Insights:")
|
||||
if metrics['parallelization_efficiency'] > 0.8:
|
||||
print(" Excellent parallelization potential!")
|
||||
elif metrics['parallelization_efficiency'] > 0.6:
|
||||
print(" Good parallelization opportunities available")
|
||||
else:
|
||||
print(" Limited parallelization - consider pipeline redesign")
|
||||
|
||||
if metrics['pipeline_depth'] <= 3:
|
||||
print(" Low latency pipeline - great for real-time applications")
|
||||
elif metrics['pipeline_depth'] <= 6:
|
||||
print(" Balanced pipeline depth - good throughput/latency trade-off")
|
||||
else:
|
||||
print(" Deep pipeline - optimized for maximum throughput")
|
||||
|
||||
print("="*60 + "\n")
|
||||
|
||||
def create_demo_pipelines():
|
||||
"""創建演示用的pipeline"""
|
||||
|
||||
# Demo 1: 簡單線性pipeline
|
||||
simple_pipeline = {
|
||||
"project_name": "Simple Linear Pipeline",
|
||||
"nodes": [
|
||||
{"id": "model_001", "name": "Object Detection", "type": "ExactModelNode"},
|
||||
{"id": "model_002", "name": "Fire Classification", "type": "ExactModelNode"},
|
||||
{"id": "model_003", "name": "Result Verification", "type": "ExactModelNode"}
|
||||
],
|
||||
"connections": [
|
||||
{"output_node": "model_001", "input_node": "model_002"},
|
||||
{"output_node": "model_002", "input_node": "model_003"}
|
||||
]
|
||||
}
|
||||
|
||||
# Demo 2: 並行pipeline
|
||||
parallel_pipeline = {
|
||||
"project_name": "Parallel Processing Pipeline",
|
||||
"nodes": [
|
||||
{"id": "model_001", "name": "RGB Processor", "type": "ExactModelNode"},
|
||||
{"id": "model_002", "name": "IR Processor", "type": "ExactModelNode"},
|
||||
{"id": "model_003", "name": "Depth Processor", "type": "ExactModelNode"},
|
||||
{"id": "model_004", "name": "Fusion Engine", "type": "ExactModelNode"}
|
||||
],
|
||||
"connections": [
|
||||
{"output_node": "model_001", "input_node": "model_004"},
|
||||
{"output_node": "model_002", "input_node": "model_004"},
|
||||
{"output_node": "model_003", "input_node": "model_004"}
|
||||
]
|
||||
}
|
||||
|
||||
# Demo 3: 複雜多層pipeline
|
||||
complex_pipeline = {
|
||||
"project_name": "Advanced Multi-Stage Fire Detection Pipeline",
|
||||
"nodes": [
|
||||
{"id": "model_rgb_001", "name": "RGB Feature Extractor", "type": "ExactModelNode"},
|
||||
{"id": "model_edge_002", "name": "Edge Feature Extractor", "type": "ExactModelNode"},
|
||||
{"id": "model_thermal_003", "name": "Thermal Feature Extractor", "type": "ExactModelNode"},
|
||||
{"id": "model_fusion_004", "name": "Feature Fusion", "type": "ExactModelNode"},
|
||||
{"id": "model_attention_005", "name": "Attention Mechanism", "type": "ExactModelNode"},
|
||||
{"id": "model_classifier_006", "name": "Fire Classifier", "type": "ExactModelNode"}
|
||||
],
|
||||
"connections": [
|
||||
{"output_node": "model_rgb_001", "input_node": "model_fusion_004"},
|
||||
{"output_node": "model_edge_002", "input_node": "model_fusion_004"},
|
||||
{"output_node": "model_thermal_003", "input_node": "model_attention_005"},
|
||||
{"output_node": "model_fusion_004", "input_node": "model_classifier_006"},
|
||||
{"output_node": "model_attention_005", "input_node": "model_classifier_006"}
|
||||
]
|
||||
}
|
||||
|
||||
# Demo 4: 有循環的pipeline (測試循環檢測)
|
||||
cycle_pipeline = {
|
||||
"project_name": "Pipeline with Cycles (Testing)",
|
||||
"nodes": [
|
||||
{"id": "model_A", "name": "Model A", "type": "ExactModelNode"},
|
||||
{"id": "model_B", "name": "Model B", "type": "ExactModelNode"},
|
||||
{"id": "model_C", "name": "Model C", "type": "ExactModelNode"}
|
||||
],
|
||||
"connections": [
|
||||
{"output_node": "model_A", "input_node": "model_B"},
|
||||
{"output_node": "model_B", "input_node": "model_C"},
|
||||
{"output_node": "model_C", "input_node": "model_A"} # 創建循環!
|
||||
]
|
||||
}
|
||||
|
||||
return [simple_pipeline, parallel_pipeline, complex_pipeline, cycle_pipeline]
|
||||
|
||||
def main():
|
||||
"""主演示函數"""
|
||||
print("INTELLIGENT PIPELINE TOPOLOGY SORTING DEMONSTRATION")
|
||||
print("="*60)
|
||||
print("This demo showcases our advanced pipeline analysis capabilities:")
|
||||
print("• Automatic dependency resolution")
|
||||
print("• Parallel execution optimization")
|
||||
print("• Cycle detection and prevention")
|
||||
print("• Critical path analysis")
|
||||
print("• Performance metrics calculation")
|
||||
print("="*60 + "\n")
|
||||
|
||||
demo = TopologyDemo()
|
||||
pipelines = create_demo_pipelines()
|
||||
demo_names = ["Simple Linear", "Parallel Processing", "Complex Multi-Stage", "Cycle Detection"]
|
||||
|
||||
for i, (pipeline, name) in enumerate(zip(pipelines, demo_names), 1):
|
||||
print(f"DEMO {i}: {name} Pipeline")
|
||||
print("="*50)
|
||||
demo.analyze_pipeline(pipeline)
|
||||
print("\n")
|
||||
|
||||
print("ALL DEMONSTRATIONS COMPLETED SUCCESSFULLY!")
|
||||
print("Ready for production deployment and progress reporting!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -23,10 +23,11 @@ Usage:
|
||||
|
||||
import json
|
||||
import os
|
||||
from typing import List, Dict, Any, Tuple
|
||||
from typing import List, Dict, Any, Tuple, Optional
|
||||
from dataclasses import dataclass
|
||||
|
||||
from InferencePipeline import StageConfig, InferencePipeline
|
||||
from .InferencePipeline import StageConfig, InferencePipeline
|
||||
from .Multidongle import PostProcessor, PostProcessorOptions, PostProcessType
|
||||
|
||||
|
||||
class DefaultProcessors:
|
||||
@ -463,12 +464,86 @@ class MFlowConverter:
|
||||
|
||||
print("="*60 + "\n")
|
||||
|
||||
def _build_multi_series_config_from_properties(self, properties: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Build multi-series configuration from node properties"""
|
||||
try:
|
||||
enabled_series = properties.get('enabled_series', [])
|
||||
assets_folder = properties.get('assets_folder', '')
|
||||
|
||||
if not enabled_series:
|
||||
print("Warning: No enabled_series found in multi-series mode")
|
||||
return {}
|
||||
|
||||
multi_series_config = {}
|
||||
|
||||
for series in enabled_series:
|
||||
# Get port IDs for this series
|
||||
port_ids_str = properties.get(f'kl{series}_port_ids', '')
|
||||
if not port_ids_str or not port_ids_str.strip():
|
||||
print(f"Warning: No port IDs configured for KL{series}")
|
||||
continue
|
||||
|
||||
# Parse port IDs (comma-separated string to list of integers)
|
||||
try:
|
||||
port_ids = [int(pid.strip()) for pid in port_ids_str.split(',') if pid.strip()]
|
||||
if not port_ids:
|
||||
continue
|
||||
except ValueError:
|
||||
print(f"Warning: Invalid port IDs for KL{series}: {port_ids_str}")
|
||||
continue
|
||||
|
||||
# Build series configuration
|
||||
series_config = {
|
||||
"port_ids": port_ids
|
||||
}
|
||||
|
||||
# Add model path if assets folder is configured
|
||||
if assets_folder:
|
||||
import os
|
||||
model_folder = os.path.join(assets_folder, 'Models', f'KL{series}')
|
||||
if os.path.exists(model_folder):
|
||||
# Look for .nef files in the model folder
|
||||
nef_files = [f for f in os.listdir(model_folder) if f.endswith('.nef')]
|
||||
if nef_files:
|
||||
series_config["model_path"] = os.path.join(model_folder, nef_files[0])
|
||||
print(f"Found model for KL{series}: {series_config['model_path']}")
|
||||
|
||||
# Add firmware paths if available
|
||||
firmware_folder = os.path.join(assets_folder, 'Firmware', f'KL{series}')
|
||||
if os.path.exists(firmware_folder):
|
||||
scpu_path = os.path.join(firmware_folder, 'fw_scpu.bin')
|
||||
ncpu_path = os.path.join(firmware_folder, 'fw_ncpu.bin')
|
||||
|
||||
if os.path.exists(scpu_path) and os.path.exists(ncpu_path):
|
||||
series_config["firmware_paths"] = {
|
||||
"scpu": scpu_path,
|
||||
"ncpu": ncpu_path
|
||||
}
|
||||
print(f"Found firmware for KL{series}: scpu={scpu_path}, ncpu={ncpu_path}")
|
||||
|
||||
multi_series_config[f'KL{series}'] = series_config
|
||||
print(f"Configured KL{series} with {len(port_ids)} devices on ports {port_ids}")
|
||||
|
||||
return multi_series_config if multi_series_config else {}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error building multi-series config from properties: {e}")
|
||||
return {}
|
||||
|
||||
def _create_stage_configs(self, model_nodes: List[Dict], preprocess_nodes: List[Dict],
|
||||
postprocess_nodes: List[Dict], connections: List[Dict]) -> List[StageConfig]:
|
||||
"""Create StageConfig objects for each model node"""
|
||||
# Note: preprocess_nodes, postprocess_nodes, connections reserved for future enhanced processing
|
||||
"""Create StageConfig objects for each model node with postprocessing support"""
|
||||
stage_configs = []
|
||||
|
||||
# Build connection mapping for efficient lookup
|
||||
connection_map = {}
|
||||
for conn in connections:
|
||||
output_node_id = conn.get('output_node')
|
||||
input_node_id = conn.get('input_node')
|
||||
if output_node_id not in connection_map:
|
||||
connection_map[output_node_id] = []
|
||||
connection_map[output_node_id].append(input_node_id)
|
||||
|
||||
for i, model_node in enumerate(self.stage_order):
|
||||
properties = model_node.get('properties', {})
|
||||
|
||||
@ -502,16 +577,107 @@ class MFlowConverter:
|
||||
# Queue size
|
||||
max_queue_size = properties.get('max_queue_size', 50)
|
||||
|
||||
# Create StageConfig
|
||||
stage_config = StageConfig(
|
||||
stage_id=stage_id,
|
||||
port_ids=port_ids,
|
||||
scpu_fw_path=scpu_fw_path,
|
||||
ncpu_fw_path=ncpu_fw_path,
|
||||
model_path=model_path,
|
||||
upload_fw=upload_fw,
|
||||
max_queue_size=max_queue_size
|
||||
)
|
||||
# Find connected postprocessing node
|
||||
stage_postprocessor = None
|
||||
model_node_id = model_node.get('id')
|
||||
|
||||
if model_node_id and model_node_id in connection_map:
|
||||
connected_nodes = connection_map[model_node_id]
|
||||
# Look for postprocessing nodes among connected nodes
|
||||
for connected_id in connected_nodes:
|
||||
for postprocess_node in postprocess_nodes:
|
||||
if postprocess_node.get('id') == connected_id:
|
||||
# Found a connected postprocessing node
|
||||
postprocess_props = postprocess_node.get('properties', {})
|
||||
|
||||
# Extract postprocessing configuration
|
||||
postprocess_type_str = postprocess_props.get('postprocess_type', 'fire_detection')
|
||||
confidence_threshold = postprocess_props.get('confidence_threshold', 0.5)
|
||||
nms_threshold = postprocess_props.get('nms_threshold', 0.5)
|
||||
max_detections = postprocess_props.get('max_detections', 100)
|
||||
class_names_str = postprocess_props.get('class_names', '')
|
||||
|
||||
# Parse class names from node (highest priority)
|
||||
if isinstance(class_names_str, str) and class_names_str.strip():
|
||||
class_names = [name.strip() for name in class_names_str.split(',') if name.strip()]
|
||||
else:
|
||||
class_names = []
|
||||
|
||||
# Map string to PostProcessType enum
|
||||
type_mapping = {
|
||||
'fire_detection': PostProcessType.FIRE_DETECTION,
|
||||
'yolo_v3': PostProcessType.YOLO_V3,
|
||||
'yolo_v5': PostProcessType.YOLO_V5,
|
||||
'classification': PostProcessType.CLASSIFICATION,
|
||||
'raw_output': PostProcessType.RAW_OUTPUT
|
||||
}
|
||||
|
||||
postprocess_type = type_mapping.get(postprocess_type_str, PostProcessType.FIRE_DETECTION)
|
||||
|
||||
# Smart defaults for YOLOv5 labels when none provided
|
||||
if postprocess_type == PostProcessType.YOLO_V5 and not class_names:
|
||||
# Try to load labels near the model file
|
||||
loaded = self._load_labels_for_model(model_path)
|
||||
if loaded:
|
||||
class_names = loaded
|
||||
else:
|
||||
# Fallback to COCO-80
|
||||
class_names = self._default_coco_labels()
|
||||
|
||||
print(f"Found postprocessing for {stage_id}: type={postprocess_type.value}, threshold={confidence_threshold}, classes={len(class_names)}")
|
||||
|
||||
# Create PostProcessorOptions and PostProcessor
|
||||
try:
|
||||
postprocess_options = PostProcessorOptions(
|
||||
postprocess_type=postprocess_type,
|
||||
threshold=confidence_threshold,
|
||||
class_names=class_names,
|
||||
nms_threshold=nms_threshold,
|
||||
max_detections_per_class=max_detections
|
||||
)
|
||||
stage_postprocessor = PostProcessor(postprocess_options)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to create postprocessor for {stage_id}: {e}")
|
||||
|
||||
break # Use the first postprocessing node found
|
||||
|
||||
if stage_postprocessor is None:
|
||||
print(f"No postprocessing node found for {stage_id}, using default")
|
||||
|
||||
# Check if multi-series mode is enabled
|
||||
multi_series_mode = properties.get('multi_series_mode', False)
|
||||
multi_series_config = None
|
||||
|
||||
if multi_series_mode:
|
||||
# Build multi-series config from node properties
|
||||
multi_series_config = self._build_multi_series_config_from_properties(properties)
|
||||
print(f"Multi-series config for {stage_id}: {multi_series_config}")
|
||||
|
||||
# Create StageConfig for multi-series mode
|
||||
stage_config = StageConfig(
|
||||
stage_id=stage_id,
|
||||
port_ids=[], # Will be handled by multi_series_config
|
||||
scpu_fw_path='', # Will be handled by multi_series_config
|
||||
ncpu_fw_path='', # Will be handled by multi_series_config
|
||||
model_path='', # Will be handled by multi_series_config
|
||||
upload_fw=upload_fw,
|
||||
max_queue_size=max_queue_size,
|
||||
multi_series_config=multi_series_config,
|
||||
stage_postprocessor=stage_postprocessor
|
||||
)
|
||||
else:
|
||||
# Create StageConfig for single-series mode (legacy)
|
||||
stage_config = StageConfig(
|
||||
stage_id=stage_id,
|
||||
port_ids=port_ids,
|
||||
scpu_fw_path=scpu_fw_path,
|
||||
ncpu_fw_path=ncpu_fw_path,
|
||||
model_path=model_path,
|
||||
upload_fw=upload_fw,
|
||||
max_queue_size=max_queue_size,
|
||||
multi_series_config=None,
|
||||
stage_postprocessor=stage_postprocessor
|
||||
)
|
||||
|
||||
stage_configs.append(stage_config)
|
||||
|
||||
@ -567,6 +733,99 @@ class MFlowConverter:
|
||||
|
||||
return configs
|
||||
|
||||
# ---------- Label helpers ----------
|
||||
def _load_labels_for_model(self, model_path: str) -> Optional[List[str]]:
|
||||
"""Attempt to load class labels from files near the model path.
|
||||
Priority: <model>.names -> names.txt -> classes.txt -> labels.txt -> data.yaml/dataset.yaml (names)
|
||||
Returns None if not found.
|
||||
"""
|
||||
try:
|
||||
if not model_path:
|
||||
return None
|
||||
base = os.path.splitext(model_path)[0]
|
||||
dir_ = os.path.dirname(model_path)
|
||||
candidates = [
|
||||
f"{base}.names",
|
||||
os.path.join(dir_, 'names.txt'),
|
||||
os.path.join(dir_, 'classes.txt'),
|
||||
os.path.join(dir_, 'labels.txt'),
|
||||
os.path.join(dir_, 'data.yaml'),
|
||||
os.path.join(dir_, 'dataset.yaml'),
|
||||
]
|
||||
for path in candidates:
|
||||
if os.path.exists(path):
|
||||
if path.lower().endswith('.yaml'):
|
||||
labels = self._load_labels_from_yaml(path)
|
||||
else:
|
||||
labels = self._load_labels_from_lines(path)
|
||||
if labels:
|
||||
print(f"Loaded {len(labels)} labels from {os.path.basename(path)}")
|
||||
return labels
|
||||
except Exception as e:
|
||||
print(f"Warning: failed loading labels near model: {e}")
|
||||
return None
|
||||
|
||||
def _load_labels_from_lines(self, path: str) -> List[str]:
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
lines = [ln.strip() for ln in f.readlines()]
|
||||
return [ln for ln in lines if ln and not ln.startswith('#')]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
def _load_labels_from_yaml(self, path: str) -> List[str]:
|
||||
# Try PyYAML if available; else fallback to simple parse
|
||||
try:
|
||||
import yaml # type: ignore
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
data = yaml.safe_load(f)
|
||||
names = data.get('names') if isinstance(data, dict) else None
|
||||
if isinstance(names, dict):
|
||||
# Ordered by key if numeric, else values
|
||||
items = sorted(names.items(), key=lambda kv: int(kv[0]) if str(kv[0]).isdigit() else kv[0])
|
||||
return [str(v) for _, v in items]
|
||||
elif isinstance(names, list):
|
||||
return [str(x) for x in names]
|
||||
except Exception:
|
||||
pass
|
||||
# Minimal fallback: naive scan
|
||||
try:
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
if 'names:' in content:
|
||||
after = content.split('names:', 1)[1]
|
||||
# Look for block list
|
||||
lines = [ln.strip() for ln in after.splitlines()]
|
||||
block = []
|
||||
for ln in lines:
|
||||
if ln.startswith('- '):
|
||||
block.append(ln[2:].strip())
|
||||
elif block:
|
||||
break
|
||||
if block:
|
||||
return block
|
||||
# Look for bracket list
|
||||
if '[' in after and ']' in after:
|
||||
inside = after.split('[', 1)[1].split(']', 1)[0]
|
||||
return [x.strip().strip('"\'') for x in inside.split(',') if x.strip()]
|
||||
except Exception:
|
||||
pass
|
||||
return []
|
||||
|
||||
def _default_coco_labels(self) -> List[str]:
|
||||
# Standard COCO 80 class names
|
||||
return [
|
||||
'person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
|
||||
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
|
||||
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
|
||||
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
|
||||
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
|
||||
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'sofa',
|
||||
'pottedplant', 'bed', 'diningtable', 'toilet', 'tvmonitor', 'laptop', 'mouse', 'remote', 'keyboard',
|
||||
'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
|
||||
'teddy bear', 'hair drier', 'toothbrush'
|
||||
]
|
||||
|
||||
def _extract_postprocessing_configs(self, postprocess_nodes: List[Dict]) -> List[Dict[str, Any]]:
|
||||
"""Extract postprocessing configurations"""
|
||||
configs = []
|
||||
@ -625,22 +884,87 @@ class MFlowConverter:
|
||||
"""Validate individual stage configuration"""
|
||||
errors = []
|
||||
|
||||
# Check model path
|
||||
if not stage_config.model_path:
|
||||
errors.append(f"Stage {stage_num}: Model path is required")
|
||||
elif not os.path.exists(stage_config.model_path):
|
||||
errors.append(f"Stage {stage_num}: Model file not found: {stage_config.model_path}")
|
||||
# Check if this is multi-series configuration
|
||||
if stage_config.multi_series_config:
|
||||
# Multi-series validation
|
||||
errors.extend(self._validate_multi_series_config(stage_config.multi_series_config, stage_num))
|
||||
else:
|
||||
# Single-series validation (legacy)
|
||||
# Check model path
|
||||
if not stage_config.model_path:
|
||||
errors.append(f"Stage {stage_num}: Model path is required")
|
||||
elif not os.path.exists(stage_config.model_path):
|
||||
errors.append(f"Stage {stage_num}: Model file not found: {stage_config.model_path}")
|
||||
|
||||
# Check firmware paths if upload_fw is True
|
||||
if stage_config.upload_fw:
|
||||
if not os.path.exists(stage_config.scpu_fw_path):
|
||||
errors.append(f"Stage {stage_num}: SCPU firmware not found: {stage_config.scpu_fw_path}")
|
||||
if not os.path.exists(stage_config.ncpu_fw_path):
|
||||
errors.append(f"Stage {stage_num}: NCPU firmware not found: {stage_config.ncpu_fw_path}")
|
||||
# Check firmware paths if upload_fw is True
|
||||
if stage_config.upload_fw:
|
||||
if not os.path.exists(stage_config.scpu_fw_path):
|
||||
errors.append(f"Stage {stage_num}: SCPU firmware not found: {stage_config.scpu_fw_path}")
|
||||
if not os.path.exists(stage_config.ncpu_fw_path):
|
||||
errors.append(f"Stage {stage_num}: NCPU firmware not found: {stage_config.ncpu_fw_path}")
|
||||
|
||||
# Check port IDs
|
||||
if not stage_config.port_ids:
|
||||
errors.append(f"Stage {stage_num}: At least one port ID is required")
|
||||
# Check port IDs
|
||||
if not stage_config.port_ids:
|
||||
errors.append(f"Stage {stage_num}: At least one port ID is required")
|
||||
|
||||
return errors
|
||||
|
||||
def _validate_multi_series_config(self, multi_series_config: Dict[str, Any], stage_num: int) -> List[str]:
|
||||
"""Validate multi-series configuration"""
|
||||
errors = []
|
||||
|
||||
if not multi_series_config:
|
||||
errors.append(f"Stage {stage_num}: Multi-series configuration is empty")
|
||||
return errors
|
||||
|
||||
print(f"Validating multi-series config for stage {stage_num}: {list(multi_series_config.keys())}")
|
||||
|
||||
# Check each series configuration
|
||||
for series_name, series_config in multi_series_config.items():
|
||||
if not isinstance(series_config, dict):
|
||||
errors.append(f"Stage {stage_num}: Invalid configuration for {series_name}")
|
||||
continue
|
||||
|
||||
# Check port IDs
|
||||
port_ids = series_config.get('port_ids', [])
|
||||
if not port_ids:
|
||||
errors.append(f"Stage {stage_num}: {series_name} has no port IDs configured")
|
||||
continue
|
||||
|
||||
if not isinstance(port_ids, list) or not all(isinstance(p, int) for p in port_ids):
|
||||
errors.append(f"Stage {stage_num}: {series_name} port IDs must be a list of integers")
|
||||
continue
|
||||
|
||||
print(f" {series_name}: {len(port_ids)} ports configured")
|
||||
|
||||
# Check model path
|
||||
model_path = series_config.get('model_path')
|
||||
if model_path:
|
||||
if not os.path.exists(model_path):
|
||||
errors.append(f"Stage {stage_num}: {series_name} model file not found: {model_path}")
|
||||
else:
|
||||
print(f" {series_name}: Model validated: {model_path}")
|
||||
else:
|
||||
print(f" {series_name}: No model path specified (optional for multi-series)")
|
||||
|
||||
# Check firmware paths if specified
|
||||
firmware_paths = series_config.get('firmware_paths')
|
||||
if firmware_paths and isinstance(firmware_paths, dict):
|
||||
scpu_path = firmware_paths.get('scpu')
|
||||
ncpu_path = firmware_paths.get('ncpu')
|
||||
|
||||
if scpu_path and not os.path.exists(scpu_path):
|
||||
errors.append(f"Stage {stage_num}: {series_name} SCPU firmware not found: {scpu_path}")
|
||||
elif scpu_path:
|
||||
print(f" {series_name}: SCPU firmware validated: {scpu_path}")
|
||||
|
||||
if ncpu_path and not os.path.exists(ncpu_path):
|
||||
errors.append(f"Stage {stage_num}: {series_name} NCPU firmware not found: {ncpu_path}")
|
||||
elif ncpu_path:
|
||||
print(f" {series_name}: NCPU firmware validated: {ncpu_path}")
|
||||
|
||||
if not errors:
|
||||
print(f"Stage {stage_num}: Multi-series configuration validation passed")
|
||||
|
||||
return errors
|
||||
|
||||
|
||||
@ -3,8 +3,18 @@ import json
|
||||
import csv
|
||||
import os
|
||||
import time
|
||||
import dataclasses
|
||||
from typing import Any, Dict, List
|
||||
|
||||
|
||||
class _InferenceResultEncoder(json.JSONEncoder):
|
||||
"""將 dataclass 推論結果物件轉為可序列化的 dict。"""
|
||||
def default(self, o):
|
||||
if dataclasses.is_dataclass(o) and not isinstance(o, type):
|
||||
return dataclasses.asdict(o)
|
||||
return super().default(o)
|
||||
|
||||
|
||||
class ResultSerializer:
|
||||
"""
|
||||
Serializes inference results into various formats.
|
||||
@ -12,8 +22,10 @@ class ResultSerializer:
|
||||
def to_json(self, data: Dict[str, Any]) -> str:
|
||||
"""
|
||||
Serializes data to a JSON string.
|
||||
Dataclass objects (ObjectDetectionResult, ClassificationResult, etc.)
|
||||
are automatically converted to dicts via _InferenceResultEncoder.
|
||||
"""
|
||||
return json.dumps(data, indent=2)
|
||||
return json.dumps(data, indent=2, cls=_InferenceResultEncoder)
|
||||
|
||||
def to_csv(self, data: List[Dict[str, Any]], fieldnames: List[str]) -> str:
|
||||
"""
|
||||
|
||||
146
core/functions/yolo_v5_postprocess_reference.py
Normal file
146
core/functions/yolo_v5_postprocess_reference.py
Normal file
@ -0,0 +1,146 @@
|
||||
import numpy as np
|
||||
|
||||
# Constants based on Kneron example_utils implementation
|
||||
YOLO_V3_CELL_BOX_NUM = 3
|
||||
YOLO_V5_ANCHORS = np.array([
|
||||
[[10, 13], [16, 30], [33, 23]],
|
||||
[[30, 61], [62, 45], [59, 119]],
|
||||
[[116, 90], [156, 198], [373, 326]]
|
||||
])
|
||||
NMS_THRESH_YOLOV5 = 0.5
|
||||
YOLO_MAX_DETECTION_PER_CLASS = 100
|
||||
|
||||
|
||||
def _sigmoid(x):
|
||||
return 1.0 / (1.0 + np.exp(-x))
|
||||
|
||||
|
||||
def _iou(box_src, boxes_dst):
|
||||
max_x1 = np.maximum(box_src[0], boxes_dst[:, 0])
|
||||
max_y1 = np.maximum(box_src[1], boxes_dst[:, 1])
|
||||
min_x2 = np.minimum(box_src[2], boxes_dst[:, 2])
|
||||
min_y2 = np.minimum(box_src[3], boxes_dst[:, 3])
|
||||
|
||||
area_intersection = np.maximum(0, (min_x2 - max_x1)) * np.maximum(0, (min_y2 - max_y1))
|
||||
area_src = (box_src[2] - box_src[0]) * (box_src[3] - box_src[1])
|
||||
area_dst = (boxes_dst[:, 2] - boxes_dst[:, 0]) * (boxes_dst[:, 1] - boxes_dst[:, 1] + (boxes_dst[:, 3] - boxes_dst[:, 1]))
|
||||
# Correct dst area computation
|
||||
area_dst = (boxes_dst[:, 2] - boxes_dst[:, 0]) * (boxes_dst[:, 3] - boxes_dst[:, 1])
|
||||
area_union = area_src + area_dst - area_intersection
|
||||
iou = area_intersection / np.maximum(area_union, 1e-6)
|
||||
return iou
|
||||
|
||||
|
||||
def _boxes_scale(boxes, hw):
|
||||
"""Rollback padding and scale to original image size using HwPreProcInfo."""
|
||||
ratio_w = hw.img_width / max(1, float(getattr(hw, 'resized_img_width', hw.img_width)))
|
||||
ratio_h = hw.img_height / max(1, float(getattr(hw, 'resized_img_height', hw.img_height)))
|
||||
|
||||
pad_left = int(getattr(hw, 'pad_left', 0))
|
||||
pad_top = int(getattr(hw, 'pad_top', 0))
|
||||
|
||||
boxes[..., :4] = boxes[..., :4] - np.array([pad_left, pad_top, pad_left, pad_top])
|
||||
boxes[..., :4] = boxes[..., :4] * np.array([ratio_w, ratio_h, ratio_w, ratio_h])
|
||||
return boxes
|
||||
|
||||
|
||||
def post_process_yolo_v5_reference(inf_list, hw_preproc_info, thresh_value=0.5):
|
||||
"""
|
||||
Reference YOLOv5 postprocess copied and adapted from Kneron example_utils.
|
||||
|
||||
Args:
|
||||
inf_list: list of outputs; each item has .ndarray or is ndarray of shape [1, 255, H, W]
|
||||
hw_preproc_info: kp.HwPreProcInfo providing model input and resize/pad info
|
||||
thresh_value: confidence threshold (0.0~1.0)
|
||||
|
||||
Returns:
|
||||
List of tuples: (x1, y1, x2, y2, score, class_num)
|
||||
"""
|
||||
feature_map_list = []
|
||||
candidate_boxes_list = []
|
||||
|
||||
for i in range(len(inf_list)):
|
||||
arr = inf_list[i].ndarray if hasattr(inf_list[i], 'ndarray') else inf_list[i]
|
||||
# Expect shape [1, 255, H, W]
|
||||
anchor_offset = int(arr.shape[1] / YOLO_V3_CELL_BOX_NUM)
|
||||
feature_map = arr.transpose((0, 2, 3, 1))
|
||||
feature_map = _sigmoid(feature_map)
|
||||
feature_map = feature_map.reshape((feature_map.shape[0],
|
||||
feature_map.shape[1],
|
||||
feature_map.shape[2],
|
||||
YOLO_V3_CELL_BOX_NUM,
|
||||
anchor_offset))
|
||||
|
||||
# ratio based on model input vs output grid size
|
||||
ratio_w = float(getattr(hw_preproc_info, 'model_input_width', arr.shape[3])) / arr.shape[3]
|
||||
ratio_h = float(getattr(hw_preproc_info, 'model_input_height', arr.shape[2])) / arr.shape[2]
|
||||
nrows = arr.shape[2]
|
||||
ncols = arr.shape[3]
|
||||
grids = np.expand_dims(np.stack(np.meshgrid(np.arange(ncols), np.arange(nrows)), 2), axis=0)
|
||||
|
||||
for anchor_idx in range(YOLO_V3_CELL_BOX_NUM):
|
||||
feature_map[..., anchor_idx, 0:2] = (feature_map[..., anchor_idx, 0:2] * 2. - 0.5 + grids) * np.array(
|
||||
[ratio_h, ratio_w])
|
||||
feature_map[..., anchor_idx, 2:4] = (feature_map[..., anchor_idx, 2:4] * 2) ** 2 * YOLO_V5_ANCHORS[i][anchor_idx]
|
||||
|
||||
# Convert to (x1,y1,x2,y2)
|
||||
feature_map[..., anchor_idx, 0:2] = feature_map[..., anchor_idx, 0:2] - (feature_map[..., anchor_idx, 2:4] / 2.)
|
||||
feature_map[..., anchor_idx, 2:4] = feature_map[..., anchor_idx, 0:2] + feature_map[..., anchor_idx, 2:4]
|
||||
|
||||
# Rollback padding and resize to original img size
|
||||
feature_map = _boxes_scale(boxes=feature_map, hw=hw_preproc_info)
|
||||
feature_map_list.append(feature_map)
|
||||
|
||||
# Concatenate and apply objectness * class prob
|
||||
predict_bboxes = np.concatenate(
|
||||
[np.reshape(fm, (-1, fm.shape[-1])) for fm in feature_map_list], axis=0)
|
||||
predict_bboxes[..., 5:] = np.repeat(predict_bboxes[..., 4][..., np.newaxis],
|
||||
predict_bboxes[..., 5:].shape[1], axis=1) * predict_bboxes[..., 5:]
|
||||
predict_bboxes_mask = (predict_bboxes[..., 5:] > thresh_value).sum(axis=1)
|
||||
predict_bboxes = predict_bboxes[predict_bboxes_mask >= 1]
|
||||
|
||||
# Per-class NMS
|
||||
H = int(getattr(hw_preproc_info, 'img_height', 0))
|
||||
W = int(getattr(hw_preproc_info, 'img_width', 0))
|
||||
|
||||
for class_idx in range(5, predict_bboxes.shape[1]):
|
||||
candidate_boxes_mask = predict_bboxes[..., class_idx] > thresh_value
|
||||
class_good_box_count = int(candidate_boxes_mask.sum())
|
||||
if class_good_box_count == 1:
|
||||
bb = predict_bboxes[candidate_boxes_mask][0]
|
||||
candidate_boxes_list.append((
|
||||
int(max(0, min(bb[0] + 0.5, W - 1))),
|
||||
int(max(0, min(bb[1] + 0.5, H - 1))),
|
||||
int(max(0, min(bb[2] + 0.5, W - 1))),
|
||||
int(max(0, min(bb[3] + 0.5, H - 1))),
|
||||
float(bb[class_idx]),
|
||||
class_idx - 5
|
||||
))
|
||||
elif class_good_box_count > 1:
|
||||
candidate_boxes = predict_bboxes[candidate_boxes_mask].copy()
|
||||
candidate_boxes = candidate_boxes[candidate_boxes[:, class_idx].argsort()][::-1]
|
||||
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0] - 1):
|
||||
if candidate_boxes[candidate_box_idx][class_idx] != 0:
|
||||
ious = _iou(candidate_boxes[candidate_box_idx], candidate_boxes[candidate_box_idx + 1:])
|
||||
remove_mask = ious > NMS_THRESH_YOLOV5
|
||||
candidate_boxes[candidate_box_idx + 1:][remove_mask, class_idx] = 0
|
||||
|
||||
good_count = 0
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0]):
|
||||
if candidate_boxes[candidate_box_idx, class_idx] > 0:
|
||||
bb = candidate_boxes[candidate_box_idx]
|
||||
candidate_boxes_list.append((
|
||||
int(max(0, min(bb[0] + 0.5, W - 1))),
|
||||
int(max(0, min(bb[1] + 0.5, H - 1))),
|
||||
int(max(0, min(bb[2] + 0.5, W - 1))),
|
||||
int(max(0, min(bb[3] + 0.5, H - 1))),
|
||||
float(bb[class_idx]),
|
||||
class_idx - 5
|
||||
))
|
||||
good_count += 1
|
||||
if good_count == YOLO_MAX_DETECTION_PER_CLASS:
|
||||
break
|
||||
|
||||
return candidate_boxes_list
|
||||
|
||||
@ -5,6 +5,8 @@ This module provides node implementations that exactly match the original
|
||||
properties and behavior from the monolithic UI.py file.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
try:
|
||||
from NodeGraphQt import BaseNode
|
||||
NODEGRAPH_AVAILABLE = True
|
||||
@ -115,20 +117,60 @@ class ExactModelNode(BaseNode):
|
||||
self.create_property('port_id', '')
|
||||
self.create_property('upload_fw', True)
|
||||
|
||||
# Multi-series properties
|
||||
self.create_property('multi_series_mode', False)
|
||||
self.create_property('assets_folder', '')
|
||||
self.create_property('enabled_series', ['520', '720'])
|
||||
|
||||
# Series-specific port ID configurations
|
||||
self.create_property('kl520_port_ids', '')
|
||||
self.create_property('kl720_port_ids', '')
|
||||
self.create_property('kl630_port_ids', '')
|
||||
self.create_property('kl730_port_ids', '')
|
||||
# self.create_property('kl540_port_ids', '')
|
||||
|
||||
self.create_property('max_queue_size', 100)
|
||||
self.create_property('result_buffer_size', 1000)
|
||||
self.create_property('batch_size', 1)
|
||||
self.create_property('enable_preprocessing', False)
|
||||
self.create_property('enable_postprocessing', False)
|
||||
|
||||
# Original property options - exact match
|
||||
self._property_options = {
|
||||
'dongle_series': ['520', '720', '1080', 'Custom'],
|
||||
'dongle_series': ['520', '720'],
|
||||
'num_dongles': {'min': 1, 'max': 16},
|
||||
'model_path': {'type': 'file_path', 'filter': 'NEF Model files (*.nef)'},
|
||||
'scpu_fw_path': {'type': 'file_path', 'filter': 'SCPU Firmware files (*.bin)'},
|
||||
'ncpu_fw_path': {'type': 'file_path', 'filter': 'NCPU Firmware files (*.bin)'},
|
||||
'port_id': {'placeholder': 'e.g., 8080 or auto'},
|
||||
'upload_fw': {'type': 'bool', 'default': True, 'description': 'Upload firmware to dongle if needed'}
|
||||
'upload_fw': {'type': 'bool', 'default': True, 'description': 'Upload firmware to dongle if needed'},
|
||||
|
||||
# Multi-series property options
|
||||
'multi_series_mode': {'type': 'bool', 'default': False, 'description': 'Enable multi-series dongle support'},
|
||||
'assets_folder': {'type': 'file_path', 'filter': 'Folder', 'mode': 'directory'},
|
||||
'enabled_series': {'type': 'list', 'options': ['520', '720', '630', '730', '540'], 'default': ['520', '720']},
|
||||
|
||||
# Series-specific port ID options
|
||||
'kl520_port_ids': {'placeholder': 'e.g., 28,32 (comma-separated port IDs for KL520)', 'description': 'Port IDs for KL520 dongles'},
|
||||
'kl720_port_ids': {'placeholder': 'e.g., 30,34 (comma-separated port IDs for KL720)', 'description': 'Port IDs for KL720 dongles'},
|
||||
'kl630_port_ids': {'placeholder': 'e.g., 36,38 (comma-separated port IDs for KL630)', 'description': 'Port IDs for KL630 dongles'},
|
||||
'kl730_port_ids': {'placeholder': 'e.g., 40,42 (comma-separated port IDs for KL730)', 'description': 'Port IDs for KL730 dongles'},
|
||||
# 'kl540_port_ids': {'placeholder': 'e.g., 44,46 (comma-separated port IDs for KL540)', 'description': 'Port IDs for KL540 dongles'},
|
||||
|
||||
'max_queue_size': {'min': 1, 'max': 1000, 'default': 100},
|
||||
'result_buffer_size': {'min': 100, 'max': 10000, 'default': 1000},
|
||||
'batch_size': {'min': 1, 'max': 32, 'default': 1},
|
||||
'enable_preprocessing': {'type': 'bool', 'default': False},
|
||||
'enable_postprocessing': {'type': 'bool', 'default': False}
|
||||
}
|
||||
|
||||
# Create custom properties dictionary for UI compatibility
|
||||
self._populate_custom_properties()
|
||||
|
||||
# Set up custom property handlers for folder selection
|
||||
if NODEGRAPH_AVAILABLE:
|
||||
self._setup_custom_property_handlers()
|
||||
|
||||
def _populate_custom_properties(self):
|
||||
"""Populate the custom properties dictionary for UI compatibility."""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
@ -166,8 +208,400 @@ class ExactModelNode(BaseNode):
|
||||
|
||||
def get_display_properties(self):
|
||||
"""Return properties that should be displayed in the UI panel."""
|
||||
# Customize which properties appear for Model nodes
|
||||
return ['model_path', 'scpu_fw_path', 'ncpu_fw_path', 'dongle_series', 'num_dongles', 'port_id', 'upload_fw']
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return []
|
||||
|
||||
# Base properties that are always shown
|
||||
base_props = ['multi_series_mode']
|
||||
|
||||
try:
|
||||
# Check if we're in multi-series mode
|
||||
multi_series_mode = self.get_property('multi_series_mode')
|
||||
|
||||
if multi_series_mode:
|
||||
# Multi-series mode: show multi-series specific properties
|
||||
multi_props = ['assets_folder', 'enabled_series']
|
||||
|
||||
# Add port ID configurations for enabled series
|
||||
try:
|
||||
enabled_series = self.get_property('enabled_series') or []
|
||||
for series in enabled_series:
|
||||
port_prop = f'kl{series}_port_ids'
|
||||
if port_prop not in multi_props: # Avoid duplicates
|
||||
multi_props.append(port_prop)
|
||||
except:
|
||||
pass # If can't get enabled_series, just show basic properties
|
||||
|
||||
# Add other multi-series properties
|
||||
multi_props.extend([
|
||||
'max_queue_size', 'result_buffer_size', 'batch_size',
|
||||
'enable_preprocessing', 'enable_postprocessing'
|
||||
])
|
||||
|
||||
return base_props + multi_props
|
||||
else:
|
||||
# Single-series mode: show traditional properties
|
||||
return base_props + [
|
||||
'model_path', 'scpu_fw_path', 'ncpu_fw_path',
|
||||
'dongle_series', 'num_dongles', 'port_id', 'upload_fw'
|
||||
]
|
||||
except:
|
||||
# Fallback to single-series mode if property access fails
|
||||
return base_props + [
|
||||
'model_path', 'scpu_fw_path', 'ncpu_fw_path',
|
||||
'dongle_series', 'num_dongles', 'port_id', 'upload_fw'
|
||||
]
|
||||
|
||||
def get_inference_config(self):
|
||||
"""Get configuration for inference pipeline"""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return {}
|
||||
|
||||
try:
|
||||
multi_series_mode = self.get_property('multi_series_mode')
|
||||
|
||||
if multi_series_mode:
|
||||
# Multi-series configuration with series-specific port IDs
|
||||
config = {
|
||||
'multi_series_mode': True,
|
||||
'assets_folder': self.get_property('assets_folder'),
|
||||
'enabled_series': self.get_property('enabled_series'),
|
||||
'max_queue_size': self.get_property('max_queue_size'),
|
||||
'result_buffer_size': self.get_property('result_buffer_size'),
|
||||
'batch_size': self.get_property('batch_size'),
|
||||
'enable_preprocessing': self.get_property('enable_preprocessing'),
|
||||
'enable_postprocessing': self.get_property('enable_postprocessing')
|
||||
}
|
||||
|
||||
# Build multi-series config for MultiDongle
|
||||
multi_series_config = self._build_multi_series_config()
|
||||
if multi_series_config:
|
||||
config['multi_series_config'] = multi_series_config
|
||||
|
||||
return config
|
||||
else:
|
||||
# Single-series configuration
|
||||
return {
|
||||
'multi_series_mode': False,
|
||||
'model_path': self.get_property('model_path'),
|
||||
'scpu_fw_path': self.get_property('scpu_fw_path'),
|
||||
'ncpu_fw_path': self.get_property('ncpu_fw_path'),
|
||||
'dongle_series': self.get_property('dongle_series'),
|
||||
'num_dongles': self.get_property('num_dongles'),
|
||||
'port_id': self.get_property('port_id'),
|
||||
'upload_fw': self.get_property('upload_fw')
|
||||
}
|
||||
except:
|
||||
# Fallback to single-series configuration
|
||||
return {
|
||||
'multi_series_mode': False,
|
||||
'model_path': self.get_property('model_path', ''),
|
||||
'scpu_fw_path': self.get_property('scpu_fw_path', ''),
|
||||
'ncpu_fw_path': self.get_property('ncpu_fw_path', ''),
|
||||
'dongle_series': self.get_property('dongle_series', '520'),
|
||||
'num_dongles': self.get_property('num_dongles', 1),
|
||||
'port_id': self.get_property('port_id', ''),
|
||||
'upload_fw': self.get_property('upload_fw', True)
|
||||
}
|
||||
|
||||
def _build_multi_series_config(self):
|
||||
"""Build multi-series configuration for MultiDongle"""
|
||||
try:
|
||||
enabled_series = self.get_property('enabled_series') or []
|
||||
assets_folder = self.get_property('assets_folder') or ''
|
||||
|
||||
if not enabled_series:
|
||||
return None
|
||||
|
||||
multi_series_config = {}
|
||||
|
||||
for series in enabled_series:
|
||||
# Get port IDs for this series
|
||||
port_ids_str = self.get_property(f'kl{series}_port_ids') or ''
|
||||
if not port_ids_str.strip():
|
||||
continue # Skip series without port IDs
|
||||
|
||||
# Parse port IDs (comma-separated string to list of integers)
|
||||
try:
|
||||
port_ids = [int(pid.strip()) for pid in port_ids_str.split(',') if pid.strip()]
|
||||
if not port_ids:
|
||||
continue
|
||||
except ValueError:
|
||||
print(f"Warning: Invalid port IDs for KL{series}: {port_ids_str}")
|
||||
continue
|
||||
|
||||
# Build series configuration
|
||||
series_config = {
|
||||
"port_ids": port_ids
|
||||
}
|
||||
|
||||
# Add model path if assets folder is configured
|
||||
if assets_folder:
|
||||
import os
|
||||
model_folder = os.path.join(assets_folder, 'Models', f'KL{series}')
|
||||
if os.path.exists(model_folder):
|
||||
# Look for .nef files in the model folder
|
||||
nef_files = [f for f in os.listdir(model_folder) if f.endswith('.nef')]
|
||||
if nef_files:
|
||||
series_config["model_path"] = os.path.join(model_folder, nef_files[0])
|
||||
|
||||
# Add firmware paths if available
|
||||
firmware_folder = os.path.join(assets_folder, 'Firmware', f'KL{series}')
|
||||
if os.path.exists(firmware_folder):
|
||||
scpu_path = os.path.join(firmware_folder, 'fw_scpu.bin')
|
||||
ncpu_path = os.path.join(firmware_folder, 'fw_ncpu.bin')
|
||||
|
||||
if os.path.exists(scpu_path) and os.path.exists(ncpu_path):
|
||||
series_config["firmware_paths"] = {
|
||||
"scpu": scpu_path,
|
||||
"ncpu": ncpu_path
|
||||
}
|
||||
|
||||
multi_series_config[f'KL{series}'] = series_config
|
||||
|
||||
return multi_series_config if multi_series_config else None
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error building multi-series config: {e}")
|
||||
return None
|
||||
|
||||
def get_hardware_requirements(self):
|
||||
"""Get hardware requirements for this node"""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return {}
|
||||
|
||||
try:
|
||||
multi_series_mode = self.get_property('multi_series_mode')
|
||||
|
||||
if multi_series_mode:
|
||||
enabled_series = self.get_property('enabled_series')
|
||||
return {
|
||||
'multi_series_mode': True,
|
||||
'required_series': enabled_series,
|
||||
'estimated_dongles': len(enabled_series) * 2 # Assume 2 dongles per series
|
||||
}
|
||||
else:
|
||||
dongle_series = self.get_property('dongle_series')
|
||||
num_dongles = self.get_property('num_dongles')
|
||||
return {
|
||||
'multi_series_mode': False,
|
||||
'required_series': [f'KL{dongle_series}'],
|
||||
'estimated_dongles': num_dongles
|
||||
}
|
||||
except:
|
||||
return {'multi_series_mode': False, 'required_series': ['KL520'], 'estimated_dongles': 1}
|
||||
|
||||
def _setup_custom_property_handlers(self):
|
||||
"""Setup custom property handlers, especially for folder selection."""
|
||||
try:
|
||||
# For assets_folder, we want to trigger folder selection dialog
|
||||
# This might require custom widget or property handling
|
||||
# For now, we'll use the standard approach but add validation
|
||||
|
||||
# You can override the property widget here if needed
|
||||
# This is a placeholder for custom folder selection implementation
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not setup custom property handlers: {e}")
|
||||
|
||||
def select_assets_folder(self):
|
||||
"""Method to open folder selection dialog for assets folder using improved utility."""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return ""
|
||||
|
||||
try:
|
||||
from utils.folder_dialog import select_assets_folder
|
||||
|
||||
# Get current folder path as initial directory
|
||||
current_folder = ""
|
||||
try:
|
||||
current_folder = self.get_property('assets_folder') or ""
|
||||
except:
|
||||
pass
|
||||
|
||||
# Use the specialized assets folder dialog with validation
|
||||
result = select_assets_folder(initial_dir=current_folder)
|
||||
|
||||
if result['path']:
|
||||
# Set the property
|
||||
if NODEGRAPH_AVAILABLE:
|
||||
self.set_property('assets_folder', result['path'])
|
||||
|
||||
# Print validation results
|
||||
if result['valid']:
|
||||
print(f"✓ Valid Assets folder set to: {result['path']}")
|
||||
if 'details' in result and 'available_series' in result['details']:
|
||||
series = result['details']['available_series']
|
||||
print(f" Available series: {', '.join(series)}")
|
||||
else:
|
||||
print(f"⚠ Assets folder set to: {result['path']}")
|
||||
print(f" Warning: {result['message']}")
|
||||
print(" Expected structure: Assets/Firmware/ and Assets/Models/ with series subfolders")
|
||||
|
||||
return result['path']
|
||||
else:
|
||||
print("No folder selected")
|
||||
return ""
|
||||
|
||||
except ImportError:
|
||||
print("utils.folder_dialog not available, falling back to simple input")
|
||||
# Fallback to manual input
|
||||
folder_path = input("Enter Assets folder path: ").strip()
|
||||
if folder_path and NODEGRAPH_AVAILABLE:
|
||||
self.set_property('assets_folder', folder_path)
|
||||
return folder_path
|
||||
except Exception as e:
|
||||
print(f"Error selecting assets folder: {e}")
|
||||
|
||||
return ""
|
||||
|
||||
def _validate_assets_folder(self, folder_path):
|
||||
"""Validate that the assets folder has the expected structure."""
|
||||
try:
|
||||
import os
|
||||
|
||||
# Check if Firmware and Models folders exist
|
||||
firmware_path = os.path.join(folder_path, 'Firmware')
|
||||
models_path = os.path.join(folder_path, 'Models')
|
||||
|
||||
has_firmware = os.path.exists(firmware_path) and os.path.isdir(firmware_path)
|
||||
has_models = os.path.exists(models_path) and os.path.isdir(models_path)
|
||||
|
||||
if not (has_firmware and has_models):
|
||||
return False
|
||||
|
||||
# Check for at least one series subfolder
|
||||
expected_series = ['KL520', 'KL720', 'KL630', 'KL730']
|
||||
|
||||
firmware_series = [d for d in os.listdir(firmware_path)
|
||||
if os.path.isdir(os.path.join(firmware_path, d)) and d in expected_series]
|
||||
|
||||
models_series = [d for d in os.listdir(models_path)
|
||||
if os.path.isdir(os.path.join(models_path, d)) and d in expected_series]
|
||||
|
||||
# At least one series should exist in both firmware and models
|
||||
return len(firmware_series) > 0 and len(models_series) > 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error validating assets folder: {e}")
|
||||
return False
|
||||
|
||||
def get_assets_folder_info(self):
|
||||
"""Get information about the configured assets folder."""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return {}
|
||||
|
||||
try:
|
||||
folder_path = self.get_property('assets_folder')
|
||||
if not folder_path:
|
||||
return {'status': 'not_set', 'message': 'No assets folder selected'}
|
||||
|
||||
if not os.path.exists(folder_path):
|
||||
return {'status': 'invalid', 'message': 'Selected folder does not exist'}
|
||||
|
||||
info = {'status': 'valid', 'path': folder_path, 'series': []}
|
||||
|
||||
# Get available series
|
||||
firmware_path = os.path.join(folder_path, 'Firmware')
|
||||
models_path = os.path.join(folder_path, 'Models')
|
||||
|
||||
if os.path.exists(firmware_path):
|
||||
firmware_series = [d for d in os.listdir(firmware_path)
|
||||
if os.path.isdir(os.path.join(firmware_path, d))]
|
||||
info['firmware_series'] = firmware_series
|
||||
|
||||
if os.path.exists(models_path):
|
||||
models_series = [d for d in os.listdir(models_path)
|
||||
if os.path.isdir(os.path.join(models_path, d))]
|
||||
info['models_series'] = models_series
|
||||
|
||||
# Find common series
|
||||
if 'firmware_series' in info and 'models_series' in info:
|
||||
common_series = list(set(info['firmware_series']) & set(info['models_series']))
|
||||
info['available_series'] = common_series
|
||||
|
||||
if not common_series:
|
||||
info['status'] = 'incomplete'
|
||||
info['message'] = 'No series found with both firmware and models'
|
||||
|
||||
return info
|
||||
|
||||
except Exception as e:
|
||||
return {'status': 'error', 'message': f'Error reading assets folder: {e}'}
|
||||
|
||||
def validate_configuration(self) -> tuple[bool, str]:
|
||||
"""
|
||||
Validate the current node configuration.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_valid, error_message)
|
||||
"""
|
||||
if not NODEGRAPH_AVAILABLE:
|
||||
return True, ""
|
||||
|
||||
try:
|
||||
multi_series_mode = self.get_property('multi_series_mode')
|
||||
|
||||
if multi_series_mode:
|
||||
# Multi-series validation
|
||||
enabled_series = self.get_property('enabled_series')
|
||||
if not enabled_series:
|
||||
return False, "No series enabled in multi-series mode"
|
||||
|
||||
# Check if at least one series has port IDs configured
|
||||
has_valid_series = False
|
||||
for series in enabled_series:
|
||||
port_ids_str = self.get_property(f'kl{series}_port_ids', '')
|
||||
if port_ids_str and port_ids_str.strip():
|
||||
# Validate port ID format
|
||||
try:
|
||||
port_ids = [int(pid.strip()) for pid in port_ids_str.split(',') if pid.strip()]
|
||||
if port_ids:
|
||||
has_valid_series = True
|
||||
print(f"Valid series config found for KL{series}: ports {port_ids}")
|
||||
except ValueError:
|
||||
print(f"Warning: Invalid port ID format for KL{series}: {port_ids_str}")
|
||||
continue
|
||||
|
||||
if not has_valid_series:
|
||||
return False, "At least one series must have valid port IDs configured"
|
||||
|
||||
# Assets folder validation (optional for multi-series)
|
||||
assets_folder = self.get_property('assets_folder')
|
||||
if assets_folder:
|
||||
if not os.path.exists(assets_folder):
|
||||
print(f"Warning: Assets folder does not exist: {assets_folder}")
|
||||
else:
|
||||
# Validate assets folder structure if provided
|
||||
assets_info = self.get_assets_folder_info()
|
||||
if assets_info.get('status') == 'error':
|
||||
print(f"Warning: Assets folder issue: {assets_info.get('message', 'Unknown error')}")
|
||||
|
||||
print("Multi-series mode validation passed")
|
||||
return True, ""
|
||||
else:
|
||||
# Single-series validation (legacy)
|
||||
model_path = self.get_property('model_path')
|
||||
if not model_path:
|
||||
return False, "Model path is required"
|
||||
|
||||
if not os.path.exists(model_path):
|
||||
return False, f"Model file does not exist: {model_path}"
|
||||
|
||||
# Check dongle series
|
||||
dongle_series = self.get_property('dongle_series')
|
||||
if dongle_series not in ['520', '720', '1080', 'Custom']:
|
||||
return False, f"Invalid dongle series: {dongle_series}"
|
||||
|
||||
# Check number of dongles
|
||||
num_dongles = self.get_property('num_dongles')
|
||||
if not isinstance(num_dongles, int) or num_dongles < 1:
|
||||
return False, "Number of dongles must be at least 1"
|
||||
|
||||
return True, ""
|
||||
|
||||
except Exception as e:
|
||||
return False, f"Validation error: {str(e)}"
|
||||
|
||||
|
||||
class ExactPreprocessNode(BaseNode):
|
||||
@ -239,7 +673,7 @@ class ExactPreprocessNode(BaseNode):
|
||||
|
||||
|
||||
class ExactPostprocessNode(BaseNode):
|
||||
"""Postprocessing node - exact match to original."""
|
||||
"""Postprocessing node with full MultiDongle postprocessing support."""
|
||||
|
||||
__identifier__ = 'com.cluster.postprocess_node.ExactPostprocessNode'
|
||||
NODE_NAME = 'Postprocess Node'
|
||||
@ -253,18 +687,33 @@ class ExactPostprocessNode(BaseNode):
|
||||
self.add_output('output', color=(0, 255, 0))
|
||||
self.set_color(153, 51, 51)
|
||||
|
||||
# Original properties - exact match
|
||||
# Enhanced properties with MultiDongle postprocessing support
|
||||
self.create_property('postprocess_type', 'fire_detection')
|
||||
self.create_property('class_names', 'No Fire,Fire')
|
||||
self.create_property('output_format', 'JSON')
|
||||
self.create_property('confidence_threshold', 0.5)
|
||||
self.create_property('nms_threshold', 0.4)
|
||||
self.create_property('max_detections', 100)
|
||||
self.create_property('enable_confidence_filter', True)
|
||||
self.create_property('enable_nms', True)
|
||||
self.create_property('coordinate_system', 'relative')
|
||||
self.create_property('operations', 'filter,nms,format')
|
||||
|
||||
# Original property options - exact match
|
||||
# Enhanced property options with MultiDongle integration
|
||||
self._property_options = {
|
||||
'output_format': ['JSON', 'XML', 'CSV', 'Binary'],
|
||||
'confidence_threshold': {'min': 0.0, 'max': 1.0, 'step': 0.1},
|
||||
'nms_threshold': {'min': 0.0, 'max': 1.0, 'step': 0.1},
|
||||
'max_detections': {'min': 1, 'max': 1000}
|
||||
'postprocess_type': ['fire_detection', 'yolo_v3', 'yolo_v5', 'classification', 'raw_output'],
|
||||
'class_names': {
|
||||
'placeholder': 'comma-separated class names',
|
||||
'description': 'Class names for model output (e.g., "No Fire,Fire" or "person,car,bicycle")'
|
||||
},
|
||||
'output_format': ['JSON', 'XML', 'CSV', 'Binary', 'MessagePack', 'YAML'],
|
||||
'confidence_threshold': {'min': 0.0, 'max': 1.0, 'step': 0.01},
|
||||
'nms_threshold': {'min': 0.0, 'max': 1.0, 'step': 0.01},
|
||||
'max_detections': {'min': 1, 'max': 1000},
|
||||
'enable_confidence_filter': {'type': 'bool', 'default': True},
|
||||
'enable_nms': {'type': 'bool', 'default': True},
|
||||
'coordinate_system': ['relative', 'absolute', 'center', 'custom'],
|
||||
'operations': {'placeholder': 'comma-separated: filter,nms,format,validate,transform'}
|
||||
}
|
||||
|
||||
# Create custom properties dictionary for UI compatibility
|
||||
@ -305,6 +754,120 @@ class ExactPostprocessNode(BaseNode):
|
||||
pass
|
||||
return properties
|
||||
|
||||
def get_multidongle_postprocess_options(self):
|
||||
"""Create PostProcessorOptions from node configuration."""
|
||||
try:
|
||||
from ..functions.Multidongle import PostProcessType, PostProcessorOptions
|
||||
|
||||
postprocess_type_str = self.get_property('postprocess_type')
|
||||
|
||||
# Map string to enum
|
||||
type_mapping = {
|
||||
'fire_detection': PostProcessType.FIRE_DETECTION,
|
||||
'yolo_v3': PostProcessType.YOLO_V3,
|
||||
'yolo_v5': PostProcessType.YOLO_V5,
|
||||
'classification': PostProcessType.CLASSIFICATION,
|
||||
'raw_output': PostProcessType.RAW_OUTPUT
|
||||
}
|
||||
|
||||
postprocess_type = type_mapping.get(postprocess_type_str, PostProcessType.FIRE_DETECTION)
|
||||
|
||||
# Parse class names
|
||||
class_names_str = self.get_property('class_names')
|
||||
class_names = [name.strip() for name in class_names_str.split(',') if name.strip()] if class_names_str else []
|
||||
|
||||
return PostProcessorOptions(
|
||||
postprocess_type=postprocess_type,
|
||||
threshold=self.get_property('confidence_threshold'),
|
||||
class_names=class_names,
|
||||
nms_threshold=self.get_property('nms_threshold'),
|
||||
max_detections_per_class=self.get_property('max_detections')
|
||||
)
|
||||
except ImportError:
|
||||
print("Warning: PostProcessorOptions not available")
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Error creating PostProcessorOptions: {e}")
|
||||
return None
|
||||
|
||||
def get_postprocessing_config(self):
|
||||
"""Get postprocessing configuration for pipeline execution."""
|
||||
return {
|
||||
'node_id': self.id,
|
||||
'node_name': self.name(),
|
||||
# MultiDongle postprocessing integration
|
||||
'postprocess_type': self.get_property('postprocess_type'),
|
||||
'class_names': self._parse_class_list(self.get_property('class_names')),
|
||||
'multidongle_options': self.get_multidongle_postprocess_options(),
|
||||
# Core postprocessing properties
|
||||
'output_format': self.get_property('output_format'),
|
||||
'confidence_threshold': self.get_property('confidence_threshold'),
|
||||
'enable_confidence_filter': self.get_property('enable_confidence_filter'),
|
||||
'nms_threshold': self.get_property('nms_threshold'),
|
||||
'enable_nms': self.get_property('enable_nms'),
|
||||
'max_detections': self.get_property('max_detections'),
|
||||
'coordinate_system': self.get_property('coordinate_system'),
|
||||
'operations': self._parse_operations_list(self.get_property('operations'))
|
||||
}
|
||||
|
||||
def _parse_class_list(self, value_str):
|
||||
"""Parse comma-separated class names or indices."""
|
||||
if not value_str:
|
||||
return []
|
||||
return [x.strip() for x in value_str.split(',') if x.strip()]
|
||||
|
||||
def _parse_operations_list(self, operations_str):
|
||||
"""Parse comma-separated operations list."""
|
||||
if not operations_str:
|
||||
return []
|
||||
return [op.strip() for op in operations_str.split(',') if op.strip()]
|
||||
|
||||
def validate_configuration(self):
|
||||
"""Validate the current node configuration."""
|
||||
try:
|
||||
# Check confidence threshold
|
||||
confidence_threshold = self.get_property('confidence_threshold')
|
||||
if not isinstance(confidence_threshold, (int, float)) or confidence_threshold < 0 or confidence_threshold > 1:
|
||||
return False, "Confidence threshold must be between 0 and 1"
|
||||
|
||||
# Check NMS threshold
|
||||
nms_threshold = self.get_property('nms_threshold')
|
||||
if not isinstance(nms_threshold, (int, float)) or nms_threshold < 0 or nms_threshold > 1:
|
||||
return False, "NMS threshold must be between 0 and 1"
|
||||
|
||||
# Check max detections
|
||||
max_detections = self.get_property('max_detections')
|
||||
if not isinstance(max_detections, int) or max_detections < 1:
|
||||
return False, "Max detections must be at least 1"
|
||||
|
||||
# Validate operations string
|
||||
operations = self.get_property('operations')
|
||||
valid_operations = ['filter', 'nms', 'format', 'validate', 'transform', 'track', 'aggregate']
|
||||
|
||||
if operations:
|
||||
ops_list = [op.strip() for op in operations.split(',')]
|
||||
invalid_ops = [op for op in ops_list if op not in valid_operations]
|
||||
if invalid_ops:
|
||||
return False, f"Invalid operations: {', '.join(invalid_ops)}"
|
||||
|
||||
return True, ""
|
||||
except Exception as e:
|
||||
return False, f"Validation error: {str(e)}"
|
||||
|
||||
def get_display_properties(self):
|
||||
"""Return properties that should be displayed in the UI panel."""
|
||||
# Core properties that should always be visible for easy mode switching
|
||||
return [
|
||||
'postprocess_type',
|
||||
'class_names',
|
||||
'confidence_threshold',
|
||||
'nms_threshold',
|
||||
'output_format',
|
||||
'enable_confidence_filter',
|
||||
'enable_nms',
|
||||
'max_detections'
|
||||
]
|
||||
|
||||
|
||||
class ExactOutputNode(BaseNode):
|
||||
"""Output data sink node - exact match to original."""
|
||||
|
||||
@ -19,6 +19,7 @@ Usage:
|
||||
"""
|
||||
|
||||
from .base_node import BaseNodeWithProperties
|
||||
from ..functions.Multidongle import PostProcessType, PostProcessorOptions
|
||||
|
||||
|
||||
class PostprocessNode(BaseNodeWithProperties):
|
||||
@ -45,6 +46,17 @@ class PostprocessNode(BaseNodeWithProperties):
|
||||
|
||||
def setup_properties(self):
|
||||
"""Initialize postprocessing-specific properties."""
|
||||
# Postprocessing type - NEW: Integration with MultiDongle postprocessing
|
||||
self.create_business_property('postprocess_type', 'fire_detection', [
|
||||
'fire_detection', 'yolo_v3', 'yolo_v5', 'classification', 'raw_output'
|
||||
])
|
||||
|
||||
# Class names for postprocessing
|
||||
self.create_business_property('class_names', 'No Fire,Fire', {
|
||||
'placeholder': 'comma-separated class names',
|
||||
'description': 'Class names for model output (e.g., "No Fire,Fire" or "person,car,bicycle")'
|
||||
})
|
||||
|
||||
# Output format
|
||||
self.create_business_property('output_format', 'JSON', [
|
||||
'JSON', 'XML', 'CSV', 'Binary', 'MessagePack', 'YAML'
|
||||
@ -179,6 +191,33 @@ class PostprocessNode(BaseNodeWithProperties):
|
||||
|
||||
return True, ""
|
||||
|
||||
def get_multidongle_postprocess_options(self) -> 'PostProcessorOptions':
|
||||
"""Create PostProcessorOptions from node configuration."""
|
||||
postprocess_type_str = self.get_property('postprocess_type')
|
||||
|
||||
# Map string to enum
|
||||
type_mapping = {
|
||||
'fire_detection': PostProcessType.FIRE_DETECTION,
|
||||
'yolo_v3': PostProcessType.YOLO_V3,
|
||||
'yolo_v5': PostProcessType.YOLO_V5,
|
||||
'classification': PostProcessType.CLASSIFICATION,
|
||||
'raw_output': PostProcessType.RAW_OUTPUT
|
||||
}
|
||||
|
||||
postprocess_type = type_mapping.get(postprocess_type_str, PostProcessType.FIRE_DETECTION)
|
||||
|
||||
# Parse class names
|
||||
class_names_str = self.get_property('class_names')
|
||||
class_names = [name.strip() for name in class_names_str.split(',') if name.strip()] if class_names_str else []
|
||||
|
||||
return PostProcessorOptions(
|
||||
postprocess_type=postprocess_type,
|
||||
threshold=self.get_property('confidence_threshold'),
|
||||
class_names=class_names,
|
||||
nms_threshold=self.get_property('nms_threshold'),
|
||||
max_detections_per_class=self.get_property('max_detections')
|
||||
)
|
||||
|
||||
def get_postprocessing_config(self) -> dict:
|
||||
"""
|
||||
Get postprocessing configuration for pipeline execution.
|
||||
@ -189,6 +228,11 @@ class PostprocessNode(BaseNodeWithProperties):
|
||||
return {
|
||||
'node_id': self.id,
|
||||
'node_name': self.name(),
|
||||
# NEW: MultiDongle postprocessing integration
|
||||
'postprocess_type': self.get_property('postprocess_type'),
|
||||
'class_names': self._parse_class_list(self.get_property('class_names')),
|
||||
'multidongle_options': self.get_multidongle_postprocess_options(),
|
||||
# Original properties
|
||||
'output_format': self.get_property('output_format'),
|
||||
'confidence_threshold': self.get_property('confidence_threshold'),
|
||||
'enable_confidence_filter': self.get_property('enable_confidence_filter'),
|
||||
|
||||
1
core/optimization/__init__.py
Normal file
1
core/optimization/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""core/optimization — Pipeline 優化建議模組。"""
|
||||
248
core/optimization/engine.py
Normal file
248
core/optimization/engine.py
Normal file
@ -0,0 +1,248 @@
|
||||
"""
|
||||
core/optimization/engine.py
|
||||
|
||||
OptimizationEngine — 分析 Pipeline 執行統計,產生可執行的優化建議。
|
||||
|
||||
設計重點:
|
||||
- analyze_pipeline 接受來自 InferencePipeline.get_pipeline_statistics() 的 stats 字典。
|
||||
- 三條優化規則(rebalance_devices、adjust_queue、add_devices)各自獨立,
|
||||
可個別觸發,不互斥。
|
||||
- apply_suggestion 對 rebalance_devices 呼叫 device_manager.assign_device;
|
||||
其他類型(add_devices、adjust_queue)需要人工操作,僅記錄 log 後回傳 True。
|
||||
- predict_performance 使用保守係數 0.6 的啟發式估算。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 優化規則閾值
|
||||
_QUEUE_FILL_THRESHOLD = 0.70 # queue_fill_rate > 此值觸發 rebalance_devices
|
||||
_TIME_RATIO_THRESHOLD = 2.0 # max/min avg_processing_time > 此值觸發 adjust_queue
|
||||
_UTILIZATION_THRESHOLD = 85.0 # 所有裝置 utilization_pct > 此值觸發 add_devices
|
||||
_CONSERVATIVE_FACTOR = 0.6 # predict_performance 的保守係數
|
||||
|
||||
|
||||
@dataclass
|
||||
class OptimizationSuggestion:
|
||||
"""單一優化建議。
|
||||
|
||||
屬性:
|
||||
suggestion_id: 唯一識別碼(UUID 字串)。
|
||||
type: 建議類型,如 "rebalance_devices" | "adjust_queue" | "add_devices"。
|
||||
description: 使用者可讀的說明(避免技術術語)。
|
||||
estimated_improvement_pct: 預估改善百分比(0.0–100.0)。
|
||||
confidence: 信心程度,"high" | "medium" | "low"。
|
||||
action_params: 執行建議所需的參數字典。
|
||||
"""
|
||||
suggestion_id: str
|
||||
type: str
|
||||
description: str
|
||||
estimated_improvement_pct: float
|
||||
confidence: str
|
||||
action_params: Dict[str, Any]
|
||||
|
||||
|
||||
class OptimizationEngine:
|
||||
"""分析 Pipeline 執行統計並產生優化建議。"""
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 公開介面
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def analyze_pipeline(
|
||||
self,
|
||||
stats: Dict[str, Any],
|
||||
) -> List[OptimizationSuggestion]:
|
||||
"""分析 Pipeline 執行統計,產生優化建議清單。
|
||||
|
||||
參數:
|
||||
stats: 來自 InferencePipeline.get_pipeline_statistics() 的字典,
|
||||
格式詳見模組文件。
|
||||
|
||||
回傳:
|
||||
可能為空的 OptimizationSuggestion 清單。
|
||||
"""
|
||||
stages: Dict[str, Any] = stats.get("stages", {})
|
||||
devices: Dict[str, Any] = stats.get("devices", {})
|
||||
|
||||
suggestions: List[OptimizationSuggestion] = []
|
||||
|
||||
suggestions.extend(self._check_rebalance_devices(stages))
|
||||
suggestions.extend(self._check_adjust_queue(stages))
|
||||
suggestions.extend(self._check_add_devices(devices))
|
||||
|
||||
return suggestions
|
||||
|
||||
def predict_performance(
|
||||
self,
|
||||
config: List[Any],
|
||||
available_devices: List[Any],
|
||||
) -> Dict[str, float]:
|
||||
"""以啟發式方法估算 Pipeline 效能。
|
||||
|
||||
公式:
|
||||
estimated_fps = sum(device.gops for d in available_devices) / num_stages * 0.6
|
||||
estimated_latency_ms = 1000 / estimated_fps
|
||||
confidence_range = (estimated_fps * 0.8, estimated_fps * 1.2)
|
||||
|
||||
參數:
|
||||
config: Stage 設定列表(每個元素代表一個 Stage)。
|
||||
available_devices: DeviceInfo 物件列表(具備 gops 屬性)。
|
||||
|
||||
回傳:
|
||||
包含 estimated_fps、estimated_latency_ms、confidence_range 的字典。
|
||||
"""
|
||||
num_stages = len(config)
|
||||
total_gops = sum(getattr(d, "gops", 0) for d in available_devices)
|
||||
|
||||
if num_stages == 0 or total_gops == 0:
|
||||
return {
|
||||
"estimated_fps": 0.0,
|
||||
"estimated_latency_ms": 0.0,
|
||||
"confidence_range": (0.0, 0.0),
|
||||
}
|
||||
|
||||
estimated_fps = total_gops / num_stages * _CONSERVATIVE_FACTOR
|
||||
estimated_latency_ms = 1000.0 / estimated_fps
|
||||
confidence_range = (estimated_fps * 0.8, estimated_fps * 1.2)
|
||||
|
||||
return {
|
||||
"estimated_fps": estimated_fps,
|
||||
"estimated_latency_ms": estimated_latency_ms,
|
||||
"confidence_range": confidence_range,
|
||||
}
|
||||
|
||||
def apply_suggestion(
|
||||
self,
|
||||
suggestion: OptimizationSuggestion,
|
||||
device_manager: Any,
|
||||
) -> bool:
|
||||
"""執行優化建議。
|
||||
|
||||
- rebalance_devices:呼叫 device_manager.assign_device 並回傳其結果。
|
||||
- add_devices / adjust_queue:記錄 log(需人工操作),回傳 True。
|
||||
|
||||
參數:
|
||||
suggestion: 要執行的優化建議。
|
||||
device_manager: DeviceManager 實例。
|
||||
|
||||
回傳:
|
||||
執行是否成功。
|
||||
"""
|
||||
if suggestion.type == "rebalance_devices":
|
||||
device_id = suggestion.action_params.get("device_id", "")
|
||||
stage_id = suggestion.action_params.get("stage_id", "")
|
||||
success = device_manager.assign_device(device_id, stage_id)
|
||||
if success:
|
||||
logger.info(
|
||||
"已將裝置 %s 重新分配至 Stage %s", device_id, stage_id
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"無法將裝置 %s 分配至 Stage %s", device_id, stage_id
|
||||
)
|
||||
return success
|
||||
|
||||
if suggestion.type in ("add_devices", "adjust_queue"):
|
||||
logger.info(
|
||||
"優化建議 [%s]:%s(需要人工操作)",
|
||||
suggestion.type,
|
||||
suggestion.description,
|
||||
)
|
||||
return True
|
||||
|
||||
logger.warning("未知的建議類型:%s", suggestion.type)
|
||||
return False
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 內部規則實作
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _check_rebalance_devices(
|
||||
self, stages: Dict[str, Any]
|
||||
) -> List[OptimizationSuggestion]:
|
||||
"""規則 1:queue_fill_rate > 0.70 → 建議重新分配裝置。"""
|
||||
suggestions = []
|
||||
for stage_id, stage_data in stages.items():
|
||||
fill_rate: float = stage_data.get("queue_fill_rate", 0.0)
|
||||
if fill_rate > _QUEUE_FILL_THRESHOLD:
|
||||
pct = round((fill_rate - _QUEUE_FILL_THRESHOLD) / _QUEUE_FILL_THRESHOLD * 100, 1)
|
||||
suggestions.append(
|
||||
OptimizationSuggestion(
|
||||
suggestion_id=str(uuid.uuid4()),
|
||||
type="rebalance_devices",
|
||||
description=(
|
||||
f"{stage_id} 的佇列使用率偏高({fill_rate:.0%}),"
|
||||
"建議將算力較高的裝置分配給此階段以降低積壓。"
|
||||
),
|
||||
estimated_improvement_pct=min(pct, 40.0),
|
||||
confidence="medium",
|
||||
action_params={"stage_id": stage_id, "device_id": ""},
|
||||
)
|
||||
)
|
||||
return suggestions
|
||||
|
||||
def _check_adjust_queue(
|
||||
self, stages: Dict[str, Any]
|
||||
) -> List[OptimizationSuggestion]:
|
||||
"""規則 2:avg_processing_time 最大/最小比值 > 2.0 → 建議調整佇列大小。"""
|
||||
if len(stages) < 2:
|
||||
return []
|
||||
|
||||
times = {
|
||||
sid: data.get("avg_processing_time", 0.0)
|
||||
for sid, data in stages.items()
|
||||
}
|
||||
max_time = max(times.values())
|
||||
min_time = min(times.values())
|
||||
|
||||
if min_time <= 0 or max_time / min_time <= _TIME_RATIO_THRESHOLD:
|
||||
return []
|
||||
|
||||
ratio = max_time / min_time
|
||||
return [
|
||||
OptimizationSuggestion(
|
||||
suggestion_id=str(uuid.uuid4()),
|
||||
type="adjust_queue",
|
||||
description=(
|
||||
f"各 Stage 的處理時間差異達 {ratio:.1f} 倍,"
|
||||
"建議調整佇列大小以平衡各階段的吞吐量。"
|
||||
),
|
||||
estimated_improvement_pct=min((ratio - 2.0) * 10.0, 30.0),
|
||||
confidence="low",
|
||||
action_params={"max_stage": max(times, key=times.get), "ratio": ratio},
|
||||
)
|
||||
]
|
||||
|
||||
def _check_add_devices(
|
||||
self, devices: Dict[str, Any]
|
||||
) -> List[OptimizationSuggestion]:
|
||||
"""規則 3:所有 Dongle 使用率 > 85% → 建議增加更多 Dongle。"""
|
||||
if not devices:
|
||||
return []
|
||||
|
||||
utilizations = [
|
||||
data.get("utilization_pct", 0.0) for data in devices.values()
|
||||
]
|
||||
if not all(u > _UTILIZATION_THRESHOLD for u in utilizations):
|
||||
return []
|
||||
|
||||
avg_util = sum(utilizations) / len(utilizations)
|
||||
return [
|
||||
OptimizationSuggestion(
|
||||
suggestion_id=str(uuid.uuid4()),
|
||||
type="add_devices",
|
||||
description=(
|
||||
f"所有裝置的平均使用率已達 {avg_util:.1f}%,"
|
||||
"系統已接近飽和,建議增加更多 NPU 裝置。"
|
||||
),
|
||||
estimated_improvement_pct=min((avg_util - 85.0) * 2.0, 50.0),
|
||||
confidence="high",
|
||||
action_params={"current_avg_utilization": avg_util},
|
||||
)
|
||||
]
|
||||
23
core/performance/__init__.py
Normal file
23
core/performance/__init__.py
Normal file
@ -0,0 +1,23 @@
|
||||
"""
|
||||
core/performance — 效能測試與歷史記錄模組。
|
||||
|
||||
提供 Benchmark 執行、結果儲存與回歸分析功能。
|
||||
|
||||
使用範例:
|
||||
from core.performance import (
|
||||
PerformanceBenchmarker,
|
||||
BenchmarkConfig,
|
||||
BenchmarkResult,
|
||||
PerformanceHistory,
|
||||
)
|
||||
"""
|
||||
|
||||
from .benchmarker import BenchmarkConfig, BenchmarkResult, PerformanceBenchmarker
|
||||
from .history import PerformanceHistory
|
||||
|
||||
__all__ = [
|
||||
"BenchmarkConfig",
|
||||
"BenchmarkResult",
|
||||
"PerformanceBenchmarker",
|
||||
"PerformanceHistory",
|
||||
]
|
||||
247
core/performance/benchmarker.py
Normal file
247
core/performance/benchmarker.py
Normal file
@ -0,0 +1,247 @@
|
||||
"""
|
||||
core/performance/benchmarker.py — 效能基準測試模組。
|
||||
|
||||
提供 BenchmarkConfig、BenchmarkResult 資料結構,
|
||||
以及 PerformanceBenchmarker 執行單/多裝置效能測試並計算加速倍數。
|
||||
|
||||
設計重點:
|
||||
- 實際推論呼叫透過 inference_runner callable 注入,
|
||||
方便在沒有硬體的環境下進行單元測試(注入 Mock)。
|
||||
- 純計算邏輯(calculate_speedup 等)可直接測試,無需 Mock。
|
||||
|
||||
使用範例(測試環境):
|
||||
config = BenchmarkConfig(pipeline_config=[], test_input_source="test.mp4")
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
|
||||
def mock_runner(frame_data):
|
||||
return {"result": "ok"}
|
||||
|
||||
seq = benchmarker.run_sequential_benchmark(config, inference_runner=mock_runner)
|
||||
par = benchmarker.run_parallel_benchmark(config, inference_runner=mock_runner)
|
||||
speedup = benchmarker.calculate_speedup(seq, par)
|
||||
"""
|
||||
|
||||
import time
|
||||
import statistics
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Callable, Dict, List, Optional, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class BenchmarkConfig:
|
||||
"""Benchmark 測試設定。
|
||||
|
||||
屬性:
|
||||
pipeline_config: Pipeline 各 Stage 的設定列表(來自 UI)。
|
||||
test_input_source: 測試輸入來源(影片檔路徑或相機索引)。
|
||||
test_duration_seconds: 測試持續時間(秒),不含暖機階段。
|
||||
warmup_frames: 暖機幀數,不計入統計。
|
||||
"""
|
||||
pipeline_config: List[Any]
|
||||
test_input_source: str
|
||||
test_duration_seconds: float = 30.0
|
||||
warmup_frames: int = 50
|
||||
|
||||
|
||||
@dataclass
|
||||
class BenchmarkResult:
|
||||
"""單次 Benchmark 的測試結果。
|
||||
|
||||
屬性:
|
||||
mode: 測試模式,'sequential'(單裝置)或 'parallel'(多裝置)。
|
||||
fps: 每秒幀數。
|
||||
avg_latency_ms: 平均推論延遲(毫秒)。
|
||||
p95_latency_ms: 95th percentile 延遲(毫秒)。
|
||||
total_frames: 測試期間處理的總幀數(不含暖機)。
|
||||
timestamp: 測試開始的 Unix timestamp。
|
||||
device_config: 裝置分配設定,例如 {"KL520": 1}。
|
||||
id: 唯一識別碼(由 PerformanceHistory.record() 填入)。
|
||||
"""
|
||||
mode: str
|
||||
fps: float
|
||||
avg_latency_ms: float
|
||||
p95_latency_ms: float
|
||||
total_frames: int
|
||||
timestamp: float
|
||||
device_config: Dict[str, Any]
|
||||
id: Optional[str] = field(default=None)
|
||||
|
||||
|
||||
class PerformanceBenchmarker:
|
||||
"""執行單裝置 vs 多裝置效能測試,計算加速倍數。
|
||||
|
||||
設計為可測試性(Testability-First):
|
||||
- run_sequential_benchmark / run_parallel_benchmark 接受 inference_runner 參數,
|
||||
讓測試時可注入 Mock 而不需要真實硬體。
|
||||
- calculate_speedup 為純函式,直接接受 BenchmarkResult 計算。
|
||||
|
||||
屬性:
|
||||
device_config: 裝置設定資訊,會填入 BenchmarkResult.device_config。
|
||||
"""
|
||||
|
||||
def __init__(self, device_config: Optional[Dict[str, Any]] = None):
|
||||
"""初始化 PerformanceBenchmarker。
|
||||
|
||||
參數:
|
||||
device_config: 裝置設定,例如 {"KL520": 1}。未指定時使用空字典。
|
||||
"""
|
||||
self.device_config: Dict[str, Any] = device_config or {}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 公開介面
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def run_sequential_benchmark(
|
||||
self,
|
||||
config: BenchmarkConfig,
|
||||
inference_runner: Optional[Callable[[Any], Any]] = None,
|
||||
) -> BenchmarkResult:
|
||||
"""以單裝置(循序)模式執行 Benchmark。
|
||||
|
||||
參數:
|
||||
config: 測試設定。
|
||||
inference_runner: 推論執行函式,簽名為 ``(frame_data: Any) -> Any``。
|
||||
若為 None,使用 no-op 函式(僅供架構驗證)。
|
||||
|
||||
回傳:
|
||||
mode='sequential' 的 BenchmarkResult。
|
||||
"""
|
||||
runner = inference_runner or self._default_runner
|
||||
return self._run_benchmark(config, runner, mode="sequential")
|
||||
|
||||
def run_parallel_benchmark(
|
||||
self,
|
||||
config: BenchmarkConfig,
|
||||
inference_runner: Optional[Callable[[Any], Any]] = None,
|
||||
) -> BenchmarkResult:
|
||||
"""以多裝置(平行)模式執行 Benchmark。
|
||||
|
||||
參數:
|
||||
config: 測試設定。
|
||||
inference_runner: 推論執行函式,簽名為 ``(frame_data: Any) -> Any``。
|
||||
若為 None,使用 no-op 函式(僅供架構驗證)。
|
||||
|
||||
回傳:
|
||||
mode='parallel' 的 BenchmarkResult。
|
||||
"""
|
||||
runner = inference_runner or self._default_runner
|
||||
return self._run_benchmark(config, runner, mode="parallel")
|
||||
|
||||
def calculate_speedup(
|
||||
self,
|
||||
seq: BenchmarkResult,
|
||||
par: BenchmarkResult,
|
||||
) -> float:
|
||||
"""計算平行相對於循序的加速倍數。
|
||||
|
||||
計算公式:par.fps / seq.fps
|
||||
|
||||
參數:
|
||||
seq: 循序模式的 BenchmarkResult。
|
||||
par: 平行模式的 BenchmarkResult。
|
||||
|
||||
回傳:
|
||||
加速倍數(float)。
|
||||
|
||||
引發:
|
||||
ValueError: 當 seq.fps <= 0 時(避免除以零)。
|
||||
"""
|
||||
if seq.fps <= 0:
|
||||
raise ValueError(
|
||||
f"循序模式的 FPS 必須大於 0,收到:{seq.fps}"
|
||||
)
|
||||
return par.fps / seq.fps
|
||||
|
||||
def run_full_benchmark(
|
||||
self,
|
||||
config: BenchmarkConfig,
|
||||
inference_runner: Optional[Callable[[Any], Any]] = None,
|
||||
) -> Tuple[BenchmarkResult, BenchmarkResult, float]:
|
||||
"""執行完整 Benchmark:循序 → 平行 → 計算加速倍數。
|
||||
|
||||
執行序列:
|
||||
1. 執行循序 Benchmark
|
||||
2. 執行平行 Benchmark
|
||||
3. 計算加速倍數
|
||||
|
||||
參數:
|
||||
config: 測試設定。
|
||||
inference_runner: 推論執行函式(可注入 Mock)。
|
||||
|
||||
回傳:
|
||||
Tuple[BenchmarkResult, BenchmarkResult, float]
|
||||
即 (sequential_result, parallel_result, speedup)。
|
||||
"""
|
||||
seq_result = self.run_sequential_benchmark(config, inference_runner)
|
||||
par_result = self.run_parallel_benchmark(config, inference_runner)
|
||||
speedup = self.calculate_speedup(seq_result, par_result)
|
||||
return seq_result, par_result, speedup
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 內部實作
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _run_benchmark(
|
||||
self,
|
||||
config: BenchmarkConfig,
|
||||
runner: Callable[[Any], Any],
|
||||
mode: str,
|
||||
) -> BenchmarkResult:
|
||||
"""執行 Benchmark 的共用邏輯。
|
||||
|
||||
流程:
|
||||
1. 暖機(warmup_frames 幀,不計入統計)
|
||||
2. 正式測試(test_duration_seconds 秒)
|
||||
3. 計算 FPS、平均延遲、p95 延遲
|
||||
|
||||
參數:
|
||||
config: 測試設定。
|
||||
runner: 推論執行函式。
|
||||
mode: 'sequential' 或 'parallel'。
|
||||
|
||||
回傳:
|
||||
BenchmarkResult。
|
||||
"""
|
||||
# 暖機階段
|
||||
for _ in range(config.warmup_frames):
|
||||
runner(None)
|
||||
|
||||
# 正式測試
|
||||
latencies: List[float] = []
|
||||
test_start = time.time()
|
||||
|
||||
while time.time() - test_start < config.test_duration_seconds:
|
||||
frame_start = time.time()
|
||||
runner(None)
|
||||
frame_end = time.time()
|
||||
latencies.append((frame_end - frame_start) * 1000.0) # 轉換為毫秒
|
||||
|
||||
total_frames = len(latencies)
|
||||
elapsed = time.time() - test_start
|
||||
|
||||
# 計算統計數值
|
||||
if total_frames == 0:
|
||||
fps = 0.0
|
||||
avg_latency_ms = 0.0
|
||||
p95_latency_ms = 0.0
|
||||
else:
|
||||
fps = total_frames / elapsed if elapsed > 0 else 0.0
|
||||
avg_latency_ms = statistics.mean(latencies)
|
||||
sorted_latencies = sorted(latencies)
|
||||
p95_index = int(len(sorted_latencies) * 0.95)
|
||||
p95_latency_ms = sorted_latencies[min(p95_index, len(sorted_latencies) - 1)]
|
||||
|
||||
return BenchmarkResult(
|
||||
mode=mode,
|
||||
fps=fps,
|
||||
avg_latency_ms=avg_latency_ms,
|
||||
p95_latency_ms=p95_latency_ms,
|
||||
total_frames=total_frames,
|
||||
timestamp=test_start,
|
||||
device_config=dict(self.device_config),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _default_runner(frame_data: Any) -> Any:
|
||||
"""預設的推論執行函式(no-op,僅供架構驗證)。"""
|
||||
return None
|
||||
233
core/performance/history.py
Normal file
233
core/performance/history.py
Normal file
@ -0,0 +1,233 @@
|
||||
"""
|
||||
core/performance/history.py — Benchmark 歷史記錄模組。
|
||||
|
||||
提供 PerformanceHistory 類別,負責:
|
||||
- 將 BenchmarkResult 以 JSON 格式持久化到本地磁碟。
|
||||
- 依條件(limit / mode)查詢歷史記錄。
|
||||
- 產生兩次測試間的回歸比較報告。
|
||||
|
||||
儲存格式範例:
|
||||
{
|
||||
"records": [
|
||||
{
|
||||
"id": "benchmark_20260405_143022",
|
||||
"mode": "parallel",
|
||||
"fps": 45.2,
|
||||
"avg_latency_ms": 22.1,
|
||||
"p95_latency_ms": 35.0,
|
||||
"total_frames": 1356,
|
||||
"timestamp": 1743856222.0,
|
||||
"device_config": {"KL720": 2}
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .benchmarker import BenchmarkResult
|
||||
|
||||
|
||||
class PerformanceHistory:
|
||||
"""本地 Benchmark 歷史記錄管理器。
|
||||
|
||||
屬性:
|
||||
storage_path: JSON 儲存檔案的完整路徑。
|
||||
預設為 ``~/.cluster4npu/benchmark_history.json``。
|
||||
"""
|
||||
|
||||
DEFAULT_STORAGE_PATH = os.path.join(
|
||||
os.path.expanduser("~"), ".cluster4npu", "benchmark_history.json"
|
||||
)
|
||||
|
||||
def __init__(self, storage_path: str = DEFAULT_STORAGE_PATH):
|
||||
"""初始化 PerformanceHistory。
|
||||
|
||||
若儲存目錄不存在,會自動建立。
|
||||
|
||||
參數:
|
||||
storage_path: JSON 儲存檔案路徑。
|
||||
"""
|
||||
self.storage_path = storage_path
|
||||
self._ensure_storage_directory()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 公開介面
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def record(self, result: BenchmarkResult) -> None:
|
||||
"""記錄一筆 BenchmarkResult 並持久化至 JSON。
|
||||
|
||||
此方法會:
|
||||
1. 為結果產生唯一 id(若尚未有 id)。
|
||||
2. 將 id 寫回 result.id。
|
||||
3. 追加到 JSON 儲存。
|
||||
|
||||
參數:
|
||||
result: 要記錄的 BenchmarkResult。
|
||||
"""
|
||||
data = self._load_raw()
|
||||
|
||||
# 產生唯一 id
|
||||
record_id = self._generate_id(result)
|
||||
result.id = record_id
|
||||
|
||||
record_dict = self._result_to_dict(result)
|
||||
data["records"].append(record_dict)
|
||||
|
||||
self._save_raw(data)
|
||||
|
||||
def get_history(
|
||||
self,
|
||||
limit: int = 50,
|
||||
mode: Optional[str] = None,
|
||||
) -> List[BenchmarkResult]:
|
||||
"""查詢歷史記錄。
|
||||
|
||||
回傳最新優先(reverse chronological)的記錄列表。
|
||||
|
||||
參數:
|
||||
limit: 最多回傳幾筆,預設 50。
|
||||
mode: 若指定,只回傳符合 mode 的記錄('sequential' 或 'parallel')。
|
||||
|
||||
回傳:
|
||||
List[BenchmarkResult],最新的記錄排在最前面。
|
||||
"""
|
||||
data = self._load_raw()
|
||||
records = data.get("records", [])
|
||||
|
||||
# 過濾 mode
|
||||
if mode is not None:
|
||||
records = [r for r in records if r.get("mode") == mode]
|
||||
|
||||
# 最新優先(依 timestamp 降序)
|
||||
records = sorted(records, key=lambda r: r.get("timestamp", 0), reverse=True)
|
||||
|
||||
# 套用 limit
|
||||
records = records[:limit]
|
||||
|
||||
return [self._dict_to_result(r) for r in records]
|
||||
|
||||
def get_regression_report(
|
||||
self,
|
||||
baseline_id: str,
|
||||
compare_id: str,
|
||||
) -> Dict[str, Any]:
|
||||
"""比較兩次測試的效能差異,產生回歸報告。
|
||||
|
||||
參數:
|
||||
baseline_id: 基準測試的 id。
|
||||
compare_id: 比較測試的 id。
|
||||
|
||||
回傳:
|
||||
包含以下鍵的字典:
|
||||
- baseline: BenchmarkResult(基準)
|
||||
- compare: BenchmarkResult(比較對象)
|
||||
- fps_change_pct: FPS 變化百分比(正值為改善)
|
||||
- avg_latency_change_pct: 平均延遲變化百分比(負值為改善)
|
||||
- p95_latency_change_pct: P95 延遲變化百分比(負值為改善)
|
||||
|
||||
引發:
|
||||
ValueError: 若任一 id 不存在於歷史記錄中。
|
||||
"""
|
||||
data = self._load_raw()
|
||||
all_records = {r["id"]: r for r in data.get("records", [])}
|
||||
|
||||
if baseline_id not in all_records:
|
||||
raise ValueError(f"找不到基準測試 id:{baseline_id}")
|
||||
if compare_id not in all_records:
|
||||
raise ValueError(f"找不到比較測試 id:{compare_id}")
|
||||
|
||||
baseline = self._dict_to_result(all_records[baseline_id])
|
||||
compare = self._dict_to_result(all_records[compare_id])
|
||||
|
||||
def pct_change(old: float, new: float) -> float:
|
||||
"""計算相對變化百分比。"""
|
||||
if old == 0:
|
||||
return 0.0
|
||||
return (new - old) / old * 100.0
|
||||
|
||||
return {
|
||||
"baseline": baseline,
|
||||
"compare": compare,
|
||||
"fps_change_pct": pct_change(baseline.fps, compare.fps),
|
||||
"avg_latency_change_pct": pct_change(
|
||||
baseline.avg_latency_ms, compare.avg_latency_ms
|
||||
),
|
||||
"p95_latency_change_pct": pct_change(
|
||||
baseline.p95_latency_ms, compare.p95_latency_ms
|
||||
),
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 內部實作
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _ensure_storage_directory(self) -> None:
|
||||
"""若儲存目錄不存在,自動建立。"""
|
||||
parent_dir = os.path.dirname(self.storage_path)
|
||||
if parent_dir:
|
||||
os.makedirs(parent_dir, exist_ok=True)
|
||||
|
||||
def _load_raw(self) -> Dict[str, Any]:
|
||||
"""從 JSON 檔案讀取原始資料。若檔案不存在或損毀,回傳空結構。"""
|
||||
if not os.path.exists(self.storage_path):
|
||||
return {"records": []}
|
||||
try:
|
||||
with open(self.storage_path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.warning("歷史記錄 JSON 檔案損毀,降級回傳空結構:%s", e)
|
||||
return {"records": []}
|
||||
except (IOError, OSError) as e:
|
||||
logger.warning("無法讀取歷史記錄檔案,降級回傳空結構:%s", e)
|
||||
return {"records": []}
|
||||
|
||||
def _save_raw(self, data: Dict[str, Any]) -> None:
|
||||
"""將資料寫入 JSON 檔案。"""
|
||||
with open(self.storage_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
|
||||
@staticmethod
|
||||
def _generate_id(result: BenchmarkResult) -> str:
|
||||
"""依 timestamp 產生唯一識別碼。
|
||||
|
||||
格式:``benchmark_YYYYMMDD_HHMMSSffffff``
|
||||
"""
|
||||
dt = datetime.fromtimestamp(result.timestamp)
|
||||
return dt.strftime("benchmark_%Y%m%d_%H%M%S%f")
|
||||
|
||||
@staticmethod
|
||||
def _result_to_dict(result: BenchmarkResult) -> Dict[str, Any]:
|
||||
"""將 BenchmarkResult 轉換為可序列化的字典。"""
|
||||
return {
|
||||
"id": result.id,
|
||||
"mode": result.mode,
|
||||
"fps": result.fps,
|
||||
"avg_latency_ms": result.avg_latency_ms,
|
||||
"p95_latency_ms": result.p95_latency_ms,
|
||||
"total_frames": result.total_frames,
|
||||
"timestamp": result.timestamp,
|
||||
"device_config": result.device_config,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _dict_to_result(data: Dict[str, Any]) -> BenchmarkResult:
|
||||
"""將字典轉換回 BenchmarkResult。"""
|
||||
return BenchmarkResult(
|
||||
id=data.get("id"),
|
||||
mode=data["mode"],
|
||||
fps=data["fps"],
|
||||
avg_latency_ms=data["avg_latency_ms"],
|
||||
p95_latency_ms=data["p95_latency_ms"],
|
||||
total_frames=data["total_frames"],
|
||||
timestamp=data["timestamp"],
|
||||
device_config=data.get("device_config", {}),
|
||||
)
|
||||
428
core/performance/report_exporter.py
Normal file
428
core/performance/report_exporter.py
Normal file
@ -0,0 +1,428 @@
|
||||
"""
|
||||
core/performance/report_exporter.py — 效能報告匯出模組。
|
||||
|
||||
提供 DeviceSummary、ReportData 資料結構與 ReportExporter 主類別,
|
||||
支援將 Benchmark 結果匯出為 PDF(需要 reportlab)或 CSV(標準庫)。
|
||||
|
||||
設計重點:
|
||||
- ReportExporter 不依賴 PyQt5,只依賴 reportlab 與標準庫。
|
||||
- reportlab 以 try/except ImportError 保護;若未安裝,export_pdf() 拋出 ImportError。
|
||||
- export_csv() 只用標準庫 csv,永遠可用。
|
||||
- 無狀態設計(stateless):每次匯出建立新實例或直接呼叫靜態方法。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, List, Optional
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# reportlab 可用性旗標
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
try:
|
||||
from reportlab.platypus import SimpleDocTemplate # noqa: F401
|
||||
_REPORTLAB_AVAILABLE = True
|
||||
except ImportError:
|
||||
_REPORTLAB_AVAILABLE = False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 資料結構
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@dataclass
|
||||
class DeviceSummary:
|
||||
"""單一裝置的摘要資訊,來自 DeviceManager。"""
|
||||
device_id: str
|
||||
product_name: str # 如 "KL720"
|
||||
firmware_version: str
|
||||
is_active: bool
|
||||
|
||||
|
||||
@dataclass
|
||||
class ReportData:
|
||||
"""
|
||||
報告所需的完整資料,由呼叫方(UI 層)從各模組收集後傳入 ReportExporter。
|
||||
設計為純資料容器,與 UI / SDK 解耦,方便單元測試。
|
||||
"""
|
||||
# 報告基本資訊
|
||||
report_title: str = "效能測試報告"
|
||||
generated_at: float = field(default_factory=time.time) # UNIX timestamp
|
||||
pipeline_name: str = "" # 來自 .mflow 檔名或使用者命名
|
||||
|
||||
# Benchmark 結果(來自 PerformanceBenchmarker.run_full_benchmark())
|
||||
sequential_result: Optional[Any] = None # BenchmarkResult
|
||||
parallel_result: Optional[Any] = None # BenchmarkResult
|
||||
speedup: Optional[float] = None # par.fps / seq.fps
|
||||
|
||||
# 歷史記錄(來自 PerformanceHistory.get_history())
|
||||
history_records: List[Any] = field(default_factory=list) # List[BenchmarkResult]
|
||||
|
||||
# 裝置資訊(來自 DeviceManager.get_all_devices())
|
||||
devices: List[DeviceSummary] = field(default_factory=list)
|
||||
|
||||
# 圖表截圖(由 UI 層在匯出前擷取)
|
||||
chart_image_bytes: Optional[bytes] = None # PNG bytes,來自 PerformanceDashboard
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ReportExporter
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class ReportExporter:
|
||||
"""
|
||||
負責將 ReportData 序列化為 PDF 或 CSV 檔案。
|
||||
無狀態設計(stateless):每次匯出建立新實例或直接呼叫靜態方法。
|
||||
"""
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# PDF 匯出
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def export_pdf(
|
||||
self,
|
||||
data: ReportData,
|
||||
output_path: "str | Path",
|
||||
) -> Path:
|
||||
"""
|
||||
將完整效能報告匯出為 PDF。
|
||||
回傳實際寫入的檔案路徑。
|
||||
若 output_path 的父目錄不存在,自動建立。
|
||||
|
||||
引發:
|
||||
ImportError: 若 reportlab 未安裝,提示安裝指令。
|
||||
"""
|
||||
if not _REPORTLAB_AVAILABLE:
|
||||
raise ImportError(
|
||||
"reportlab is required for PDF export. Install with: pip install reportlab>=4.0.0"
|
||||
)
|
||||
|
||||
try:
|
||||
from reportlab.platypus import (
|
||||
SimpleDocTemplate,
|
||||
Table,
|
||||
TableStyle,
|
||||
Paragraph,
|
||||
Spacer,
|
||||
Image,
|
||||
)
|
||||
from reportlab.lib.pagesizes import A4
|
||||
from reportlab.lib.styles import getSampleStyleSheet
|
||||
from reportlab.lib import colors
|
||||
from reportlab.lib.units import mm
|
||||
import reportlab # noqa: F401 — 確認已安裝
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
f"reportlab 未安裝,請執行:pip install reportlab>=4.0.0\n原始錯誤:{e}"
|
||||
) from e
|
||||
|
||||
output_path = Path(output_path)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
doc = SimpleDocTemplate(
|
||||
str(output_path),
|
||||
pagesize=A4,
|
||||
rightMargin=20 * mm,
|
||||
leftMargin=20 * mm,
|
||||
topMargin=20 * mm,
|
||||
bottomMargin=20 * mm,
|
||||
)
|
||||
|
||||
story: list = []
|
||||
styles = getSampleStyleSheet()
|
||||
|
||||
# 封面(用 Paragraph 實作,封面 callback 難以在無 GUI 環境穩定測試)
|
||||
self._build_cover_paragraphs(story, data, styles, Paragraph, Spacer)
|
||||
|
||||
# Benchmark 結果表
|
||||
self._build_benchmark_table(story, data, styles, Table, TableStyle, Paragraph, Spacer, colors)
|
||||
|
||||
# 趨勢圖
|
||||
self._build_trend_chart(story, data, styles, Paragraph, Spacer, Image)
|
||||
|
||||
# 歷史記錄表
|
||||
self._build_history_table(story, data, styles, Table, TableStyle, Paragraph, Spacer, colors)
|
||||
|
||||
# 裝置資訊
|
||||
self._build_device_info(story, data, styles, Paragraph, Spacer)
|
||||
|
||||
doc.build(story)
|
||||
return output_path
|
||||
|
||||
def _build_cover_page(self, canvas, data: ReportData) -> None:
|
||||
"""繪製封面:報告標題、生成時間、Pipeline 名稱、裝置清單(canvas callback 版本)"""
|
||||
canvas.saveState()
|
||||
canvas.setFont("Helvetica-Bold", 24)
|
||||
canvas.drawCentredString(
|
||||
canvas._pagesize[0] / 2,
|
||||
canvas._pagesize[1] * 0.65,
|
||||
data.report_title,
|
||||
)
|
||||
canvas.setFont("Helvetica", 12)
|
||||
canvas.drawCentredString(
|
||||
canvas._pagesize[0] / 2,
|
||||
canvas._pagesize[1] * 0.58,
|
||||
f"生成時間:{self._get_timestamp_str(data.generated_at)}",
|
||||
)
|
||||
if data.pipeline_name:
|
||||
canvas.drawCentredString(
|
||||
canvas._pagesize[0] / 2,
|
||||
canvas._pagesize[1] * 0.53,
|
||||
f"Pipeline:{data.pipeline_name}",
|
||||
)
|
||||
canvas.drawCentredString(
|
||||
canvas._pagesize[0] / 2,
|
||||
canvas._pagesize[1] * 0.48,
|
||||
f"裝置數量:{len(data.devices)}",
|
||||
)
|
||||
canvas.restoreState()
|
||||
|
||||
def _build_cover_paragraphs(self, story, data, styles, Paragraph, Spacer) -> None:
|
||||
"""以 Paragraph flowable 形式建立封面內容(嵌入 story 流)。"""
|
||||
story.append(Spacer(1, 60))
|
||||
story.append(Paragraph(data.report_title, styles["Title"]))
|
||||
story.append(Spacer(1, 12))
|
||||
story.append(Paragraph(
|
||||
f"生成時間:{self._get_timestamp_str(data.generated_at)}",
|
||||
styles["Normal"],
|
||||
))
|
||||
if data.pipeline_name:
|
||||
story.append(Paragraph(f"Pipeline:{data.pipeline_name}", styles["Normal"]))
|
||||
story.append(Paragraph(f"裝置數量:{len(data.devices)}", styles["Normal"]))
|
||||
story.append(Spacer(1, 30))
|
||||
|
||||
def _build_benchmark_table(
|
||||
self, story, data, styles=None,
|
||||
Table=None, TableStyle=None, Paragraph=None, Spacer=None, colors=None,
|
||||
) -> None:
|
||||
"""
|
||||
建立 Benchmark 結果對比表(reportlab Table)。
|
||||
欄位:指標 / 循序模式 / 平行模式 / 差異%
|
||||
指標:FPS、平均延遲(ms)、P95 延遲(ms)、總幀數
|
||||
"""
|
||||
if Paragraph is None:
|
||||
return
|
||||
story.append(Paragraph("Benchmark 結果", styles["Heading1"]))
|
||||
story.append(Spacer(1, 8))
|
||||
|
||||
seq = data.sequential_result
|
||||
par = data.parallel_result
|
||||
|
||||
if seq is None or par is None:
|
||||
story.append(Paragraph("無 Benchmark 資料", styles["Normal"]))
|
||||
story.append(Spacer(1, 12))
|
||||
return
|
||||
|
||||
def diff_pct(a, b):
|
||||
if a and a != 0:
|
||||
return f"{(b - a) / a * 100:+.1f}%"
|
||||
return "—"
|
||||
|
||||
table_data = [
|
||||
["指標", "循序模式", "平行模式", "差異%"],
|
||||
["FPS", f"{seq.fps:.1f}", f"{par.fps:.1f}", diff_pct(seq.fps, par.fps)],
|
||||
["平均延遲(ms)", f"{seq.avg_latency_ms:.1f}", f"{par.avg_latency_ms:.1f}", diff_pct(seq.avg_latency_ms, par.avg_latency_ms)],
|
||||
["P95 延遲(ms)", f"{seq.p95_latency_ms:.1f}", f"{par.p95_latency_ms:.1f}", diff_pct(seq.p95_latency_ms, par.p95_latency_ms)],
|
||||
["總幀數", str(seq.total_frames), str(par.total_frames), "—"],
|
||||
]
|
||||
if data.speedup is not None:
|
||||
table_data.append(["加速倍數", "—", f"{data.speedup:.2f}x", "—"])
|
||||
|
||||
t = Table(table_data)
|
||||
t.setStyle(TableStyle([
|
||||
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
|
||||
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
|
||||
("ALIGN", (0, 0), (-1, -1), "CENTER"),
|
||||
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
|
||||
("GRID", (0, 0), (-1, -1), 0.5, colors.black),
|
||||
]))
|
||||
story.append(t)
|
||||
story.append(Spacer(1, 20))
|
||||
|
||||
def _build_trend_chart(
|
||||
self, story, data, styles=None,
|
||||
Paragraph=None, Spacer=None, Image=None,
|
||||
) -> None:
|
||||
"""
|
||||
若 data.chart_image_bytes 不為 None,將圖表 PNG 嵌入 PDF。
|
||||
若為 None,插入「無圖表資料」的提示文字。
|
||||
"""
|
||||
if Paragraph is None:
|
||||
return
|
||||
story.append(Paragraph("效能趨勢圖", styles["Heading1"]))
|
||||
story.append(Spacer(1, 8))
|
||||
if data.chart_image_bytes is not None:
|
||||
img_buf = io.BytesIO(data.chart_image_bytes)
|
||||
img = Image(img_buf, width=400, height=200)
|
||||
story.append(img)
|
||||
else:
|
||||
story.append(Paragraph("(無圖表資料)", styles["Normal"]))
|
||||
story.append(Spacer(1, 20))
|
||||
|
||||
def _build_history_table(
|
||||
self, story, data, styles=None,
|
||||
Table=None, TableStyle=None, Paragraph=None, Spacer=None, colors=None,
|
||||
) -> None:
|
||||
"""
|
||||
建立歷史記錄表(最多顯示 20 筆,超過則截斷並標注)。
|
||||
欄位:測試時間 / 模式 / FPS / 平均延遲(ms) / P95 延遲(ms)
|
||||
"""
|
||||
if Paragraph is None:
|
||||
return
|
||||
story.append(Paragraph("歷史記錄", styles["Heading1"]))
|
||||
story.append(Spacer(1, 8))
|
||||
|
||||
records = data.history_records[:20]
|
||||
truncated = len(data.history_records) > 20
|
||||
|
||||
table_data = [["測試時間", "模式", "FPS", "平均延遲(ms)", "P95 延遲(ms)"]]
|
||||
for r in records:
|
||||
table_data.append([
|
||||
self._get_timestamp_str(r.timestamp),
|
||||
r.mode,
|
||||
f"{r.fps:.1f}",
|
||||
f"{r.avg_latency_ms:.1f}",
|
||||
f"{r.p95_latency_ms:.1f}",
|
||||
])
|
||||
|
||||
if not records:
|
||||
table_data.append(["(無記錄)", "", "", "", ""])
|
||||
|
||||
t = Table(table_data)
|
||||
t.setStyle(TableStyle([
|
||||
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
|
||||
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
|
||||
("ALIGN", (0, 0), (-1, -1), "CENTER"),
|
||||
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
|
||||
("GRID", (0, 0), (-1, -1), 0.5, colors.black),
|
||||
]))
|
||||
story.append(t)
|
||||
|
||||
if truncated:
|
||||
story.append(Spacer(1, 6))
|
||||
story.append(Paragraph(
|
||||
f"(僅顯示最新 20 筆,共 {len(data.history_records)} 筆)",
|
||||
styles["Normal"],
|
||||
))
|
||||
story.append(Spacer(1, 20))
|
||||
|
||||
def _build_device_info(
|
||||
self, story, data, styles=None,
|
||||
Paragraph=None, Spacer=None,
|
||||
) -> None:
|
||||
"""列出測試時連接的裝置清單:裝置 ID、型號、韌體版本、是否啟用。"""
|
||||
if Paragraph is None:
|
||||
return
|
||||
story.append(Paragraph("裝置資訊", styles["Heading1"]))
|
||||
story.append(Spacer(1, 8))
|
||||
if not data.devices:
|
||||
story.append(Paragraph("(無裝置資訊)", styles["Normal"]))
|
||||
else:
|
||||
for dev in data.devices:
|
||||
status = "啟用" if dev.is_active else "停用"
|
||||
story.append(Paragraph(
|
||||
f"裝置 {dev.device_id}:{dev.product_name},韌體 {dev.firmware_version},{status}",
|
||||
styles["Normal"],
|
||||
))
|
||||
story.append(Spacer(1, 12))
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# CSV 匯出
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def export_csv(
|
||||
self,
|
||||
data: ReportData,
|
||||
output_path: "str | Path",
|
||||
) -> Path:
|
||||
"""
|
||||
將 Benchmark 結果與歷史記錄匯出為 CSV。
|
||||
CSV 包含兩個邏輯區塊(以空行分隔):
|
||||
1. Benchmark 摘要(循序 vs 平行對比)
|
||||
2. 歷史記錄(每筆 BenchmarkResult 一行)
|
||||
回傳實際寫入的檔案路徑。
|
||||
|
||||
引發:
|
||||
ValueError: sequential_result 或 parallel_result 為 None 時。
|
||||
"""
|
||||
if data.sequential_result is None or data.parallel_result is None:
|
||||
raise ValueError(
|
||||
"export_csv() 需要 sequential_result 與 parallel_result,但其中一個為 None。"
|
||||
)
|
||||
|
||||
output_path = Path(output_path)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
seq = data.sequential_result
|
||||
par = data.parallel_result
|
||||
|
||||
def diff_pct(a, b):
|
||||
if a and a != 0:
|
||||
return f"{(b - a) / a * 100:+.1f}%"
|
||||
return "—"
|
||||
|
||||
with output_path.open("w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.writer(f)
|
||||
|
||||
# 區塊 1:Benchmark 摘要
|
||||
writer.writerow(["section", "metric", "sequential", "parallel", "diff_pct"])
|
||||
writer.writerow([
|
||||
"benchmark_summary", "fps",
|
||||
f"{seq.fps:.1f}", f"{par.fps:.1f}",
|
||||
diff_pct(seq.fps, par.fps),
|
||||
])
|
||||
writer.writerow([
|
||||
"benchmark_summary", "avg_latency_ms",
|
||||
f"{seq.avg_latency_ms:.1f}", f"{par.avg_latency_ms:.1f}",
|
||||
diff_pct(seq.avg_latency_ms, par.avg_latency_ms),
|
||||
])
|
||||
writer.writerow([
|
||||
"benchmark_summary", "p95_latency_ms",
|
||||
f"{seq.p95_latency_ms:.1f}", f"{par.p95_latency_ms:.1f}",
|
||||
diff_pct(seq.p95_latency_ms, par.p95_latency_ms),
|
||||
])
|
||||
writer.writerow([
|
||||
"benchmark_summary", "total_frames",
|
||||
str(seq.total_frames), str(par.total_frames),
|
||||
"—",
|
||||
])
|
||||
speedup_val = f"{data.speedup:.2f}x" if data.speedup is not None else "—"
|
||||
writer.writerow([
|
||||
"benchmark_summary", "speedup",
|
||||
"—", speedup_val,
|
||||
"—",
|
||||
])
|
||||
|
||||
# 空行分隔
|
||||
writer.writerow([])
|
||||
|
||||
# 區塊 2:歷史記錄
|
||||
writer.writerow(["id", "timestamp", "mode", "fps", "avg_latency_ms", "p95_latency_ms", "total_frames"])
|
||||
for r in data.history_records:
|
||||
writer.writerow([
|
||||
r.id or "",
|
||||
self._get_timestamp_str(r.timestamp),
|
||||
r.mode,
|
||||
f"{r.fps:.1f}",
|
||||
f"{r.avg_latency_ms:.1f}",
|
||||
f"{r.p95_latency_ms:.1f}",
|
||||
str(r.total_frames),
|
||||
])
|
||||
|
||||
return output_path
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 工廠方法
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _get_timestamp_str(ts: float) -> str:
|
||||
"""將 UNIX timestamp 格式化為 'YYYY-MM-DD HH:MM:SS'(本地時間)。"""
|
||||
import time as _time
|
||||
local = _time.localtime(ts)
|
||||
return _time.strftime("%Y-%m-%d %H:%M:%S", local)
|
||||
@ -277,30 +277,56 @@ def find_shortest_path_distance(start_node, target_node, visited=None, distance=
|
||||
|
||||
|
||||
def find_preprocess_nodes_for_model(model_node, all_nodes):
|
||||
"""Find preprocessing nodes that connect to the given model node."""
|
||||
preprocess_nodes = []
|
||||
|
||||
# Get all nodes that connect to the model's inputs
|
||||
for input_port in model_node.inputs():
|
||||
for connected_output in input_port.connected_outputs():
|
||||
connected_node = connected_output.node()
|
||||
if isinstance(connected_node, PreprocessNode):
|
||||
preprocess_nodes.append(connected_node)
|
||||
"""Find preprocessing nodes that connect to the given model node.
|
||||
|
||||
This guards against mixed data types (e.g., string IDs from .mflow) by
|
||||
verifying attributes before traversing connections.
|
||||
"""
|
||||
preprocess_nodes: List[PreprocessNode] = []
|
||||
try:
|
||||
if hasattr(model_node, 'inputs'):
|
||||
for input_port in model_node.inputs() or []:
|
||||
try:
|
||||
if hasattr(input_port, 'connected_outputs'):
|
||||
for connected_output in input_port.connected_outputs() or []:
|
||||
try:
|
||||
if hasattr(connected_output, 'node'):
|
||||
connected_node = connected_output.node()
|
||||
if isinstance(connected_node, PreprocessNode):
|
||||
preprocess_nodes.append(connected_node)
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
# Swallow traversal errors and return what we found so far
|
||||
pass
|
||||
return preprocess_nodes
|
||||
|
||||
|
||||
def find_postprocess_nodes_for_model(model_node, all_nodes):
|
||||
"""Find postprocessing nodes that the given model node connects to."""
|
||||
postprocess_nodes = []
|
||||
|
||||
# Get all nodes that the model connects to
|
||||
for output in model_node.outputs():
|
||||
for connected_input in output.connected_inputs():
|
||||
connected_node = connected_input.node()
|
||||
if isinstance(connected_node, PostprocessNode):
|
||||
postprocess_nodes.append(connected_node)
|
||||
"""Find postprocessing nodes that the given model node connects to.
|
||||
|
||||
Defensive against cases where ports are not NodeGraphQt objects.
|
||||
"""
|
||||
postprocess_nodes: List[PostprocessNode] = []
|
||||
try:
|
||||
if hasattr(model_node, 'outputs'):
|
||||
for output in model_node.outputs() or []:
|
||||
try:
|
||||
if hasattr(output, 'connected_inputs'):
|
||||
for connected_input in output.connected_inputs() or []:
|
||||
try:
|
||||
if hasattr(connected_input, 'node'):
|
||||
connected_node = connected_input.node()
|
||||
if isinstance(connected_node, PostprocessNode):
|
||||
postprocess_nodes.append(connected_node)
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
return postprocess_nodes
|
||||
|
||||
|
||||
|
||||
1
core/templates/__init__.py
Normal file
1
core/templates/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""core/templates — Pipeline 設定範本模組。"""
|
||||
182
core/templates/manager.py
Normal file
182
core/templates/manager.py
Normal file
@ -0,0 +1,182 @@
|
||||
"""
|
||||
core/templates/manager.py
|
||||
|
||||
TemplateManager — 提供常見使用情境的預設 Pipeline 範本。
|
||||
|
||||
設計重點:
|
||||
- 三個內建範本(yolov5_detection、fire_detection、dual_model_cascade)以常數定義。
|
||||
- save_as_template 將自訂範本儲存於記憶體(in-memory),不持久化到磁碟。
|
||||
- load_template 先查內建範本,再查自訂範本;找不到時拋出 ValueError。
|
||||
- nodes/connections 格式與 .mflow JSON 相同(id、type 為必要欄位)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
@dataclass
|
||||
class PipelineTemplate:
|
||||
"""單一 Pipeline 範本。
|
||||
|
||||
屬性:
|
||||
template_id: 唯一識別碼(內建範本使用語意名稱;自訂範本以 custom_ 開頭)。
|
||||
name: 顯示名稱(如 "YOLOv5 物件偵測")。
|
||||
description: 範本說明。
|
||||
nodes: 節點定義列表,格式與 .mflow 相同,每個節點至少含 id 和 type。
|
||||
connections: 連線定義列表,每條連線含 from 和 to。
|
||||
"""
|
||||
template_id: str
|
||||
name: str
|
||||
description: str
|
||||
nodes: List[Dict[str, Any]]
|
||||
connections: List[Dict[str, Any]]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 內建範本定義
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
_BUILTIN_TEMPLATES: List[PipelineTemplate] = [
|
||||
PipelineTemplate(
|
||||
template_id="yolov5_detection",
|
||||
name="YOLOv5 物件偵測",
|
||||
description="標準 YOLOv5 物件偵測流程:輸入影像經前處理後送入模型,後處理輸出邊界框結果。",
|
||||
nodes=[
|
||||
{"id": "input_0", "type": "Input", "label": "Input"},
|
||||
{"id": "preprocess_0", "type": "Preprocess", "label": "Preprocess"},
|
||||
{"id": "model_0", "type": "Model", "label": "Model"},
|
||||
{"id": "postprocess_0","type": "Postprocess", "label": "Postprocess"},
|
||||
{"id": "output_0", "type": "Output", "label": "Output"},
|
||||
],
|
||||
connections=[
|
||||
{"from": "input_0", "to": "preprocess_0"},
|
||||
{"from": "preprocess_0", "to": "model_0"},
|
||||
{"from": "model_0", "to": "postprocess_0"},
|
||||
{"from": "postprocess_0", "to": "output_0"},
|
||||
],
|
||||
),
|
||||
PipelineTemplate(
|
||||
template_id="fire_detection",
|
||||
name="火焰偵測分類",
|
||||
description="火焰偵測流程:影像直接送入模型推論,後處理輸出火焰偵測結果(無前處理節點)。",
|
||||
nodes=[
|
||||
{"id": "input_0", "type": "Input", "label": "Input"},
|
||||
{"id": "model_0", "type": "Model", "label": "Model"},
|
||||
{"id": "postprocess_0","type": "Postprocess", "label": "Postprocess"},
|
||||
{"id": "output_0", "type": "Output", "label": "Output"},
|
||||
],
|
||||
connections=[
|
||||
{"from": "input_0", "to": "model_0"},
|
||||
{"from": "model_0", "to": "postprocess_0"},
|
||||
{"from": "postprocess_0", "to": "output_0"},
|
||||
],
|
||||
),
|
||||
PipelineTemplate(
|
||||
template_id="dual_model_cascade",
|
||||
name="雙模型串接",
|
||||
description=(
|
||||
"兩個模型串接的複合推論流程:第一個模型的輸出結果經後處理後,"
|
||||
"作為第二個模型的輸入,適合先偵測後分類的使用情境。"
|
||||
),
|
||||
nodes=[
|
||||
{"id": "input_0", "type": "Input", "label": "Input"},
|
||||
{"id": "model_0", "type": "Model", "label": "Model 1"},
|
||||
{"id": "postprocess_0", "type": "Postprocess", "label": "Postprocess 1"},
|
||||
{"id": "model_1", "type": "Model", "label": "Model 2"},
|
||||
{"id": "postprocess_1", "type": "Postprocess", "label": "Postprocess 2"},
|
||||
{"id": "output_0", "type": "Output", "label": "Output"},
|
||||
],
|
||||
connections=[
|
||||
{"from": "input_0", "to": "model_0"},
|
||||
{"from": "model_0", "to": "postprocess_0"},
|
||||
{"from": "postprocess_0", "to": "model_1"},
|
||||
{"from": "model_1", "to": "postprocess_1"},
|
||||
{"from": "postprocess_1", "to": "output_0"},
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
# 以 template_id 建立快速查找字典
|
||||
_BUILTIN_BY_ID: Dict[str, PipelineTemplate] = {
|
||||
t.template_id: t for t in _BUILTIN_TEMPLATES
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TemplateManager
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TemplateManager:
|
||||
"""管理內建與自訂 Pipeline 範本。
|
||||
|
||||
自訂範本儲存於記憶體,每個 TemplateManager 實例各自獨立。
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
# 自訂範本字典:{template_id: PipelineTemplate}
|
||||
self._custom: Dict[str, PipelineTemplate] = {}
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 公開介面
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def get_builtin_templates(self) -> List[PipelineTemplate]:
|
||||
"""回傳所有內建範本的清單(共 3 個)。
|
||||
|
||||
回傳:
|
||||
PipelineTemplate 列表(不含自訂範本)。
|
||||
"""
|
||||
return list(_BUILTIN_TEMPLATES)
|
||||
|
||||
def load_template(self, template_id: str) -> PipelineTemplate:
|
||||
"""依 template_id 載入範本。
|
||||
|
||||
查找順序:內建範本 → 自訂範本。
|
||||
|
||||
參數:
|
||||
template_id: 範本唯一識別碼。
|
||||
|
||||
回傳:
|
||||
對應的 PipelineTemplate。
|
||||
|
||||
引發:
|
||||
ValueError: 當 template_id 不存在於任何範本時。
|
||||
"""
|
||||
if template_id in _BUILTIN_BY_ID:
|
||||
return _BUILTIN_BY_ID[template_id]
|
||||
|
||||
if template_id in self._custom:
|
||||
return self._custom[template_id]
|
||||
|
||||
raise ValueError(f"Template {template_id} not found")
|
||||
|
||||
def save_as_template(
|
||||
self,
|
||||
pipeline_config: Dict[str, Any],
|
||||
name: str,
|
||||
description: str,
|
||||
) -> PipelineTemplate:
|
||||
"""將 Pipeline 設定儲存為新的自訂範本。
|
||||
|
||||
參數:
|
||||
pipeline_config: 包含 nodes 和 connections 列表的字典。
|
||||
name: 範本顯示名稱。
|
||||
description: 範本說明。
|
||||
|
||||
回傳:
|
||||
新建立的 PipelineTemplate(template_id 以 custom_ 開頭)。
|
||||
"""
|
||||
safe_name = name.lower().replace(" ", "_")
|
||||
template_id = f"custom_{safe_name}_{int(time.time() * 1000)}"
|
||||
|
||||
template = PipelineTemplate(
|
||||
template_id=template_id,
|
||||
name=name,
|
||||
description=description,
|
||||
nodes=list(pipeline_config.get("nodes", [])),
|
||||
connections=list(pipeline_config.get("connections", [])),
|
||||
)
|
||||
self._custom[template_id] = template
|
||||
return template
|
||||
58
debug_deployment.py
Normal file
58
debug_deployment.py
Normal file
@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug deployment error
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def simulate_deployment():
|
||||
"""Simulate the deployment process to find the Optional error"""
|
||||
try:
|
||||
print("Testing export_pipeline_data equivalent...")
|
||||
|
||||
# Simulate creating a node and getting properties
|
||||
from core.nodes.exact_nodes import ExactModelNode
|
||||
|
||||
# This would be similar to what dashboard does
|
||||
node = ExactModelNode()
|
||||
print("Node created")
|
||||
|
||||
# Check if node has get_business_properties
|
||||
if hasattr(node, 'get_business_properties'):
|
||||
print("Node has get_business_properties")
|
||||
try:
|
||||
props = node.get_business_properties()
|
||||
print(f"Properties extracted: {type(props)}")
|
||||
except Exception as e:
|
||||
print(f"Error in get_business_properties: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
# Test the mflow converter directly
|
||||
print("\nTesting MFlowConverter...")
|
||||
from core.functions.mflow_converter import MFlowConverter
|
||||
converter = MFlowConverter(default_fw_path='.')
|
||||
print("MFlowConverter created successfully")
|
||||
|
||||
# Test multi-series config building
|
||||
test_props = {
|
||||
'multi_series_mode': True,
|
||||
'enabled_series': ['520', '720'],
|
||||
'kl520_port_ids': '28,32',
|
||||
'kl720_port_ids': '4'
|
||||
}
|
||||
|
||||
config = converter._build_multi_series_config_from_properties(test_props)
|
||||
print(f"Multi-series config: {config}")
|
||||
|
||||
print("All tests passed!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
simulate_deployment()
|
||||
90
debug_multi_series_flow.py
Normal file
90
debug_multi_series_flow.py
Normal file
@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug the multi-series configuration flow
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def test_full_flow():
|
||||
"""Test the complete multi-series configuration flow"""
|
||||
print("=== Testing Multi-Series Configuration Flow ===")
|
||||
|
||||
# Simulate node properties as they would appear in the UI
|
||||
mock_node_properties = {
|
||||
'multi_series_mode': True,
|
||||
'enabled_series': ['520', '720'],
|
||||
'kl520_port_ids': '28,32',
|
||||
'kl720_port_ids': '4',
|
||||
'assets_folder': '',
|
||||
'max_queue_size': 100
|
||||
}
|
||||
|
||||
print(f"1. Mock node properties: {mock_node_properties}")
|
||||
|
||||
# Test the mflow converter building multi-series config
|
||||
try:
|
||||
from core.functions.mflow_converter import MFlowConverter
|
||||
converter = MFlowConverter(default_fw_path='.')
|
||||
|
||||
config = converter._build_multi_series_config_from_properties(mock_node_properties)
|
||||
print(f"2. Multi-series config built: {config}")
|
||||
|
||||
if config:
|
||||
print(" [OK] Multi-series config successfully built")
|
||||
|
||||
# Test StageConfig creation
|
||||
from core.functions.InferencePipeline import StageConfig
|
||||
|
||||
stage_config = StageConfig(
|
||||
stage_id="test_stage",
|
||||
port_ids=[], # Not used in multi-series
|
||||
scpu_fw_path='',
|
||||
ncpu_fw_path='',
|
||||
model_path='',
|
||||
upload_fw=False,
|
||||
multi_series_mode=True,
|
||||
multi_series_config=config
|
||||
)
|
||||
|
||||
print(f"3. StageConfig created with multi_series_mode: {stage_config.multi_series_mode}")
|
||||
print(f" Multi-series config: {stage_config.multi_series_config}")
|
||||
|
||||
# Test what would happen in PipelineStage initialization
|
||||
print("4. Testing PipelineStage initialization logic:")
|
||||
if stage_config.multi_series_mode and stage_config.multi_series_config:
|
||||
print(" [OK] Would initialize MultiDongle with multi_series_config")
|
||||
print(f" MultiDongle(multi_series_config={stage_config.multi_series_config})")
|
||||
else:
|
||||
print(" [ERROR] Would fall back to single-series mode")
|
||||
|
||||
else:
|
||||
print(" [ERROR] Multi-series config is None - this is the problem!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in flow test: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
def test_node_direct():
|
||||
"""Test creating a node directly and getting its inference config"""
|
||||
print("\n=== Testing Node Direct Configuration ===")
|
||||
|
||||
try:
|
||||
from core.nodes.exact_nodes import ExactModelNode
|
||||
|
||||
# This won't work without NodeGraphQt, but let's see what happens
|
||||
node = ExactModelNode()
|
||||
print("Node created (mock mode)")
|
||||
|
||||
# Test the get_business_properties method that would be called during export
|
||||
props = node.get_business_properties()
|
||||
print(f"Business properties: {props}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in node test: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_full_flow()
|
||||
test_node_direct()
|
||||
141
example_postprocess_options.py
Normal file
141
example_postprocess_options.py
Normal file
@ -0,0 +1,141 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Example demonstrating the new default postprocess options in the app.
|
||||
|
||||
This script shows how to use the different postprocessing types:
|
||||
- Fire detection (classification)
|
||||
- YOLO v3/v5 (object detection with bounding boxes)
|
||||
- General classification
|
||||
- Raw output
|
||||
|
||||
The postprocessing options are built-in to the app and provide text output
|
||||
and bounding box visualization in live view windows.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the project root to Python path
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from core.functions.Multidongle import (
|
||||
MultiDongle,
|
||||
PostProcessorOptions,
|
||||
PostProcessType,
|
||||
WebcamInferenceRunner
|
||||
)
|
||||
|
||||
|
||||
def demo_fire_detection():
|
||||
"""Demo fire detection postprocessing (default)"""
|
||||
print("=== Fire Detection Demo ===")
|
||||
|
||||
# Configure for fire detection
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.FIRE_DETECTION,
|
||||
threshold=0.5,
|
||||
class_names=["No Fire", "Fire"]
|
||||
)
|
||||
|
||||
print(f"Postprocess type: {options.postprocess_type.value}")
|
||||
print(f"Threshold: {options.threshold}")
|
||||
print(f"Class names: {options.class_names}")
|
||||
return options
|
||||
|
||||
|
||||
def demo_yolo_object_detection():
|
||||
"""Demo YOLO object detection with bounding boxes"""
|
||||
print("=== YOLO Object Detection Demo ===")
|
||||
|
||||
# Configure for YOLO v5 object detection
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.3,
|
||||
class_names=["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck"],
|
||||
nms_threshold=0.5,
|
||||
max_detections_per_class=50
|
||||
)
|
||||
|
||||
print(f"Postprocess type: {options.postprocess_type.value}")
|
||||
print(f"Detection threshold: {options.threshold}")
|
||||
print(f"NMS threshold: {options.nms_threshold}")
|
||||
print(f"Class names: {options.class_names[:5]}...") # Show first 5
|
||||
return options
|
||||
|
||||
|
||||
def demo_general_classification():
|
||||
"""Demo general classification"""
|
||||
print("=== General Classification Demo ===")
|
||||
|
||||
# Configure for general classification
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.CLASSIFICATION,
|
||||
threshold=0.6,
|
||||
class_names=["cat", "dog", "bird", "fish", "horse"]
|
||||
)
|
||||
|
||||
print(f"Postprocess type: {options.postprocess_type.value}")
|
||||
print(f"Threshold: {options.threshold}")
|
||||
print(f"Class names: {options.class_names}")
|
||||
return options
|
||||
|
||||
|
||||
def main():
|
||||
"""Main demo function"""
|
||||
print("Default Postprocess Options Demo")
|
||||
print("=" * 40)
|
||||
|
||||
# Demo different postprocessing options
|
||||
fire_options = demo_fire_detection()
|
||||
print()
|
||||
|
||||
yolo_options = demo_yolo_object_detection()
|
||||
print()
|
||||
|
||||
classification_options = demo_general_classification()
|
||||
print()
|
||||
|
||||
# Example of how to initialize MultiDongle with options
|
||||
print("=== MultiDongle Integration Example ===")
|
||||
|
||||
# NOTE: Update these paths according to your setup
|
||||
PORT_IDS = [28, 32] # Update with your device port IDs
|
||||
SCPU_FW = 'fw_scpu.bin' # Update with your firmware path
|
||||
NCPU_FW = 'fw_ncpu.bin' # Update with your firmware path
|
||||
MODEL_PATH = 'your_model.nef' # Update with your model path
|
||||
|
||||
try:
|
||||
# Example 1: Fire detection (default)
|
||||
print("Initializing with fire detection...")
|
||||
multidongle_fire = MultiDongle(
|
||||
port_id=PORT_IDS,
|
||||
scpu_fw_path=SCPU_FW,
|
||||
ncpu_fw_path=NCPU_FW,
|
||||
model_path=MODEL_PATH,
|
||||
upload_fw=False, # Set to True if you need firmware upload
|
||||
postprocess_options=fire_options
|
||||
)
|
||||
print(f"✓ Fire detection configured: {multidongle_fire.postprocess_options.postprocess_type.value}")
|
||||
|
||||
# Example 2: Change postprocessing options dynamically
|
||||
print("Changing to YOLO detection...")
|
||||
multidongle_fire.set_postprocess_options(yolo_options)
|
||||
print(f"✓ YOLO detection configured: {multidongle_fire.postprocess_options.postprocess_type.value}")
|
||||
|
||||
# Example 3: Get available types
|
||||
available_types = multidongle_fire.get_available_postprocess_types()
|
||||
print(f"Available postprocess types: {[t.value for t in available_types]}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Note: MultiDongle initialization skipped (no hardware): {e}")
|
||||
|
||||
print("\n=== Usage Notes ===")
|
||||
print("1. Fire detection option is set as default")
|
||||
print("2. Text output shows classification results with probabilities")
|
||||
print("3. Bounding box output visualizes detected objects in live view")
|
||||
print("4. All postprocessing is built-in to the app (no external dependencies)")
|
||||
print("5. Exact nodes can configure postprocessing through UI properties")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
59
example_utils/ExampleEnum.py
Normal file
59
example_utils/ExampleEnum.py
Normal file
@ -0,0 +1,59 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2021-2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class ImageType(Enum):
|
||||
GENERAL = 'general'
|
||||
BINARY = 'binary'
|
||||
|
||||
|
||||
class ImageFormat(Enum):
|
||||
RGB565 = 'RGB565'
|
||||
RGBA8888 = 'RGBA8888'
|
||||
YUYV = 'YUYV'
|
||||
CRY1CBY0 = 'CrY1CbY0'
|
||||
CBY1CRY0 = 'CbY1CrY0'
|
||||
Y1CRY0CB = 'Y1CrY0Cb'
|
||||
Y1CBY0CR = 'Y1CbY0Cr'
|
||||
CRY0CBY1 = 'CrY0CbY1'
|
||||
CBY0CRY1 = 'CbY0CrY1'
|
||||
Y0CRY1CB = 'Y0CrY1Cb'
|
||||
Y0CBY1CR = 'Y0CbY1Cr'
|
||||
RAW8 = 'RAW8'
|
||||
YUV420p = 'YUV420p'
|
||||
|
||||
|
||||
class ResizeMode(Enum):
|
||||
NONE = 'none'
|
||||
ENABLE = 'auto'
|
||||
|
||||
|
||||
class PaddingMode(Enum):
|
||||
NONE = 'none'
|
||||
PADDING_CORNER = 'corner'
|
||||
PADDING_SYMMETRIC = 'symmetric'
|
||||
|
||||
|
||||
class PostprocessMode(Enum):
|
||||
NONE = 'none'
|
||||
YOLO_V3 = 'yolo_v3'
|
||||
YOLO_V5 = 'yolo_v5'
|
||||
|
||||
|
||||
class NormalizeMode(Enum):
|
||||
NONE = 'none'
|
||||
KNERON = 'kneron'
|
||||
TENSORFLOW = 'tensorflow'
|
||||
YOLO = 'yolo'
|
||||
CUSTOMIZED_DEFAULT = 'customized_default'
|
||||
CUSTOMIZED_SUB128 = 'customized_sub128'
|
||||
CUSTOMIZED_DIV2 = 'customized_div2'
|
||||
CUSTOMIZED_SUB128_DIV2 = 'customized_sub128_div2'
|
||||
|
||||
|
||||
class InferenceRetrieveNodeMode(Enum):
|
||||
FIXED = 'fixed'
|
||||
FLOAT = 'float'
|
||||
578
example_utils/ExampleHelper.py
Normal file
578
example_utils/ExampleHelper.py
Normal file
@ -0,0 +1,578 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
from typing import List, Union
|
||||
from utils.ExampleEnum import *
|
||||
|
||||
import numpy as np
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
import cv2
|
||||
|
||||
PWD = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(1, os.path.join(PWD, '../..'))
|
||||
|
||||
import kp
|
||||
|
||||
TARGET_FW_VERSION = 'KDP2'
|
||||
|
||||
|
||||
def get_device_usb_speed_by_port_id(usb_port_id: int) -> kp.UsbSpeed:
|
||||
device_list = kp.core.scan_devices()
|
||||
|
||||
for device_descriptor in device_list.device_descriptor_list:
|
||||
if 0 == usb_port_id:
|
||||
return device_descriptor.link_speed
|
||||
elif usb_port_id == device_descriptor.usb_port_id:
|
||||
return device_descriptor.link_speed
|
||||
|
||||
raise IOError('Specified USB port ID {} not exist.'.format(usb_port_id))
|
||||
|
||||
|
||||
def get_connect_device_descriptor(target_device: str,
|
||||
scan_index_list: Union[List[int], None],
|
||||
usb_port_id_list: Union[List[int], None]):
|
||||
print('[Check Device]')
|
||||
|
||||
# scan devices
|
||||
_device_list = kp.core.scan_devices()
|
||||
|
||||
# check Kneron device exist
|
||||
if _device_list.device_descriptor_number == 0:
|
||||
print('Error: no Kneron device !')
|
||||
exit(0)
|
||||
|
||||
_index_device_descriptor_list = []
|
||||
|
||||
# get device_descriptor of specified scan index
|
||||
if scan_index_list is not None:
|
||||
for _scan_index in scan_index_list:
|
||||
if _device_list.device_descriptor_number > _scan_index >= 0:
|
||||
_index_device_descriptor_list.append([_scan_index, _device_list.device_descriptor_list[_scan_index]])
|
||||
else:
|
||||
print('Error: no matched Kneron device of specified scan index !')
|
||||
exit(0)
|
||||
# get device_descriptor of specified port ID
|
||||
elif usb_port_id_list is not None:
|
||||
for _scan_index, __device_descriptor in enumerate(_device_list.device_descriptor_list):
|
||||
for _usb_port_id in usb_port_id_list:
|
||||
if __device_descriptor.usb_port_id == _usb_port_id:
|
||||
_index_device_descriptor_list.append([_scan_index, __device_descriptor])
|
||||
|
||||
if 0 == len(_index_device_descriptor_list):
|
||||
print('Error: no matched Kneron device of specified port ID !')
|
||||
exit(0)
|
||||
# get device_descriptor of by default
|
||||
else:
|
||||
_index_device_descriptor_list = [[_scan_index, __device_descriptor] for _scan_index, __device_descriptor in
|
||||
enumerate(_device_list.device_descriptor_list)]
|
||||
|
||||
# check device_descriptor is specified target device
|
||||
if target_device.lower() == 'kl520':
|
||||
_target_device_product_id = kp.ProductId.KP_DEVICE_KL520
|
||||
elif target_device.lower() == 'kl720':
|
||||
_target_device_product_id = kp.ProductId.KP_DEVICE_KL720
|
||||
elif target_device.lower() == 'kl630':
|
||||
_target_device_product_id = kp.ProductId.KP_DEVICE_KL630
|
||||
elif target_device.lower() == 'kl730':
|
||||
_target_device_product_id = kp.ProductId.KP_DEVICE_KL730
|
||||
elif target_device.lower() == 'kl830':
|
||||
_target_device_product_id = kp.ProductId.KP_DEVICE_KL830
|
||||
|
||||
for _scan_index, __device_descriptor in _index_device_descriptor_list:
|
||||
if kp.ProductId(__device_descriptor.product_id) != _target_device_product_id:
|
||||
print('Error: Not matched Kneron device of specified target device !')
|
||||
exit(0)
|
||||
|
||||
for _scan_index, __device_descriptor in _index_device_descriptor_list:
|
||||
if TARGET_FW_VERSION not in __device_descriptor.firmware:
|
||||
print('Error: device is not running KDP2/KDP2 Loader firmware ...')
|
||||
print('please upload firmware first via \'kp.core.load_firmware_from_file()\'')
|
||||
exit(0)
|
||||
|
||||
print(' - Success')
|
||||
|
||||
return _index_device_descriptor_list
|
||||
|
||||
|
||||
def read_image(img_path: str, img_type: str, img_format: str):
|
||||
print('[Read Image]')
|
||||
if img_type == ImageType.GENERAL.value:
|
||||
_img = cv2.imread(filename=img_path)
|
||||
|
||||
if len(_img.shape) < 3:
|
||||
channel_num = 2
|
||||
else:
|
||||
channel_num = _img.shape[2]
|
||||
|
||||
if channel_num == 1:
|
||||
if img_format == ImageFormat.RGB565.value:
|
||||
color_cvt_code = cv2.COLOR_GRAY2BGR565
|
||||
elif img_format == ImageFormat.RGBA8888.value:
|
||||
color_cvt_code = cv2.COLOR_GRAY2BGRA
|
||||
elif img_format == ImageFormat.RAW8.value:
|
||||
color_cvt_code = None
|
||||
else:
|
||||
print('Error: No matched image format !')
|
||||
exit(0)
|
||||
elif channel_num == 3:
|
||||
if img_format == ImageFormat.RGB565.value:
|
||||
color_cvt_code = cv2.COLOR_BGR2BGR565
|
||||
elif img_format == ImageFormat.RGBA8888.value:
|
||||
color_cvt_code = cv2.COLOR_BGR2BGRA
|
||||
elif img_format == ImageFormat.RAW8.value:
|
||||
color_cvt_code = cv2.COLOR_BGR2GRAY
|
||||
else:
|
||||
print('Error: No matched image format !')
|
||||
exit(0)
|
||||
else:
|
||||
print('Error: Not support image format !')
|
||||
exit(0)
|
||||
|
||||
if color_cvt_code is not None:
|
||||
_img = cv2.cvtColor(src=_img, code=color_cvt_code)
|
||||
|
||||
elif img_type == ImageType.BINARY.value:
|
||||
with open(file=img_path, mode='rb') as file:
|
||||
_img = file.read()
|
||||
else:
|
||||
print('Error: Not support image type !')
|
||||
exit(0)
|
||||
|
||||
print(' - Success')
|
||||
return _img
|
||||
|
||||
|
||||
def get_kp_image_format(image_format: str) -> kp.ImageFormat:
|
||||
if image_format == ImageFormat.RGB565.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_RGB565
|
||||
elif image_format == ImageFormat.RGBA8888.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_RGBA8888
|
||||
elif image_format == ImageFormat.YUYV.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YUYV
|
||||
elif image_format == ImageFormat.CRY1CBY0.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_CRY1CBY0
|
||||
elif image_format == ImageFormat.CBY1CRY0.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_CBY1CRY0
|
||||
elif image_format == ImageFormat.Y1CRY0CB.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_Y1CRY0CB
|
||||
elif image_format == ImageFormat.Y1CBY0CR.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_Y1CBY0CR
|
||||
elif image_format == ImageFormat.CRY0CBY1.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_CRY0CBY1
|
||||
elif image_format == ImageFormat.CBY0CRY1.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_CBY0CRY1
|
||||
elif image_format == ImageFormat.Y0CRY1CB.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_Y0CRY1CB
|
||||
elif image_format == ImageFormat.Y0CBY1CR.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YCBCR422_Y0CBY1CR
|
||||
elif image_format == ImageFormat.RAW8.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_RAW8
|
||||
elif image_format == ImageFormat.YUV420p.value:
|
||||
_kp_image_format = kp.ImageFormat.KP_IMAGE_FORMAT_YUV420
|
||||
else:
|
||||
print('Error: Not support image format !')
|
||||
exit(0)
|
||||
|
||||
return _kp_image_format
|
||||
|
||||
|
||||
def get_kp_normalize_mode(norm_mode: str) -> kp.NormalizeMode:
|
||||
if norm_mode == NormalizeMode.NONE.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_DISABLE
|
||||
elif norm_mode == NormalizeMode.KNERON.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_KNERON
|
||||
elif norm_mode == NormalizeMode.YOLO.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_YOLO
|
||||
elif norm_mode == NormalizeMode.TENSORFLOW.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_TENSOR_FLOW
|
||||
elif norm_mode == NormalizeMode.CUSTOMIZED_DEFAULT.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_CUSTOMIZED_DEFAULT
|
||||
elif norm_mode == NormalizeMode.CUSTOMIZED_SUB128.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_CUSTOMIZED_SUB128
|
||||
elif norm_mode == NormalizeMode.CUSTOMIZED_DIV2.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_CUSTOMIZED_DIV2
|
||||
elif norm_mode == NormalizeMode.CUSTOMIZED_SUB128_DIV2.value:
|
||||
_kp_norm = kp.NormalizeMode.KP_NORMALIZE_CUSTOMIZED_SUB128_DIV2
|
||||
else:
|
||||
print('Error: Not support normalize mode !')
|
||||
exit(0)
|
||||
|
||||
return _kp_norm
|
||||
|
||||
|
||||
def get_kp_pre_process_resize_mode(resize_mode: str) -> kp.ResizeMode:
|
||||
if resize_mode == ResizeMode.NONE.value:
|
||||
_kp_resize_mode = kp.ResizeMode.KP_RESIZE_DISABLE
|
||||
elif resize_mode == ResizeMode.ENABLE.value:
|
||||
_kp_resize_mode = kp.ResizeMode.KP_RESIZE_ENABLE
|
||||
else:
|
||||
print('Error: Not support pre process resize mode !')
|
||||
exit(0)
|
||||
|
||||
return _kp_resize_mode
|
||||
|
||||
|
||||
def get_kp_pre_process_padding_mode(padding_mode: str) -> kp.PaddingMode:
|
||||
if padding_mode == PaddingMode.NONE.value:
|
||||
_kp_padding_mode = kp.PaddingMode.KP_PADDING_DISABLE
|
||||
elif padding_mode == PaddingMode.PADDING_CORNER.value:
|
||||
_kp_padding_mode = kp.PaddingMode.KP_PADDING_CORNER
|
||||
elif padding_mode == PaddingMode.PADDING_SYMMETRIC.value:
|
||||
_kp_padding_mode = kp.PaddingMode.KP_PADDING_SYMMETRIC
|
||||
else:
|
||||
print('Error: Not support pre process padding mode !')
|
||||
exit(0)
|
||||
|
||||
return _kp_padding_mode
|
||||
|
||||
|
||||
def get_ex_post_process_mode(post_proc: str) -> PostprocessMode:
|
||||
if post_proc in PostprocessMode._value2member_map_:
|
||||
_ex_post_proc = PostprocessMode(post_proc)
|
||||
else:
|
||||
print('Error: Not support post process mode !')
|
||||
exit(0)
|
||||
|
||||
return _ex_post_proc
|
||||
|
||||
|
||||
def parse_crop_box_from_str(crop_box_str: str) -> List[kp.InferenceCropBox]:
|
||||
_group_list = re.compile(r'\([\s]*(\d+)[\s]*,[\s]*(\d+)[\s]*,[\s]*(\d+)[\s]*,[\s]*(\d+)[\s]*\)').findall(
|
||||
crop_box_str)
|
||||
_crop_box_list = []
|
||||
|
||||
for _idx, _crop_box in enumerate(_group_list):
|
||||
_crop_box_list.append(
|
||||
kp.InferenceCropBox(
|
||||
crop_box_index=_idx,
|
||||
x=int(_crop_box[0]),
|
||||
y=int(_crop_box[1]),
|
||||
width=int(_crop_box[2]),
|
||||
height=int(_crop_box[3])
|
||||
)
|
||||
)
|
||||
|
||||
return _crop_box_list
|
||||
|
||||
|
||||
def convert_onnx_data_to_npu_data(tensor_descriptor: kp.TensorDescriptor, onnx_data: np.ndarray) -> bytes:
|
||||
def __get_npu_ndarray(__tensor_descriptor: kp.TensorDescriptor, __npu_ndarray_dtype: np.dtype):
|
||||
assert __tensor_descriptor.tensor_shape_info.version == kp.ModelTensorShapeInformationVersion.KP_MODEL_TENSOR_SHAPE_INFO_VERSION_2
|
||||
|
||||
if __tensor_descriptor.data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL]:
|
||||
""" calculate channel group stride in C language
|
||||
for (int axis = 0; axis < (int)tensor_shape_info->shape_len; axis++) {
|
||||
if (1 == tensor_shape_info->stride_npu[axis]) {
|
||||
channel_idx = axis;
|
||||
continue;
|
||||
}
|
||||
|
||||
npu_channel_group_stride_tmp = tensor_shape_info->stride_npu[axis] * tensor_shape_info->shape[axis];
|
||||
if (npu_channel_group_stride_tmp > npu_channel_group_stride)
|
||||
npu_channel_group_stride = npu_channel_group_stride_tmp;
|
||||
}
|
||||
"""
|
||||
__shape = np.array(__tensor_descriptor.tensor_shape_info.v2.shape, dtype=int)
|
||||
__stride_npu = np.array(__tensor_descriptor.tensor_shape_info.v2.stride_npu, dtype=int)
|
||||
__channel_idx = np.where(__stride_npu == 1)[0][0]
|
||||
__dimension_stride = __stride_npu * __shape
|
||||
__dimension_stride[__channel_idx] = 0
|
||||
__npu_channel_group_stride = np.max(__dimension_stride.flatten())
|
||||
|
||||
"""
|
||||
__shape = __tensor_descriptor.tensor_shape_info.v2.shape
|
||||
__max_element_num += ((__shape[__channel_idx] / 16) + (0 if (__shape[__channel_idx] % 16) == 0 else 1)) * __npu_channel_group_stride
|
||||
"""
|
||||
__max_element_num = ((__shape[__channel_idx] >> 4) + (0 if (__shape[__channel_idx] % 16) == 0 else 1)) * __npu_channel_group_stride
|
||||
else:
|
||||
__max_element_num = 0
|
||||
__dimension_num = len(__tensor_descriptor.tensor_shape_info.v2.shape)
|
||||
|
||||
for dimension in range(__dimension_num):
|
||||
__element_num = __tensor_descriptor.tensor_shape_info.v2.shape[dimension] * __tensor_descriptor.tensor_shape_info.v2.stride_npu[dimension]
|
||||
if __element_num > __max_element_num:
|
||||
__max_element_num = __element_num
|
||||
|
||||
return np.zeros(shape=__max_element_num, dtype=__npu_ndarray_dtype).flatten()
|
||||
|
||||
quantization_parameters = tensor_descriptor.quantization_parameters
|
||||
tensor_shape_info = tensor_descriptor.tensor_shape_info
|
||||
npu_data_layout = tensor_descriptor.data_layout
|
||||
|
||||
quantization_max_value = 0
|
||||
quantization_min_value = 0
|
||||
radix = 0
|
||||
scale = 0
|
||||
quantization_factor = 0
|
||||
|
||||
channel_idx = 0
|
||||
npu_channel_group_stride = -1
|
||||
|
||||
onnx_data_shape_index = None
|
||||
onnx_data_buf_offset = 0
|
||||
npu_data_buf_offset = 0
|
||||
|
||||
npu_data_element_u16b = 0
|
||||
npu_data_high_bit_offset = 16
|
||||
|
||||
npu_data_dtype = np.int8
|
||||
|
||||
if tensor_shape_info.version != kp.ModelTensorShapeInformationVersion.KP_MODEL_TENSOR_SHAPE_INFO_VERSION_2:
|
||||
raise AttributeError('Unsupport ModelTensorShapeInformationVersion {}'.format(tensor_descriptor.tensor_shape_info.version))
|
||||
|
||||
"""
|
||||
input data quantization
|
||||
"""
|
||||
if npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_4W4C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B_CH_COMPACT,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_16W1C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW4C8B_KEEP_A,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW4C8B_DROP_A,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C8B]:
|
||||
quantization_max_value = np.iinfo(np.int8).max
|
||||
quantization_min_value = np.iinfo(np.int8).min
|
||||
npu_data_dtype = np.int8
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_8W1C16B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_16B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_4W4C8BHL,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL_CH_COMPACT,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_16W1C8BHL,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C16B_LE,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C16B_BE]:
|
||||
quantization_max_value = np.iinfo(np.int16).max
|
||||
quantization_min_value = np.iinfo(np.int16).min
|
||||
npu_data_dtype = np.int16
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_FLOAT]:
|
||||
quantization_max_value = np.finfo(np.float32).max
|
||||
quantization_min_value = np.finfo(np.float32).min
|
||||
npu_data_dtype = np.float32
|
||||
else:
|
||||
raise AttributeError('Unsupport ModelTensorDataLayout {}'.format(npu_data_layout))
|
||||
|
||||
|
||||
shape = np.array(tensor_shape_info.v2.shape, dtype=np.int32)
|
||||
dimension_num = len(shape)
|
||||
quantized_axis = quantization_parameters.v1.quantized_axis
|
||||
radix = np.array([quantized_fixed_point_descriptor.radix for quantized_fixed_point_descriptor in quantization_parameters.v1.quantized_fixed_point_descriptor_list], dtype=np.int32)
|
||||
scale = np.array([quantized_fixed_point_descriptor.scale.value for quantized_fixed_point_descriptor in quantization_parameters.v1.quantized_fixed_point_descriptor_list], dtype=np.float32)
|
||||
|
||||
quantization_factor = np.power(2, radix) * scale
|
||||
if 1 < len(quantization_parameters.v1.quantized_fixed_point_descriptor_list):
|
||||
quantization_factor = np.expand_dims(quantization_factor, axis=tuple([dimension for dimension in range(dimension_num) if dimension is not quantized_axis]))
|
||||
quantization_factor = np.broadcast_to(array=quantization_factor, shape=shape)
|
||||
|
||||
onnx_quantized_data = (onnx_data * quantization_factor).astype(np.float32)
|
||||
onnx_quantized_data = np.round(onnx_quantized_data)
|
||||
onnx_quantized_data = np.clip(onnx_quantized_data, quantization_min_value, quantization_max_value).astype(npu_data_dtype)
|
||||
|
||||
"""
|
||||
flatten onnx/npu data
|
||||
"""
|
||||
onnx_quantized_data_flatten = onnx_quantized_data.flatten()
|
||||
npu_data_flatten = __get_npu_ndarray(__tensor_descriptor=tensor_descriptor, __npu_ndarray_dtype=npu_data_dtype)
|
||||
|
||||
'''
|
||||
re-arrange data from onnx to npu
|
||||
'''
|
||||
onnx_data_shape_index = np.zeros(shape=(len(shape)), dtype=int)
|
||||
stride_onnx = np.array(tensor_shape_info.v2.stride_onnx, dtype=int)
|
||||
stride_npu = np.array(tensor_shape_info.v2.stride_npu, dtype=int)
|
||||
|
||||
if npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_4W4C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B_CH_COMPACT,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_16W1C8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_8B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW4C8B_KEEP_A,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW4C8B_DROP_A,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C8B]:
|
||||
while True:
|
||||
onnx_data_buf_offset = onnx_data_shape_index.dot(stride_onnx)
|
||||
npu_data_buf_offset = onnx_data_shape_index.dot(stride_npu)
|
||||
|
||||
if npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8B]:
|
||||
if -1 == npu_channel_group_stride:
|
||||
""" calculate channel group stride in C language
|
||||
for (int axis = 0; axis < (int)tensor_shape_info->shape_len; axis++) {
|
||||
if (1 == tensor_shape_info->stride_npu[axis]) {
|
||||
channel_idx = axis;
|
||||
continue;
|
||||
}
|
||||
|
||||
npu_channel_group_stride_tmp = tensor_shape_info->stride_npu[axis] * tensor_shape_info->shape[axis];
|
||||
if (npu_channel_group_stride_tmp > npu_channel_group_stride)
|
||||
npu_channel_group_stride = npu_channel_group_stride_tmp;
|
||||
}
|
||||
|
||||
npu_channel_group_stride -= 16;
|
||||
"""
|
||||
channel_idx = np.where(stride_npu == 1)[0][0]
|
||||
dimension_stride = stride_npu * shape
|
||||
dimension_stride[channel_idx] = 0
|
||||
npu_channel_group_stride = np.max(dimension_stride.flatten()) - 16
|
||||
|
||||
"""
|
||||
npu_data_buf_offset += (onnx_data_shape_index[channel_idx] / 16) * npu_channel_group_stride
|
||||
"""
|
||||
npu_data_buf_offset += (onnx_data_shape_index[channel_idx] >> 4) * npu_channel_group_stride
|
||||
|
||||
npu_data_flatten[npu_data_buf_offset] = onnx_quantized_data_flatten[onnx_data_buf_offset]
|
||||
|
||||
'''
|
||||
update onnx_data_shape_index
|
||||
'''
|
||||
for dimension in range(dimension_num - 1, -1, -1):
|
||||
onnx_data_shape_index[dimension] += 1
|
||||
if onnx_data_shape_index[dimension] == shape[dimension]:
|
||||
if dimension == 0:
|
||||
break
|
||||
onnx_data_shape_index[dimension] = 0
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if onnx_data_shape_index[0] == shape[0]:
|
||||
break
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_8W1C16B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_16B,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C16B_LE]:
|
||||
while True:
|
||||
onnx_data_buf_offset = onnx_data_shape_index.dot(stride_onnx)
|
||||
npu_data_buf_offset = onnx_data_shape_index.dot(stride_npu)
|
||||
|
||||
npu_data_element_u16b = np.frombuffer(buffer=onnx_quantized_data_flatten[onnx_data_buf_offset].tobytes(), dtype=np.uint16)
|
||||
npu_data_flatten[npu_data_buf_offset] = np.frombuffer(buffer=(npu_data_element_u16b & 0xfffe).tobytes(), dtype=np.int16)
|
||||
|
||||
'''
|
||||
update onnx_data_shape_index
|
||||
'''
|
||||
for dimension in range(dimension_num - 1, -1, -1):
|
||||
onnx_data_shape_index[dimension] += 1
|
||||
if onnx_data_shape_index[dimension] == shape[dimension]:
|
||||
if dimension == 0:
|
||||
break
|
||||
onnx_data_shape_index[dimension] = 0
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if onnx_data_shape_index[0] == shape[0]:
|
||||
break
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_HW1C16B_BE]:
|
||||
while True:
|
||||
onnx_data_buf_offset = onnx_data_shape_index.dot(stride_onnx)
|
||||
npu_data_buf_offset = onnx_data_shape_index.dot(stride_npu)
|
||||
|
||||
npu_data_element_u16b = np.frombuffer(buffer=onnx_quantized_data_flatten[onnx_data_buf_offset].tobytes(), dtype=np.uint16)
|
||||
npu_data_element_u16b = np.frombuffer(buffer=(npu_data_element_u16b & 0xfffe).tobytes(), dtype=np.int16)
|
||||
npu_data_flatten[npu_data_buf_offset] = npu_data_element_u16b.byteswap()
|
||||
|
||||
'''
|
||||
update onnx_data_shape_index
|
||||
'''
|
||||
for dimension in range(dimension_num - 1, -1, -1):
|
||||
onnx_data_shape_index[dimension] += 1
|
||||
if onnx_data_shape_index[dimension] == shape[dimension]:
|
||||
if dimension == 0:
|
||||
break
|
||||
onnx_data_shape_index[dimension] = 0
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if onnx_data_shape_index[0] == shape[0]:
|
||||
break
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_4W4C8BHL,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL_CH_COMPACT,
|
||||
kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_16W1C8BHL]:
|
||||
|
||||
npu_data_flatten = np.frombuffer(buffer=npu_data_flatten.tobytes(), dtype=np.uint8).copy()
|
||||
|
||||
while True:
|
||||
onnx_data_buf_offset = onnx_data_shape_index.dot(stride_onnx)
|
||||
npu_data_buf_offset = onnx_data_shape_index.dot(stride_npu)
|
||||
|
||||
if npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_1W16C8BHL]:
|
||||
if -1 == npu_channel_group_stride:
|
||||
""" calculate channel group stride in C language
|
||||
for (int axis = 0; axis < (int)tensor_shape_info->shape_len; axis++) {
|
||||
if (1 == tensor_shape_info->stride_npu[axis]) {
|
||||
channel_idx = axis;
|
||||
continue;
|
||||
}
|
||||
|
||||
npu_channel_group_stride_tmp = tensor_shape_info->stride_npu[axis] * tensor_shape_info->shape[axis];
|
||||
if (npu_channel_group_stride_tmp > npu_channel_group_stride)
|
||||
npu_channel_group_stride = npu_channel_group_stride_tmp;
|
||||
}
|
||||
|
||||
npu_channel_group_stride -= 16;
|
||||
"""
|
||||
channel_idx = np.where(stride_npu == 1)[0][0]
|
||||
dimension_stride = stride_npu * shape
|
||||
dimension_stride[channel_idx] = 0
|
||||
npu_channel_group_stride = np.max(dimension_stride.flatten()) - 16
|
||||
|
||||
"""
|
||||
npu_data_buf_offset += (onnx_data_shape_index[channel_idx] / 16) * npu_channel_group_stride
|
||||
"""
|
||||
npu_data_buf_offset += (onnx_data_shape_index[channel_idx] >> 4) * npu_channel_group_stride
|
||||
|
||||
"""
|
||||
npu_data_buf_offset = (npu_data_buf_offset / 16) * 32 + (npu_data_buf_offset % 16)
|
||||
"""
|
||||
npu_data_buf_offset = ((npu_data_buf_offset >> 4) << 5) + (npu_data_buf_offset & 15)
|
||||
|
||||
npu_data_element_u16b = np.frombuffer(buffer=onnx_quantized_data_flatten[onnx_data_buf_offset].tobytes(), dtype=np.uint16)
|
||||
npu_data_element_u16b = (npu_data_element_u16b >> 1)
|
||||
npu_data_flatten[npu_data_buf_offset] = (npu_data_element_u16b & 0x007f).astype(dtype=np.uint8)
|
||||
npu_data_flatten[npu_data_buf_offset + npu_data_high_bit_offset] = ((npu_data_element_u16b >> 7) & 0x00ff).astype(dtype=np.uint8)
|
||||
|
||||
'''
|
||||
update onnx_data_shape_index
|
||||
'''
|
||||
for dimension in range(dimension_num - 1, -1, -1):
|
||||
onnx_data_shape_index[dimension] += 1
|
||||
if onnx_data_shape_index[dimension] == shape[dimension]:
|
||||
if dimension == 0:
|
||||
break
|
||||
onnx_data_shape_index[dimension] = 0
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if onnx_data_shape_index[0] == shape[0]:
|
||||
break
|
||||
elif npu_data_layout in [kp.ModelTensorDataLayout.KP_MODEL_TENSOR_DATA_LAYOUT_RAW_FLOAT]:
|
||||
while True:
|
||||
onnx_data_buf_offset = onnx_data_shape_index.dot(stride_onnx)
|
||||
npu_data_buf_offset = onnx_data_shape_index.dot(stride_npu)
|
||||
|
||||
npu_data_flatten[npu_data_buf_offset] = onnx_quantized_data_flatten[onnx_data_buf_offset]
|
||||
|
||||
'''
|
||||
update onnx_data_shape_index
|
||||
'''
|
||||
for dimension in range(dimension_num - 1, -1, -1):
|
||||
onnx_data_shape_index[dimension] += 1
|
||||
if onnx_data_shape_index[dimension] == shape[dimension]:
|
||||
if dimension == 0:
|
||||
break
|
||||
onnx_data_shape_index[dimension] = 0
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if onnx_data_shape_index[0] == shape[0]:
|
||||
break
|
||||
else:
|
||||
raise AttributeError('Unsupport ModelTensorDataLayout {}'.format(npu_data_layout))
|
||||
|
||||
return npu_data_flatten.tobytes()
|
||||
344
example_utils/ExamplePostProcess.py
Normal file
344
example_utils/ExamplePostProcess.py
Normal file
@ -0,0 +1,344 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
from typing import List
|
||||
|
||||
from utils.ExampleValue import ExampleBoundingBox, ExampleYoloResult
|
||||
|
||||
import os
|
||||
import sys
|
||||
import numpy as np
|
||||
|
||||
PWD = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(1, os.path.join(PWD, '../..'))
|
||||
|
||||
import kp
|
||||
|
||||
YOLO_V3_CELL_BOX_NUM = 3
|
||||
YOLO_V3_BOX_FIX_CH = 5
|
||||
NMS_THRESH_YOLOV3 = 0.45
|
||||
NMS_THRESH_YOLOV5 = 0.5
|
||||
MAX_POSSIBLE_BOXES = 2000
|
||||
MODEL_SHIRNK_RATIO_TYV3 = [32, 16]
|
||||
MODEL_SHIRNK_RATIO_V5 = [8, 16, 32]
|
||||
YOLO_MAX_DETECTION_PER_CLASS = 100
|
||||
|
||||
TINY_YOLO_V3_ANCHERS = np.array([
|
||||
[[81, 82], [135, 169], [344, 319]],
|
||||
[[23, 27], [37, 58], [81, 82]]
|
||||
])
|
||||
|
||||
YOLO_V5_ANCHERS = np.array([
|
||||
[[10, 13], [16, 30], [33, 23]],
|
||||
[[30, 61], [62, 45], [59, 119]],
|
||||
[[116, 90], [156, 198], [373, 326]]
|
||||
])
|
||||
|
||||
|
||||
def _sigmoid(x):
|
||||
return 1. / (1. + np.exp(-x))
|
||||
|
||||
|
||||
def _iou(box_src, boxes_dst):
|
||||
max_x1 = np.maximum(box_src[0], boxes_dst[:, 0])
|
||||
max_y1 = np.maximum(box_src[1], boxes_dst[:, 1])
|
||||
min_x2 = np.minimum(box_src[2], boxes_dst[:, 2])
|
||||
min_y2 = np.minimum(box_src[3], boxes_dst[:, 3])
|
||||
|
||||
area_intersection = np.maximum(0, (min_x2 - max_x1)) * np.maximum(0, (min_y2 - max_y1))
|
||||
area_src = (box_src[2] - box_src[0]) * (box_src[3] - box_src[1])
|
||||
area_dst = (boxes_dst[:, 2] - boxes_dst[:, 0]) * (boxes_dst[:, 3] - boxes_dst[:, 1])
|
||||
area_union = area_src + area_dst - area_intersection
|
||||
|
||||
iou = area_intersection / area_union
|
||||
|
||||
return iou
|
||||
|
||||
|
||||
def _boxes_scale(boxes, hardware_preproc_info: kp.HwPreProcInfo):
|
||||
"""
|
||||
Kneron hardware image pre-processing will do cropping, resize, padding by following ordering:
|
||||
1. cropping
|
||||
2. resize
|
||||
3. padding
|
||||
"""
|
||||
ratio_w = hardware_preproc_info.img_width / hardware_preproc_info.resized_img_width
|
||||
ratio_h = hardware_preproc_info.img_height / hardware_preproc_info.resized_img_height
|
||||
|
||||
# rollback padding
|
||||
boxes[..., :4] = boxes[..., :4] - np.array([hardware_preproc_info.pad_left, hardware_preproc_info.pad_top,
|
||||
hardware_preproc_info.pad_left, hardware_preproc_info.pad_top])
|
||||
|
||||
# scale coordinate
|
||||
boxes[..., :4] = boxes[..., :4] * np.array([ratio_w, ratio_h, ratio_w, ratio_h])
|
||||
|
||||
return boxes
|
||||
|
||||
|
||||
def post_process_tiny_yolo_v3(inference_float_node_output_list: List[kp.InferenceFloatNodeOutput],
|
||||
hardware_preproc_info: kp.HwPreProcInfo,
|
||||
thresh_value: float,
|
||||
with_sigmoid: bool = True) -> ExampleYoloResult:
|
||||
"""
|
||||
Tiny YOLO V3 post-processing function.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
inference_float_node_output_list : List[kp.InferenceFloatNodeOutput]
|
||||
A floating-point output node list, it should come from
|
||||
'kp.inference.generic_inference_retrieve_float_node()'.
|
||||
hardware_preproc_info : kp.HwPreProcInfo
|
||||
Information of Hardware Pre Process.
|
||||
thresh_value : float
|
||||
The threshold of YOLO postprocessing, range from 0.0 ~ 1.0
|
||||
with_sigmoid: bool, default=True
|
||||
Do sigmoid operation before postprocessing.
|
||||
|
||||
Returns
|
||||
-------
|
||||
yolo_result : utils.ExampleValue.ExampleYoloResult
|
||||
YoloResult object contained the post-processed result.
|
||||
|
||||
See Also
|
||||
--------
|
||||
kp.core.connect_devices : To connect multiple (including one) Kneron devices.
|
||||
kp.inference.generic_inference_retrieve_float_node : Retrieve single node output data from raw output buffer.
|
||||
kp.InferenceFloatNodeOutput
|
||||
kp.HwPreProcInfo
|
||||
utils.ExampleValue.ExampleYoloResult
|
||||
"""
|
||||
feature_map_list = []
|
||||
candidate_boxes_list = []
|
||||
|
||||
for i in range(len(inference_float_node_output_list)):
|
||||
anchor_offset = int(inference_float_node_output_list[i].shape[1] / YOLO_V3_CELL_BOX_NUM)
|
||||
feature_map = inference_float_node_output_list[i].ndarray.transpose((0, 2, 3, 1))
|
||||
feature_map = _sigmoid(feature_map) if with_sigmoid else feature_map
|
||||
feature_map = feature_map.reshape((feature_map.shape[0],
|
||||
feature_map.shape[1],
|
||||
feature_map.shape[2],
|
||||
YOLO_V3_CELL_BOX_NUM,
|
||||
anchor_offset))
|
||||
|
||||
ratio_w = hardware_preproc_info.model_input_width / inference_float_node_output_list[i].shape[3]
|
||||
ratio_h = hardware_preproc_info.model_input_height / inference_float_node_output_list[i].shape[2]
|
||||
nrows = inference_float_node_output_list[i].shape[2]
|
||||
ncols = inference_float_node_output_list[i].shape[3]
|
||||
grids = np.expand_dims(np.stack(np.meshgrid(np.arange(ncols), np.arange(nrows)), 2), axis=0)
|
||||
|
||||
for anchor_idx in range(YOLO_V3_CELL_BOX_NUM):
|
||||
feature_map[..., anchor_idx, 0:2] = (feature_map[..., anchor_idx, 0:2] + grids) * np.array(
|
||||
[ratio_h, ratio_w])
|
||||
feature_map[..., anchor_idx, 2:4] = (feature_map[..., anchor_idx, 2:4] * 2) ** 2 * TINY_YOLO_V3_ANCHERS[i][
|
||||
anchor_idx]
|
||||
|
||||
feature_map[..., anchor_idx, 0:2] = feature_map[..., anchor_idx, 0:2] - (
|
||||
feature_map[..., anchor_idx, 2:4] / 2.)
|
||||
feature_map[..., anchor_idx, 2:4] = feature_map[..., anchor_idx, 0:2] + feature_map[..., anchor_idx, 2:4]
|
||||
|
||||
feature_map = _boxes_scale(boxes=feature_map,
|
||||
hardware_preproc_info=hardware_preproc_info)
|
||||
|
||||
feature_map_list.append(feature_map)
|
||||
|
||||
predict_bboxes = np.concatenate(
|
||||
[np.reshape(feature_map, (-1, feature_map.shape[-1])) for feature_map in feature_map_list], axis=0)
|
||||
predict_bboxes[..., 5:] = np.repeat(predict_bboxes[..., 4][..., np.newaxis],
|
||||
predict_bboxes[..., 5:].shape[1],
|
||||
axis=1) * predict_bboxes[..., 5:]
|
||||
predict_bboxes_mask = (predict_bboxes[..., 5:] > thresh_value).sum(axis=1)
|
||||
predict_bboxes = predict_bboxes[predict_bboxes_mask >= 1]
|
||||
|
||||
# nms
|
||||
for class_idx in range(5, predict_bboxes.shape[1]):
|
||||
candidate_boxes_mask = predict_bboxes[..., class_idx] > thresh_value
|
||||
class_good_box_count = candidate_boxes_mask.sum()
|
||||
if class_good_box_count == 1:
|
||||
candidate_boxes_list.append(
|
||||
ExampleBoundingBox(
|
||||
x1=round(float(predict_bboxes[candidate_boxes_mask, 0][0]), 4),
|
||||
y1=round(float(predict_bboxes[candidate_boxes_mask, 1][0]), 4),
|
||||
x2=round(float(predict_bboxes[candidate_boxes_mask, 2][0]), 4),
|
||||
y2=round(float(predict_bboxes[candidate_boxes_mask, 3][0]), 4),
|
||||
score=round(float(predict_bboxes[candidate_boxes_mask, class_idx][0]), 4),
|
||||
class_num=class_idx - 5
|
||||
)
|
||||
)
|
||||
elif class_good_box_count > 1:
|
||||
candidate_boxes = predict_bboxes[candidate_boxes_mask].copy()
|
||||
candidate_boxes = candidate_boxes[candidate_boxes[:, class_idx].argsort()][::-1]
|
||||
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0] - 1):
|
||||
# origin python version post-processing
|
||||
if 0 != candidate_boxes[candidate_box_idx][class_idx]:
|
||||
remove_mask = _iou(box_src=candidate_boxes[candidate_box_idx],
|
||||
boxes_dst=candidate_boxes[candidate_box_idx + 1:]) > NMS_THRESH_YOLOV3
|
||||
candidate_boxes[candidate_box_idx + 1:][remove_mask, class_idx] = 0
|
||||
|
||||
good_count = 0
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0]):
|
||||
if candidate_boxes[candidate_box_idx, class_idx] > 0:
|
||||
candidate_boxes_list.append(
|
||||
ExampleBoundingBox(
|
||||
x1=round(float(candidate_boxes[candidate_box_idx, 0]), 4),
|
||||
y1=round(float(candidate_boxes[candidate_box_idx, 1]), 4),
|
||||
x2=round(float(candidate_boxes[candidate_box_idx, 2]), 4),
|
||||
y2=round(float(candidate_boxes[candidate_box_idx, 3]), 4),
|
||||
score=round(float(candidate_boxes[candidate_box_idx, class_idx]), 4),
|
||||
class_num=class_idx - 5
|
||||
)
|
||||
)
|
||||
good_count += 1
|
||||
|
||||
if YOLO_MAX_DETECTION_PER_CLASS == good_count:
|
||||
break
|
||||
|
||||
for idx, candidate_boxes in enumerate(candidate_boxes_list):
|
||||
candidate_boxes_list[idx].x1 = 0 if (candidate_boxes_list[idx].x1 + 0.5 < 0) else int(
|
||||
candidate_boxes_list[idx].x1 + 0.5)
|
||||
candidate_boxes_list[idx].y1 = 0 if (candidate_boxes_list[idx].y1 + 0.5 < 0) else int(
|
||||
candidate_boxes_list[idx].y1 + 0.5)
|
||||
candidate_boxes_list[idx].x2 = int(hardware_preproc_info.img_width - 1) if (
|
||||
candidate_boxes_list[idx].x2 + 0.5 > hardware_preproc_info.img_width - 1) else int(candidate_boxes_list[idx].x2 + 0.5)
|
||||
candidate_boxes_list[idx].y2 = int(hardware_preproc_info.img_height - 1) if (
|
||||
candidate_boxes_list[idx].y2 + 0.5 > hardware_preproc_info.img_height - 1) else int(candidate_boxes_list[idx].y2 + 0.5)
|
||||
|
||||
return ExampleYoloResult(
|
||||
class_count=predict_bboxes.shape[1] - 5,
|
||||
box_count=len(candidate_boxes_list),
|
||||
box_list=candidate_boxes_list
|
||||
)
|
||||
|
||||
|
||||
def post_process_yolo_v5(inference_float_node_output_list: List[kp.InferenceFloatNodeOutput],
|
||||
hardware_preproc_info: kp.HwPreProcInfo,
|
||||
thresh_value: float,
|
||||
with_sigmoid: bool = True) -> ExampleYoloResult:
|
||||
"""
|
||||
YOLO V5 post-processing function.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
inference_float_node_output_list : List[kp.InferenceFloatNodeOutput]
|
||||
A floating-point output node list, it should come from
|
||||
'kp.inference.generic_inference_retrieve_float_node()'.
|
||||
hardware_preproc_info : kp.HwPreProcInfo
|
||||
Information of Hardware Pre Process.
|
||||
thresh_value : float
|
||||
The threshold of YOLO postprocessing, range from 0.0 ~ 1.0
|
||||
with_sigmoid: bool, default=True
|
||||
Do sigmoid operation before postprocessing.
|
||||
|
||||
Returns
|
||||
-------
|
||||
yolo_result : utils.ExampleValue.ExampleYoloResult
|
||||
YoloResult object contained the post-processed result.
|
||||
|
||||
See Also
|
||||
--------
|
||||
kp.core.connect_devices : To connect multiple (including one) Kneron devices.
|
||||
kp.inference.generic_inference_retrieve_float_node : Retrieve single node output data from raw output buffer.
|
||||
kp.InferenceFloatNodeOutput
|
||||
kp.HwPreProcInfo
|
||||
utils.ExampleValue.ExampleYoloResult
|
||||
"""
|
||||
feature_map_list = []
|
||||
candidate_boxes_list = []
|
||||
|
||||
for i in range(len(inference_float_node_output_list)):
|
||||
anchor_offset = int(inference_float_node_output_list[i].shape[1] / YOLO_V3_CELL_BOX_NUM)
|
||||
feature_map = inference_float_node_output_list[i].ndarray.transpose((0, 2, 3, 1))
|
||||
feature_map = _sigmoid(feature_map) if with_sigmoid else feature_map
|
||||
feature_map = feature_map.reshape((feature_map.shape[0],
|
||||
feature_map.shape[1],
|
||||
feature_map.shape[2],
|
||||
YOLO_V3_CELL_BOX_NUM,
|
||||
anchor_offset))
|
||||
|
||||
ratio_w = hardware_preproc_info.model_input_width / inference_float_node_output_list[i].shape[3]
|
||||
ratio_h = hardware_preproc_info.model_input_height / inference_float_node_output_list[i].shape[2]
|
||||
nrows = inference_float_node_output_list[i].shape[2]
|
||||
ncols = inference_float_node_output_list[i].shape[3]
|
||||
grids = np.expand_dims(np.stack(np.meshgrid(np.arange(ncols), np.arange(nrows)), 2), axis=0)
|
||||
|
||||
for anchor_idx in range(YOLO_V3_CELL_BOX_NUM):
|
||||
feature_map[..., anchor_idx, 0:2] = (feature_map[..., anchor_idx, 0:2] * 2. - 0.5 + grids) * np.array(
|
||||
[ratio_h, ratio_w])
|
||||
feature_map[..., anchor_idx, 2:4] = (feature_map[..., anchor_idx, 2:4] * 2) ** 2 * YOLO_V5_ANCHERS[i][
|
||||
anchor_idx]
|
||||
|
||||
feature_map[..., anchor_idx, 0:2] = feature_map[..., anchor_idx, 0:2] - (
|
||||
feature_map[..., anchor_idx, 2:4] / 2.)
|
||||
feature_map[..., anchor_idx, 2:4] = feature_map[..., anchor_idx, 0:2] + feature_map[..., anchor_idx, 2:4]
|
||||
|
||||
feature_map = _boxes_scale(boxes=feature_map,
|
||||
hardware_preproc_info=hardware_preproc_info)
|
||||
|
||||
feature_map_list.append(feature_map)
|
||||
|
||||
predict_bboxes = np.concatenate(
|
||||
[np.reshape(feature_map, (-1, feature_map.shape[-1])) for feature_map in feature_map_list], axis=0)
|
||||
predict_bboxes[..., 5:] = np.repeat(predict_bboxes[..., 4][..., np.newaxis],
|
||||
predict_bboxes[..., 5:].shape[1],
|
||||
axis=1) * predict_bboxes[..., 5:]
|
||||
predict_bboxes_mask = (predict_bboxes[..., 5:] > thresh_value).sum(axis=1)
|
||||
predict_bboxes = predict_bboxes[predict_bboxes_mask >= 1]
|
||||
|
||||
# nms
|
||||
for class_idx in range(5, predict_bboxes.shape[1]):
|
||||
candidate_boxes_mask = predict_bboxes[..., class_idx] > thresh_value
|
||||
class_good_box_count = candidate_boxes_mask.sum()
|
||||
if class_good_box_count == 1:
|
||||
candidate_boxes_list.append(
|
||||
ExampleBoundingBox(
|
||||
x1=round(float(predict_bboxes[candidate_boxes_mask, 0][0]), 4),
|
||||
y1=round(float(predict_bboxes[candidate_boxes_mask, 1][0]), 4),
|
||||
x2=round(float(predict_bboxes[candidate_boxes_mask, 2][0]), 4),
|
||||
y2=round(float(predict_bboxes[candidate_boxes_mask, 3][0]), 4),
|
||||
score=round(float(predict_bboxes[candidate_boxes_mask, class_idx][0]), 4),
|
||||
class_num=class_idx - 5
|
||||
)
|
||||
)
|
||||
elif class_good_box_count > 1:
|
||||
candidate_boxes = predict_bboxes[candidate_boxes_mask].copy()
|
||||
candidate_boxes = candidate_boxes[candidate_boxes[:, class_idx].argsort()][::-1]
|
||||
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0] - 1):
|
||||
if 0 != candidate_boxes[candidate_box_idx][class_idx]:
|
||||
remove_mask = _iou(box_src=candidate_boxes[candidate_box_idx],
|
||||
boxes_dst=candidate_boxes[candidate_box_idx + 1:]) > NMS_THRESH_YOLOV5
|
||||
candidate_boxes[candidate_box_idx + 1:][remove_mask, class_idx] = 0
|
||||
|
||||
good_count = 0
|
||||
for candidate_box_idx in range(candidate_boxes.shape[0]):
|
||||
if candidate_boxes[candidate_box_idx, class_idx] > 0:
|
||||
candidate_boxes_list.append(
|
||||
ExampleBoundingBox(
|
||||
x1=round(float(candidate_boxes[candidate_box_idx, 0]), 4),
|
||||
y1=round(float(candidate_boxes[candidate_box_idx, 1]), 4),
|
||||
x2=round(float(candidate_boxes[candidate_box_idx, 2]), 4),
|
||||
y2=round(float(candidate_boxes[candidate_box_idx, 3]), 4),
|
||||
score=round(float(candidate_boxes[candidate_box_idx, class_idx]), 4),
|
||||
class_num=class_idx - 5
|
||||
)
|
||||
)
|
||||
good_count += 1
|
||||
|
||||
if YOLO_MAX_DETECTION_PER_CLASS == good_count:
|
||||
break
|
||||
|
||||
for idx, candidate_boxes in enumerate(candidate_boxes_list):
|
||||
candidate_boxes_list[idx].x1 = 0 if (candidate_boxes_list[idx].x1 + 0.5 < 0) else int(
|
||||
candidate_boxes_list[idx].x1 + 0.5)
|
||||
candidate_boxes_list[idx].y1 = 0 if (candidate_boxes_list[idx].y1 + 0.5 < 0) else int(
|
||||
candidate_boxes_list[idx].y1 + 0.5)
|
||||
candidate_boxes_list[idx].x2 = int(hardware_preproc_info.img_width - 1) if (
|
||||
candidate_boxes_list[idx].x2 + 0.5 > hardware_preproc_info.img_width - 1) else int(candidate_boxes_list[idx].x2 + 0.5)
|
||||
candidate_boxes_list[idx].y2 = int(hardware_preproc_info.img_height - 1) if (
|
||||
candidate_boxes_list[idx].y2 + 0.5 > hardware_preproc_info.img_height - 1) else int(candidate_boxes_list[idx].y2 + 0.5)
|
||||
|
||||
return ExampleYoloResult(
|
||||
class_count=predict_bboxes.shape[1] - 5,
|
||||
box_count=len(candidate_boxes_list),
|
||||
box_list=candidate_boxes_list
|
||||
)
|
||||
126
example_utils/ExampleValue.py
Normal file
126
example_utils/ExampleValue.py
Normal file
@ -0,0 +1,126 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
from typing import List
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
PWD = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(1, os.path.join(PWD, '../..'))
|
||||
|
||||
from kp.KPBaseClass.ValueBase import ValueRepresentBase
|
||||
|
||||
|
||||
class ExampleBoundingBox(ValueRepresentBase):
|
||||
"""
|
||||
Example Bounding box descriptor.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
x1 : int, default=0
|
||||
X coordinate of bounding box top-left corner.
|
||||
y1 : int, default=0
|
||||
Y coordinate of bounding box top-left corner.
|
||||
x2 : int, default=0
|
||||
X coordinate of bounding box bottom-right corner.
|
||||
y2 : int, default=0
|
||||
Y coordinate of bounding box bottom-right corner.
|
||||
score : float, default=0
|
||||
Probability score.
|
||||
class_num : int, default=0
|
||||
Class # (of many) with highest probability.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
x1: int = 0,
|
||||
y1: int = 0,
|
||||
x2: int = 0,
|
||||
y2: int = 0,
|
||||
score: float = 0,
|
||||
class_num: int = 0):
|
||||
"""
|
||||
Example Bounding box descriptor.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x1 : int, default=0
|
||||
X coordinate of bounding box top-left corner.
|
||||
y1 : int, default=0
|
||||
Y coordinate of bounding box top-left corner.
|
||||
x2 : int, default=0
|
||||
X coordinate of bounding box bottom-right corner.
|
||||
y2 : int, default=0
|
||||
Y coordinate of bounding box bottom-right corner.
|
||||
score : float, default=0
|
||||
Probability score.
|
||||
class_num : int, default=0
|
||||
Class # (of many) with highest probability.
|
||||
"""
|
||||
|
||||
self.x1 = x1
|
||||
self.y1 = y1
|
||||
self.x2 = x2
|
||||
self.y2 = y2
|
||||
self.score = score
|
||||
self.class_num = class_num
|
||||
|
||||
def get_member_variable_dict(self) -> dict:
|
||||
return {
|
||||
'x1': self.x1,
|
||||
'y1': self.y1,
|
||||
'x2': self.x2,
|
||||
'y2': self.y2,
|
||||
'score': self.score,
|
||||
'class_num': self.class_num
|
||||
}
|
||||
|
||||
|
||||
class ExampleYoloResult(ValueRepresentBase):
|
||||
"""
|
||||
Example YOLO output result descriptor.
|
||||
|
||||
Attributes
|
||||
----------
|
||||
class_count : int, default=0
|
||||
Total detectable class count.
|
||||
box_count : int, default=0
|
||||
Total bounding box number.
|
||||
box_list : List[ExampleBoundingBox], default=[]
|
||||
bounding boxes.
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
class_count: int = 0,
|
||||
box_count: int = 0,
|
||||
box_list: List[ExampleBoundingBox] = []):
|
||||
"""
|
||||
Example YOLO output result descriptor.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
class_count : int, default=0
|
||||
Total detectable class count.
|
||||
box_count : int, default=0
|
||||
Total bounding box number.
|
||||
box_list : List[ExampleBoundingBox], default=[]
|
||||
bounding boxes.
|
||||
"""
|
||||
self.class_count = class_count
|
||||
self.box_count = box_count
|
||||
self.box_list = box_list
|
||||
|
||||
def _cast_element_buffer(self) -> None:
|
||||
pass
|
||||
|
||||
def get_member_variable_dict(self) -> dict:
|
||||
member_variable_dict = {
|
||||
'class_count': self.class_count,
|
||||
'box_count': self.box_count,
|
||||
'box_list': {}
|
||||
}
|
||||
|
||||
for idx, box_element in enumerate(self.box_list):
|
||||
member_variable_dict['box_list'][idx] = box_element.get_member_variable_dict()
|
||||
|
||||
return member_variable_dict
|
||||
4
example_utils/__init__.py
Normal file
4
example_utils/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2021-2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
|
||||
4
example_utils/postprocess/__init__.py
Normal file
4
example_utils/postprocess/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2021-2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
|
||||
56
example_utils/postprocess/basetrack.py
Normal file
56
example_utils/postprocess/basetrack.py
Normal file
@ -0,0 +1,56 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
import numpy as np
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class TrackState(object):
|
||||
New = 0
|
||||
Tracked = 1
|
||||
Lost = 2
|
||||
Removed = 3
|
||||
#Overlap_candidate = 4
|
||||
|
||||
|
||||
class BaseTrack(object):
|
||||
_count = 0
|
||||
|
||||
track_id = 0
|
||||
is_activated = False
|
||||
state = TrackState.New
|
||||
|
||||
history = OrderedDict()
|
||||
features = []
|
||||
curr_feature = None
|
||||
score = 0
|
||||
start_frame = 0
|
||||
frame_id = 0
|
||||
time_since_update = 0
|
||||
|
||||
# multi-camera
|
||||
location = (np.inf, np.inf)
|
||||
|
||||
@property
|
||||
def end_frame(self):
|
||||
return self.frame_id
|
||||
|
||||
@staticmethod
|
||||
def next_id():
|
||||
BaseTrack._count += 1
|
||||
return BaseTrack._count
|
||||
|
||||
def activate(self, *args):
|
||||
raise NotImplementedError
|
||||
|
||||
def predict(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def update(self, *args, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
def mark_lost(self):
|
||||
self.state = TrackState.Lost
|
||||
|
||||
def mark_removed(self):
|
||||
self.state = TrackState.Removed
|
||||
383
example_utils/postprocess/bytetrack_postprocess.py
Normal file
383
example_utils/postprocess/bytetrack_postprocess.py
Normal file
@ -0,0 +1,383 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
import numpy as np
|
||||
from .kalman_filter import KalmanFilter
|
||||
from . import matching
|
||||
from .basetrack import BaseTrack, TrackState
|
||||
|
||||
|
||||
class STrack(BaseTrack):
|
||||
shared_kalman = KalmanFilter()
|
||||
def __init__(self, tlwh, score):
|
||||
|
||||
# wait activate
|
||||
self._tlwh = np.asarray(tlwh, dtype=np.float32)
|
||||
self.kalman_filter = None
|
||||
self.mean, self.covariance = None, None
|
||||
self.is_activated = False
|
||||
|
||||
self.score = score
|
||||
self.tracklet_len = 0
|
||||
|
||||
def predict(self):
|
||||
mean_state = self.mean.copy()
|
||||
if self.state != TrackState.Tracked:
|
||||
mean_state[7] = 0
|
||||
self.mean, self.covariance = self.kalman_filter.predict(mean_state, self.covariance)
|
||||
|
||||
@staticmethod
|
||||
def multi_predict(stracks):
|
||||
if len(stracks) > 0:
|
||||
multi_mean = np.asarray([st.mean.copy() for st in stracks])
|
||||
multi_covariance = np.asarray([st.covariance for st in stracks])
|
||||
for i, st in enumerate(stracks):
|
||||
if st.state != TrackState.Tracked:
|
||||
multi_mean[i][7] = 0
|
||||
multi_mean, multi_covariance = STrack.shared_kalman.multi_predict(multi_mean, multi_covariance)
|
||||
for i, (mean, cov) in enumerate(zip(multi_mean, multi_covariance)):
|
||||
stracks[i].mean = mean
|
||||
stracks[i].covariance = cov
|
||||
|
||||
# NOTE is activated is not triggered
|
||||
def activate(self, kalman_filter, frame_id): # new-> track
|
||||
"""Start a new tracklet"""
|
||||
self.kalman_filter = kalman_filter
|
||||
self.track_id = self.next_id()
|
||||
self.mean, self.covariance = self.kalman_filter.initiate(self.tlwh_to_xyah(self._tlwh))
|
||||
|
||||
self.tracklet_len = 0
|
||||
self.state = TrackState.Tracked
|
||||
if frame_id == 1: # only frame 1
|
||||
self.is_activated = True
|
||||
#self.is_activated = True
|
||||
self.frame_id = frame_id
|
||||
self.start_frame = frame_id
|
||||
|
||||
def re_activate(self, new_track, frame_id, new_id=False): # lost-> track
|
||||
self.mean, self.covariance = self.kalman_filter.update(
|
||||
self.mean, self.covariance, self.tlwh_to_xyah(new_track.tlwh)
|
||||
)
|
||||
|
||||
self.tracklet_len = 0
|
||||
self.state = TrackState.Tracked
|
||||
self.is_activated = True
|
||||
self.frame_id = frame_id
|
||||
if new_id:
|
||||
self.track_id = self.next_id()
|
||||
self.score = new_track.score
|
||||
|
||||
def update(self, new_track, frame_id): # track-> track
|
||||
"""
|
||||
Update a matched track
|
||||
:type new_track: STrack
|
||||
:type frame_id: int
|
||||
:return:
|
||||
"""
|
||||
self.frame_id = frame_id
|
||||
self.tracklet_len += 1
|
||||
|
||||
new_tlwh = new_track.tlwh
|
||||
self.mean, self.covariance = self.kalman_filter.update(
|
||||
self.mean, self.covariance, self.tlwh_to_xyah(new_tlwh))
|
||||
self.state = TrackState.Tracked
|
||||
self.is_activated = True
|
||||
|
||||
self.score = new_track.score
|
||||
|
||||
@property
|
||||
# @jit(nopython=True)
|
||||
def tlwh(self):
|
||||
"""Get current position in bounding box format `(top left x, top left y,
|
||||
width, height)`.
|
||||
"""
|
||||
if self.mean is None:
|
||||
return self._tlwh.copy()
|
||||
ret = self.mean[:4].copy()
|
||||
ret[2] *= ret[3]
|
||||
ret[:2] -= ret[2:] / 2
|
||||
return ret
|
||||
|
||||
@property
|
||||
# @jit(nopython=True)
|
||||
def tlbr(self):
|
||||
"""Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
|
||||
`(top left, bottom right)`.
|
||||
"""
|
||||
ret = self.tlwh.copy()
|
||||
ret[2:] += ret[:2]
|
||||
return ret
|
||||
|
||||
@property
|
||||
# @jit(nopython=True)
|
||||
def center(self):
|
||||
"""Convert bounding box to center
|
||||
"""
|
||||
ret = self.tlwh.copy()
|
||||
return ret[:2] + (ret[2:]/2)
|
||||
|
||||
@staticmethod
|
||||
# @jit(nopython=True)
|
||||
def tlwh_to_xyah(tlwh):
|
||||
"""Convert bounding box to format `(center x, center y, aspect ratio,
|
||||
height)`, where the aspect ratio is `width / height`.
|
||||
"""
|
||||
ret = np.asarray(tlwh).copy()
|
||||
ret[:2] += ret[2:] / 2
|
||||
ret[2] /= ret[3]
|
||||
return ret
|
||||
|
||||
def to_xyah(self):
|
||||
return self.tlwh_to_xyah(self.tlwh)
|
||||
|
||||
@staticmethod
|
||||
# @jit(nopython=True)
|
||||
def tlbr_to_tlwh(tlbr):
|
||||
ret = np.asarray(tlbr).copy()
|
||||
ret[2:] -= ret[:2]
|
||||
return ret
|
||||
|
||||
@staticmethod
|
||||
# @jit(nopython=True)
|
||||
def tlwh_to_tlbr(tlwh):
|
||||
ret = np.asarray(tlwh).copy()
|
||||
ret[2:] += ret[:2]
|
||||
return ret
|
||||
|
||||
def __repr__(self):
|
||||
return 'OT_{}_({}-{})'.format(self.track_id, self.start_frame, self.end_frame)
|
||||
|
||||
|
||||
|
||||
class BYTETracker(object): #
|
||||
"""
|
||||
YTE tracker
|
||||
:track_thresh: tau_high as defined in ByteTrack paper, this value separates the high/low score for tracking,
|
||||
: set to 0.6 in original paper, but for demo is set to 0.5
|
||||
: This value also has an impact on the det_thresh
|
||||
:match_thresh: set to 0.9 in original paper, but for demo is set to 0.8
|
||||
:frame_rate : frame rate of input sequences
|
||||
:track_buffer: how long we shall buffer the track
|
||||
:max_time_lost: number of frames that keep in lost state, after that state: Lost-> Removed
|
||||
:max_per_image: max number of output objects
|
||||
|
||||
"""
|
||||
def __init__(self, track_thresh = 0.6, match_thresh = 0.9, frame_rate=30, track_buffer = 120):
|
||||
|
||||
self.tracked_stracks = [] # type: list[STrack]
|
||||
self.lost_stracks = [] # type: list[STrack]
|
||||
self.removed_stracks = [] # type: list[STrack]
|
||||
|
||||
self.frame_id = 0
|
||||
self.track_thresh = track_thresh
|
||||
self.match_thresh = match_thresh
|
||||
self.det_thresh = track_thresh + 0.1
|
||||
self.buffer_size = int(frame_rate / 30.0 * track_buffer)
|
||||
self.max_time_lost = self.buffer_size
|
||||
self.mot20 = False #may open if high surveilance scenarios? (no fuse score)
|
||||
self.kalman_filter = KalmanFilter()
|
||||
|
||||
def update(self, output_results):
|
||||
'''
|
||||
dets: list of bbox information [x, y, w, h, score, class]
|
||||
'''
|
||||
|
||||
self.frame_id += 1
|
||||
activated_starcks = []
|
||||
refind_stracks = []
|
||||
lost_stracks = []
|
||||
removed_stracks = []
|
||||
|
||||
|
||||
dets = []
|
||||
dets_second = []
|
||||
if len(output_results) > 0:
|
||||
output_results = np.array(output_results)
|
||||
#if output_results.ndim == 2:
|
||||
|
||||
scores = output_results[:, 4]
|
||||
bboxes = output_results[:, :4]
|
||||
|
||||
''' Step 1: get detections '''
|
||||
|
||||
remain_inds = scores > self.track_thresh
|
||||
inds_low = scores > 0.1 # tau_Low
|
||||
inds_high = scores < self.track_thresh
|
||||
|
||||
inds_second = np.logical_and(inds_low, inds_high)
|
||||
dets_second = bboxes[inds_second] #D_low
|
||||
dets = bboxes[remain_inds] #D_high
|
||||
scores_keep = scores[remain_inds] #D_high_score
|
||||
scores_second = scores[inds_second] #D_low_score
|
||||
|
||||
if len(dets) > 0:
|
||||
'''Detections'''
|
||||
detections = [STrack(tlwh, s) for
|
||||
(tlwh, s) in zip(dets, scores_keep)]
|
||||
else:
|
||||
detections = []
|
||||
|
||||
''' Add newly detected tracklets to tracked_stracks'''
|
||||
unconfirmed = []
|
||||
tracked_stracks = [] # type: list[STrack]
|
||||
for track in self.tracked_stracks:
|
||||
if not track.is_activated:
|
||||
unconfirmed.append(track)
|
||||
else:
|
||||
tracked_stracks.append(track)
|
||||
|
||||
|
||||
''' Step 2: First association, with high score detection boxes'''
|
||||
strack_pool = joint_stracks(tracked_stracks, self.lost_stracks)
|
||||
# Predict the current location with KF
|
||||
STrack.multi_predict(strack_pool)
|
||||
# for fairmot, it is with embedding distance and fuse_motion (kalman filter gating distance)
|
||||
# for bytetrack, the distance is computed with IOU * detection scores
|
||||
# which mean the matching
|
||||
dists = matching.iou_distance(strack_pool, detections)
|
||||
if not self.mot20:
|
||||
dists = matching.fuse_score(dists, detections)
|
||||
matches, u_track, u_detection = matching.linear_assignment(dists, thresh=self.match_thresh)
|
||||
|
||||
for itracked, idet in matches:
|
||||
track = strack_pool[itracked]
|
||||
det = detections[idet]
|
||||
if track.state == TrackState.Tracked:
|
||||
track.update(detections[idet], self.frame_id)
|
||||
activated_starcks.append(track)
|
||||
else:
|
||||
track.re_activate(det, self.frame_id, new_id=False)
|
||||
refind_stracks.append(track)
|
||||
|
||||
''' Step 3: Second association, with low score detection boxes'''
|
||||
# association the untrack to the low score detections
|
||||
|
||||
if len(dets_second) > 0:
|
||||
'''Detections'''
|
||||
detections_second = [STrack(tlwh, s) for
|
||||
(tlwh, s) in zip(dets_second, scores_second)]
|
||||
else:
|
||||
detections_second = []
|
||||
r_tracked_stracks = [strack_pool[i] for i in u_track if strack_pool[i].state == TrackState.Tracked]
|
||||
dists = matching.iou_distance(r_tracked_stracks, detections_second)
|
||||
matches, u_track, u_detection_second = matching.linear_assignment(dists, thresh=0.5)
|
||||
for itracked, idet in matches:
|
||||
track = r_tracked_stracks[itracked]
|
||||
det = detections_second[idet]
|
||||
if track.state == TrackState.Tracked:
|
||||
track.update(det, self.frame_id)
|
||||
activated_starcks.append(track)
|
||||
else:
|
||||
track.re_activate(det, self.frame_id, new_id=False)
|
||||
refind_stracks.append(track)
|
||||
|
||||
for it in u_track:
|
||||
track = r_tracked_stracks[it]
|
||||
if not track.state == TrackState.Lost:
|
||||
track.mark_lost()
|
||||
lost_stracks.append(track)
|
||||
|
||||
'''Deal with unconfirmed tracks, usually tracks with only one beginning frame'''
|
||||
detections = [detections[i] for i in u_detection]
|
||||
dists = matching.iou_distance(unconfirmed, detections)
|
||||
if not self.mot20:
|
||||
dists = matching.fuse_score(dists, detections)
|
||||
matches, u_unconfirmed, u_detection = matching.linear_assignment(dists, thresh=0.7)
|
||||
for itracked, idet in matches:
|
||||
unconfirmed[itracked].update(detections[idet], self.frame_id)
|
||||
activated_starcks.append(unconfirmed[itracked])
|
||||
for it in u_unconfirmed:
|
||||
track = unconfirmed[it]
|
||||
track.mark_removed()
|
||||
removed_stracks.append(track)
|
||||
|
||||
""" Step 4: Init new stracks"""
|
||||
for inew in u_detection:
|
||||
track = detections[inew]
|
||||
if track.score < self.det_thresh:
|
||||
continue
|
||||
track.activate(self.kalman_filter, self.frame_id)
|
||||
activated_starcks.append(track)
|
||||
|
||||
|
||||
""" Step 5: Update state"""
|
||||
for track in self.lost_stracks:
|
||||
if self.frame_id - track.end_frame > self.max_time_lost:
|
||||
track.mark_removed()
|
||||
removed_stracks.append(track)
|
||||
|
||||
self.tracked_stracks = [t for t in self.tracked_stracks if t.state == TrackState.Tracked]
|
||||
self.tracked_stracks = joint_stracks(self.tracked_stracks, activated_starcks)
|
||||
self.tracked_stracks = joint_stracks(self.tracked_stracks, refind_stracks)
|
||||
self.lost_stracks = sub_stracks(self.lost_stracks, self.tracked_stracks)
|
||||
self.lost_stracks.extend(lost_stracks)
|
||||
self.lost_stracks = sub_stracks(self.lost_stracks, self.removed_stracks)
|
||||
self.removed_stracks.extend(removed_stracks)
|
||||
self.tracked_stracks, self.lost_stracks = remove_duplicate_stracks(self.tracked_stracks, self.lost_stracks)
|
||||
|
||||
# get scores of lost tracks
|
||||
output_stracks = [track for track in self.tracked_stracks if track.is_activated]
|
||||
|
||||
|
||||
return output_stracks
|
||||
|
||||
|
||||
def postprocess_(dets, tracker, min_box_area = 120, **kwargs):
|
||||
|
||||
'''
|
||||
return: frame with bboxs
|
||||
'''
|
||||
|
||||
online_targets = tracker.update(dets)
|
||||
online_tlwhs = []
|
||||
online_ids = []
|
||||
for t in online_targets:
|
||||
tlwh = t.tlwh
|
||||
tid = t.track_id
|
||||
#vertical = tlwh[2] / tlwh[3] > 1.6
|
||||
#if tlwh[2] * tlwh[3] > min_box_area and not vertical:
|
||||
online_tlwhs.append(np.round(tlwh, 2))
|
||||
online_ids.append(tid)
|
||||
return online_tlwhs, online_ids
|
||||
|
||||
|
||||
|
||||
def joint_stracks(tlista, tlistb):
|
||||
exists = {}
|
||||
res = []
|
||||
for t in tlista:
|
||||
exists[t.track_id] = 1
|
||||
res.append(t)
|
||||
for t in tlistb:
|
||||
tid = t.track_id
|
||||
if not exists.get(tid, 0):
|
||||
exists[tid] = 1
|
||||
res.append(t)
|
||||
return res
|
||||
|
||||
# remove tlisb items from tlist a
|
||||
def sub_stracks(tlista, tlistb):
|
||||
stracks = {}
|
||||
for t in tlista:
|
||||
stracks[t.track_id] = t
|
||||
for t in tlistb:
|
||||
tid = t.track_id
|
||||
if stracks.get(tid, 0):
|
||||
del stracks[tid]
|
||||
return list(stracks.values())
|
||||
|
||||
|
||||
def remove_duplicate_stracks(stracksa, stracksb): # remove track overlap with 85 %
|
||||
pdist = matching.iou_distance(stracksa, stracksb)
|
||||
pairs = np.where(pdist < 0.15)
|
||||
dupa, dupb = list(), list()
|
||||
for p, q in zip(*pairs):
|
||||
timep = stracksa[p].frame_id - stracksa[p].start_frame
|
||||
timeq = stracksb[q].frame_id - stracksb[q].start_frame
|
||||
if timep > timeq:
|
||||
dupb.append(q)
|
||||
else:
|
||||
dupa.append(p)
|
||||
resa = [t for i, t in enumerate(stracksa) if not i in dupa]
|
||||
resb = [t for i, t in enumerate(stracksb) if not i in dupb]
|
||||
return resa, resb
|
||||
274
example_utils/postprocess/kalman_filter.py
Normal file
274
example_utils/postprocess/kalman_filter.py
Normal file
@ -0,0 +1,274 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
|
||||
# vim: expandtab:ts=4:sw=4
|
||||
import numpy as np
|
||||
import scipy.linalg
|
||||
|
||||
|
||||
"""
|
||||
Table for the 0.95 quantile of the chi-square distribution with N degrees of
|
||||
freedom (contains values for N=1, ..., 9). Taken from MATLAB/Octave's chi2inv
|
||||
function and used as Mahalanobis gating threshold.
|
||||
"""
|
||||
chi2inv95 = {
|
||||
1: 3.8415,
|
||||
2: 5.9915,
|
||||
3: 7.8147,
|
||||
4: 9.4877,
|
||||
5: 11.070,
|
||||
6: 12.592,
|
||||
7: 14.067,
|
||||
8: 15.507,
|
||||
9: 16.919}
|
||||
|
||||
|
||||
class KalmanFilter(object):
|
||||
"""
|
||||
A simple Kalman filter for tracking bounding boxes in image space.
|
||||
|
||||
The 8-dimensional state space
|
||||
|
||||
x, y, a, h, vx, vy, va, vh
|
||||
|
||||
contains the bounding box center position (x, y), aspect ratio a, height h,
|
||||
and their respective velocities.
|
||||
|
||||
Object motion follows a constant velocity model. The bounding box location
|
||||
(x, y, a, h) is taken as direct observation of the state space (linear
|
||||
observation model).
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
ndim, dt = 4, 1.
|
||||
|
||||
# Create Kalman filter model matrices.
|
||||
self._motion_mat = np.eye(2 * ndim, 2 * ndim)
|
||||
for i in range(ndim):
|
||||
self._motion_mat[i, ndim + i] = dt
|
||||
self._update_mat = np.eye(ndim, 2 * ndim)
|
||||
|
||||
# Motion and observation uncertainty are chosen relative to the current
|
||||
# state estimate. These weights control the amount of uncertainty in
|
||||
# the model. This is a bit hacky.
|
||||
self._std_weight_position = 1. / 20
|
||||
self._std_weight_velocity = 1. / 160
|
||||
|
||||
def initiate(self, measurement):
|
||||
"""Create track from unassociated measurement.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
measurement : ndarray
|
||||
Bounding box coordinates (x, y, a, h) with center position (x, y),
|
||||
aspect ratio a, and height h.
|
||||
|
||||
Returns
|
||||
-------
|
||||
(ndarray, ndarray)
|
||||
Returns the mean vector (8 dimensional) and covariance matrix (8x8
|
||||
dimensional) of the new track. Unobserved velocities are initialized
|
||||
to 0 mean.
|
||||
|
||||
"""
|
||||
mean_pos = measurement
|
||||
mean_vel = np.zeros_like(mean_pos)
|
||||
mean = np.r_[mean_pos, mean_vel]
|
||||
|
||||
std = [
|
||||
2 * self._std_weight_position * measurement[3],
|
||||
2 * self._std_weight_position * measurement[3],
|
||||
1e-2,
|
||||
2 * self._std_weight_position * measurement[3],
|
||||
10 * self._std_weight_velocity * measurement[3],
|
||||
10 * self._std_weight_velocity * measurement[3],
|
||||
1e-5,
|
||||
10 * self._std_weight_velocity * measurement[3]]
|
||||
covariance = np.diag(np.square(std))
|
||||
return mean, covariance
|
||||
|
||||
def predict(self, mean, covariance):
|
||||
"""Run Kalman filter prediction step.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mean : ndarray
|
||||
The 8 dimensional mean vector of the object state at the previous
|
||||
time step.
|
||||
covariance : ndarray
|
||||
The 8x8 dimensional covariance matrix of the object state at the
|
||||
previous time step.
|
||||
|
||||
Returns
|
||||
-------
|
||||
(ndarray, ndarray)
|
||||
Returns the mean vector and covariance matrix of the predicted
|
||||
state. Unobserved velocities are initialized to 0 mean.
|
||||
|
||||
"""
|
||||
std_pos = [
|
||||
self._std_weight_position * mean[3],
|
||||
self._std_weight_position * mean[3],
|
||||
1e-2,
|
||||
self._std_weight_position * mean[3]]
|
||||
std_vel = [
|
||||
self._std_weight_velocity * mean[3],
|
||||
self._std_weight_velocity * mean[3],
|
||||
1e-5,
|
||||
self._std_weight_velocity * mean[3]]
|
||||
motion_cov = np.diag(np.square(np.r_[std_pos, std_vel]))
|
||||
|
||||
#mean = np.dot(self._motion_mat, mean)
|
||||
mean = np.dot(mean, self._motion_mat.T)
|
||||
covariance = np.linalg.multi_dot((
|
||||
self._motion_mat, covariance, self._motion_mat.T)) + motion_cov
|
||||
|
||||
return mean, covariance
|
||||
|
||||
def project(self, mean, covariance):
|
||||
"""Project state distribution to measurement space.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mean : ndarray
|
||||
The state's mean vector (8 dimensional array).
|
||||
covariance : ndarray
|
||||
The state's covariance matrix (8x8 dimensional).
|
||||
|
||||
Returns
|
||||
-------
|
||||
(ndarray, ndarray)
|
||||
Returns the projected mean and covariance matrix of the given state
|
||||
estimate.
|
||||
|
||||
"""
|
||||
std = [
|
||||
self._std_weight_position * mean[3],
|
||||
self._std_weight_position * mean[3],
|
||||
1e-1,
|
||||
self._std_weight_position * mean[3]]
|
||||
innovation_cov = np.diag(np.square(std))
|
||||
|
||||
mean = np.dot(self._update_mat, mean)
|
||||
covariance = np.linalg.multi_dot((
|
||||
self._update_mat, covariance, self._update_mat.T))
|
||||
return mean, covariance + innovation_cov
|
||||
|
||||
def multi_predict(self, mean, covariance):
|
||||
"""Run Kalman filter prediction step (Vectorized version).
|
||||
Parameters
|
||||
----------
|
||||
mean : ndarray
|
||||
The Nx8 dimensional mean matrix of the object states at the previous
|
||||
time step.
|
||||
covariance : ndarray
|
||||
The Nx8x8 dimensional covariance matrics of the object states at the
|
||||
previous time step.
|
||||
Returns
|
||||
-------
|
||||
(ndarray, ndarray)
|
||||
Returns the mean vector and covariance matrix of the predicted
|
||||
state. Unobserved velocities are initialized to 0 mean.
|
||||
"""
|
||||
std_pos = [
|
||||
self._std_weight_position * mean[:, 3],
|
||||
self._std_weight_position * mean[:, 3],
|
||||
1e-2 * np.ones_like(mean[:, 3]),
|
||||
self._std_weight_position * mean[:, 3]]
|
||||
std_vel = [
|
||||
self._std_weight_velocity * mean[:, 3],
|
||||
self._std_weight_velocity * mean[:, 3],
|
||||
1e-5 * np.ones_like(mean[:, 3]),
|
||||
self._std_weight_velocity * mean[:, 3]]
|
||||
sqr = np.square(np.r_[std_pos, std_vel]).T
|
||||
|
||||
motion_cov = []
|
||||
for i in range(len(mean)):
|
||||
motion_cov.append(np.diag(sqr[i]))
|
||||
motion_cov = np.asarray(motion_cov)
|
||||
|
||||
mean = np.dot(mean, self._motion_mat.T)
|
||||
left = np.dot(self._motion_mat, covariance).transpose((1, 0, 2))
|
||||
covariance = np.dot(left, self._motion_mat.T) + motion_cov
|
||||
|
||||
return mean, covariance
|
||||
|
||||
def update(self, mean, covariance, measurement):
|
||||
"""Run Kalman filter correction step.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
mean : ndarray
|
||||
The predicted state's mean vector (8 dimensional).
|
||||
covariance : ndarray
|
||||
The state's covariance matrix (8x8 dimensional).
|
||||
measurement : ndarray
|
||||
The 4 dimensional measurement vector (x, y, a, h), where (x, y)
|
||||
is the center position, a the aspect ratio, and h the height of the
|
||||
bounding box.
|
||||
|
||||
Returns
|
||||
-------
|
||||
(ndarray, ndarray)
|
||||
Returns the measurement-corrected state distribution.
|
||||
|
||||
"""
|
||||
projected_mean, projected_cov = self.project(mean, covariance)
|
||||
|
||||
chol_factor, lower = scipy.linalg.cho_factor(
|
||||
projected_cov, lower=True, check_finite=False)
|
||||
kalman_gain = scipy.linalg.cho_solve(
|
||||
(chol_factor, lower), np.dot(covariance, self._update_mat.T).T,
|
||||
check_finite=False).T
|
||||
innovation = measurement - projected_mean
|
||||
|
||||
new_mean = mean + np.dot(innovation, kalman_gain.T)
|
||||
new_covariance = covariance - np.linalg.multi_dot((
|
||||
kalman_gain, projected_cov, kalman_gain.T))
|
||||
return new_mean, new_covariance
|
||||
|
||||
def gating_distance(self, mean, covariance, measurements,
|
||||
only_position=False, metric='maha'):
|
||||
"""Compute gating distance between state distribution and measurements.
|
||||
A suitable distance threshold can be obtained from `chi2inv95`. If
|
||||
`only_position` is False, the chi-square distribution has 4 degrees of
|
||||
freedom, otherwise 2.
|
||||
Parameters
|
||||
----------
|
||||
mean : ndarray
|
||||
Mean vector over the state distribution (8 dimensional).
|
||||
covariance : ndarray
|
||||
Covariance of the state distribution (8x8 dimensional).
|
||||
measurements : ndarray
|
||||
An Nx4 dimensional matrix of N measurements, each in
|
||||
format (x, y, a, h) where (x, y) is the bounding box center
|
||||
position, a the aspect ratio, and h the height.
|
||||
only_position : Optional[bool]
|
||||
If True, distance computation is done with respect to the bounding
|
||||
box center position only.
|
||||
Returns
|
||||
-------
|
||||
ndarray
|
||||
Returns an array of length N, where the i-th element contains the
|
||||
squared Mahalanobis distance between (mean, covariance) and
|
||||
`measurements[i]`.
|
||||
"""
|
||||
mean, covariance = self.project(mean, covariance)
|
||||
if only_position:
|
||||
mean, covariance = mean[:2], covariance[:2, :2]
|
||||
measurements = measurements[:, :2]
|
||||
|
||||
d = measurements - mean
|
||||
if metric == 'gaussian':
|
||||
return np.sum(d * d, axis=1)
|
||||
elif metric == 'maha':
|
||||
cholesky_factor = np.linalg.cholesky(covariance)
|
||||
z = scipy.linalg.solve_triangular(
|
||||
cholesky_factor, d.T, lower=True, check_finite=False,
|
||||
overwrite_b=True)
|
||||
squared_maha = np.sum(z * z, axis=0)
|
||||
return squared_maha
|
||||
else:
|
||||
raise ValueError('invalid distance metric')
|
||||
481
example_utils/postprocess/matching.py
Normal file
481
example_utils/postprocess/matching.py
Normal file
@ -0,0 +1,481 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
import cv2
|
||||
import numpy as np
|
||||
#import scipy
|
||||
from scipy.spatial.distance import cdist
|
||||
|
||||
#from cython_bbox import bbox_overlaps as bbox_ious
|
||||
#import lap
|
||||
|
||||
def linear_sum_assignment(cost_matrix,
|
||||
extend_cost=False,
|
||||
cost_limit=np.inf,
|
||||
return_cost=True):
|
||||
"""Solve the linear sum assignment problem.
|
||||
The linear sum assignment problem is also known as minimum weight matching
|
||||
in bipartite graphs. A problem instance is described by a matrix C, where
|
||||
each C[i,j] is the cost of matching vertex i of the first partite set
|
||||
(a "worker") and vertex j of the second set (a "job"). The goal is to find
|
||||
a complete assignment of workers to jobs of minimal cost.
|
||||
Formally, let X be a boolean matrix where :math:`X[i,j] = 1` iff row i is
|
||||
assigned to column j. Then the optimal assignment has cost
|
||||
.. math::
|
||||
\min \sum_i \sum_j C_{i,j} X_{i,j}
|
||||
s.t. each row is assignment to at most one column, and each column to at
|
||||
most one row.
|
||||
This function can also solve a generalization of the classic assignment
|
||||
problem where the cost matrix is rectangular. If it has more rows than
|
||||
columns, then not every row needs to be assigned to a column, and vice
|
||||
versa.
|
||||
The method used is the Hungarian algorithm, also known as the Munkres or
|
||||
Kuhn-Munkres algorithm.
|
||||
Parameters
|
||||
----------
|
||||
cost_matrix : array
|
||||
The cost matrix of the bipartite graph.
|
||||
Returns
|
||||
-------
|
||||
row_ind, col_ind : array
|
||||
An array of row indices and one of corresponding column indices giving
|
||||
the optimal assignment. The cost of the assignment can be computed
|
||||
as ``cost_matrix[row_ind, col_ind].sum()``. The row indices will be
|
||||
sorted; in the case of a square cost matrix they will be equal to
|
||||
``numpy.arange(cost_matrix.shape[0])``.
|
||||
Notes
|
||||
-----
|
||||
.. versionadded:: 0.17.0
|
||||
Examples
|
||||
--------
|
||||
>>> cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
|
||||
>>> from scipy.optimize import linear_sum_assignment
|
||||
>>> row_ind, col_ind = linear_sum_assignment(cost)
|
||||
>>> col_ind
|
||||
array([1, 0, 2])
|
||||
>>> cost[row_ind, col_ind].sum()
|
||||
5
|
||||
References
|
||||
----------
|
||||
1. http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html
|
||||
2. Harold W. Kuhn. The Hungarian Method for the assignment problem.
|
||||
*Naval Research Logistics Quarterly*, 2:83-97, 1955.
|
||||
3. Harold W. Kuhn. Variants of the Hungarian method for assignment
|
||||
problems. *Naval Research Logistics Quarterly*, 3: 253-258, 1956.
|
||||
4. Munkres, J. Algorithms for the Assignment and Transportation Problems.
|
||||
*J. SIAM*, 5(1):32-38, March, 1957.
|
||||
5. https://en.wikipedia.org/wiki/Hungarian_algorithm
|
||||
"""
|
||||
cost_c = cost_matrix
|
||||
n_rows = cost_c.shape[0]
|
||||
n_cols = cost_c.shape[1]
|
||||
n = 0
|
||||
if n_rows == n_cols:
|
||||
n = n_rows
|
||||
else:
|
||||
if not extend_cost:
|
||||
raise ValueError(
|
||||
'Square cost array expected. If cost is intentionally '
|
||||
'non-square, pass extend_cost=True.')
|
||||
|
||||
if extend_cost or cost_limit < np.inf:
|
||||
n = n_rows + n_cols
|
||||
cost_c_extended = np.empty((n, n), dtype=np.double)
|
||||
if cost_limit < np.inf:
|
||||
cost_c_extended[:] = cost_limit / 2.
|
||||
else:
|
||||
cost_c_extended[:] = cost_c.max() + 1
|
||||
cost_c_extended[n_rows:, n_cols:] = 0
|
||||
cost_c_extended[:n_rows, :n_cols] = cost_c
|
||||
cost_matrix = cost_c_extended
|
||||
|
||||
cost_matrix = np.asarray(cost_matrix)
|
||||
if len(cost_matrix.shape) != 2:
|
||||
raise ValueError("expected a matrix (2-d array), got a %r array" %
|
||||
(cost_matrix.shape, ))
|
||||
|
||||
# The algorithm expects more columns than rows in the cost matrix.
|
||||
if cost_matrix.shape[1] < cost_matrix.shape[0]:
|
||||
cost_matrix = cost_matrix.T
|
||||
transposed = True
|
||||
else:
|
||||
transposed = False
|
||||
|
||||
state = _Hungary(cost_matrix)
|
||||
|
||||
# No need to bother with assignments if one of the dimensions
|
||||
# of the cost matrix is zero-length.
|
||||
step = None if 0 in cost_matrix.shape else _step1
|
||||
|
||||
while step is not None:
|
||||
step = step(state)
|
||||
|
||||
if transposed:
|
||||
marked = state.marked.T
|
||||
else:
|
||||
marked = state.marked
|
||||
return np.where(marked == 1)
|
||||
|
||||
|
||||
class _Hungary(object):
|
||||
"""State of the Hungarian algorithm.
|
||||
Parameters
|
||||
----------
|
||||
cost_matrix : 2D matrix
|
||||
The cost matrix. Must have shape[1] >= shape[0].
|
||||
"""
|
||||
|
||||
def __init__(self, cost_matrix):
|
||||
self.C = cost_matrix.copy()
|
||||
|
||||
n, m = self.C.shape
|
||||
self.row_uncovered = np.ones(n, dtype=bool)
|
||||
self.col_uncovered = np.ones(m, dtype=bool)
|
||||
self.Z0_r = 0
|
||||
self.Z0_c = 0
|
||||
self.path = np.zeros((n + m, 2), dtype=int)
|
||||
self.marked = np.zeros((n, m), dtype=int)
|
||||
|
||||
def _clear_covers(self):
|
||||
"""Clear all covered matrix cells"""
|
||||
self.row_uncovered[:] = True
|
||||
self.col_uncovered[:] = True
|
||||
|
||||
|
||||
# Individual steps of the algorithm follow, as a state machine: they return
|
||||
# the next step to be taken (function to be called), if any.
|
||||
|
||||
|
||||
def _step1(state):
|
||||
"""Steps 1 and 2 in the Wikipedia page."""
|
||||
|
||||
# Step 1: For each row of the matrix, find the smallest element and
|
||||
# subtract it from every element in its row.
|
||||
state.C -= state.C.min(axis=1)[:, np.newaxis]
|
||||
# Step 2: Find a zero (Z) in the resulting matrix. If there is no
|
||||
# starred zero in its row or column, star Z. Repeat for each element
|
||||
# in the matrix.
|
||||
for i, j in zip(*np.where(state.C == 0)):
|
||||
if state.col_uncovered[j] and state.row_uncovered[i]:
|
||||
state.marked[i, j] = 1
|
||||
state.col_uncovered[j] = False
|
||||
state.row_uncovered[i] = False
|
||||
|
||||
state._clear_covers()
|
||||
return _step3
|
||||
|
||||
|
||||
def _step3(state):
|
||||
"""
|
||||
Cover each column containing a starred zero. If n columns are covered,
|
||||
the starred zeros describe a complete set of unique assignments.
|
||||
In this case, Go to DONE, otherwise, Go to Step 4.
|
||||
"""
|
||||
marked = (state.marked == 1)
|
||||
state.col_uncovered[np.any(marked, axis=0)] = False
|
||||
|
||||
if marked.sum() < state.C.shape[0]:
|
||||
return _step4
|
||||
|
||||
|
||||
def _step4(state):
|
||||
"""
|
||||
Find a noncovered zero and prime it. If there is no starred zero
|
||||
in the row containing this primed zero, Go to Step 5. Otherwise,
|
||||
cover this row and uncover the column containing the starred
|
||||
zero. Continue in this manner until there are no uncovered zeros
|
||||
left. Save the smallest uncovered value and Go to Step 6.
|
||||
"""
|
||||
# We convert to int as numpy operations are faster on int
|
||||
C = (state.C == 0).astype(int)
|
||||
covered_C = C * state.row_uncovered[:, np.newaxis]
|
||||
covered_C *= np.asarray(state.col_uncovered, dtype=int)
|
||||
n = state.C.shape[0]
|
||||
m = state.C.shape[1]
|
||||
|
||||
while True:
|
||||
# Find an uncovered zero
|
||||
row, col = np.unravel_index(np.argmax(covered_C), (n, m))
|
||||
if covered_C[row, col] == 0:
|
||||
return _step6
|
||||
else:
|
||||
state.marked[row, col] = 2
|
||||
# Find the first starred element in the row
|
||||
star_col = np.argmax(state.marked[row] == 1)
|
||||
if state.marked[row, star_col] != 1:
|
||||
# Could not find one
|
||||
state.Z0_r = row
|
||||
state.Z0_c = col
|
||||
return _step5
|
||||
else:
|
||||
col = star_col
|
||||
state.row_uncovered[row] = False
|
||||
state.col_uncovered[col] = True
|
||||
covered_C[:,
|
||||
col] = C[:, col] * (np.asarray(state.row_uncovered,
|
||||
dtype=int))
|
||||
covered_C[row] = 0
|
||||
|
||||
|
||||
def _step5(state):
|
||||
"""
|
||||
Construct a series of alternating primed and starred zeros as follows.
|
||||
Let Z0 represent the uncovered primed zero found in Step 4.
|
||||
Let Z1 denote the starred zero in the column of Z0 (if any).
|
||||
Let Z2 denote the primed zero in the row of Z1 (there will always be one).
|
||||
Continue until the series terminates at a primed zero that has no starred
|
||||
zero in its column. Unstar each starred zero of the series, star each
|
||||
primed zero of the series, erase all primes and uncover every line in the
|
||||
matrix. Return to Step 3
|
||||
"""
|
||||
count = 0
|
||||
path = state.path
|
||||
path[count, 0] = state.Z0_r
|
||||
path[count, 1] = state.Z0_c
|
||||
|
||||
while True:
|
||||
# Find the first starred element in the col defined by
|
||||
# the path.
|
||||
row = np.argmax(state.marked[:, path[count, 1]] == 1)
|
||||
if state.marked[row, path[count, 1]] != 1:
|
||||
# Could not find one
|
||||
break
|
||||
else:
|
||||
count += 1
|
||||
path[count, 0] = row
|
||||
path[count, 1] = path[count - 1, 1]
|
||||
|
||||
# Find the first prime element in the row defined by the
|
||||
# first path step
|
||||
col = np.argmax(state.marked[path[count, 0]] == 2)
|
||||
if state.marked[row, col] != 2:
|
||||
col = -1
|
||||
count += 1
|
||||
path[count, 0] = path[count - 1, 0]
|
||||
path[count, 1] = col
|
||||
|
||||
# Convert paths
|
||||
for i in range(count + 1):
|
||||
if state.marked[path[i, 0], path[i, 1]] == 1:
|
||||
state.marked[path[i, 0], path[i, 1]] = 0
|
||||
else:
|
||||
state.marked[path[i, 0], path[i, 1]] = 1
|
||||
|
||||
state._clear_covers()
|
||||
# Erase all prime markings
|
||||
state.marked[state.marked == 2] = 0
|
||||
return _step3
|
||||
|
||||
|
||||
def _step6(state):
|
||||
"""
|
||||
Add the value found in Step 4 to every element of each covered row,
|
||||
and subtract it from every element of each uncovered column.
|
||||
Return to Step 4 without altering any stars, primes, or covered lines.
|
||||
"""
|
||||
# the smallest uncovered value in the matrix
|
||||
if np.any(state.row_uncovered) and np.any(state.col_uncovered):
|
||||
minval = np.min(state.C[state.row_uncovered], axis=0)
|
||||
minval = np.min(minval[state.col_uncovered])
|
||||
state.C[~state.row_uncovered] += minval
|
||||
state.C[:, state.col_uncovered] -= minval
|
||||
return _step4
|
||||
|
||||
|
||||
def bbox_ious(boxes, query_boxes):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
boxes: (N, 4) ndarray of float
|
||||
query_boxes: (K, 4) ndarray of float
|
||||
Returns
|
||||
-------
|
||||
overlaps: (N, K) ndarray of overlap between boxes and query_boxes
|
||||
"""
|
||||
DTYPE = np.float32
|
||||
N = boxes.shape[0]
|
||||
K = query_boxes.shape[0]
|
||||
overlaps = np.zeros((N, K), dtype=DTYPE)
|
||||
|
||||
for k in range(K):
|
||||
box_area = ((query_boxes[k, 2] - query_boxes[k, 0] + 1) *
|
||||
(query_boxes[k, 3] - query_boxes[k, 1] + 1))
|
||||
for n in range(N):
|
||||
iw = (min(boxes[n, 2], query_boxes[k, 2]) -
|
||||
max(boxes[n, 0], query_boxes[k, 0]) + 1)
|
||||
if iw > 0:
|
||||
ih = (min(boxes[n, 3], query_boxes[k, 3]) -
|
||||
max(boxes[n, 1], query_boxes[k, 1]) + 1)
|
||||
if ih > 0:
|
||||
ua = float((boxes[n, 2] - boxes[n, 0] + 1) *
|
||||
(boxes[n, 3] - boxes[n, 1] + 1) + box_area -
|
||||
iw * ih)
|
||||
overlaps[n, k] = iw * ih / ua
|
||||
return overlaps
|
||||
|
||||
|
||||
|
||||
chi2inv95 = {
|
||||
1: 3.8415,
|
||||
2: 5.9915,
|
||||
3: 7.8147,
|
||||
4: 9.4877,
|
||||
5: 11.070,
|
||||
6: 12.592,
|
||||
7: 14.067,
|
||||
8: 15.507,
|
||||
9: 16.919}
|
||||
|
||||
def linear_assignment(cost_matrix, thresh):
|
||||
if cost_matrix.size == 0:
|
||||
return np.empty((0, 2), dtype=int), tuple(range(cost_matrix.shape[0])), tuple(range(cost_matrix.shape[1]))
|
||||
'''
|
||||
matches, unmatched_a, unmatched_b = [], [], []
|
||||
# https://blog.csdn.net/u014386899/article/details/109224746
|
||||
#https://github.com/gatagat/lap
|
||||
# https://github.com/gatagat/lap/blob/c2b6309ba246d18205a71228cdaea67210e1a039/lap/lapmod.py
|
||||
cost, x, y = lap.lapjv(cost_matrix, extend_cost=True, cost_limit=thresh)
|
||||
#extend_cost: whether or not extend a non-square matrix [default: False]
|
||||
#cost_limit: an upper limit for a cost of a single assignment
|
||||
# [default: np.inf]
|
||||
for ix, mx in enumerate(x):
|
||||
if mx >= 0:
|
||||
matches.append([ix, mx])
|
||||
unmatched_a = np.where(x < 0)[0]
|
||||
unmatched_b = np.where(y < 0)[0]
|
||||
matches = np.asarray(matches)
|
||||
return matches, unmatched_a, unmatched_b
|
||||
'''
|
||||
cost_matrix_r, cost_matrix_c = cost_matrix.shape[:2]
|
||||
r, c = linear_sum_assignment(cost_matrix,
|
||||
extend_cost=True,
|
||||
cost_limit=thresh)
|
||||
|
||||
sorted_c = sorted(range(len(c)), key=lambda k: c[k])
|
||||
sorted_c = sorted_c[:cost_matrix_c]
|
||||
sorted_c = np.asarray(sorted_c)
|
||||
matches_c = []
|
||||
for ix, mx in enumerate(c):
|
||||
if mx < cost_matrix_c and ix < cost_matrix_r:
|
||||
matches_c.append([ix, mx])
|
||||
cut_c = c[:cost_matrix_r]
|
||||
unmatched_r = np.where(cut_c >= cost_matrix_c)[0]
|
||||
unmatched_c = np.where(sorted_c >= cost_matrix_r)[0]
|
||||
matches_c = np.asarray(matches_c)
|
||||
return matches_c, unmatched_r, unmatched_c
|
||||
|
||||
|
||||
|
||||
def computeIOU(rec1, rec2):
|
||||
cx1, cy1, cx2, cy2 = rec1
|
||||
gx1, gy1, gx2, gy2 = rec2
|
||||
S_rec1 = (cx2 - cx1 + 1) * (cy2 - cy1 + 1)
|
||||
S_rec2 = (gx2 - gx1 + 1) * (gy2 - gy1 + 1)
|
||||
x1 = max(cx1, gx1)
|
||||
y1 = max(cy1, gy1)
|
||||
x2 = min(cx2, gx2)
|
||||
y2 = min(cy2, gy2)
|
||||
|
||||
w = max(0, x2 - x1 + 1)
|
||||
h = max(0, y2 - y1 + 1)
|
||||
area = w * h
|
||||
iou = area / (S_rec1 + S_rec2 - area)
|
||||
return iou
|
||||
|
||||
|
||||
def ious(atlbrs, btlbrs):
|
||||
"""
|
||||
Compute cost based on IoU
|
||||
:type atlbrs: list[tlbr] | np.ndarray
|
||||
:type atlbrs: list[tlbr] | np.ndarray
|
||||
|
||||
:rtype ious np.ndarray
|
||||
"""
|
||||
ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float32)
|
||||
if ious.size == 0:
|
||||
return ious
|
||||
|
||||
ious = bbox_ious(
|
||||
np.ascontiguousarray(atlbrs, dtype=np.float32),
|
||||
np.ascontiguousarray(btlbrs, dtype=np.float32)
|
||||
)
|
||||
|
||||
return ious
|
||||
|
||||
|
||||
def iou_distance(atracks, btracks):
|
||||
"""
|
||||
Compute cost based on IoU
|
||||
:type atracks: list[STrack]
|
||||
:type btracks: list[STrack]
|
||||
|
||||
:rtype cost_matrix np.ndarray
|
||||
"""
|
||||
|
||||
if (len(atracks)>0 and isinstance(atracks[0], np.ndarray)) or (len(btracks) > 0 and isinstance(btracks[0], np.ndarray)):
|
||||
atlbrs = atracks
|
||||
btlbrs = btracks
|
||||
else:
|
||||
atlbrs = [track.tlbr for track in atracks]
|
||||
btlbrs = [track.tlbr for track in btracks]
|
||||
_ious = ious(atlbrs, btlbrs)
|
||||
cost_matrix = 1 - _ious
|
||||
|
||||
return cost_matrix
|
||||
|
||||
#https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html
|
||||
|
||||
def embedding_distance(tracks, detections, metric='cosine'):
|
||||
"""
|
||||
:param tracks: list[STrack]
|
||||
:param detections: list[BaseTrack]
|
||||
:param metric:
|
||||
:return: cost_matrix np.ndarray
|
||||
"""
|
||||
|
||||
cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32)
|
||||
if cost_matrix.size == 0:
|
||||
return cost_matrix
|
||||
det_features = np.asarray([track.curr_feat for track in detections], dtype=np.float32)
|
||||
#for i, track in enumerate(tracks):
|
||||
#cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric))
|
||||
track_features = np.asarray([track.smooth_feat for track in tracks], dtype=np.float32)
|
||||
cost_matrix = np.maximum(0.0, cdist(track_features, det_features, metric)) # Nomalized features
|
||||
return cost_matrix
|
||||
|
||||
|
||||
def gate_cost_matrix(kf, cost_matrix, tracks, detections, only_position=False):
|
||||
if cost_matrix.size == 0:
|
||||
return cost_matrix
|
||||
gating_dim = 2 if only_position else 4
|
||||
gating_threshold = chi2inv95[gating_dim]
|
||||
measurements = np.asarray([det.to_xyah() for det in detections])
|
||||
for row, track in enumerate(tracks):
|
||||
gating_distance = kf.gating_distance(
|
||||
track.mean, track.covariance, measurements, only_position)
|
||||
cost_matrix[row, gating_distance > gating_threshold] = np.inf
|
||||
return cost_matrix
|
||||
|
||||
|
||||
def fuse_motion(kf, cost_matrix, tracks, detections, only_position=False, lambda_=0.98):
|
||||
if cost_matrix.size == 0:
|
||||
return cost_matrix
|
||||
gating_dim = 2 if only_position else 4
|
||||
gating_threshold = chi2inv95[gating_dim]
|
||||
measurements = np.asarray([det.to_xyah() for det in detections])
|
||||
for row, track in enumerate(tracks):
|
||||
gating_distance = kf.gating_distance(
|
||||
track.mean, track.covariance, measurements, only_position, metric='maha')
|
||||
cost_matrix[row, gating_distance > gating_threshold] = np.inf
|
||||
cost_matrix[row] = lambda_ * cost_matrix[row] + (1 - lambda_) * gating_distance
|
||||
return cost_matrix
|
||||
|
||||
def fuse_score(cost_matrix, detections):
|
||||
if cost_matrix.size == 0:
|
||||
return cost_matrix
|
||||
iou_sim = 1 - cost_matrix
|
||||
det_scores = np.array([det.score for det in detections])
|
||||
det_scores = np.expand_dims(det_scores, axis=0).repeat(cost_matrix.shape[0], axis=0)
|
||||
fuse_sim = iou_sim * det_scores
|
||||
fuse_cost = 1 - fuse_sim
|
||||
return fuse_cost
|
||||
142
force_cleanup.py
Normal file
142
force_cleanup.py
Normal file
@ -0,0 +1,142 @@
|
||||
"""
|
||||
Force cleanup of all app data and processes
|
||||
"""
|
||||
|
||||
import psutil
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import tempfile
|
||||
|
||||
def kill_all_python_processes():
|
||||
"""Force kill ALL Python processes (use with caution)"""
|
||||
killed_processes = []
|
||||
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
||||
try:
|
||||
if 'python' in proc.info['name'].lower():
|
||||
print(f"Killing Python process: {proc.info['pid']} - {proc.info['name']}")
|
||||
proc.kill()
|
||||
killed_processes.append(proc.info['pid'])
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
pass
|
||||
|
||||
if killed_processes:
|
||||
print(f"Killed {len(killed_processes)} Python processes")
|
||||
time.sleep(3) # Give more time for cleanup
|
||||
else:
|
||||
print("No Python processes found")
|
||||
|
||||
def clear_shared_memory():
|
||||
"""Clear Qt shared memory"""
|
||||
try:
|
||||
from PyQt5.QtCore import QSharedMemory
|
||||
app_names = ["Cluster4NPU", "cluster4npu", "main"]
|
||||
|
||||
for app_name in app_names:
|
||||
shared_mem = QSharedMemory(app_name)
|
||||
if shared_mem.attach():
|
||||
shared_mem.detach()
|
||||
print(f"Cleared shared memory for: {app_name}")
|
||||
except Exception as e:
|
||||
print(f"Could not clear shared memory: {e}")
|
||||
|
||||
def clean_all_temp_files():
|
||||
"""Remove all possible lock and temp files"""
|
||||
possible_files = [
|
||||
'app.lock',
|
||||
'.app.lock',
|
||||
'cluster4npu.lock',
|
||||
'.cluster4npu.lock',
|
||||
'main.lock',
|
||||
'.main.lock'
|
||||
]
|
||||
|
||||
# Check in current directory
|
||||
current_dir_files = []
|
||||
for filename in possible_files:
|
||||
filepath = os.path.join(os.getcwd(), filename)
|
||||
if os.path.exists(filepath):
|
||||
try:
|
||||
os.remove(filepath)
|
||||
current_dir_files.append(filepath)
|
||||
print(f"Removed: {filepath}")
|
||||
except Exception as e:
|
||||
print(f"Could not remove {filepath}: {e}")
|
||||
|
||||
# Check in temp directory
|
||||
temp_dir = tempfile.gettempdir()
|
||||
temp_files = []
|
||||
for filename in possible_files:
|
||||
filepath = os.path.join(temp_dir, filename)
|
||||
if os.path.exists(filepath):
|
||||
try:
|
||||
os.remove(filepath)
|
||||
temp_files.append(filepath)
|
||||
print(f"Removed: {filepath}")
|
||||
except Exception as e:
|
||||
print(f"Could not remove {filepath}: {e}")
|
||||
|
||||
# Check in user home directory
|
||||
home_dir = os.path.expanduser('~')
|
||||
home_files = []
|
||||
for filename in possible_files:
|
||||
filepath = os.path.join(home_dir, filename)
|
||||
if os.path.exists(filepath):
|
||||
try:
|
||||
os.remove(filepath)
|
||||
home_files.append(filepath)
|
||||
print(f"Removed: {filepath}")
|
||||
except Exception as e:
|
||||
print(f"Could not remove {filepath}: {e}")
|
||||
|
||||
total_removed = len(current_dir_files) + len(temp_files) + len(home_files)
|
||||
if total_removed == 0:
|
||||
print("No lock files found")
|
||||
|
||||
def force_unlock_files():
|
||||
"""Try to unlock any locked files"""
|
||||
try:
|
||||
# On Windows, try to reset file handles
|
||||
import subprocess
|
||||
result = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq python.exe'],
|
||||
capture_output=True, text=True, timeout=10)
|
||||
if result.returncode == 0:
|
||||
lines = result.stdout.strip().split('\n')
|
||||
for line in lines[3:]: # Skip header lines
|
||||
if 'python.exe' in line:
|
||||
parts = line.split()
|
||||
if len(parts) >= 2:
|
||||
pid = parts[1]
|
||||
try:
|
||||
subprocess.run(['taskkill', '/F', '/PID', pid], timeout=5)
|
||||
print(f"Force killed PID: {pid}")
|
||||
except:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"Could not force unlock files: {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("FORCE CLEANUP - This will kill ALL Python processes!")
|
||||
print("=" * 60)
|
||||
|
||||
response = input("Are you sure? This will close ALL Python programs (y/N): ")
|
||||
if response.lower() in ['y', 'yes']:
|
||||
print("\n1. Killing all Python processes...")
|
||||
kill_all_python_processes()
|
||||
|
||||
print("\n2. Clearing shared memory...")
|
||||
clear_shared_memory()
|
||||
|
||||
print("\n3. Removing lock files...")
|
||||
clean_all_temp_files()
|
||||
|
||||
print("\n4. Force unlocking files...")
|
||||
force_unlock_files()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("FORCE CLEANUP COMPLETE!")
|
||||
print("All Python processes killed and lock files removed.")
|
||||
print("You can now start the app with 'python main.py'")
|
||||
else:
|
||||
print("Cleanup cancelled.")
|
||||
121
gentle_cleanup.py
Normal file
121
gentle_cleanup.py
Normal file
@ -0,0 +1,121 @@
|
||||
"""
|
||||
Gentle cleanup of app data (safer approach)
|
||||
"""
|
||||
|
||||
import psutil
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
def find_and_kill_app_processes():
|
||||
"""Find and kill only the Cluster4NPU app processes"""
|
||||
killed_processes = []
|
||||
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline', 'cwd']):
|
||||
try:
|
||||
if 'python' in proc.info['name'].lower():
|
||||
cmdline = proc.info['cmdline']
|
||||
cwd = proc.info['cwd']
|
||||
|
||||
# Check if this is our app
|
||||
if (cmdline and
|
||||
(any('main.py' in arg for arg in cmdline) or
|
||||
any('cluster4npu' in arg.lower() for arg in cmdline) or
|
||||
(cwd and 'cluster4npu' in cwd.lower()))):
|
||||
|
||||
print(f"Found app process: {proc.info['pid']}")
|
||||
print(f" Command: {' '.join(cmdline) if cmdline else 'N/A'}")
|
||||
print(f" Working dir: {cwd}")
|
||||
|
||||
# Try gentle termination first
|
||||
proc.terminate()
|
||||
time.sleep(2)
|
||||
|
||||
# If still running, force kill
|
||||
if proc.is_running():
|
||||
proc.kill()
|
||||
print(f" Force killed: {proc.info['pid']}")
|
||||
else:
|
||||
print(f" Gently terminated: {proc.info['pid']}")
|
||||
|
||||
killed_processes.append(proc.info['pid'])
|
||||
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
pass
|
||||
|
||||
if killed_processes:
|
||||
print(f"\nKilled {len(killed_processes)} app processes")
|
||||
time.sleep(2)
|
||||
else:
|
||||
print("No app processes found")
|
||||
|
||||
def clear_app_locks():
|
||||
"""Remove only app-specific lock files"""
|
||||
app_specific_locks = [
|
||||
'cluster4npu.lock',
|
||||
'.cluster4npu.lock',
|
||||
'Cluster4NPU.lock',
|
||||
'main.lock',
|
||||
'.main.lock'
|
||||
]
|
||||
|
||||
locations = [
|
||||
os.getcwd(), # Current directory
|
||||
os.path.expanduser('~'), # User home
|
||||
os.path.join(os.path.expanduser('~'), '.cluster4npu'), # App data dir
|
||||
'C:\\temp' if os.name == 'nt' else '/tmp', # System temp
|
||||
]
|
||||
|
||||
removed_files = []
|
||||
|
||||
for location in locations:
|
||||
if not os.path.exists(location):
|
||||
continue
|
||||
|
||||
for lock_name in app_specific_locks:
|
||||
lock_path = os.path.join(location, lock_name)
|
||||
if os.path.exists(lock_path):
|
||||
try:
|
||||
os.remove(lock_path)
|
||||
removed_files.append(lock_path)
|
||||
print(f"Removed lock: {lock_path}")
|
||||
except Exception as e:
|
||||
print(f"Could not remove {lock_path}: {e}")
|
||||
|
||||
if not removed_files:
|
||||
print("No lock files found")
|
||||
|
||||
def reset_shared_memory():
|
||||
"""Reset Qt shared memory for the app"""
|
||||
try:
|
||||
from PyQt5.QtCore import QSharedMemory
|
||||
|
||||
shared_mem = QSharedMemory("Cluster4NPU")
|
||||
if shared_mem.attach():
|
||||
print("Found shared memory, detaching...")
|
||||
shared_mem.detach()
|
||||
|
||||
# Try to create and destroy to fully reset
|
||||
if shared_mem.create(1):
|
||||
shared_mem.detach()
|
||||
print("Reset shared memory")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Could not reset shared memory: {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Gentle App Cleanup")
|
||||
print("=" * 30)
|
||||
|
||||
print("\n1. Looking for app processes...")
|
||||
find_and_kill_app_processes()
|
||||
|
||||
print("\n2. Clearing app locks...")
|
||||
clear_app_locks()
|
||||
|
||||
print("\n3. Resetting shared memory...")
|
||||
reset_shared_memory()
|
||||
|
||||
print("\n" + "=" * 30)
|
||||
print("Cleanup complete!")
|
||||
print("You can now start the app with 'python main.py'")
|
||||
66
kill_app_processes.py
Normal file
66
kill_app_processes.py
Normal file
@ -0,0 +1,66 @@
|
||||
"""
|
||||
Kill any running app processes and clean up locks
|
||||
"""
|
||||
|
||||
import psutil
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
def kill_python_processes():
|
||||
"""Kill any Python processes that might be running the app"""
|
||||
killed_processes = []
|
||||
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
||||
try:
|
||||
# Check if it's a Python process
|
||||
if 'python' in proc.info['name'].lower():
|
||||
cmdline = proc.info['cmdline']
|
||||
if cmdline and any('main.py' in arg for arg in cmdline):
|
||||
print(f"Killing process: {proc.info['pid']} - {' '.join(cmdline)}")
|
||||
proc.kill()
|
||||
killed_processes.append(proc.info['pid'])
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
||||
pass
|
||||
|
||||
if killed_processes:
|
||||
print(f"Killed {len(killed_processes)} Python processes")
|
||||
time.sleep(2) # Give processes time to cleanup
|
||||
else:
|
||||
print("No running app processes found")
|
||||
|
||||
def clean_lock_files():
|
||||
"""Remove any lock files that might prevent app startup"""
|
||||
possible_lock_files = [
|
||||
'app.lock',
|
||||
'.app.lock',
|
||||
'cluster4npu.lock',
|
||||
os.path.expanduser('~/.cluster4npu.lock'),
|
||||
'/tmp/cluster4npu.lock',
|
||||
'C:\\temp\\cluster4npu.lock'
|
||||
]
|
||||
|
||||
removed_files = []
|
||||
for lock_file in possible_lock_files:
|
||||
try:
|
||||
if os.path.exists(lock_file):
|
||||
os.remove(lock_file)
|
||||
removed_files.append(lock_file)
|
||||
print(f"Removed lock file: {lock_file}")
|
||||
except Exception as e:
|
||||
print(f"Could not remove {lock_file}: {e}")
|
||||
|
||||
if removed_files:
|
||||
print(f"Removed {len(removed_files)} lock files")
|
||||
else:
|
||||
print("No lock files found")
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Cleaning up app processes and lock files...")
|
||||
print("=" * 50)
|
||||
|
||||
kill_python_processes()
|
||||
clean_lock_files()
|
||||
|
||||
print("=" * 50)
|
||||
print("Cleanup complete! You can now start the app with 'python main.py'")
|
||||
265
main.py
265
main.py
@ -23,8 +23,8 @@ import sys
|
||||
import os
|
||||
import tempfile
|
||||
from PyQt5.QtWidgets import QApplication, QMessageBox
|
||||
from PyQt5.QtGui import QFont
|
||||
from PyQt5.QtCore import Qt, QSharedMemory
|
||||
from PyQt5.QtGui import QFont, QTextCursor # QTextCursor import registers it with Qt meta-type system
|
||||
from PyQt5.QtCore import Qt, QSharedMemory, QCoreApplication
|
||||
|
||||
# Import fcntl only on Unix-like systems
|
||||
try:
|
||||
@ -41,67 +41,201 @@ from ui.windows.login import DashboardLogin
|
||||
|
||||
|
||||
class SingleInstance:
|
||||
"""Ensure only one instance of the application can run."""
|
||||
"""Enhanced single instance handler with better error recovery."""
|
||||
|
||||
def __init__(self, app_name="Cluster4NPU"):
|
||||
self.app_name = app_name
|
||||
self.shared_memory = QSharedMemory(app_name)
|
||||
self.lock_file = None
|
||||
self.lock_fd = None
|
||||
self.process_check_enabled = True
|
||||
|
||||
def is_running(self):
|
||||
"""Check if another instance is already running."""
|
||||
# Try to create shared memory
|
||||
if self.shared_memory.attach():
|
||||
# Another instance is already running
|
||||
"""Check if another instance is already running with recovery mechanisms."""
|
||||
# First, try to detect and clean up stale instances
|
||||
if self._detect_and_cleanup_stale_instances():
|
||||
print("Cleaned up stale application instances")
|
||||
|
||||
# Try shared memory approach
|
||||
if self._check_shared_memory():
|
||||
return True
|
||||
|
||||
# Try to create the shared memory
|
||||
if not self.shared_memory.create(1):
|
||||
# Failed to create, likely another instance exists
|
||||
# Try file locking approach
|
||||
if self._check_file_lock():
|
||||
return True
|
||||
|
||||
# Also use file locking as backup (works better on some systems)
|
||||
if HAS_FCNTL:
|
||||
try:
|
||||
self.lock_file = os.path.join(tempfile.gettempdir(), f"{self.app_name}.lock")
|
||||
self.lock_fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
fcntl.lockf(self.lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except (OSError, IOError):
|
||||
# Another instance is running
|
||||
if self.lock_fd:
|
||||
os.close(self.lock_fd)
|
||||
return True
|
||||
else:
|
||||
# On Windows, try simple file creation
|
||||
try:
|
||||
self.lock_file = os.path.join(tempfile.gettempdir(), f"{self.app_name}.lock")
|
||||
self.lock_fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
except (OSError, IOError):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def cleanup(self):
|
||||
"""Clean up resources."""
|
||||
if self.shared_memory.isAttached():
|
||||
self.shared_memory.detach()
|
||||
def _detect_and_cleanup_stale_instances(self):
|
||||
"""Detect and clean up stale instances that might have crashed."""
|
||||
cleaned_up = False
|
||||
|
||||
if self.lock_fd:
|
||||
try:
|
||||
try:
|
||||
import psutil
|
||||
|
||||
# Check if there are any actual running processes
|
||||
app_processes = []
|
||||
for proc in psutil.process_iter(['pid', 'name', 'cmdline', 'create_time']):
|
||||
try:
|
||||
if 'python' in proc.info['name'].lower():
|
||||
cmdline = proc.info['cmdline']
|
||||
if cmdline and any('main.py' in arg for arg in cmdline):
|
||||
app_processes.append(proc)
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
||||
continue
|
||||
|
||||
# If no actual app processes are running, clean up stale locks
|
||||
if not app_processes:
|
||||
cleaned_up = self._force_cleanup_locks()
|
||||
|
||||
except ImportError:
|
||||
# psutil not available, try basic cleanup
|
||||
cleaned_up = self._force_cleanup_locks()
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not detect stale instances: {e}")
|
||||
|
||||
return cleaned_up
|
||||
|
||||
def _force_cleanup_locks(self):
|
||||
"""Force cleanup of stale locks."""
|
||||
cleaned_up = False
|
||||
|
||||
# Try to clean up shared memory
|
||||
try:
|
||||
if self.shared_memory.attach():
|
||||
self.shared_memory.detach()
|
||||
cleaned_up = True
|
||||
except:
|
||||
pass
|
||||
|
||||
# Try to clean up lock file
|
||||
try:
|
||||
lock_file = os.path.join(tempfile.gettempdir(), f"{self.app_name}.lock")
|
||||
if os.path.exists(lock_file):
|
||||
os.unlink(lock_file)
|
||||
cleaned_up = True
|
||||
except:
|
||||
pass
|
||||
|
||||
return cleaned_up
|
||||
|
||||
def _check_shared_memory(self):
|
||||
"""Check shared memory for running instance."""
|
||||
try:
|
||||
# Try to attach to existing shared memory
|
||||
if self.shared_memory.attach():
|
||||
# Check if the shared memory is actually valid
|
||||
try:
|
||||
# Try to read from it to verify it's not corrupted
|
||||
data = self.shared_memory.data()
|
||||
if data is not None:
|
||||
return True # Valid instance found
|
||||
else:
|
||||
# Corrupted shared memory, clean it up
|
||||
self.shared_memory.detach()
|
||||
except:
|
||||
# Error reading, clean up
|
||||
self.shared_memory.detach()
|
||||
|
||||
# Try to create new shared memory
|
||||
if not self.shared_memory.create(1):
|
||||
# Could not create, but attachment failed too - might be corruption
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: Shared memory check failed: {e}")
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
def _check_file_lock(self):
|
||||
"""Check file lock for running instance."""
|
||||
try:
|
||||
self.lock_file = os.path.join(tempfile.gettempdir(), f"{self.app_name}.lock")
|
||||
|
||||
if HAS_FCNTL:
|
||||
# Unix-like systems
|
||||
try:
|
||||
self.lock_fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
fcntl.lockf(self.lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
return False # Successfully locked, no other instance
|
||||
except (OSError, IOError):
|
||||
return True # Could not lock, another instance exists
|
||||
else:
|
||||
# Windows
|
||||
try:
|
||||
self.lock_fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
return False # Successfully created, no other instance
|
||||
except (OSError, IOError):
|
||||
# File exists, but check if the process that created it is still running
|
||||
if self._is_lock_file_stale():
|
||||
# Stale lock file, remove it and try again
|
||||
try:
|
||||
os.unlink(self.lock_file)
|
||||
self.lock_fd = os.open(self.lock_file, os.O_CREAT | os.O_EXCL | os.O_RDWR)
|
||||
return False
|
||||
except:
|
||||
pass
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Warning: File lock check failed: {e}")
|
||||
return False
|
||||
|
||||
def _is_lock_file_stale(self):
|
||||
"""Check if the lock file is from a stale process."""
|
||||
try:
|
||||
if not os.path.exists(self.lock_file):
|
||||
return True
|
||||
|
||||
# Check file age - if older than 5 minutes, consider it stale
|
||||
import time
|
||||
file_age = time.time() - os.path.getmtime(self.lock_file)
|
||||
if file_age > 300: # 5 minutes
|
||||
return True
|
||||
|
||||
# On Windows, we can't easily check if the process is still running
|
||||
# without additional information, so we rely on age check
|
||||
return False
|
||||
|
||||
except:
|
||||
return True # If we can't check, assume it's stale
|
||||
|
||||
def cleanup(self):
|
||||
"""Enhanced cleanup with better error handling."""
|
||||
try:
|
||||
if self.shared_memory.isAttached():
|
||||
self.shared_memory.detach()
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not detach shared memory: {e}")
|
||||
|
||||
try:
|
||||
if self.lock_fd is not None:
|
||||
if HAS_FCNTL:
|
||||
fcntl.lockf(self.lock_fd, fcntl.LOCK_UN)
|
||||
os.close(self.lock_fd)
|
||||
self.lock_fd = None
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not close lock file descriptor: {e}")
|
||||
|
||||
try:
|
||||
if self.lock_file and os.path.exists(self.lock_file):
|
||||
os.unlink(self.lock_file)
|
||||
except:
|
||||
pass
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not remove lock file: {e}")
|
||||
|
||||
def force_cleanup(self):
|
||||
"""Force cleanup of all locks (use when app crashed)."""
|
||||
print("Force cleaning up application locks...")
|
||||
self._force_cleanup_locks()
|
||||
print("Force cleanup completed")
|
||||
|
||||
|
||||
def setup_application():
|
||||
"""Initialize and configure the QApplication."""
|
||||
# Enable high DPI support BEFORE creating QApplication
|
||||
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
|
||||
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
|
||||
# High DPI attributes must be set before QApplication is created.
|
||||
# They are set in main() before the first QApplication instantiation.
|
||||
# Do NOT set them here — QApplication already exists at this point.
|
||||
|
||||
# Create QApplication if it doesn't exist
|
||||
if not QApplication.instance():
|
||||
@ -125,19 +259,62 @@ def setup_application():
|
||||
|
||||
def main():
|
||||
"""Main application entry point."""
|
||||
# Create a minimal QApplication first for the message box
|
||||
# Ensure high DPI attributes are set BEFORE any QApplication is created
|
||||
try:
|
||||
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
|
||||
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
|
||||
except Exception:
|
||||
pass
|
||||
# Check for command line arguments
|
||||
if '--force-cleanup' in sys.argv or '--cleanup' in sys.argv:
|
||||
print("Force cleanup mode enabled")
|
||||
single_instance = SingleInstance()
|
||||
single_instance.force_cleanup()
|
||||
print("Cleanup completed. You can now start the application normally.")
|
||||
sys.exit(0)
|
||||
|
||||
# Check for help argument
|
||||
if '--help' in sys.argv or '-h' in sys.argv:
|
||||
print("Cluster4NPU Application")
|
||||
print("Usage: python main.py [options]")
|
||||
print("Options:")
|
||||
print(" --force-cleanup, --cleanup Force cleanup of stale application locks")
|
||||
print(" --help, -h Show this help message")
|
||||
sys.exit(0)
|
||||
|
||||
# Create a minimal QApplication first for the message box (attributes already set above)
|
||||
temp_app = QApplication(sys.argv) if not QApplication.instance() else QApplication.instance()
|
||||
|
||||
# Check for single instance
|
||||
single_instance = SingleInstance()
|
||||
|
||||
if single_instance.is_running():
|
||||
QMessageBox.warning(
|
||||
reply = QMessageBox.question(
|
||||
None,
|
||||
"Application Already Running",
|
||||
"Cluster4NPU is already running. Please check your taskbar or system tray.",
|
||||
"Cluster4NPU is already running. \n\n"
|
||||
"Would you like to:\n"
|
||||
"• Click 'Yes' to force cleanup and restart\n"
|
||||
"• Click 'No' to cancel startup",
|
||||
QMessageBox.Yes | QMessageBox.No,
|
||||
QMessageBox.No
|
||||
)
|
||||
sys.exit(0)
|
||||
|
||||
if reply == QMessageBox.Yes:
|
||||
print("User requested force cleanup...")
|
||||
single_instance.force_cleanup()
|
||||
print("Cleanup completed, proceeding with startup...")
|
||||
# Create a new instance checker after cleanup
|
||||
single_instance = SingleInstance()
|
||||
if single_instance.is_running():
|
||||
QMessageBox.critical(
|
||||
None,
|
||||
"Cleanup Failed",
|
||||
"Could not clean up the existing instance. Please restart your computer."
|
||||
)
|
||||
sys.exit(1)
|
||||
else:
|
||||
sys.exit(0)
|
||||
|
||||
try:
|
||||
# Setup the full application
|
||||
|
||||
38
main.spec
Normal file
38
main.spec
Normal file
@ -0,0 +1,38 @@
|
||||
# -*- mode: python ; coding: utf-8 -*-
|
||||
|
||||
|
||||
a = Analysis(
|
||||
['main.py'],
|
||||
pathex=[],
|
||||
binaries=[],
|
||||
datas=[('config', 'config'), ('core', 'core'), ('resources', 'resources'), ('ui', 'ui'), ('utils', 'utils'), ('C:\\Users\\mason\\miniconda3\\envs\\cluster\\Lib\\site-packages\\kp', 'kp\\')],
|
||||
hiddenimports=['json', 'base64', 'os', 'pathlib', 'NodeGraphQt', 'threading', 'queue', 'collections', 'datetime', 'cv2', 'numpy', 'PyQt5.QtCore', 'PyQt5.QtWidgets', 'PyQt5.QtGui', 'sys', 'traceback', 'io', 'contextlib'],
|
||||
hookspath=[],
|
||||
hooksconfig={},
|
||||
runtime_hooks=[],
|
||||
excludes=[],
|
||||
noarchive=False,
|
||||
optimize=0,
|
||||
)
|
||||
pyz = PYZ(a.pure)
|
||||
|
||||
exe = EXE(
|
||||
pyz,
|
||||
a.scripts,
|
||||
a.binaries,
|
||||
a.datas,
|
||||
[],
|
||||
name='main',
|
||||
debug=False,
|
||||
bootloader_ignore_signals=False,
|
||||
strip=False,
|
||||
upx=True,
|
||||
upx_exclude=[],
|
||||
runtime_tmpdir=None,
|
||||
console=False,
|
||||
disable_windowed_traceback=False,
|
||||
argv_emulation=False,
|
||||
target_arch=None,
|
||||
codesign_identity=None,
|
||||
entitlements_file=None,
|
||||
)
|
||||
193
mutliseries.py
193
mutliseries.py
@ -1,193 +0,0 @@
|
||||
import kp
|
||||
from collections import defaultdict
|
||||
from typing import Union
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import time
|
||||
import threading
|
||||
import queue
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
# PWD = os.path.dirname(os.path.abspath(__file__))
|
||||
# sys.path.insert(1, os.path.join(PWD, '..'))
|
||||
IMAGE_FILE_PATH = r"c:\Users\mason\Downloads\kneron_plus_v3.1.2\kneron_plus\res\images\people_talk_in_street_640x640.bmp"
|
||||
LOOP_TIME = 100
|
||||
|
||||
|
||||
def _image_send_function(_device_group: kp.DeviceGroup,
|
||||
_loop_time: int,
|
||||
_generic_inference_input_descriptor: kp.GenericImageInferenceDescriptor,
|
||||
_image: Union[bytes, np.ndarray],
|
||||
_image_format: kp.ImageFormat) -> None:
|
||||
for _loop in range(_loop_time):
|
||||
try:
|
||||
_generic_inference_input_descriptor.inference_number = _loop
|
||||
_generic_inference_input_descriptor.input_node_image_list = [kp.GenericInputNodeImage(
|
||||
image=_image,
|
||||
image_format=_image_format,
|
||||
resize_mode=kp.ResizeMode.KP_RESIZE_ENABLE,
|
||||
padding_mode=kp.PaddingMode.KP_PADDING_CORNER,
|
||||
normalize_mode=kp.NormalizeMode.KP_NORMALIZE_KNERON
|
||||
)]
|
||||
|
||||
kp.inference.generic_image_inference_send(device_group=device_groups[1],
|
||||
generic_inference_input_descriptor=_generic_inference_input_descriptor)
|
||||
except kp.ApiKPException as exception:
|
||||
print(' - Error: inference failed, error = {}'.format(exception))
|
||||
exit(0)
|
||||
|
||||
|
||||
def _result_receive_function(_device_group: kp.DeviceGroup,
|
||||
_loop_time: int,
|
||||
_result_queue: queue.Queue) -> None:
|
||||
_generic_raw_result = None
|
||||
|
||||
for _loop in range(_loop_time):
|
||||
try:
|
||||
_generic_raw_result = kp.inference.generic_image_inference_receive(device_group=device_groups[1])
|
||||
|
||||
if _generic_raw_result.header.inference_number != _loop:
|
||||
print(' - Error: incorrect inference_number {} at frame {}'.format(
|
||||
_generic_raw_result.header.inference_number, _loop))
|
||||
|
||||
print('.', end='', flush=True)
|
||||
|
||||
except kp.ApiKPException as exception:
|
||||
print(' - Error: inference failed, error = {}'.format(exception))
|
||||
exit(0)
|
||||
|
||||
_result_queue.put(_generic_raw_result)
|
||||
|
||||
model_path = ["C:\\Users\\mason\\Downloads\\kneron_plus_v3.1.2\\kneron_plus\\res\\models\\KL520\\yolov5-noupsample_w640h640_kn-model-zoo\\kl520_20005_yolov5-noupsample_w640h640.nef", r"C:\Users\mason\Downloads\kneron_plus_v3.1.2\kneron_plus\res\models\KL720\yolov5-noupsample_w640h640_kn-model-zoo\kl720_20005_yolov5-noupsample_w640h640.nef"]
|
||||
SCPU_FW_PATH_520 = "C:\\Users\\mason\\Downloads\\kneron_plus_v3.1.2\\kneron_plus\\res\\firmware\\KL520\\fw_scpu.bin"
|
||||
NCPU_FW_PATH_520 = "C:\\Users\\mason\\Downloads\\kneron_plus_v3.1.2\\kneron_plus\\res\\firmware\\KL520\\fw_ncpu.bin"
|
||||
SCPU_FW_PATH_720 = "C:\\Users\\mason\\Downloads\\kneron_plus_v3.1.2\\kneron_plus\\res\\firmware\\KL720\\fw_scpu.bin"
|
||||
NCPU_FW_PATH_720 = "C:\\Users\\mason\\Downloads\\kneron_plus_v3.1.2\\kneron_plus\\res\\firmware\\KL720\\fw_ncpu.bin"
|
||||
device_list = kp.core.scan_devices()
|
||||
|
||||
grouped_devices = defaultdict(list)
|
||||
|
||||
for device in device_list.device_descriptor_list:
|
||||
grouped_devices[device.product_id].append(device.usb_port_id)
|
||||
|
||||
print(f"Found device groups: {dict(grouped_devices)}")
|
||||
|
||||
device_groups = []
|
||||
|
||||
for product_id, usb_port_id in grouped_devices.items():
|
||||
try:
|
||||
group = kp.core.connect_devices(usb_port_id)
|
||||
device_groups.append(group)
|
||||
print(f"Successfully connected to group for product ID {product_id} with ports{usb_port_id}")
|
||||
except kp.ApiKPException as e:
|
||||
print(f"Failed to connect to group for product ID {product_id}: {e}")
|
||||
|
||||
print(device_groups)
|
||||
|
||||
print('[Set Device Timeout]')
|
||||
kp.core.set_timeout(device_group=device_groups[0], milliseconds=5000)
|
||||
kp.core.set_timeout(device_group=device_groups[1], milliseconds=5000)
|
||||
print(' - Success')
|
||||
|
||||
try:
|
||||
print('[Upload Firmware]')
|
||||
kp.core.load_firmware_from_file(device_group=device_groups[0],
|
||||
scpu_fw_path=SCPU_FW_PATH_520,
|
||||
ncpu_fw_path=NCPU_FW_PATH_520)
|
||||
kp.core.load_firmware_from_file(device_group=device_groups[1],
|
||||
scpu_fw_path=SCPU_FW_PATH_720,
|
||||
ncpu_fw_path=NCPU_FW_PATH_720)
|
||||
print(' - Success')
|
||||
except kp.ApiKPException as exception:
|
||||
print('Error: upload firmware failed, error = \'{}\''.format(str(exception)))
|
||||
exit(0)
|
||||
|
||||
print('[Upload Model]')
|
||||
model_nef_descriptors = []
|
||||
# for group in device_groups:
|
||||
model_nef_descriptor = kp.core.load_model_from_file(device_group=device_groups[0], file_path=model_path[0])
|
||||
model_nef_descriptors.append(model_nef_descriptor)
|
||||
model_nef_descriptor = kp.core.load_model_from_file(device_group=device_groups[1], file_path=model_path[1])
|
||||
model_nef_descriptors.append(model_nef_descriptor)
|
||||
print(' - Success')
|
||||
|
||||
"""
|
||||
prepare the image
|
||||
"""
|
||||
print('[Read Image]')
|
||||
img = cv2.imread(filename=IMAGE_FILE_PATH)
|
||||
img_bgr565 = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2BGR565)
|
||||
print(' - Success')
|
||||
|
||||
"""
|
||||
prepare generic image inference input descriptor
|
||||
"""
|
||||
print(model_nef_descriptors)
|
||||
generic_inference_input_descriptor = kp.GenericImageInferenceDescriptor(
|
||||
model_id=model_nef_descriptors[1].models[0].id,
|
||||
)
|
||||
|
||||
"""
|
||||
starting inference work
|
||||
"""
|
||||
print('[Starting Inference Work]')
|
||||
print(' - Starting inference loop {} times'.format(LOOP_TIME))
|
||||
print(' - ', end='')
|
||||
result_queue = queue.Queue()
|
||||
|
||||
send_thread = threading.Thread(target=_image_send_function, args=(device_groups[1],
|
||||
LOOP_TIME,
|
||||
generic_inference_input_descriptor,
|
||||
img_bgr565,
|
||||
kp.ImageFormat.KP_IMAGE_FORMAT_RGB565))
|
||||
|
||||
receive_thread = threading.Thread(target=_result_receive_function, args=(device_groups[1],
|
||||
LOOP_TIME,
|
||||
result_queue))
|
||||
|
||||
start_inference_time = time.time()
|
||||
|
||||
send_thread.start()
|
||||
receive_thread.start()
|
||||
|
||||
try:
|
||||
while send_thread.is_alive():
|
||||
send_thread.join(1)
|
||||
|
||||
while receive_thread.is_alive():
|
||||
receive_thread.join(1)
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
print('\n - Received keyboard interrupt, quitting threads.')
|
||||
exit(0)
|
||||
|
||||
end_inference_time = time.time()
|
||||
time_spent = end_inference_time - start_inference_time
|
||||
|
||||
try:
|
||||
generic_raw_result = result_queue.get(timeout=3)
|
||||
except Exception as exception:
|
||||
print('Error: Result queue is empty !')
|
||||
exit(0)
|
||||
print()
|
||||
|
||||
print('[Result]')
|
||||
print(" - Total inference {} images".format(LOOP_TIME))
|
||||
print(" - Time spent: {:.2f} secs, FPS = {:.1f}".format(time_spent, LOOP_TIME / time_spent))
|
||||
|
||||
"""
|
||||
retrieve inference node output
|
||||
"""
|
||||
print('[Retrieve Inference Node Output ]')
|
||||
inf_node_output_list = []
|
||||
for node_idx in range(generic_raw_result.header.num_output_node):
|
||||
inference_float_node_output = kp.inference.generic_inference_retrieve_float_node(node_idx=node_idx,
|
||||
generic_raw_result=generic_raw_result,
|
||||
channels_ordering=kp.ChannelOrdering.KP_CHANNEL_ORDERING_CHW)
|
||||
inf_node_output_list.append(inference_float_node_output)
|
||||
|
||||
print(' - Success')
|
||||
|
||||
print('[Result]')
|
||||
print(inf_node_output_list)
|
||||
@ -8,3 +8,11 @@ dependencies = [
|
||||
"nodegraphqt>=0.6.40",
|
||||
"pyqt5>=5.15.11",
|
||||
]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests/unit"]
|
||||
pythonpath = ["."]
|
||||
addopts = "--import-mode=importlib"
|
||||
python_files = ["test_*.py"]
|
||||
python_classes = ["Test*"]
|
||||
python_functions = ["test_*", "should_*"]
|
||||
|
||||
185
tests/KL520KnModelZooGenericImageInferenceYolov5.py
Normal file
185
tests/KL520KnModelZooGenericImageInferenceYolov5.py
Normal file
@ -0,0 +1,185 @@
|
||||
# ******************************************************************************
|
||||
# Copyright (c) 2021-2022. Kneron Inc. All rights reserved. *
|
||||
# ******************************************************************************
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
PWD = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(1, os.path.join(PWD, '..'))
|
||||
sys.path.insert(1, os.path.join(PWD, '../example/'))
|
||||
|
||||
from example_utils.ExampleHelper import get_device_usb_speed_by_port_id
|
||||
from example_utils.ExamplePostProcess import post_process_yolo_v5
|
||||
import kp
|
||||
import cv2
|
||||
|
||||
SCPU_FW_PATH = os.path.join(PWD, '../../res/firmware/KL520/fw_scpu.bin')
|
||||
NCPU_FW_PATH = os.path.join(PWD, '../../res/firmware/KL520/fw_ncpu.bin')
|
||||
MODEL_FILE_PATH = os.path.join(PWD,
|
||||
'../../res/models/KL520/yolov5-noupsample_w640h640_kn-model-zoo/kl520_20005_yolov5-noupsample_w640h640.nef')
|
||||
IMAGE_FILE_PATH = os.path.join(PWD, '../../res/images/people_talk_in_street_1500x1500.bmp')
|
||||
LOOP_TIME = 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='KL520 Kneron Model Zoo Generic Image Inference Example - YoloV5.')
|
||||
parser.add_argument('-p',
|
||||
'--port_id',
|
||||
help='Using specified port ID for connecting device (Default: port ID of first scanned Kneron '
|
||||
'device)',
|
||||
default=0,
|
||||
type=int)
|
||||
parser.add_argument('-m',
|
||||
'--model',
|
||||
help='Model file path (.nef) (Default: {})'.format(MODEL_FILE_PATH),
|
||||
default=MODEL_FILE_PATH,
|
||||
type=str)
|
||||
parser.add_argument('-i',
|
||||
'--img',
|
||||
help='Image file path (Default: {})'.format(IMAGE_FILE_PATH),
|
||||
default=IMAGE_FILE_PATH,
|
||||
type=str)
|
||||
args = parser.parse_args()
|
||||
|
||||
usb_port_id = args.port_id
|
||||
MODEL_FILE_PATH = args.model
|
||||
IMAGE_FILE_PATH = args.img
|
||||
|
||||
"""
|
||||
check device USB speed (Recommend run KL520 at high speed)
|
||||
"""
|
||||
try:
|
||||
if kp.UsbSpeed.KP_USB_SPEED_HIGH != get_device_usb_speed_by_port_id(usb_port_id=usb_port_id):
|
||||
print('\033[91m' + '[Warning] Device is not run at high speed.' + '\033[0m')
|
||||
except Exception as exception:
|
||||
print('Error: check device USB speed fail, port ID = \'{}\', error msg: [{}]'.format(usb_port_id,
|
||||
str(exception)))
|
||||
exit(0)
|
||||
|
||||
"""
|
||||
connect the device
|
||||
"""
|
||||
try:
|
||||
print('[Connect Device]')
|
||||
device_group = kp.core.connect_devices(usb_port_ids=[usb_port_id])
|
||||
print(' - Success')
|
||||
except kp.ApiKPException as exception:
|
||||
print('Error: connect device fail, port ID = \'{}\', error msg: [{}]'.format(usb_port_id,
|
||||
str(exception)))
|
||||
exit(0)
|
||||
|
||||
"""
|
||||
setting timeout of the usb communication with the device
|
||||
"""
|
||||
print('[Set Device Timeout]')
|
||||
kp.core.set_timeout(device_group=device_group, milliseconds=5000)
|
||||
print(' - Success')
|
||||
|
||||
"""
|
||||
upload firmware to device
|
||||
"""
|
||||
try:
|
||||
print('[Upload Firmware]')
|
||||
kp.core.load_firmware_from_file(device_group=device_group,
|
||||
scpu_fw_path=SCPU_FW_PATH,
|
||||
ncpu_fw_path=NCPU_FW_PATH)
|
||||
print(' - Success')
|
||||
except kp.ApiKPException as exception:
|
||||
print('Error: upload firmware failed, error = \'{}\''.format(str(exception)))
|
||||
exit(0)
|
||||
|
||||
"""
|
||||
upload model to device
|
||||
"""
|
||||
try:
|
||||
print('[Upload Model]')
|
||||
model_nef_descriptor = kp.core.load_model_from_file(device_group=device_group,
|
||||
file_path=MODEL_FILE_PATH)
|
||||
print(' - Success')
|
||||
except kp.ApiKPException as exception:
|
||||
print('Error: upload model failed, error = \'{}\''.format(str(exception)))
|
||||
exit(0)
|
||||
|
||||
"""
|
||||
prepare the image
|
||||
"""
|
||||
print('[Read Image]')
|
||||
img = cv2.imread(filename=IMAGE_FILE_PATH)
|
||||
img_bgr565 = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2BGR565)
|
||||
print(' - Success')
|
||||
|
||||
"""
|
||||
prepare generic image inference input descriptor
|
||||
"""
|
||||
generic_inference_input_descriptor = kp.GenericImageInferenceDescriptor(
|
||||
model_id=model_nef_descriptor.models[0].id,
|
||||
inference_number=0,
|
||||
input_node_image_list=[
|
||||
kp.GenericInputNodeImage(
|
||||
image=img_bgr565,
|
||||
image_format=kp.ImageFormat.KP_IMAGE_FORMAT_RGB565,
|
||||
resize_mode=kp.ResizeMode.KP_RESIZE_ENABLE,
|
||||
padding_mode=kp.PaddingMode.KP_PADDING_CORNER,
|
||||
normalize_mode=kp.NormalizeMode.KP_NORMALIZE_KNERON
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
"""
|
||||
starting inference work
|
||||
"""
|
||||
print('[Starting Inference Work]')
|
||||
print(' - Starting inference loop {} times'.format(LOOP_TIME))
|
||||
print(' - ', end='')
|
||||
for i in range(LOOP_TIME):
|
||||
try:
|
||||
kp.inference.generic_image_inference_send(device_group=device_group,
|
||||
generic_inference_input_descriptor=generic_inference_input_descriptor)
|
||||
|
||||
generic_raw_result = kp.inference.generic_image_inference_receive(device_group=device_group)
|
||||
except kp.ApiKPException as exception:
|
||||
print(' - Error: inference failed, error = {}'.format(exception))
|
||||
exit(0)
|
||||
|
||||
print('.', end='', flush=True)
|
||||
print()
|
||||
|
||||
"""
|
||||
retrieve inference node output
|
||||
"""
|
||||
print('[Retrieve Inference Node Output ]')
|
||||
inf_node_output_list = []
|
||||
for node_idx in range(generic_raw_result.header.num_output_node):
|
||||
inference_float_node_output = kp.inference.generic_inference_retrieve_float_node(node_idx=node_idx,
|
||||
generic_raw_result=generic_raw_result,
|
||||
channels_ordering=kp.ChannelOrdering.KP_CHANNEL_ORDERING_CHW
|
||||
)
|
||||
inf_node_output_list.append(inference_float_node_output)
|
||||
|
||||
print(' - Success')
|
||||
|
||||
yolo_result = post_process_yolo_v5(inference_float_node_output_list=inf_node_output_list,
|
||||
hardware_preproc_info=generic_raw_result.header.hw_pre_proc_info_list[0],
|
||||
thresh_value=0.2)
|
||||
|
||||
print('[Result]')
|
||||
print(' - Number of boxes detected')
|
||||
print(' - ' + str(len(yolo_result.box_list)))
|
||||
output_img_name = 'output_{}'.format(os.path.basename(IMAGE_FILE_PATH))
|
||||
print(' - Output bounding boxes on \'{}\''.format(output_img_name))
|
||||
print(" - Bounding boxes info (xmin,ymin,xmax,ymax):")
|
||||
for yolo_box_result in yolo_result.box_list:
|
||||
b = 100 + (25 * yolo_box_result.class_num) % 156
|
||||
g = 100 + (80 + 40 * yolo_box_result.class_num) % 156
|
||||
r = 100 + (120 + 60 * yolo_box_result.class_num) % 156
|
||||
color = (b, g, r)
|
||||
|
||||
cv2.rectangle(img=img,
|
||||
pt1=(int(yolo_box_result.x1), int(yolo_box_result.y1)),
|
||||
pt2=(int(yolo_box_result.x2), int(yolo_box_result.y2)),
|
||||
color=color,
|
||||
thickness=2)
|
||||
print("(" + str(yolo_box_result.x1) + "," + str(yolo_box_result.y1) + ',' + str(yolo_box_result.x2) + ',' + str(
|
||||
yolo_box_result.y2) + ")")
|
||||
cv2.imwrite(os.path.join(PWD, './{}'.format(output_img_name)), img=img)
|
||||
46
tests/conftest.py
Normal file
46
tests/conftest.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""
|
||||
tests/conftest.py — 單元測試環境設定。
|
||||
|
||||
此 conftest.py 位於 tests/ 目錄(非 Python 套件),
|
||||
可在 root __init__.py 被觸發前完成 Mock 注入。
|
||||
|
||||
在沒有 Kneron NPU 硬體、PyQt5、NodeGraphQt 的環境下,
|
||||
仍可測試 core/performance/ 的純 Python 邏輯。
|
||||
"""
|
||||
import sys
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def _install_mock(name: str) -> None:
|
||||
"""若模組尚未存在,安裝空 MagicMock 作為替代。"""
|
||||
if name not in sys.modules:
|
||||
sys.modules[name] = MagicMock()
|
||||
|
||||
|
||||
# Kneron KP SDK(需要硬體驅動程式)
|
||||
_install_mock("kp")
|
||||
|
||||
# NumPy(可能未安裝)
|
||||
try:
|
||||
import numpy # noqa: F401
|
||||
except ImportError:
|
||||
_install_mock("numpy")
|
||||
|
||||
# PyQt5 相關模組(需要 GUI 環境)
|
||||
for _mod in [
|
||||
"PyQt5",
|
||||
"PyQt5.QtWidgets",
|
||||
"PyQt5.QtCore",
|
||||
"PyQt5.QtGui",
|
||||
"PyQt5.QtChart",
|
||||
]:
|
||||
_install_mock(_mod)
|
||||
|
||||
# NodeGraphQt(依賴 PyQt5)
|
||||
_install_mock("NodeGraphQt")
|
||||
_install_mock("NodeGraphQt.constants")
|
||||
_install_mock("NodeGraphQt.base")
|
||||
_install_mock("NodeGraphQt.base.node")
|
||||
|
||||
# OpenCV(可能未安裝)
|
||||
_install_mock("cv2")
|
||||
149
tests/debug_detection_issues.py
Normal file
149
tests/debug_detection_issues.py
Normal file
@ -0,0 +1,149 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Debug script to investigate abnormal detection results.
|
||||
檢查異常偵測結果的調試腳本。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from core.functions.Multidongle import BoundingBox, ObjectDetectionResult
|
||||
|
||||
def analyze_detection_result(result: ObjectDetectionResult):
|
||||
"""分析偵測結果,找出異常情況"""
|
||||
print("=== DETECTION RESULT ANALYSIS ===")
|
||||
print(f"Class count: {result.class_count}")
|
||||
print(f"Box count: {result.box_count}")
|
||||
|
||||
if not result.box_list:
|
||||
print("No bounding boxes found.")
|
||||
return
|
||||
|
||||
# 統計分析
|
||||
class_counts = {}
|
||||
coordinate_issues = []
|
||||
score_issues = []
|
||||
|
||||
for i, box in enumerate(result.box_list):
|
||||
# 統計每個類別的數量
|
||||
class_counts[box.class_name] = class_counts.get(box.class_name, 0) + 1
|
||||
|
||||
# 檢查座標問題
|
||||
if box.x1 < 0 or box.y1 < 0 or box.x2 < 0 or box.y2 < 0:
|
||||
coordinate_issues.append(f"Box {i}: Negative coordinates ({box.x1},{box.y1},{box.x2},{box.y2})")
|
||||
|
||||
if box.x1 >= box.x2 or box.y1 >= box.y2:
|
||||
coordinate_issues.append(f"Box {i}: Invalid box dimensions ({box.x1},{box.y1},{box.x2},{box.y2})")
|
||||
|
||||
if box.x1 == box.x2 and box.y1 == box.y2:
|
||||
coordinate_issues.append(f"Box {i}: Zero-area box ({box.x1},{box.y1},{box.x2},{box.y2})")
|
||||
|
||||
# 檢查分數問題
|
||||
if box.score < 0 or box.score > 1:
|
||||
score_issues.append(f"Box {i}: Unusual score {box.score} for {box.class_name}")
|
||||
|
||||
# 報告結果
|
||||
print("\n--- CLASS DISTRIBUTION ---")
|
||||
for class_name, count in sorted(class_counts.items()):
|
||||
if count > 50: # 標記異常高的數量
|
||||
print(f"⚠ {class_name}: {count} (ABNORMALLY HIGH)")
|
||||
else:
|
||||
print(f"✓ {class_name}: {count}")
|
||||
|
||||
print(f"\n--- COORDINATE ISSUES ({len(coordinate_issues)}) ---")
|
||||
for issue in coordinate_issues[:10]: # 只顯示前10個
|
||||
print(f"⚠ {issue}")
|
||||
if len(coordinate_issues) > 10:
|
||||
print(f"... and {len(coordinate_issues) - 10} more coordinate issues")
|
||||
|
||||
print(f"\n--- SCORE ISSUES ({len(score_issues)}) ---")
|
||||
for issue in score_issues[:10]: # 只顯示前10個
|
||||
print(f"⚠ {issue}")
|
||||
if len(score_issues) > 10:
|
||||
print(f"... and {len(score_issues) - 10} more score issues")
|
||||
|
||||
# 建議
|
||||
print("\n--- RECOMMENDATIONS ---")
|
||||
if any(count > 50 for count in class_counts.values()):
|
||||
print("⚠ Abnormally high detection counts suggest:")
|
||||
print(" 1. Model output format mismatch")
|
||||
print(" 2. Confidence threshold too low")
|
||||
print(" 3. Test/debug mode accidentally enabled")
|
||||
|
||||
if coordinate_issues:
|
||||
print("⚠ Coordinate issues suggest:")
|
||||
print(" 1. Coordinate transformation problems")
|
||||
print(" 2. Model output scaling issues")
|
||||
print(" 3. Hardware preprocessing info missing")
|
||||
|
||||
if score_issues:
|
||||
print("⚠ Score issues suggest:")
|
||||
print(" 1. Score values might be in log space")
|
||||
print(" 2. Wrong score interpretation")
|
||||
print(" 3. Need score normalization")
|
||||
|
||||
def create_mock_problematic_result():
|
||||
"""創建一個模擬的有問題的偵測結果用於測試"""
|
||||
boxes = []
|
||||
|
||||
# 模擬您遇到的問題
|
||||
class_names = ['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'toothbrush', 'hair drier']
|
||||
|
||||
# 添加大量異常的邊界框
|
||||
for i in range(100):
|
||||
box = BoundingBox(
|
||||
x1=i % 5, # 很小的座標
|
||||
y1=(i + 1) % 4,
|
||||
x2=(i + 2) % 6,
|
||||
y2=(i + 3) % 5,
|
||||
score=2.0 + (i * 0.1), # 異常的分數值
|
||||
class_num=i % len(class_names),
|
||||
class_name=class_names[i % len(class_names)]
|
||||
)
|
||||
boxes.append(box)
|
||||
|
||||
return ObjectDetectionResult(
|
||||
class_count=len(class_names),
|
||||
box_count=len(boxes),
|
||||
box_list=boxes
|
||||
)
|
||||
|
||||
def suggest_fixes():
|
||||
"""提供修復建議"""
|
||||
print("\n=== SUGGESTED FIXES ===")
|
||||
|
||||
print("\n1. 檢查模型配置:")
|
||||
print(" - 確認使用正確的後處理類型(YOLO_V3, YOLO_V5, etc.)")
|
||||
print(" - 檢查類別名稱列表是否正確")
|
||||
print(" - 驗證信心閾值設定(建議 0.3-0.7)")
|
||||
|
||||
print("\n2. 檢查座標轉換:")
|
||||
print(" - 確認模型輸出格式(中心座標 vs 角點座標)")
|
||||
print(" - 檢查圖片尺寸縮放")
|
||||
print(" - 驗證硬體預處理信息")
|
||||
|
||||
print("\n3. 添加結果過濾:")
|
||||
print(" - 過濾無效座標的邊界框")
|
||||
print(" - 限制每個類別的最大檢測數量")
|
||||
print(" - 添加 NMS(非極大值抑制)")
|
||||
|
||||
print("\n4. 調試步驟:")
|
||||
print(" - 添加詳細的調試日誌")
|
||||
print(" - 檢查原始模型輸出")
|
||||
print(" - 測試不同的後處理參數")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Detection Issues Debug Tool")
|
||||
print("=" * 50)
|
||||
|
||||
# 測試與您遇到類似問題的模擬結果
|
||||
print("Testing with mock problematic result...")
|
||||
mock_result = create_mock_problematic_result()
|
||||
analyze_detection_result(mock_result)
|
||||
|
||||
suggest_fixes()
|
||||
|
||||
print("\nTo use this tool with real results:")
|
||||
print("from debug_detection_issues import analyze_detection_result")
|
||||
print("analyze_detection_result(your_detection_result)")
|
||||
25
tests/emergency_filter.py
Normal file
25
tests/emergency_filter.py
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
def emergency_filter_detections(boxes, max_total=50, max_per_class=10):
|
||||
"""緊急過濾檢測結果"""
|
||||
if len(boxes) <= max_total:
|
||||
return boxes
|
||||
|
||||
# 按類別分組
|
||||
from collections import defaultdict
|
||||
class_groups = defaultdict(list)
|
||||
for box in boxes:
|
||||
class_groups[box.class_name].append(box)
|
||||
|
||||
# 每類保留最高分數的檢測
|
||||
filtered = []
|
||||
for class_name, class_boxes in class_groups.items():
|
||||
class_boxes.sort(key=lambda x: x.score, reverse=True)
|
||||
keep_count = min(len(class_boxes), max_per_class)
|
||||
filtered.extend(class_boxes[:keep_count])
|
||||
|
||||
# 總數限制
|
||||
if len(filtered) > max_total:
|
||||
filtered.sort(key=lambda x: x.score, reverse=True)
|
||||
filtered = filtered[:max_total]
|
||||
|
||||
return filtered
|
||||
201
tests/fire_detection_520.py
Normal file
201
tests/fire_detection_520.py
Normal file
@ -0,0 +1,201 @@
|
||||
"""
|
||||
fire_detection_inference.py
|
||||
|
||||
此模組提供火災檢測推論介面函式:
|
||||
inference(frame, params={})
|
||||
|
||||
當作為主程式執行時,也可以使用命令列參數測試推論。
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import argparse
|
||||
import cv2
|
||||
import numpy as np
|
||||
import kp
|
||||
|
||||
# 固定路徑設定
|
||||
# SCPU_FW_PATH = r'external\res\firmware\KL520\fw_scpu.bin'
|
||||
# NCPU_FW_PATH = r'external\res\firmware\KL520\fw_ncpu.bin'
|
||||
# MODEL_FILE_PATH = r'src\utils\models\fire_detection_520.nef'
|
||||
# 若作為測試使用,預設的圖片檔案路徑(請根據實際環境調整)
|
||||
# IMAGE_FILE_PATH = r'test_images\fire4.jpeg'
|
||||
|
||||
|
||||
def preprocess_frame(frame):
|
||||
"""
|
||||
將輸入的 numpy 陣列進行預處理:
|
||||
1. 調整大小至 (128, 128)
|
||||
2. 轉換為 BGR565 格式(KL520 常用格式)
|
||||
"""
|
||||
if frame is None:
|
||||
raise Exception("輸入的 frame 為 None")
|
||||
|
||||
print("預處理步驟:")
|
||||
print(f" - 原始 frame 大小: {frame.shape}")
|
||||
|
||||
# 調整大小
|
||||
frame_resized = cv2.resize(frame, (128, 128))
|
||||
print(f" - 調整後大小: {frame_resized.shape}")
|
||||
|
||||
# 轉換為 BGR565 格式
|
||||
# 注意:cv2.cvtColor 直接轉換到 BGR565 並非 OpenCV 標準用法,但假設此方法在 kneron SDK 下有效
|
||||
frame_bgr565 = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2BGR565)
|
||||
print(" - 轉換為 BGR565 格式")
|
||||
|
||||
return frame_bgr565
|
||||
|
||||
|
||||
def postprocess(pre_output):
|
||||
"""
|
||||
後處理函式:將模型輸出轉換為二元分類結果(這裡假設輸出為單一數值)
|
||||
"""
|
||||
probability = pre_output[0] # 假設模型輸出僅一個數值
|
||||
return probability
|
||||
|
||||
|
||||
def inference(frame, params={}):
|
||||
"""
|
||||
推論介面函式
|
||||
- frame: numpy 陣列(BGR 格式),輸入的原始影像
|
||||
- params: dict,包含額外參數,例如:
|
||||
'port_id': (int) 預設 0
|
||||
'model': (str) 模型檔案路徑,預設 MODEL_FILE_PATH
|
||||
回傳一個 dict,內容包含:
|
||||
- result: "Fire" 或 "No Fire"
|
||||
- probability: 推論信心分數
|
||||
- inference_time_ms: 推論耗時 (毫秒)
|
||||
"""
|
||||
# 取得參數(若未提供則使用預設值)
|
||||
port_id = params.get('usb_port_id', 0)
|
||||
model_path = params.get('model')
|
||||
IMAGE_FILE_PATH = params.get('file_path')
|
||||
SCPU_FW_PATH = params.get('scpu_path')
|
||||
NCPU_FW_PATH = params.get('ncpu_path')
|
||||
|
||||
print("Parameters received from main app:", params)
|
||||
try:
|
||||
# 1. 設備連接與初始化
|
||||
print('[連接設備]')
|
||||
device_group = kp.core.connect_devices(usb_port_ids=[port_id])
|
||||
print(' - 成功')
|
||||
|
||||
print('[設置超時]')
|
||||
kp.core.set_timeout(device_group=device_group, milliseconds=5000)
|
||||
print(' - 成功')
|
||||
|
||||
print('[上傳韌體]')
|
||||
kp.core.load_firmware_from_file(device_group=device_group,
|
||||
scpu_fw_path=SCPU_FW_PATH,
|
||||
ncpu_fw_path=NCPU_FW_PATH)
|
||||
print(' - 成功')
|
||||
|
||||
print('[上傳模型]')
|
||||
model_descriptor = kp.core.load_model_from_file(device_group=device_group,
|
||||
file_path=model_path)
|
||||
print(' - 成功')
|
||||
|
||||
# 2. 圖像預處理:從 frame 轉換到符合 KL520 格式的輸入
|
||||
print('[預處理影像]')
|
||||
img_processed = preprocess_frame(frame)
|
||||
|
||||
# 3. 建立推論描述物件
|
||||
inference_input_descriptor = kp.GenericImageInferenceDescriptor(
|
||||
model_id=model_descriptor.models[0].id,
|
||||
inference_number=0,
|
||||
input_node_image_list=[
|
||||
kp.GenericInputNodeImage(
|
||||
image=img_processed,
|
||||
image_format=kp.ImageFormat.KP_IMAGE_FORMAT_RGB565,
|
||||
resize_mode=kp.ResizeMode.KP_RESIZE_ENABLE,
|
||||
padding_mode=kp.PaddingMode.KP_PADDING_CORNER,
|
||||
normalize_mode=kp.NormalizeMode.KP_NORMALIZE_KNERON
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
# 4. 執行推論
|
||||
print('[執行推論]')
|
||||
start_time = time.time()
|
||||
kp.inference.generic_image_inference_send(
|
||||
device_group=device_group,
|
||||
generic_inference_input_descriptor=inference_input_descriptor
|
||||
)
|
||||
generic_raw_result = kp.inference.generic_image_inference_receive(
|
||||
device_group=device_group
|
||||
)
|
||||
inference_time = (time.time() - start_time) * 1000 # 毫秒
|
||||
print(f' - 推論耗時: {inference_time:.2f} ms')
|
||||
|
||||
# 5. 處理推論結果
|
||||
print('[處理結果]')
|
||||
inf_node_output_list = []
|
||||
for node_idx in range(generic_raw_result.header.num_output_node):
|
||||
inference_float_node_output = kp.inference.generic_inference_retrieve_float_node(
|
||||
node_idx=node_idx,
|
||||
generic_raw_result=generic_raw_result,
|
||||
channels_ordering=kp.ChannelOrdering.KP_CHANNEL_ORDERING_CHW
|
||||
)
|
||||
inf_node_output_list.append(inference_float_node_output.ndarray.copy())
|
||||
|
||||
# 整理成一維陣列並後處理
|
||||
probability = postprocess(np.array(inf_node_output_list).flatten())
|
||||
result_str = "Fire" if probability > 0.5 else "No Fire"
|
||||
|
||||
# 6. 斷開設備連接
|
||||
kp.core.disconnect_devices(device_group=device_group)
|
||||
print('[已斷開設備連接]')
|
||||
|
||||
# 回傳結果
|
||||
return {
|
||||
"result": result_str,
|
||||
"probability": probability,
|
||||
"inference_time_ms": inference_time
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"錯誤: {str(e)}")
|
||||
# 嘗試斷開設備(若有連線)
|
||||
try:
|
||||
kp.core.disconnect_devices(device_group=device_group)
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
|
||||
|
||||
# 若作為主程式執行,支援從命令列讀取圖片檔案並測試推論
|
||||
# if __name__ == '__main__':
|
||||
# parser = argparse.ArgumentParser(
|
||||
# description='KL520 Fire Detection Model Inference'
|
||||
# )
|
||||
# parser.add_argument(
|
||||
# '-p', '--port_id', help='Port ID (Default: 0)', default=0, type=int
|
||||
# )
|
||||
# parser.add_argument(
|
||||
# '-m', '--model', help='NEF model path', default=model_path, type=str
|
||||
# )
|
||||
# parser.add_argument(
|
||||
# '-i', '--img', help='Image path', default=IMAGE_FILE_PATH, type=str
|
||||
# )
|
||||
# args = parser.parse_args()
|
||||
|
||||
# # 讀取圖片(使用 cv2 讀取)
|
||||
# test_image = cv2.imread(args.img)
|
||||
# if test_image is None:
|
||||
# print(f"無法讀取圖片: {args.img}")
|
||||
# sys.exit(1)
|
||||
|
||||
# # 構造參數字典
|
||||
# params = {
|
||||
# "port_id": args.port_id,
|
||||
# "model": args.model
|
||||
# }
|
||||
|
||||
# # 呼叫推論介面函式
|
||||
# result = inference(test_image, params)
|
||||
|
||||
# print("\n結果摘要:")
|
||||
# print(f"預測結果: {result['result']}")
|
||||
# print(f"信心分數: {result['probability']:.4f}")
|
||||
# print(f"推論時間: {result['inference_time_ms']:.2f} ms")
|
||||
184
tests/fix_yolov5_postprocessing.py
Normal file
184
tests/fix_yolov5_postprocessing.py
Normal file
@ -0,0 +1,184 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to fix YOLOv5 postprocessing configuration issues
|
||||
|
||||
This script demonstrates how to properly configure YOLOv5 postprocessing
|
||||
to resolve negative probability values and incorrect result formatting.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add core functions to path
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'core', 'functions'))
|
||||
|
||||
def create_yolov5_postprocessor_options():
|
||||
"""Create properly configured PostProcessorOptions for YOLOv5"""
|
||||
from Multidongle import PostProcessType, PostProcessorOptions
|
||||
|
||||
# COCO dataset class names (80 classes for YOLOv5)
|
||||
yolo_class_names = [
|
||||
"person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
|
||||
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
|
||||
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack",
|
||||
"umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball",
|
||||
"kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket",
|
||||
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
|
||||
"sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair",
|
||||
"sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse",
|
||||
"remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator",
|
||||
"book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
|
||||
]
|
||||
|
||||
# Create YOLOv5 postprocessor options
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.3, # Confidence threshold (0.3 is good for detection)
|
||||
class_names=yolo_class_names, # All 80 COCO classes
|
||||
nms_threshold=0.5, # Non-Maximum Suppression threshold
|
||||
max_detections_per_class=50 # Maximum detections per class
|
||||
)
|
||||
|
||||
return options
|
||||
|
||||
def create_fire_detection_postprocessor_options():
|
||||
"""Create properly configured PostProcessorOptions for Fire Detection"""
|
||||
from Multidongle import PostProcessType, PostProcessorOptions
|
||||
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.FIRE_DETECTION,
|
||||
threshold=0.5, # Fire detection threshold
|
||||
class_names=["No Fire", "Fire"] # Binary classification
|
||||
)
|
||||
|
||||
return options
|
||||
|
||||
def test_postprocessor_options():
|
||||
"""Test both postprocessor configurations"""
|
||||
|
||||
print("=" * 60)
|
||||
print("Testing PostProcessorOptions Configuration")
|
||||
print("=" * 60)
|
||||
|
||||
# Test YOLOv5 configuration
|
||||
print("\n1. YOLOv5 Configuration:")
|
||||
try:
|
||||
yolo_options = create_yolov5_postprocessor_options()
|
||||
print(f" ✓ Postprocess Type: {yolo_options.postprocess_type.value}")
|
||||
print(f" ✓ Confidence Threshold: {yolo_options.threshold}")
|
||||
print(f" ✓ NMS Threshold: {yolo_options.nms_threshold}")
|
||||
print(f" ✓ Max Detections: {yolo_options.max_detections_per_class}")
|
||||
print(f" ✓ Number of Classes: {len(yolo_options.class_names)}")
|
||||
print(f" ✓ Sample Classes: {yolo_options.class_names[:5]}...")
|
||||
except Exception as e:
|
||||
print(f" ✗ YOLOv5 configuration failed: {e}")
|
||||
|
||||
# Test Fire Detection configuration
|
||||
print("\n2. Fire Detection Configuration:")
|
||||
try:
|
||||
fire_options = create_fire_detection_postprocessor_options()
|
||||
print(f" ✓ Postprocess Type: {fire_options.postprocess_type.value}")
|
||||
print(f" ✓ Confidence Threshold: {fire_options.threshold}")
|
||||
print(f" ✓ Class Names: {fire_options.class_names}")
|
||||
except Exception as e:
|
||||
print(f" ✗ Fire Detection configuration failed: {e}")
|
||||
|
||||
def demonstrate_multidongle_creation():
|
||||
"""Demonstrate creating MultiDongle with correct postprocessing"""
|
||||
from Multidongle import MultiDongle
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("Creating MultiDongle with YOLOv5 Postprocessing")
|
||||
print("=" * 60)
|
||||
|
||||
# Create YOLOv5 postprocessor options
|
||||
yolo_options = create_yolov5_postprocessor_options()
|
||||
|
||||
# Example configuration (adjust paths to match your setup)
|
||||
PORT_IDS = [28, 32] # Your dongle port IDs
|
||||
MODEL_PATH = "path/to/yolov5_model.nef" # Your YOLOv5 model path
|
||||
|
||||
print(f"Configuration:")
|
||||
print(f" Port IDs: {PORT_IDS}")
|
||||
print(f" Model Path: {MODEL_PATH}")
|
||||
print(f" Postprocess Type: {yolo_options.postprocess_type.value}")
|
||||
print(f" Confidence Threshold: {yolo_options.threshold}")
|
||||
|
||||
# NOTE: Uncomment below to actually create MultiDongle instance
|
||||
# (requires actual dongle hardware and valid paths)
|
||||
"""
|
||||
try:
|
||||
multidongle = MultiDongle(
|
||||
port_id=PORT_IDS,
|
||||
model_path=MODEL_PATH,
|
||||
auto_detect=True,
|
||||
postprocess_options=yolo_options # This is the key fix!
|
||||
)
|
||||
|
||||
print(" ✓ MultiDongle created successfully with YOLOv5 postprocessing")
|
||||
print(" ✓ This should resolve negative probability issues")
|
||||
|
||||
# Initialize and start
|
||||
multidongle.initialize()
|
||||
multidongle.start()
|
||||
|
||||
print(" ✓ MultiDongle initialized and started")
|
||||
|
||||
# Don't forget to stop when done
|
||||
multidongle.stop()
|
||||
|
||||
except Exception as e:
|
||||
print(f" ✗ MultiDongle creation failed: {e}")
|
||||
"""
|
||||
|
||||
print(f"\n 📝 To fix your current issue:")
|
||||
print(f" 1. Change postprocess_type from 'fire_detection' to 'yolo_v5'")
|
||||
print(f" 2. Set proper class names (80 COCO classes)")
|
||||
print(f" 3. Adjust confidence threshold to 0.3 (instead of 0.5)")
|
||||
print(f" 4. Set NMS threshold to 0.5")
|
||||
|
||||
def show_configuration_summary():
|
||||
"""Show summary of configuration changes needed"""
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("CONFIGURATION FIX SUMMARY")
|
||||
print("=" * 60)
|
||||
|
||||
print("\n🔧 Current Issue:")
|
||||
print(" - YOLOv5 model with FIRE_DETECTION postprocessing")
|
||||
print(" - Results in negative probabilities like -0.39")
|
||||
print(" - Incorrect result formatting")
|
||||
|
||||
print("\n✅ Solution:")
|
||||
print(" 1. Use PostProcessType.YOLO_V5 instead of FIRE_DETECTION")
|
||||
print(" 2. Set confidence threshold to 0.3 (good for object detection)")
|
||||
print(" 3. Use 80 COCO class names for YOLOv5")
|
||||
print(" 4. Set NMS threshold to 0.5 for proper object filtering")
|
||||
|
||||
print("\n📁 File Changes Needed:")
|
||||
print(" - multi_series_example.mflow: Add ExactPostprocessNode")
|
||||
print(" - Set 'enable_postprocessing': true in model node")
|
||||
print(" - Configure postprocess_type: 'yolo_v5'")
|
||||
|
||||
print("\n🚀 Expected Result After Fix:")
|
||||
print(" - Positive probabilities (0.0 to 1.0)")
|
||||
print(" - Object detection results with bounding boxes")
|
||||
print(" - Proper class names like 'person', 'car', etc.")
|
||||
print(" - Multiple objects detected per frame")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("YOLOv5 Postprocessing Fix Utility")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
test_postprocessor_options()
|
||||
demonstrate_multidongle_creation()
|
||||
show_configuration_summary()
|
||||
|
||||
print("\n🎉 Configuration examples completed successfully!")
|
||||
print(" Use the fixed .mflow file or update your configuration.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Script failed with error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
442
tests/improved_yolo_postprocessing.py
Normal file
442
tests/improved_yolo_postprocessing.py
Normal file
@ -0,0 +1,442 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Improved YOLO postprocessing with better error handling and filtering.
|
||||
改進的 YOLO 後處理,包含更好的錯誤處理和過濾機制。
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
from typing import List
|
||||
from collections import defaultdict
|
||||
|
||||
# 假設這些類別已經在原始檔案中定義
|
||||
from core.functions.Multidongle import BoundingBox, ObjectDetectionResult
|
||||
|
||||
|
||||
class ImprovedYOLOPostProcessor:
|
||||
"""改進的 YOLO 後處理器,包含異常檢測和過濾"""
|
||||
|
||||
def __init__(self, options):
|
||||
self.options = options
|
||||
self.max_detections_total = 500 # 總檢測數量限制
|
||||
self.max_detections_per_class = 50 # 每類檢測數量限制
|
||||
self.min_box_area = 4 # 最小邊界框面積
|
||||
self.max_score = 10.0 # 最大允許分數(用於檢測異常)
|
||||
|
||||
def _is_valid_box(self, x1, y1, x2, y2, score, class_id):
|
||||
"""檢查邊界框是否有效"""
|
||||
# 基本座標檢查
|
||||
if x1 < 0 or y1 < 0 or x1 >= x2 or y1 >= y2:
|
||||
return False, "Invalid coordinates"
|
||||
|
||||
# 面積檢查
|
||||
area = (x2 - x1) * (y2 - y1)
|
||||
if area < self.min_box_area:
|
||||
return False, f"Box too small (area={area})"
|
||||
|
||||
# 分數檢查
|
||||
if score <= 0 or score > self.max_score:
|
||||
return False, f"Invalid score ({score})"
|
||||
|
||||
# 類別檢查
|
||||
if class_id < 0 or (self.options.class_names and class_id >= len(self.options.class_names)):
|
||||
return False, f"Invalid class_id ({class_id})"
|
||||
|
||||
return True, "Valid"
|
||||
|
||||
def _filter_excessive_detections(self, boxes: List[BoundingBox]) -> List[BoundingBox]:
|
||||
"""過濾過多的檢測結果"""
|
||||
if len(boxes) <= self.max_detections_total:
|
||||
return boxes
|
||||
|
||||
print(f"WARNING: Too many detections ({len(boxes)}), filtering to {self.max_detections_total}")
|
||||
|
||||
# 按分數排序,保留最高分數的檢測
|
||||
boxes.sort(key=lambda x: x.score, reverse=True)
|
||||
return boxes[:self.max_detections_total]
|
||||
|
||||
def _filter_by_class_count(self, boxes: List[BoundingBox]) -> List[BoundingBox]:
|
||||
"""限制每個類別的檢測數量"""
|
||||
class_counts = defaultdict(list)
|
||||
|
||||
# 按類別分組
|
||||
for box in boxes:
|
||||
class_counts[box.class_num].append(box)
|
||||
|
||||
filtered_boxes = []
|
||||
for class_id, class_boxes in class_counts.items():
|
||||
# 按分數排序,保留最高分數的檢測
|
||||
class_boxes.sort(key=lambda x: x.score, reverse=True)
|
||||
|
||||
# 限制每個類別的數量
|
||||
keep_count = min(len(class_boxes), self.max_detections_per_class)
|
||||
if len(class_boxes) > self.max_detections_per_class:
|
||||
class_name = class_boxes[0].class_name
|
||||
print(f"WARNING: Too many {class_name} detections ({len(class_boxes)}), keeping top {keep_count}")
|
||||
|
||||
filtered_boxes.extend(class_boxes[:keep_count])
|
||||
|
||||
return filtered_boxes
|
||||
|
||||
def _detect_anomalous_pattern(self, boxes: List[BoundingBox]) -> bool:
|
||||
"""檢測異常的檢測模式"""
|
||||
if not boxes:
|
||||
return False
|
||||
|
||||
# 檢查是否有大量相同座標的檢測
|
||||
coord_counts = defaultdict(int)
|
||||
for box in boxes:
|
||||
coord_key = (box.x1, box.y1, box.x2, box.y2)
|
||||
coord_counts[coord_key] += 1
|
||||
|
||||
max_coord_count = max(coord_counts.values())
|
||||
if max_coord_count > 10:
|
||||
print(f"WARNING: Anomalous pattern detected - {max_coord_count} boxes with same coordinates")
|
||||
return True
|
||||
|
||||
# 檢查分數分布
|
||||
scores = [box.score for box in boxes]
|
||||
if scores:
|
||||
avg_score = np.mean(scores)
|
||||
if avg_score > 2.0: # 分數過高可能表示對數空間
|
||||
print(f"WARNING: Unusually high average score: {avg_score:.3f}")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def process_yolo_output(self, inference_output_list: List, hardware_preproc_info=None, version="v3") -> ObjectDetectionResult:
|
||||
"""改進的 YOLO 輸出處理"""
|
||||
boxes = []
|
||||
invalid_box_count = 0
|
||||
|
||||
try:
|
||||
if not inference_output_list or len(inference_output_list) == 0:
|
||||
return ObjectDetectionResult(
|
||||
class_count=len(self.options.class_names) if self.options.class_names else 0,
|
||||
box_count=0,
|
||||
box_list=[]
|
||||
)
|
||||
|
||||
print(f"DEBUG: Processing {len(inference_output_list)} YOLO output nodes")
|
||||
|
||||
for i, output in enumerate(inference_output_list):
|
||||
try:
|
||||
# 提取數組數據
|
||||
if hasattr(output, 'ndarray'):
|
||||
arr = output.ndarray
|
||||
elif hasattr(output, 'flatten'):
|
||||
arr = output
|
||||
elif isinstance(output, np.ndarray):
|
||||
arr = output
|
||||
else:
|
||||
print(f"WARNING: Unknown output type for node {i}: {type(output)}")
|
||||
continue
|
||||
|
||||
# 檢查數組形狀
|
||||
if not hasattr(arr, 'shape'):
|
||||
print(f"WARNING: Output node {i} has no shape attribute")
|
||||
continue
|
||||
|
||||
print(f"DEBUG: Output node {i} shape: {arr.shape}")
|
||||
|
||||
# YOLOv5 格式處理: [batch, num_detections, features]
|
||||
if len(arr.shape) == 3:
|
||||
batch_size, num_detections, num_features = arr.shape
|
||||
print(f"DEBUG: YOLOv5 format: {batch_size}x{num_detections}x{num_features}")
|
||||
|
||||
# 檢查異常大的檢測數量
|
||||
if num_detections > 10000:
|
||||
print(f"WARNING: Extremely high detection count: {num_detections}, limiting to 1000")
|
||||
num_detections = 1000
|
||||
|
||||
detections = arr[0] # 只處理第一批次
|
||||
|
||||
for det_idx in range(min(num_detections, 1000)): # 限制處理數量
|
||||
detection = detections[det_idx]
|
||||
|
||||
try:
|
||||
# 提取座標和信心度
|
||||
x_center = float(detection[0])
|
||||
y_center = float(detection[1])
|
||||
width = float(detection[2])
|
||||
height = float(detection[3])
|
||||
obj_conf = float(detection[4])
|
||||
|
||||
# 檢查是否是有效數值
|
||||
if not all(np.isfinite([x_center, y_center, width, height, obj_conf])):
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
# 跳過低信心度檢測
|
||||
if obj_conf < self.options.threshold:
|
||||
continue
|
||||
|
||||
# 尋找最佳類別
|
||||
class_probs = detection[5:] if num_features > 5 else []
|
||||
if len(class_probs) > 0:
|
||||
class_scores = class_probs * obj_conf
|
||||
best_class = int(np.argmax(class_scores))
|
||||
best_score = float(class_scores[best_class])
|
||||
|
||||
if best_score < self.options.threshold:
|
||||
continue
|
||||
else:
|
||||
best_class = 0
|
||||
best_score = obj_conf
|
||||
|
||||
# 座標轉換
|
||||
x1 = int(x_center - width / 2)
|
||||
y1 = int(y_center - height / 2)
|
||||
x2 = int(x_center + width / 2)
|
||||
y2 = int(y_center + height / 2)
|
||||
|
||||
# 驗證邊界框
|
||||
is_valid, reason = self._is_valid_box(x1, y1, x2, y2, best_score, best_class)
|
||||
if not is_valid:
|
||||
invalid_box_count += 1
|
||||
if invalid_box_count <= 5: # 只報告前5個錯誤
|
||||
print(f"DEBUG: Invalid box rejected: {reason}")
|
||||
continue
|
||||
|
||||
# 獲取類別名稱
|
||||
if self.options.class_names and best_class < len(self.options.class_names):
|
||||
class_name = self.options.class_names[best_class]
|
||||
else:
|
||||
class_name = f"Class_{best_class}"
|
||||
|
||||
box = BoundingBox(
|
||||
x1=max(0, x1),
|
||||
y1=max(0, y1),
|
||||
x2=x2,
|
||||
y2=y2,
|
||||
score=best_score,
|
||||
class_num=best_class,
|
||||
class_name=class_name
|
||||
)
|
||||
boxes.append(box)
|
||||
|
||||
except Exception as e:
|
||||
invalid_box_count += 1
|
||||
if invalid_box_count <= 5:
|
||||
print(f"DEBUG: Error processing detection {det_idx}: {e}")
|
||||
continue
|
||||
|
||||
elif len(arr.shape) == 2:
|
||||
# 2D 格式處理
|
||||
print(f"DEBUG: 2D YOLO output: {arr.shape}")
|
||||
num_detections, num_features = arr.shape
|
||||
|
||||
if num_detections > 1000:
|
||||
print(f"WARNING: Too many 2D detections: {num_detections}, limiting to 1000")
|
||||
num_detections = 1000
|
||||
|
||||
for det_idx in range(min(num_detections, 1000)):
|
||||
detection = arr[det_idx]
|
||||
|
||||
try:
|
||||
if num_features >= 6:
|
||||
x_center = float(detection[0])
|
||||
y_center = float(detection[1])
|
||||
width = float(detection[2])
|
||||
height = float(detection[3])
|
||||
confidence = float(detection[4])
|
||||
class_id = int(detection[5])
|
||||
|
||||
if not all(np.isfinite([x_center, y_center, width, height, confidence])):
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
if confidence > self.options.threshold:
|
||||
x1 = int(x_center - width / 2)
|
||||
y1 = int(y_center - height / 2)
|
||||
x2 = int(x_center + width / 2)
|
||||
y2 = int(y_center + height / 2)
|
||||
|
||||
is_valid, reason = self._is_valid_box(x1, y1, x2, y2, confidence, class_id)
|
||||
if not is_valid:
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
class_name = self.options.class_names[class_id] if class_id < len(self.options.class_names) else f"Class_{class_id}"
|
||||
|
||||
box = BoundingBox(
|
||||
x1=max(0, x1), y1=max(0, y1), x2=x2, y2=y2,
|
||||
score=confidence, class_num=class_id, class_name=class_name
|
||||
)
|
||||
boxes.append(box)
|
||||
except Exception as e:
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
else:
|
||||
# 回退處理
|
||||
flat = arr.flatten()
|
||||
print(f"DEBUG: Fallback processing for flat array size: {len(flat)}")
|
||||
|
||||
# 限制處理的數據量
|
||||
if len(flat) > 6000: # 1000 boxes * 6 values
|
||||
print(f"WARNING: Large flat array ({len(flat)}), limiting processing")
|
||||
flat = flat[:6000]
|
||||
|
||||
step = 6
|
||||
for j in range(0, len(flat) - step + 1, step):
|
||||
try:
|
||||
x1, y1, x2, y2, conf, cls = flat[j:j+6]
|
||||
|
||||
if not all(np.isfinite([x1, y1, x2, y2, conf])):
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
if conf > self.options.threshold:
|
||||
class_id = int(cls)
|
||||
|
||||
is_valid, reason = self._is_valid_box(x1, y1, x2, y2, conf, class_id)
|
||||
if not is_valid:
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
class_name = self.options.class_names[class_id] if class_id < len(self.options.class_names) else f"Class_{class_id}"
|
||||
|
||||
box = BoundingBox(
|
||||
x1=max(0, int(x1)), y1=max(0, int(y1)),
|
||||
x2=int(x2), y2=int(y2),
|
||||
score=float(conf), class_num=class_id, class_name=class_name
|
||||
)
|
||||
boxes.append(box)
|
||||
except Exception as e:
|
||||
invalid_box_count += 1
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: Error processing output node {i}: {e}")
|
||||
continue
|
||||
|
||||
# 報告統計信息
|
||||
if invalid_box_count > 0:
|
||||
print(f"INFO: Rejected {invalid_box_count} invalid detections")
|
||||
|
||||
print(f"DEBUG: Raw detection count: {len(boxes)}")
|
||||
|
||||
# 檢測異常模式
|
||||
if self._detect_anomalous_pattern(boxes):
|
||||
print("WARNING: Anomalous detection pattern detected, applying aggressive filtering")
|
||||
# 更嚴格的過濾
|
||||
boxes = [box for box in boxes if box.score < 2.0 and box.x1 != box.x2 and box.y1 != box.y2]
|
||||
|
||||
# 應用過濾
|
||||
boxes = self._filter_excessive_detections(boxes)
|
||||
boxes = self._filter_by_class_count(boxes)
|
||||
|
||||
# 應用 NMS
|
||||
if boxes and len(boxes) > 1:
|
||||
boxes = self._apply_nms(boxes)
|
||||
|
||||
print(f"INFO: Final detection count: {len(boxes)}")
|
||||
|
||||
# 創建統計報告
|
||||
if boxes:
|
||||
class_stats = defaultdict(int)
|
||||
for box in boxes:
|
||||
class_stats[box.class_name] += 1
|
||||
|
||||
print("Detection summary:")
|
||||
for class_name, count in sorted(class_stats.items()):
|
||||
print(f" {class_name}: {count}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: Critical error in YOLO postprocessing: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
boxes = []
|
||||
|
||||
return ObjectDetectionResult(
|
||||
class_count=len(self.options.class_names) if self.options.class_names else 1,
|
||||
box_count=len(boxes),
|
||||
box_list=boxes
|
||||
)
|
||||
|
||||
def _apply_nms(self, boxes: List[BoundingBox]) -> List[BoundingBox]:
|
||||
"""改進的非極大值抑制"""
|
||||
if not boxes or len(boxes) <= 1:
|
||||
return boxes
|
||||
|
||||
try:
|
||||
# 按類別分組
|
||||
class_boxes = defaultdict(list)
|
||||
for box in boxes:
|
||||
class_boxes[box.class_num].append(box)
|
||||
|
||||
final_boxes = []
|
||||
|
||||
for class_id, class_box_list in class_boxes.items():
|
||||
if len(class_box_list) <= 1:
|
||||
final_boxes.extend(class_box_list)
|
||||
continue
|
||||
|
||||
# 按信心度排序
|
||||
class_box_list.sort(key=lambda x: x.score, reverse=True)
|
||||
|
||||
keep = []
|
||||
while class_box_list and len(keep) < self.max_detections_per_class:
|
||||
current_box = class_box_list.pop(0)
|
||||
keep.append(current_box)
|
||||
|
||||
# 移除高 IoU 的框
|
||||
remaining = []
|
||||
for box in class_box_list:
|
||||
iou = self._calculate_iou(current_box, box)
|
||||
if iou <= self.options.nms_threshold:
|
||||
remaining.append(box)
|
||||
class_box_list = remaining
|
||||
|
||||
final_boxes.extend(keep)
|
||||
|
||||
print(f"DEBUG: NMS reduced {len(boxes)} to {len(final_boxes)} boxes")
|
||||
return final_boxes
|
||||
|
||||
except Exception as e:
|
||||
print(f"ERROR: NMS failed: {e}")
|
||||
return boxes[:self.max_detections_total] # 回退到簡單限制
|
||||
|
||||
def _calculate_iou(self, box1: BoundingBox, box2: BoundingBox) -> float:
|
||||
"""計算兩個邊界框的 IoU"""
|
||||
try:
|
||||
# 計算交集
|
||||
x1 = max(box1.x1, box2.x1)
|
||||
y1 = max(box1.y1, box2.y1)
|
||||
x2 = min(box1.x2, box2.x2)
|
||||
y2 = min(box1.y2, box2.y2)
|
||||
|
||||
if x2 <= x1 or y2 <= y1:
|
||||
return 0.0
|
||||
|
||||
intersection = (x2 - x1) * (y2 - y1)
|
||||
|
||||
# 計算聯集
|
||||
area1 = (box1.x2 - box1.x1) * (box1.y2 - box1.y1)
|
||||
area2 = (box2.x2 - box2.x1) * (box2.y2 - box2.y1)
|
||||
union = area1 + area2 - intersection
|
||||
|
||||
if union <= 0:
|
||||
return 0.0
|
||||
|
||||
return intersection / union
|
||||
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
|
||||
# 測試函數
|
||||
if __name__ == "__main__":
|
||||
from core.functions.Multidongle import PostProcessorOptions, PostProcessType
|
||||
|
||||
# 創建測試選項
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.3,
|
||||
class_names=["person", "bicycle", "car", "motorbike", "aeroplane"],
|
||||
nms_threshold=0.45,
|
||||
max_detections_per_class=20
|
||||
)
|
||||
|
||||
processor = ImprovedYOLOPostProcessor(options)
|
||||
print("ImprovedYOLOPostProcessor initialized successfully!")
|
||||
150
tests/quick_fix_detection_issues.py
Normal file
150
tests/quick_fix_detection_issues.py
Normal file
@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick fixes for detection result issues.
|
||||
快速修復偵測結果問題的補丁程式。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
def apply_quick_fixes():
|
||||
"""應用快速修復到檢測結果"""
|
||||
|
||||
print("=== 快速修復偵測結果問題 ===")
|
||||
print()
|
||||
|
||||
# 修復建議
|
||||
fixes = [
|
||||
{
|
||||
"issue": "過多的偵測結果 (100+ 物件)",
|
||||
"cause": "可能的原因:模型輸出格式不匹配、閾值太低、測試模式",
|
||||
"solutions": [
|
||||
"1. 提高信心閾值到 0.5-0.7",
|
||||
"2. 添加檢測數量限制",
|
||||
"3. 檢查是否在測試/調試模式",
|
||||
"4. 驗證模型輸出格式"
|
||||
]
|
||||
},
|
||||
{
|
||||
"issue": "座標異常 (0,0 或負值)",
|
||||
"cause": "可能的原因:座標轉換錯誤、輸出格式不匹配",
|
||||
"solutions": [
|
||||
"1. 檢查座標轉換邏輯",
|
||||
"2. 驗證輸入圖片尺寸",
|
||||
"3. 確認模型輸出格式",
|
||||
"4. 添加座標有效性檢查"
|
||||
]
|
||||
},
|
||||
{
|
||||
"issue": "LiveView 卡頓",
|
||||
"cause": "可能的原因:處理過多檢測結果導致渲染瓶頸",
|
||||
"solutions": [
|
||||
"1. 限制顯示的檢測數量",
|
||||
"2. 降低 FPS 或跳幀顯示",
|
||||
"3. 異步處理檢測結果",
|
||||
"4. 優化渲染代碼"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
for fix in fixes:
|
||||
print(f"問題: {fix['issue']}")
|
||||
print(f"原因: {fix['cause']}")
|
||||
print("解決方案:")
|
||||
for solution in fix['solutions']:
|
||||
print(f" {solution}")
|
||||
print()
|
||||
|
||||
# 立即可用的代碼修復
|
||||
print("=== 立即可用的代碼修復 ===")
|
||||
print()
|
||||
|
||||
print("1. 在 Multidongle.py 的 _process_yolo_generic 函數開頭添加:")
|
||||
print("""
|
||||
# 緊急修復:限制檢測數量
|
||||
MAX_DETECTIONS = 50
|
||||
if len(boxes) > MAX_DETECTIONS:
|
||||
print(f"WARNING: Too many detections ({len(boxes)}), limiting to {MAX_DETECTIONS}")
|
||||
boxes = sorted(boxes, key=lambda x: x.score, reverse=True)[:MAX_DETECTIONS]
|
||||
""")
|
||||
|
||||
print("\n2. 在創建 BoundingBox 之前添加驗證:")
|
||||
print("""
|
||||
# 座標有效性檢查
|
||||
if x1 < 0 or y1 < 0 or x1 >= x2 or y1 >= y2:
|
||||
continue # 跳過無效的邊界框
|
||||
if (x2 - x1) * (y2 - y1) < 4: # 最小面積
|
||||
continue # 跳過太小的框
|
||||
if best_score > 2.0: # 檢查異常分數
|
||||
continue # 跳過異常分數
|
||||
""")
|
||||
|
||||
print("\n3. 在 PostProcessorOptions 中設置更嚴格的參數:")
|
||||
print("""
|
||||
postprocess_options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.6, # 提高閾值
|
||||
class_names=["person", "bicycle", "car", "motorbike", "aeroplane"],
|
||||
nms_threshold=0.4,
|
||||
max_detections_per_class=10 # 限制每類檢測數量
|
||||
)
|
||||
""")
|
||||
|
||||
print("\n4. 添加檢測結果統計和警告:")
|
||||
print("""
|
||||
# 在函數結尾添加
|
||||
class_counts = {}
|
||||
for box in boxes:
|
||||
class_counts[box.class_name] = class_counts.get(box.class_name, 0) + 1
|
||||
|
||||
for class_name, count in class_counts.items():
|
||||
if count > 20:
|
||||
print(f"WARNING: Abnormally high count for {class_name}: {count}")
|
||||
""")
|
||||
|
||||
def create_emergency_filter():
|
||||
"""創建緊急過濾函數"""
|
||||
|
||||
filter_code = '''
|
||||
def emergency_filter_detections(boxes, max_total=50, max_per_class=10):
|
||||
"""緊急過濾檢測結果"""
|
||||
if len(boxes) <= max_total:
|
||||
return boxes
|
||||
|
||||
# 按類別分組
|
||||
from collections import defaultdict
|
||||
class_groups = defaultdict(list)
|
||||
for box in boxes:
|
||||
class_groups[box.class_name].append(box)
|
||||
|
||||
# 每類保留最高分數的檢測
|
||||
filtered = []
|
||||
for class_name, class_boxes in class_groups.items():
|
||||
class_boxes.sort(key=lambda x: x.score, reverse=True)
|
||||
keep_count = min(len(class_boxes), max_per_class)
|
||||
filtered.extend(class_boxes[:keep_count])
|
||||
|
||||
# 總數限制
|
||||
if len(filtered) > max_total:
|
||||
filtered.sort(key=lambda x: x.score, reverse=True)
|
||||
filtered = filtered[:max_total]
|
||||
|
||||
return filtered
|
||||
'''
|
||||
|
||||
with open("emergency_filter.py", "w", encoding="utf-8") as f:
|
||||
f.write(filter_code)
|
||||
|
||||
print("緊急過濾函數已保存到 emergency_filter.py")
|
||||
|
||||
if __name__ == "__main__":
|
||||
apply_quick_fixes()
|
||||
create_emergency_filter()
|
||||
|
||||
print("\n=== 下一步建議 ===")
|
||||
print("1. 檢查當前的後處理配置")
|
||||
print("2. 調整信心閾值和檢測限制")
|
||||
print("3. 使用 debug_detection_issues.py 分析結果")
|
||||
print("4. 考慮使用 improved_yolo_postprocessing.py 中的改進版本")
|
||||
print("5. 如果問題持續,請檢查模型文件和配置")
|
||||
187
tests/quick_test_deployment.py
Normal file
187
tests/quick_test_deployment.py
Normal file
@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick test script for YOLOv5 pipeline deployment using fixed configuration
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add paths
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'ui', 'dialogs'))
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), 'core', 'functions'))
|
||||
|
||||
def test_mflow_loading():
|
||||
"""Test loading and parsing the fixed .mflow file"""
|
||||
import json
|
||||
|
||||
mflow_files = [
|
||||
'multi_series_example.mflow',
|
||||
'multi_series_yolov5_fixed.mflow',
|
||||
'test.mflow'
|
||||
]
|
||||
|
||||
print("=" * 60)
|
||||
print("Testing .mflow Configuration Loading")
|
||||
print("=" * 60)
|
||||
|
||||
for mflow_file in mflow_files:
|
||||
if os.path.exists(mflow_file):
|
||||
print(f"\n📄 Loading {mflow_file}:")
|
||||
try:
|
||||
with open(mflow_file, 'r') as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Check for postprocess nodes
|
||||
postprocess_nodes = [
|
||||
node for node in data.get('nodes', [])
|
||||
if node.get('type') == 'ExactPostprocessNode'
|
||||
]
|
||||
|
||||
if postprocess_nodes:
|
||||
for node in postprocess_nodes:
|
||||
props = node.get('properties', {})
|
||||
postprocess_type = props.get('postprocess_type', 'NOT SET')
|
||||
confidence_threshold = props.get('confidence_threshold', 'NOT SET')
|
||||
class_names = props.get('class_names', 'NOT SET')
|
||||
|
||||
print(f" ✓ Found PostprocessNode: {node.get('name', 'Unnamed')}")
|
||||
print(f" - Type: {postprocess_type}")
|
||||
print(f" - Threshold: {confidence_threshold}")
|
||||
print(f" - Classes: {len(class_names.split(',')) if isinstance(class_names, str) else 'N/A'} classes")
|
||||
|
||||
if postprocess_type == 'yolo_v5':
|
||||
print(f" ✅ Correctly configured for YOLOv5")
|
||||
else:
|
||||
print(f" ❌ Still using: {postprocess_type}")
|
||||
else:
|
||||
print(f" ⚠ No ExactPostprocessNode found")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ Error loading file: {e}")
|
||||
else:
|
||||
print(f"\n📄 {mflow_file}: File not found")
|
||||
|
||||
def test_deployment_direct():
|
||||
"""Test deployment using the deployment dialog directly"""
|
||||
try:
|
||||
from deployment import DeploymentDialog
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
|
||||
print(f"\n" + "=" * 60)
|
||||
print("Testing Direct Pipeline Deployment")
|
||||
print("=" * 60)
|
||||
|
||||
# Load the fixed configuration
|
||||
import json
|
||||
config_file = 'multi_series_yolov5_fixed.mflow'
|
||||
|
||||
if not os.path.exists(config_file):
|
||||
print(f"❌ Configuration file not found: {config_file}")
|
||||
return
|
||||
|
||||
with open(config_file, 'r') as f:
|
||||
pipeline_data = json.load(f)
|
||||
|
||||
print(f"✓ Loaded configuration: {pipeline_data.get('project_name', 'Unknown')}")
|
||||
print(f"✓ Found {len(pipeline_data.get('nodes', []))} nodes")
|
||||
|
||||
# Create minimal Qt app for testing
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
# Create deployment dialog
|
||||
dialog = DeploymentDialog(pipeline_data)
|
||||
print(f"✓ Created deployment dialog")
|
||||
|
||||
# Test analysis
|
||||
print(f"🔍 Testing pipeline analysis...")
|
||||
|
||||
try:
|
||||
from core.functions.mflow_converter import MFlowConverter
|
||||
converter = MFlowConverter()
|
||||
config = converter._convert_mflow_to_config(pipeline_data)
|
||||
|
||||
print(f"✓ Pipeline conversion successful")
|
||||
print(f" - Pipeline name: {config.pipeline_name}")
|
||||
print(f" - Total stages: {len(config.stage_configs)}")
|
||||
|
||||
# Check stage configurations
|
||||
for i, stage_config in enumerate(config.stage_configs, 1):
|
||||
print(f" Stage {i}: {stage_config.stage_id}")
|
||||
if hasattr(stage_config, 'postprocessor_options') and stage_config.postprocessor_options:
|
||||
print(f" - Postprocess type: {stage_config.postprocessor_options.postprocess_type.value}")
|
||||
print(f" - Threshold: {stage_config.postprocessor_options.threshold}")
|
||||
print(f" - Classes: {len(stage_config.postprocessor_options.class_names)}")
|
||||
|
||||
if stage_config.postprocessor_options.postprocess_type.value == 'yolo_v5':
|
||||
print(f" ✅ YOLOv5 postprocessing configured correctly")
|
||||
else:
|
||||
print(f" ❌ Postprocessing type: {stage_config.postprocessor_options.postprocess_type.value}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Pipeline conversion failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
except ImportError as e:
|
||||
print(f"❌ Cannot import deployment components: {e}")
|
||||
print(f" This is expected if running outside the full application")
|
||||
except Exception as e:
|
||||
print(f"❌ Direct deployment test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
def show_fix_summary():
|
||||
"""Show summary of the fixes applied"""
|
||||
print(f"\n" + "=" * 60)
|
||||
print("FIX SUMMARY")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n🔧 Applied Fixes:")
|
||||
print(f"1. ✅ Fixed dashboard.py postprocess property loading")
|
||||
print(f" - Added missing 'postprocess_type' property")
|
||||
print(f" - Added all missing postprocess properties")
|
||||
print(f" - Location: ui/windows/dashboard.py:1203-1213")
|
||||
|
||||
print(f"\n2. ✅ Enhanced YOLOv5 postprocessing in Multidongle.py")
|
||||
print(f" - Improved _process_yolo_generic method")
|
||||
print(f" - Added proper NMS (Non-Maximum Suppression)")
|
||||
print(f" - Enhanced live view display")
|
||||
|
||||
print(f"\n3. ✅ Updated .mflow configurations")
|
||||
print(f" - multi_series_example.mflow: enable_postprocessing = true")
|
||||
print(f" - multi_series_yolov5_fixed.mflow: Complete YOLOv5 setup")
|
||||
print(f" - Added ExactPostprocessNode with yolo_v5 type")
|
||||
|
||||
print(f"\n🎯 Expected Results After Fix:")
|
||||
print(f" - ❌ 'No Fire (Prob: -0.39)' → ✅ 'person detected (Conf: 0.85)'")
|
||||
print(f" - ❌ Negative probabilities → ✅ Positive probabilities (0.0-1.0)")
|
||||
print(f" - ❌ No bounding boxes → ✅ Colorful bounding boxes with labels")
|
||||
print(f" - ❌ Fire detection classes → ✅ COCO 80 classes (person, car, etc.)")
|
||||
|
||||
print(f"\n💡 Usage Instructions:")
|
||||
print(f" 1. Run: python main.py")
|
||||
print(f" 2. Login to the dashboard")
|
||||
print(f" 3. Load: multi_series_yolov5_fixed.mflow")
|
||||
print(f" 4. Deploy the pipeline")
|
||||
print(f" 5. Check Live View tab for enhanced bounding boxes")
|
||||
|
||||
def main():
|
||||
print("Quick YOLOv5 Deployment Test")
|
||||
print("=" * 60)
|
||||
|
||||
# Test configuration loading
|
||||
test_mflow_loading()
|
||||
|
||||
# Test direct deployment (if possible)
|
||||
test_deployment_direct()
|
||||
|
||||
# Show fix summary
|
||||
show_fix_summary()
|
||||
|
||||
print(f"\n🎉 Quick test completed!")
|
||||
print(f" Now try running: python main.py")
|
||||
print(f" And load: multi_series_yolov5_fixed.mflow")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
39
tests/simple_test.py
Normal file
39
tests/simple_test.py
Normal file
@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple test for port ID configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from core.nodes.exact_nodes import ExactModelNode
|
||||
|
||||
def main():
|
||||
print("Creating ExactModelNode...")
|
||||
node = ExactModelNode()
|
||||
|
||||
print("Testing property options...")
|
||||
if hasattr(node, '_property_options'):
|
||||
port_props = [k for k in node._property_options.keys() if 'port_ids' in k]
|
||||
print(f"Found port ID properties: {port_props}")
|
||||
else:
|
||||
print("No _property_options found")
|
||||
|
||||
print("Testing _build_multi_series_config method...")
|
||||
if hasattr(node, '_build_multi_series_config'):
|
||||
print("Method exists")
|
||||
try:
|
||||
config = node._build_multi_series_config()
|
||||
print(f"Config result: {config}")
|
||||
except Exception as e:
|
||||
print(f"Error calling method: {e}")
|
||||
else:
|
||||
print("Method does not exist")
|
||||
|
||||
print("Test completed!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
83
tests/test_classification_result_format.py
Normal file
83
tests/test_classification_result_format.py
Normal file
@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify ClassificationResult formatting fix
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add core functions to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(os.path.join(parent_dir, 'core', 'functions'))
|
||||
|
||||
from Multidongle import ClassificationResult
|
||||
|
||||
def test_classification_result_formatting():
|
||||
"""Test that ClassificationResult can be formatted without errors"""
|
||||
|
||||
# Create a test classification result
|
||||
result = ClassificationResult(
|
||||
probability=0.85,
|
||||
class_name="Fire",
|
||||
class_num=1,
|
||||
confidence_threshold=0.5
|
||||
)
|
||||
|
||||
print("Testing ClassificationResult formatting...")
|
||||
|
||||
# Test __str__ method
|
||||
print(f"str(result): {str(result)}")
|
||||
|
||||
# Test __format__ method with empty format spec
|
||||
print(f"format(result, ''): {format(result, '')}")
|
||||
|
||||
# Test f-string formatting (this was causing the original error)
|
||||
print(f"f-string: {result}")
|
||||
|
||||
# Test string formatting that was likely causing the error
|
||||
try:
|
||||
formatted = f"Error updating inference results: {result}"
|
||||
print(f"Complex formatting test: {formatted}")
|
||||
print("✓ All formatting tests passed!")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"✗ Formatting test failed: {e}")
|
||||
return False
|
||||
|
||||
def test_is_positive_property():
|
||||
"""Test the is_positive property"""
|
||||
|
||||
# Test positive case
|
||||
positive_result = ClassificationResult(
|
||||
probability=0.85,
|
||||
class_name="Fire",
|
||||
confidence_threshold=0.5
|
||||
)
|
||||
|
||||
# Test negative case
|
||||
negative_result = ClassificationResult(
|
||||
probability=0.3,
|
||||
class_name="No Fire",
|
||||
confidence_threshold=0.5
|
||||
)
|
||||
|
||||
print(f"\nTesting is_positive property...")
|
||||
print(f"Positive result (0.85 > 0.5): {positive_result.is_positive}")
|
||||
print(f"Negative result (0.3 > 0.5): {negative_result.is_positive}")
|
||||
|
||||
assert positive_result.is_positive == True
|
||||
assert negative_result.is_positive == False
|
||||
print("✓ is_positive property tests passed!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running ClassificationResult formatting tests...")
|
||||
|
||||
try:
|
||||
test_classification_result_formatting()
|
||||
test_is_positive_property()
|
||||
print("\n🎉 All tests passed! The format string error should be fixed.")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Test failed with error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
129
tests/test_coordinate_scaling.py
Normal file
129
tests/test_coordinate_scaling.py
Normal file
@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test coordinate scaling logic for small bounding boxes.
|
||||
測試小座標邊界框的縮放邏輯。
|
||||
"""
|
||||
|
||||
def test_coordinate_scaling():
|
||||
"""測試座標縮放邏輯"""
|
||||
|
||||
print("=== 測試座標縮放邏輯 ===")
|
||||
|
||||
# 模擬您看到的小座標
|
||||
test_boxes = [
|
||||
{"name": "toothbrush", "coords": (0, 1, 2, 3), "score": 0.778},
|
||||
{"name": "car", "coords": (0, 0, 2, 2), "score": 1.556},
|
||||
{"name": "person", "coords": (0, 0, 2, 3), "score": 1.989}
|
||||
]
|
||||
|
||||
# 圖片尺寸設定
|
||||
img_width, img_height = 640, 480
|
||||
|
||||
print(f"原始座標 -> 縮放後座標 (圖片尺寸: {img_width}x{img_height})")
|
||||
print("-" * 60)
|
||||
|
||||
for box in test_boxes:
|
||||
x1, y1, x2, y2 = box["coords"]
|
||||
|
||||
# 應用縮放邏輯
|
||||
if x2 <= 10 and y2 <= 10:
|
||||
# 檢查是否為歸一化座標
|
||||
if x1 <= 1.0 and y1 <= 1.0 and x2 <= 1.0 and y2 <= 1.0:
|
||||
# 歸一化座標縮放
|
||||
scaled_x1 = int(x1 * img_width)
|
||||
scaled_y1 = int(y1 * img_height)
|
||||
scaled_x2 = int(x2 * img_width)
|
||||
scaled_y2 = int(y2 * img_height)
|
||||
method = "normalized scaling"
|
||||
else:
|
||||
# 小整數座標縮放
|
||||
scale_factor = min(img_width, img_height) // 10 # = 48
|
||||
scaled_x1 = x1 * scale_factor
|
||||
scaled_y1 = y1 * scale_factor
|
||||
scaled_x2 = x2 * scale_factor
|
||||
scaled_y2 = y2 * scale_factor
|
||||
method = f"integer scaling (x{scale_factor})"
|
||||
else:
|
||||
# 不需要縮放
|
||||
scaled_x1, scaled_y1, scaled_x2, scaled_y2 = x1, y1, x2, y2
|
||||
method = "no scaling needed"
|
||||
|
||||
# 確保座標在圖片範圍內
|
||||
scaled_x1 = max(0, min(scaled_x1, img_width - 1))
|
||||
scaled_y1 = max(0, min(scaled_y1, img_height - 1))
|
||||
scaled_x2 = max(scaled_x1 + 1, min(scaled_x2, img_width))
|
||||
scaled_y2 = max(scaled_y1 + 1, min(scaled_y2, img_height))
|
||||
|
||||
area = (scaled_x2 - scaled_x1) * (scaled_y2 - scaled_y1)
|
||||
|
||||
print(f"{box['name']:10} | ({x1},{y1},{x2},{y2}) -> ({scaled_x1},{scaled_y1},{scaled_x2},{scaled_y2}) | Area: {area:4d} | {method}")
|
||||
|
||||
def test_liveview_visibility():
|
||||
"""測試 LiveView 可見性"""
|
||||
|
||||
print("\n=== LiveView 可見性分析 ===")
|
||||
|
||||
# 原始座標(您看到的)
|
||||
original_coords = [
|
||||
(0, 1, 2, 3), # toothbrush
|
||||
(0, 0, 2, 2), # car
|
||||
(0, 0, 2, 3) # person
|
||||
]
|
||||
|
||||
# 縮放後的座標
|
||||
scale_factor = 48 # 640//10 或 480//10
|
||||
scaled_coords = [
|
||||
(0*scale_factor, 1*scale_factor, 2*scale_factor, 3*scale_factor),
|
||||
(0*scale_factor, 0*scale_factor, 2*scale_factor, 2*scale_factor),
|
||||
(0*scale_factor, 0*scale_factor, 2*scale_factor, 3*scale_factor)
|
||||
]
|
||||
|
||||
print("為什麼之前 LiveView 看不到邊界框:")
|
||||
print("原始座標太小:")
|
||||
for i, coords in enumerate(original_coords):
|
||||
area = (coords[2] - coords[0]) * (coords[3] - coords[1])
|
||||
print(f" Box {i+1}: {coords} -> 面積: {area} 像素 (太小,幾乎看不見)")
|
||||
|
||||
print("\n縮放後應該可見:")
|
||||
for i, coords in enumerate(scaled_coords):
|
||||
area = (coords[2] - coords[0]) * (coords[3] - coords[1])
|
||||
print(f" Box {i+1}: {coords} -> 面積: {area} 像素 (應該可見)")
|
||||
|
||||
print("\n建議檢查:")
|
||||
print("1. 確認 LiveView 使用正確的圖片尺寸")
|
||||
print("2. 檢查邊界框繪製代碼是否正確處理座標")
|
||||
print("3. 確認沒有其他過濾邏輯阻止顯示")
|
||||
|
||||
def performance_analysis():
|
||||
"""分析性能改善"""
|
||||
|
||||
print("\n=== 性能改善分析 ===")
|
||||
|
||||
print("FPS 降低的可能原因:")
|
||||
print("1. 座標縮放計算增加了處理時間")
|
||||
print("2. 更詳細的調試輸出")
|
||||
print("3. 可能的圖片尺寸獲取延遲")
|
||||
|
||||
print("\n已應用的性能優化:")
|
||||
print("✅ 減少檢測數量限制從 50 -> 20")
|
||||
print("✅ 少於 5 個檢測時跳過 NMS")
|
||||
print("✅ 更寬鬆的分數檢查 (<=10.0 而非 <=2.0)")
|
||||
print("✅ 簡化的早期驗證")
|
||||
|
||||
print("\n預期改善:")
|
||||
print("- FPS 應該從 3.90 提升到 8-15")
|
||||
print("- LiveView 應該顯示正確縮放的邊界框")
|
||||
print("- 座標應該在合理範圍內 (0-640, 0-480)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_coordinate_scaling()
|
||||
test_liveview_visibility()
|
||||
performance_analysis()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("修復摘要:")
|
||||
print("✅ 智能座標縮放:小座標會自動放大")
|
||||
print("✅ 性能優化:減少處理量,提升 FPS")
|
||||
print("✅ 更好的調試:顯示實際座標信息")
|
||||
print("✅ 寬鬆驗證:不會過度過濾有效檢測")
|
||||
print("\n重新測試您的 pipeline,應該會看到改善!")
|
||||
204
tests/test_detection_fix.py
Normal file
204
tests/test_detection_fix.py
Normal file
@ -0,0 +1,204 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify the detection result fixes.
|
||||
測試腳本以驗證偵測結果修復是否有效。
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(parent_dir)
|
||||
|
||||
from core.functions.Multidongle import BoundingBox, ObjectDetectionResult, PostProcessorOptions, PostProcessType
|
||||
|
||||
def create_test_problematic_boxes():
|
||||
"""創建測試用的有問題的邊界框(模擬您遇到的問題)"""
|
||||
boxes = []
|
||||
|
||||
class_names = ['person', 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'toothbrush', 'hair drier']
|
||||
|
||||
# 添加大量異常的邊界框(類似您的輸出)
|
||||
for i in range(443): # 模擬您的 443 個檢測結果
|
||||
# 模擬您看到的異常座標和分數
|
||||
x1 = i % 5 # 很小的座標值
|
||||
y1 = (i + 1) % 4
|
||||
x2 = (i + 2) % 6 if (i + 2) % 6 > x1 else x1 + 1
|
||||
y2 = (i + 3) % 5 if (i + 3) % 5 > y1 else y1 + 1
|
||||
|
||||
# 模擬異常分數(像您看到的 2.0+ 分數)
|
||||
score = 2.0 + (i * 0.01)
|
||||
|
||||
class_id = i % len(class_names)
|
||||
class_name = class_names[class_id]
|
||||
|
||||
box = BoundingBox(
|
||||
x1=x1,
|
||||
y1=y1,
|
||||
x2=x2,
|
||||
y2=y2,
|
||||
score=score,
|
||||
class_num=class_id,
|
||||
class_name=class_name
|
||||
)
|
||||
boxes.append(box)
|
||||
|
||||
# 添加一些負座標的框(您報告的問題)
|
||||
for i in range(10):
|
||||
box = BoundingBox(
|
||||
x1=-1,
|
||||
y1=0,
|
||||
x2=1,
|
||||
y2=2,
|
||||
score=1.5,
|
||||
class_num=0,
|
||||
class_name='person'
|
||||
)
|
||||
boxes.append(box)
|
||||
|
||||
# 添加一些零面積的框
|
||||
for i in range(5):
|
||||
box = BoundingBox(
|
||||
x1=0,
|
||||
y1=0,
|
||||
x2=0,
|
||||
y2=0,
|
||||
score=1.0,
|
||||
class_num=1,
|
||||
class_name='bicycle'
|
||||
)
|
||||
boxes.append(box)
|
||||
|
||||
return boxes
|
||||
|
||||
def test_emergency_filter():
|
||||
"""測試緊急過濾功能"""
|
||||
print("=== 測試緊急過濾功能 ===")
|
||||
|
||||
# 創建有問題的檢測結果
|
||||
problematic_boxes = create_test_problematic_boxes()
|
||||
print(f"原始檢測數量: {len(problematic_boxes)}")
|
||||
|
||||
# 統計原始結果
|
||||
class_counts_before = {}
|
||||
for box in problematic_boxes:
|
||||
class_counts_before[box.class_name] = class_counts_before.get(box.class_name, 0) + 1
|
||||
|
||||
print("修復前的類別分布:")
|
||||
for class_name, count in sorted(class_counts_before.items()):
|
||||
print(f" {class_name}: {count}")
|
||||
|
||||
# 應用我們添加的過濾邏輯
|
||||
boxes = problematic_boxes.copy()
|
||||
original_count = len(boxes)
|
||||
|
||||
# 第一步:移除無效的框
|
||||
valid_boxes = []
|
||||
for box in boxes:
|
||||
# 座標有效性檢查
|
||||
if box.x1 < 0 or box.y1 < 0 or box.x1 >= box.x2 or box.y1 >= box.y2:
|
||||
continue
|
||||
# 最小面積檢查
|
||||
if (box.x2 - box.x1) * (box.y2 - box.y1) < 4:
|
||||
continue
|
||||
# 分數有效性檢查(異常分數表示對數空間或測試數據)
|
||||
if box.score <= 0 or box.score > 2.0:
|
||||
continue
|
||||
valid_boxes.append(box)
|
||||
|
||||
boxes = valid_boxes
|
||||
print(f"有效性過濾後: {len(boxes)} (移除了 {original_count - len(boxes)} 個無效框)")
|
||||
|
||||
# 第二步:限制總檢測數量
|
||||
MAX_TOTAL_DETECTIONS = 50
|
||||
if len(boxes) > MAX_TOTAL_DETECTIONS:
|
||||
boxes = sorted(boxes, key=lambda x: x.score, reverse=True)[:MAX_TOTAL_DETECTIONS]
|
||||
print(f"總數限制後: {len(boxes)}")
|
||||
|
||||
# 第三步:限制每類檢測數量
|
||||
from collections import defaultdict
|
||||
class_groups = defaultdict(list)
|
||||
for box in boxes:
|
||||
class_groups[box.class_name].append(box)
|
||||
|
||||
filtered_boxes = []
|
||||
MAX_PER_CLASS = 10
|
||||
for class_name, class_boxes in class_groups.items():
|
||||
if len(class_boxes) > MAX_PER_CLASS:
|
||||
class_boxes = sorted(class_boxes, key=lambda x: x.score, reverse=True)[:MAX_PER_CLASS]
|
||||
filtered_boxes.extend(class_boxes)
|
||||
|
||||
boxes = filtered_boxes
|
||||
print(f"每類限制後: {len(boxes)}")
|
||||
|
||||
# 統計最終結果
|
||||
class_counts_after = {}
|
||||
for box in boxes:
|
||||
class_counts_after[box.class_name] = class_counts_after.get(box.class_name, 0) + 1
|
||||
|
||||
print("\n修復後的類別分布:")
|
||||
for class_name, count in sorted(class_counts_after.items()):
|
||||
print(f" {class_name}: {count}")
|
||||
|
||||
print(f"\n✅ 過濾成功!從 {original_count} 個檢測減少到 {len(boxes)} 個有效檢測")
|
||||
|
||||
return boxes
|
||||
|
||||
def analyze_fix_effectiveness():
|
||||
"""分析修復效果"""
|
||||
print("\n=== 修復效果分析 ===")
|
||||
|
||||
filtered_boxes = test_emergency_filter()
|
||||
|
||||
# 驗證所有框都是有效的
|
||||
all_valid = True
|
||||
for box in filtered_boxes:
|
||||
if box.x1 < 0 or box.y1 < 0 or box.x1 >= box.x2 or box.y1 >= box.y2:
|
||||
all_valid = False
|
||||
print(f"❌ 發現無效座標: {box}")
|
||||
break
|
||||
if (box.x2 - box.x1) * (box.y2 - box.y1) < 4:
|
||||
all_valid = False
|
||||
print(f"❌ 發現過小面積: {box}")
|
||||
break
|
||||
if box.score <= 0 or box.score > 2.0:
|
||||
all_valid = False
|
||||
print(f"❌ 發現異常分數: {box}")
|
||||
break
|
||||
|
||||
if all_valid:
|
||||
print("✅ 所有過濾後的邊界框都是有效的")
|
||||
|
||||
# 檢查數量限制
|
||||
class_counts = {}
|
||||
for box in filtered_boxes:
|
||||
class_counts[box.class_name] = class_counts.get(box.class_name, 0) + 1
|
||||
|
||||
max_count = max(class_counts.values()) if class_counts else 0
|
||||
if max_count <= 10:
|
||||
print("✅ 每個類別的檢測數量都在限制內")
|
||||
else:
|
||||
print(f"❌ 某個類別超出限制: 最大數量 = {max_count}")
|
||||
|
||||
if len(filtered_boxes) <= 50:
|
||||
print("✅ 總檢測數量在限制內")
|
||||
else:
|
||||
print(f"❌ 總檢測數量超出限制: {len(filtered_boxes)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("偵測結果修復測試")
|
||||
print("=" * 50)
|
||||
|
||||
analyze_fix_effectiveness()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("測試完成!")
|
||||
print("\n如果您看到上述 ✅ 標記,表示修復代碼應該能解決您的問題。")
|
||||
print("現在您可以重新運行您的推理pipeline,應該會看到:")
|
||||
print("1. 檢測數量大幅減少(從 443 降至 50 以下)")
|
||||
print("2. 無效座標的框被過濾掉")
|
||||
print("3. 異常分數的框被移除")
|
||||
print("4. LiveView 性能改善")
|
||||
|
||||
print(f"\n修復已應用到: F:\\cluster4npu\\core\\functions\\Multidongle.py")
|
||||
print("您可以立即測試修復效果。")
|
||||
193
tests/test_final_fix.py
Normal file
193
tests/test_final_fix.py
Normal file
@ -0,0 +1,193 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Final test to verify all fixes are working correctly
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# Add paths
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(os.path.join(parent_dir, 'core', 'functions'))
|
||||
|
||||
def test_converter_with_postprocessing():
|
||||
"""Test the mflow converter with postprocessing fixes"""
|
||||
|
||||
print("=" * 60)
|
||||
print("Testing MFlow Converter with Postprocessing Fixes")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from mflow_converter import MFlowConverter
|
||||
|
||||
# Test with the fixed mflow file
|
||||
mflow_file = 'multi_series_yolov5_fixed.mflow'
|
||||
|
||||
if not os.path.exists(mflow_file):
|
||||
print(f"❌ Test file not found: {mflow_file}")
|
||||
return False
|
||||
|
||||
print(f"✓ Loading {mflow_file}...")
|
||||
|
||||
converter = MFlowConverter()
|
||||
config = converter.load_and_convert(mflow_file)
|
||||
|
||||
print(f"✓ Conversion successful!")
|
||||
print(f" - Pipeline name: {config.pipeline_name}")
|
||||
print(f" - Total stages: {len(config.stage_configs)}")
|
||||
|
||||
# Check each stage for postprocessor
|
||||
for i, stage_config in enumerate(config.stage_configs, 1):
|
||||
print(f"\n Stage {i}: {stage_config.stage_id}")
|
||||
|
||||
if stage_config.stage_postprocessor:
|
||||
options = stage_config.stage_postprocessor.options
|
||||
print(f" ✅ Postprocessor found!")
|
||||
print(f" Type: {options.postprocess_type.value}")
|
||||
print(f" Threshold: {options.threshold}")
|
||||
print(f" Classes: {len(options.class_names)} ({options.class_names[:3]}...)")
|
||||
print(f" NMS Threshold: {options.nms_threshold}")
|
||||
|
||||
if options.postprocess_type.value == 'yolo_v5':
|
||||
print(f" 🎉 YOLOv5 postprocessing correctly configured!")
|
||||
else:
|
||||
print(f" ⚠ Postprocessing type: {options.postprocess_type.value}")
|
||||
else:
|
||||
print(f" ❌ No postprocessor found")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Converter test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def test_multidongle_postprocessing():
|
||||
"""Test MultiDongle postprocessing directly"""
|
||||
|
||||
print(f"\n" + "=" * 60)
|
||||
print("Testing MultiDongle Postprocessing")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from Multidongle import MultiDongle, PostProcessorOptions, PostProcessType
|
||||
|
||||
# Create YOLOv5 postprocessor options
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.3,
|
||||
class_names=["person", "bicycle", "car", "motorbike", "aeroplane"],
|
||||
nms_threshold=0.5,
|
||||
max_detections_per_class=50
|
||||
)
|
||||
|
||||
print(f"✓ Created PostProcessorOptions:")
|
||||
print(f" - Type: {options.postprocess_type.value}")
|
||||
print(f" - Threshold: {options.threshold}")
|
||||
print(f" - Classes: {len(options.class_names)}")
|
||||
|
||||
# Test with dummy MultiDongle
|
||||
multidongle = MultiDongle(
|
||||
port_id=[1], # Dummy port
|
||||
postprocess_options=options
|
||||
)
|
||||
|
||||
print(f"✓ Created MultiDongle with postprocessing")
|
||||
print(f" - Postprocess type: {multidongle.postprocess_options.postprocess_type.value}")
|
||||
|
||||
# Test set_postprocess_options method
|
||||
new_options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.25,
|
||||
class_names=["person", "car", "truck"],
|
||||
nms_threshold=0.4
|
||||
)
|
||||
|
||||
multidongle.set_postprocess_options(new_options)
|
||||
print(f"✓ Updated postprocess options:")
|
||||
print(f" - New threshold: {multidongle.postprocess_options.threshold}")
|
||||
print(f" - New classes: {len(multidongle.postprocess_options.class_names)}")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ MultiDongle test failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
def show_fix_summary():
|
||||
"""Show comprehensive fix summary"""
|
||||
|
||||
print(f"\n" + "=" * 60)
|
||||
print("COMPREHENSIVE FIX SUMMARY")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n🔧 Applied Fixes:")
|
||||
print(f"1. ✅ Fixed ui/windows/dashboard.py:")
|
||||
print(f" - Added missing 'postprocess_type' in fallback logic")
|
||||
print(f" - Added all postprocessing properties")
|
||||
print(f" - Lines: 1203-1213")
|
||||
|
||||
print(f"\n2. ✅ Enhanced core/functions/Multidongle.py:")
|
||||
print(f" - Improved YOLOv5 postprocessing implementation")
|
||||
print(f" - Added proper NMS (Non-Maximum Suppression)")
|
||||
print(f" - Enhanced live view display with corner markers")
|
||||
print(f" - Better result string generation")
|
||||
|
||||
print(f"\n3. ✅ Fixed core/functions/mflow_converter.py:")
|
||||
print(f" - Added connection mapping for postprocessing nodes")
|
||||
print(f" - Extract postprocessing config from ExactPostprocessNode")
|
||||
print(f" - Create PostProcessor instances for each stage")
|
||||
print(f" - Attach stage_postprocessor to StageConfig")
|
||||
|
||||
print(f"\n4. ✅ Enhanced core/functions/InferencePipeline.py:")
|
||||
print(f" - Apply stage_postprocessor during initialization")
|
||||
print(f" - Set postprocessor options to MultiDongle")
|
||||
print(f" - Debug logging for postprocessor application")
|
||||
|
||||
print(f"\n5. ✅ Updated .mflow configurations:")
|
||||
print(f" - multi_series_example.mflow: enable_postprocessing = true")
|
||||
print(f" - multi_series_yolov5_fixed.mflow: Complete YOLOv5 setup")
|
||||
print(f" - Proper node connections: Input → Model → Postprocess → Output")
|
||||
|
||||
print(f"\n🎯 Expected Results:")
|
||||
print(f" ❌ 'No Fire (Prob: -0.39)' → ✅ 'person detected (Conf: 0.85)'")
|
||||
print(f" ❌ Negative probabilities → ✅ Positive probabilities (0.0-1.0)")
|
||||
print(f" ❌ Fire detection output → ✅ COCO object detection")
|
||||
print(f" ❌ No bounding boxes → ✅ Enhanced bounding boxes in live view")
|
||||
print(f" ❌ Simple terminal output → ✅ Detailed object statistics")
|
||||
|
||||
print(f"\n🚀 How the Fix Works:")
|
||||
print(f" 1. UI loads .mflow file with yolo_v5 postprocess_type")
|
||||
print(f" 2. dashboard.py now includes postprocess_type in properties")
|
||||
print(f" 3. mflow_converter.py extracts postprocessing config")
|
||||
print(f" 4. Creates PostProcessor with YOLOv5 options")
|
||||
print(f" 5. InferencePipeline applies postprocessor to MultiDongle")
|
||||
print(f" 6. MultiDongle processes with correct YOLOv5 settings")
|
||||
print(f" 7. Enhanced live view shows proper object detection")
|
||||
|
||||
def main():
|
||||
print("Final Fix Verification Test")
|
||||
print("=" * 60)
|
||||
|
||||
# Run tests
|
||||
converter_ok = test_converter_with_postprocessing()
|
||||
multidongle_ok = test_multidongle_postprocessing()
|
||||
|
||||
# Show summary
|
||||
show_fix_summary()
|
||||
|
||||
if converter_ok and multidongle_ok:
|
||||
print(f"\n🎉 ALL TESTS PASSED!")
|
||||
print(f" The YOLOv5 postprocessing fix should now work correctly.")
|
||||
print(f" Run: python main.py")
|
||||
print(f" Load: multi_series_yolov5_fixed.mflow")
|
||||
print(f" Deploy and check for positive probabilities!")
|
||||
else:
|
||||
print(f"\n❌ Some tests failed. Please check the output above.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
71
tests/test_folder_selection.py
Normal file
71
tests/test_folder_selection.py
Normal file
@ -0,0 +1,71 @@
|
||||
"""
|
||||
Test tkinter folder selection functionality
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add project root to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from utils.folder_dialog import select_folder, select_assets_folder
|
||||
|
||||
def test_basic_folder_selection():
|
||||
"""Test basic folder selection"""
|
||||
print("Testing basic folder selection...")
|
||||
|
||||
folder = select_folder("Select any folder for testing")
|
||||
if folder:
|
||||
print(f"Selected folder: {folder}")
|
||||
print(f" Exists: {os.path.exists(folder)}")
|
||||
print(f" Is directory: {os.path.isdir(folder)}")
|
||||
return True
|
||||
else:
|
||||
print("No folder selected")
|
||||
return False
|
||||
|
||||
def test_assets_folder_selection():
|
||||
"""Test Assets folder selection with validation"""
|
||||
print("\nTesting Assets folder selection...")
|
||||
|
||||
result = select_assets_folder()
|
||||
|
||||
print(f"Selected path: {result['path']}")
|
||||
print(f"Valid: {result['valid']}")
|
||||
print(f"Message: {result['message']}")
|
||||
|
||||
if 'details' in result:
|
||||
details = result['details']
|
||||
print(f"Details:")
|
||||
print(f" Has Firmware folder: {details.get('has_firmware_folder', False)}")
|
||||
print(f" Has Models folder: {details.get('has_models_folder', False)}")
|
||||
print(f" Firmware series: {details.get('firmware_series', [])}")
|
||||
print(f" Models series: {details.get('models_series', [])}")
|
||||
print(f" Available series: {details.get('available_series', [])}")
|
||||
print(f" Series with files: {details.get('series_with_files', [])}")
|
||||
|
||||
return result['valid']
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Testing Folder Selection Dialog")
|
||||
print("=" * 40)
|
||||
|
||||
# Test basic functionality
|
||||
basic_works = test_basic_folder_selection()
|
||||
|
||||
# Test Assets folder functionality
|
||||
assets_works = test_assets_folder_selection()
|
||||
|
||||
print("\n" + "=" * 40)
|
||||
print("Test Results:")
|
||||
print(f"Basic folder selection: {'PASS' if basic_works else 'FAIL'}")
|
||||
print(f"Assets folder selection: {'PASS' if assets_works else 'FAIL'}")
|
||||
|
||||
if basic_works:
|
||||
print("\ntkinter folder selection is working!")
|
||||
print("You can now use this in your ExactModelNode.")
|
||||
else:
|
||||
print("\ntkinter might not be available or there's an issue.")
|
||||
print("Consider using PyQt5 QFileDialog as fallback.")
|
||||
136
tests/test_multi_series_fix.py
Normal file
136
tests/test_multi_series_fix.py
Normal file
@ -0,0 +1,136 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify multi-series configuration fix
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
# Test the mflow_converter functionality
|
||||
def test_multi_series_config_building():
|
||||
"""Test building multi-series config from properties"""
|
||||
print("Testing multi-series config building...")
|
||||
|
||||
from core.functions.mflow_converter import MFlowConverter
|
||||
|
||||
# Create converter instance
|
||||
converter = MFlowConverter(default_fw_path='.')
|
||||
|
||||
# Mock properties data that would come from a node
|
||||
test_properties = {
|
||||
'multi_series_mode': True,
|
||||
'enabled_series': ['520', '720'],
|
||||
'kl520_port_ids': '28,32',
|
||||
'kl720_port_ids': '4',
|
||||
'assets_folder': '', # Empty for this test
|
||||
'max_queue_size': 100
|
||||
}
|
||||
|
||||
# Test building config
|
||||
config = converter._build_multi_series_config_from_properties(test_properties)
|
||||
|
||||
print(f"Generated config: {config}")
|
||||
|
||||
if config:
|
||||
# Verify structure
|
||||
assert 'KL520' in config, "KL520 should be in config"
|
||||
assert 'KL720' in config, "KL720 should be in config"
|
||||
|
||||
# Check KL520 config
|
||||
kl520_config = config['KL520']
|
||||
assert 'port_ids' in kl520_config, "KL520 should have port_ids"
|
||||
assert kl520_config['port_ids'] == [28, 32], f"KL520 port_ids should be [28, 32], got {kl520_config['port_ids']}"
|
||||
|
||||
# Check KL720 config
|
||||
kl720_config = config['KL720']
|
||||
assert 'port_ids' in kl720_config, "KL720 should have port_ids"
|
||||
assert kl720_config['port_ids'] == [4], f"KL720 port_ids should be [4], got {kl720_config['port_ids']}"
|
||||
|
||||
print("[OK] Multi-series config structure is correct")
|
||||
else:
|
||||
print("[ERROR] Config building returned None")
|
||||
return False
|
||||
|
||||
# Test with invalid port IDs
|
||||
invalid_properties = {
|
||||
'multi_series_mode': True,
|
||||
'enabled_series': ['520'],
|
||||
'kl520_port_ids': 'invalid,port,ids',
|
||||
'assets_folder': ''
|
||||
}
|
||||
|
||||
invalid_config = converter._build_multi_series_config_from_properties(invalid_properties)
|
||||
assert invalid_config is None, "Invalid port IDs should result in None config"
|
||||
print("[OK] Invalid port IDs handled correctly")
|
||||
|
||||
return True
|
||||
|
||||
def test_stage_config():
|
||||
"""Test StageConfig with multi-series support"""
|
||||
print("\\nTesting StageConfig with multi-series...")
|
||||
|
||||
from core.functions.InferencePipeline import StageConfig
|
||||
|
||||
# Test creating StageConfig with multi-series
|
||||
multi_series_config = {
|
||||
"KL520": {"port_ids": [28, 32]},
|
||||
"KL720": {"port_ids": [4]}
|
||||
}
|
||||
|
||||
stage_config = StageConfig(
|
||||
stage_id="test_stage",
|
||||
port_ids=[], # Not used in multi-series mode
|
||||
scpu_fw_path='',
|
||||
ncpu_fw_path='',
|
||||
model_path='',
|
||||
upload_fw=False,
|
||||
multi_series_mode=True,
|
||||
multi_series_config=multi_series_config
|
||||
)
|
||||
|
||||
print(f"Created StageConfig with multi_series_mode: {stage_config.multi_series_mode}")
|
||||
print(f"Multi-series config: {stage_config.multi_series_config}")
|
||||
|
||||
assert stage_config.multi_series_mode == True, "multi_series_mode should be True"
|
||||
assert stage_config.multi_series_config == multi_series_config, "multi_series_config should match"
|
||||
|
||||
print("[OK] StageConfig supports multi-series configuration")
|
||||
return True
|
||||
|
||||
def main():
|
||||
"""Run all tests"""
|
||||
print("Testing Multi-Series Configuration Fix")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
# Test config building
|
||||
if not test_multi_series_config_building():
|
||||
print("[ERROR] Config building test failed")
|
||||
return False
|
||||
|
||||
# Test StageConfig
|
||||
if not test_stage_config():
|
||||
print("[ERROR] StageConfig test failed")
|
||||
return False
|
||||
|
||||
print("\\n" + "=" * 50)
|
||||
print("[SUCCESS] All tests passed!")
|
||||
print("\\nThe fix should now properly:")
|
||||
print("1. Detect multi_series_mode from node properties")
|
||||
print("2. Build multi_series_config from series-specific port IDs")
|
||||
print("3. Pass the config to MultiDongle for true multi-series operation")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Test failed with exception: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = main()
|
||||
sys.exit(0 if success else 1)
|
||||
205
tests/test_multi_series_integration_final.py
Normal file
205
tests/test_multi_series_integration_final.py
Normal file
@ -0,0 +1,205 @@
|
||||
"""
|
||||
Final Integration Test for Multi-Series Multidongle
|
||||
|
||||
Comprehensive test suite for the completed multi-series integration
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add project root (core/functions) to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, os.path.join(parent_dir, 'core', 'functions'))
|
||||
|
||||
from Multidongle import MultiDongle, DongleSeriesSpec
|
||||
|
||||
class TestMultiSeriesIntegration(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures"""
|
||||
self.multi_series_config = {
|
||||
"KL520": {
|
||||
"port_ids": [28, 32],
|
||||
"model_path": "/path/to/kl520_model.nef",
|
||||
"firmware_paths": {
|
||||
"scpu": "/path/to/kl520_scpu.bin",
|
||||
"ncpu": "/path/to/kl520_ncpu.bin"
|
||||
}
|
||||
},
|
||||
"KL720": {
|
||||
"port_ids": [40, 44],
|
||||
"model_path": "/path/to/kl720_model.nef",
|
||||
"firmware_paths": {
|
||||
"scpu": "/path/to/kl720_scpu.bin",
|
||||
"ncpu": "/path/to/kl720_ncpu.bin"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def test_multi_series_initialization_success(self):
|
||||
"""Test that multi-series initialization works correctly"""
|
||||
multidongle = MultiDongle(multi_series_config=self.multi_series_config)
|
||||
|
||||
# Should be in multi-series mode
|
||||
self.assertTrue(multidongle.multi_series_mode)
|
||||
|
||||
# Should have series groups configured
|
||||
self.assertIsNotNone(multidongle.series_groups)
|
||||
self.assertIn("KL520", multidongle.series_groups)
|
||||
self.assertIn("KL720", multidongle.series_groups)
|
||||
|
||||
# Should have correct configuration for each series
|
||||
kl520_config = multidongle.series_groups["KL520"]
|
||||
self.assertEqual(kl520_config["port_ids"], [28, 32])
|
||||
self.assertEqual(kl520_config["model_path"], "/path/to/kl520_model.nef")
|
||||
|
||||
kl720_config = multidongle.series_groups["KL720"]
|
||||
self.assertEqual(kl720_config["port_ids"], [40, 44])
|
||||
self.assertEqual(kl720_config["model_path"], "/path/to/kl720_model.nef")
|
||||
|
||||
# Should have GOPS weights calculated
|
||||
self.assertIsNotNone(multidongle.gops_weights)
|
||||
self.assertIn("KL520", multidongle.gops_weights)
|
||||
self.assertIn("KL720", multidongle.gops_weights)
|
||||
|
||||
# KL720 should have higher weight due to higher GOPS (28 vs 3 GOPS)
|
||||
# But since both have 2 devices: KL520=3*2=6 total GOPS, KL720=28*2=56 total GOPS
|
||||
# Total = 62 GOPS, so KL520 weight = 6/62 ≈ 0.097, KL720 weight = 56/62 ≈ 0.903
|
||||
self.assertGreater(multidongle.gops_weights["KL720"],
|
||||
multidongle.gops_weights["KL720"])
|
||||
|
||||
# Weights should sum to 1.0
|
||||
total_weight = sum(multidongle.gops_weights.values())
|
||||
self.assertAlmostEqual(total_weight, 1.0, places=5)
|
||||
|
||||
print("Multi-series initialization test passed")
|
||||
|
||||
def test_single_series_to_multi_series_conversion_success(self):
|
||||
"""Test that single-series config gets converted to multi-series internally"""
|
||||
# Legacy single-series initialization
|
||||
multidongle = MultiDongle(
|
||||
port_id=[28, 32],
|
||||
scpu_fw_path="/path/to/scpu.bin",
|
||||
ncpu_fw_path="/path/to/ncpu.bin",
|
||||
model_path="/path/to/model.nef",
|
||||
upload_fw=True
|
||||
)
|
||||
|
||||
# Should NOT be in explicit multi-series mode (legacy mode)
|
||||
self.assertFalse(multidongle.multi_series_mode)
|
||||
|
||||
# But should internally convert to multi-series format
|
||||
self.assertIsNotNone(multidongle.series_groups)
|
||||
self.assertEqual(len(multidongle.series_groups), 1)
|
||||
|
||||
# Should auto-detect series (will be KL520 based on available devices or fallback)
|
||||
series_keys = list(multidongle.series_groups.keys())
|
||||
self.assertEqual(len(series_keys), 1)
|
||||
detected_series = series_keys[0]
|
||||
self.assertIn(detected_series, DongleSeriesSpec.SERIES_SPECS.keys())
|
||||
|
||||
# Should have correct port configuration
|
||||
series_config = multidongle.series_groups[detected_series]
|
||||
self.assertEqual(series_config["port_ids"], [28, 32])
|
||||
self.assertEqual(series_config["model_path"], "/path/to/model.nef")
|
||||
|
||||
# Should have 100% weight since it's single series
|
||||
self.assertEqual(multidongle.gops_weights[detected_series], 1.0)
|
||||
|
||||
print(f"Single-to-multi-series conversion test passed (detected: {detected_series})")
|
||||
|
||||
def test_load_balancing_success(self):
|
||||
"""Test that load balancing works based on GOPS weights"""
|
||||
multidongle = MultiDongle(multi_series_config=self.multi_series_config)
|
||||
|
||||
# Should have load balancing method
|
||||
optimal_series = multidongle._select_optimal_series()
|
||||
self.assertIsNotNone(optimal_series)
|
||||
self.assertIn(optimal_series, ["KL520", "KL720"])
|
||||
|
||||
# With zero load, should select the series with highest weight (KL720)
|
||||
self.assertEqual(optimal_series, "KL720")
|
||||
|
||||
# Test load balancing under different conditions
|
||||
# Simulate high load on KL720
|
||||
multidongle.current_loads["KL720"] = 100
|
||||
multidongle.current_loads["KL520"] = 0
|
||||
|
||||
# Now should prefer KL520 despite lower GOPS due to lower load
|
||||
optimal_series_with_load = multidongle._select_optimal_series()
|
||||
self.assertEqual(optimal_series_with_load, "KL520")
|
||||
|
||||
print("Load balancing test passed")
|
||||
|
||||
def test_backward_compatibility_maintained(self):
|
||||
"""Test that existing single-series API still works perfectly"""
|
||||
# This should work exactly as before
|
||||
multidongle = MultiDongle(
|
||||
port_id=[28, 32],
|
||||
scpu_fw_path="/path/to/scpu.bin",
|
||||
ncpu_fw_path="/path/to/ncpu.bin",
|
||||
model_path="/path/to/model.nef"
|
||||
)
|
||||
|
||||
# Legacy properties should still exist and work
|
||||
self.assertIsNotNone(multidongle.port_id)
|
||||
self.assertEqual(multidongle.port_id, [28, 32])
|
||||
self.assertEqual(multidongle.model_path, "/path/to/model.nef")
|
||||
self.assertEqual(multidongle.scpu_fw_path, "/path/to/scpu.bin")
|
||||
self.assertEqual(multidongle.ncpu_fw_path, "/path/to/ncpu.bin")
|
||||
|
||||
# Legacy attributes should be available
|
||||
self.assertIsNotNone(multidongle.device_group) # Will be None initially
|
||||
self.assertIsNotNone(multidongle._input_queue)
|
||||
self.assertIsNotNone(multidongle._output_queue)
|
||||
|
||||
print("Backward compatibility test passed")
|
||||
|
||||
def test_series_specs_are_correct(self):
|
||||
"""Test that series specifications match expected values"""
|
||||
specs = DongleSeriesSpec.SERIES_SPECS
|
||||
|
||||
# Check that all expected series are present
|
||||
expected_series = ["KL520", "KL720", "KL630", "KL730", "KL540"]
|
||||
for series in expected_series:
|
||||
self.assertIn(series, specs)
|
||||
|
||||
# Check GOPS values are reasonable
|
||||
self.assertEqual(specs["KL520"]["gops"], 3)
|
||||
self.assertEqual(specs["KL720"]["gops"], 28)
|
||||
self.assertEqual(specs["KL630"]["gops"], 400)
|
||||
self.assertEqual(specs["KL730"]["gops"], 1600)
|
||||
self.assertEqual(specs["KL540"]["gops"], 800)
|
||||
|
||||
print("Series specifications test passed")
|
||||
|
||||
def test_edge_cases(self):
|
||||
"""Test various edge cases and error handling"""
|
||||
|
||||
# Test with empty port list (single-series)
|
||||
multidongle_empty = MultiDongle(port_id=[])
|
||||
self.assertEqual(len(multidongle_empty.series_groups), 0)
|
||||
|
||||
# Test with unknown series (should raise error)
|
||||
with self.assertRaises(ValueError):
|
||||
MultiDongle(multi_series_config={"UNKNOWN_SERIES": {"port_ids": [1, 2]}})
|
||||
|
||||
# Test with no port IDs in multi-series config
|
||||
config_no_ports = {
|
||||
"KL520": {
|
||||
"port_ids": [],
|
||||
"model_path": "/path/to/model.nef"
|
||||
}
|
||||
}
|
||||
multidongle_no_ports = MultiDongle(multi_series_config=config_no_ports)
|
||||
self.assertEqual(multidongle_no_ports.gops_weights["KL520"], 0.0) # 0 weight due to no devices
|
||||
|
||||
print("Edge cases test passed")
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Running Multi-Series Integration Tests")
|
||||
print("=" * 50)
|
||||
|
||||
unittest.main(verbosity=2)
|
||||
172
tests/test_multi_series_multidongle.py
Normal file
172
tests/test_multi_series_multidongle.py
Normal file
@ -0,0 +1,172 @@
|
||||
"""
|
||||
Test Multi-Series Integration for Multidongle
|
||||
|
||||
Testing the integration of multi-series functionality into the existing Multidongle class
|
||||
following TDD principles.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import sys
|
||||
import os
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
|
||||
# Add project root (core/functions) to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, os.path.join(parent_dir, 'core', 'functions'))
|
||||
|
||||
from Multidongle import MultiDongle
|
||||
|
||||
class TestMultiSeriesMultidongle(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures"""
|
||||
self.multi_series_config = {
|
||||
"KL520": {
|
||||
"port_ids": [28, 32],
|
||||
"model_path": "/path/to/kl520_model.nef",
|
||||
"firmware_paths": {
|
||||
"scpu": "/path/to/kl520_scpu.bin",
|
||||
"ncpu": "/path/to/kl520_ncpu.bin"
|
||||
}
|
||||
},
|
||||
"KL720": {
|
||||
"port_ids": [40, 44],
|
||||
"model_path": "/path/to/kl720_model.nef",
|
||||
"firmware_paths": {
|
||||
"scpu": "/path/to/kl720_scpu.bin",
|
||||
"ncpu": "/path/to/kl720_ncpu.bin"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def test_multi_series_initialization_should_fail(self):
|
||||
"""
|
||||
Test that multi-series initialization accepts config and sets up series groups
|
||||
This should FAIL initially since the functionality doesn't exist yet
|
||||
"""
|
||||
# This should work but will fail initially
|
||||
try:
|
||||
multidongle = MultiDongle(multi_series_config=self.multi_series_config)
|
||||
|
||||
# Should have series groups configured
|
||||
self.assertIsNotNone(multidongle.series_groups)
|
||||
self.assertIn("KL520", multidongle.series_groups)
|
||||
self.assertIn("KL720", multidongle.series_groups)
|
||||
|
||||
# Should have GOPS weights calculated
|
||||
self.assertIsNotNone(multidongle.gops_weights)
|
||||
self.assertIn("KL520", multidongle.gops_weights)
|
||||
self.assertIn("KL720", multidongle.gops_weights)
|
||||
|
||||
# KL720 should have higher weight due to higher GOPS
|
||||
self.assertGreater(multidongle.gops_weights["KL720"],
|
||||
multidongle.gops_weights["KL520"])
|
||||
|
||||
self.fail("Multi-series initialization should not work yet - test should fail")
|
||||
|
||||
except (AttributeError, TypeError) as e:
|
||||
# Expected to fail at this stage
|
||||
print(f"Expected failure: {e}")
|
||||
self.assertTrue(True, "Multi-series initialization correctly fails (not implemented yet)")
|
||||
|
||||
def test_single_series_to_multi_series_conversion_should_fail(self):
|
||||
"""
|
||||
Test that single-series config gets converted to multi-series internally
|
||||
This should FAIL initially
|
||||
"""
|
||||
try:
|
||||
# Legacy single-series initialization
|
||||
multidongle = MultiDongle(
|
||||
port_id=[28, 32],
|
||||
scpu_fw_path="/path/to/scpu.bin",
|
||||
ncpu_fw_path="/path/to/ncpu.bin",
|
||||
model_path="/path/to/model.nef",
|
||||
upload_fw=True
|
||||
)
|
||||
|
||||
# Should internally convert to multi-series format
|
||||
self.assertIsNotNone(multidongle.series_groups)
|
||||
self.assertEqual(len(multidongle.series_groups), 1)
|
||||
|
||||
# Should auto-detect series from device scan or use default
|
||||
series_keys = list(multidongle.series_groups.keys())
|
||||
self.assertEqual(len(series_keys), 1)
|
||||
|
||||
self.fail("Single to multi-series conversion should not work yet")
|
||||
|
||||
except (AttributeError, TypeError) as e:
|
||||
# Expected to fail at this stage
|
||||
print(f"Expected failure: {e}")
|
||||
self.assertTrue(True, "Single-series conversion correctly fails (not implemented yet)")
|
||||
|
||||
def test_load_balancing_should_fail(self):
|
||||
"""
|
||||
Test that load balancing works based on GOPS weights
|
||||
This should FAIL initially
|
||||
"""
|
||||
try:
|
||||
multidongle = MultiDongle(multi_series_config=self.multi_series_config)
|
||||
|
||||
# Should have load balancing method
|
||||
optimal_series = multidongle._select_optimal_series()
|
||||
self.assertIsNotNone(optimal_series)
|
||||
self.assertIn(optimal_series, ["KL520", "KL720"])
|
||||
|
||||
self.fail("Load balancing should not work yet")
|
||||
|
||||
except (AttributeError, TypeError) as e:
|
||||
# Expected to fail at this stage
|
||||
print(f"Expected failure: {e}")
|
||||
self.assertTrue(True, "Load balancing correctly fails (not implemented yet)")
|
||||
|
||||
def test_backward_compatibility_should_work(self):
|
||||
"""
|
||||
Test that existing single-series API still works
|
||||
This should PASS (existing functionality)
|
||||
"""
|
||||
# This should still work with existing code
|
||||
try:
|
||||
multidongle = MultiDongle(
|
||||
port_id=[28, 32],
|
||||
scpu_fw_path="/path/to/scpu.bin",
|
||||
ncpu_fw_path="/path/to/ncpu.bin",
|
||||
model_path="/path/to/model.nef"
|
||||
)
|
||||
|
||||
# Basic properties should still exist
|
||||
self.assertIsNotNone(multidongle.port_id)
|
||||
self.assertEqual(multidongle.port_id, [28, 32])
|
||||
self.assertEqual(multidongle.model_path, "/path/to/model.nef")
|
||||
|
||||
print("Backward compatibility test passed")
|
||||
|
||||
except Exception as e:
|
||||
self.fail(f"Backward compatibility should work: {e}")
|
||||
|
||||
def test_multi_series_device_grouping_should_fail(self):
|
||||
"""
|
||||
Test that devices are properly grouped by series
|
||||
This should FAIL initially
|
||||
"""
|
||||
try:
|
||||
multidongle = MultiDongle(multi_series_config=self.multi_series_config)
|
||||
multidongle.initialize()
|
||||
|
||||
# Should have device groups for each series
|
||||
self.assertIsNotNone(multidongle.device_groups)
|
||||
self.assertEqual(len(multidongle.device_groups), 2)
|
||||
|
||||
# Each series should have its device group
|
||||
for series_name, config in self.multi_series_config.items():
|
||||
self.assertIn(series_name, multidongle.device_groups)
|
||||
|
||||
self.fail("Multi-series device grouping should not work yet")
|
||||
|
||||
except (AttributeError, TypeError) as e:
|
||||
# Expected to fail
|
||||
print(f"Expected failure: {e}")
|
||||
self.assertTrue(True, "Device grouping correctly fails (not implemented yet)")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
48
tests/test_multidongle_start.py
Normal file
48
tests/test_multidongle_start.py
Normal file
@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test MultiDongle start/stop functionality
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
def test_multidongle_start():
|
||||
"""Test MultiDongle start method"""
|
||||
try:
|
||||
from core.functions.Multidongle import MultiDongle
|
||||
|
||||
# Test multi-series configuration
|
||||
multi_series_config = {
|
||||
"KL520": {"port_ids": [28, 32]},
|
||||
"KL720": {"port_ids": [4]}
|
||||
}
|
||||
|
||||
print("Creating MultiDongle with multi-series config...")
|
||||
multidongle = MultiDongle(multi_series_config=multi_series_config)
|
||||
|
||||
print(f"Multi-series mode: {multidongle.multi_series_mode}")
|
||||
print(f"Has _start_multi_series method: {hasattr(multidongle, '_start_multi_series')}")
|
||||
print(f"Has _stop_multi_series method: {hasattr(multidongle, '_stop_multi_series')}")
|
||||
|
||||
print("MultiDongle created successfully!")
|
||||
|
||||
# Test that the required attributes exist
|
||||
expected_attrs = ['send_threads', 'receive_threads', 'dispatcher_thread', 'result_ordering_thread']
|
||||
for attr in expected_attrs:
|
||||
if hasattr(multidongle, attr):
|
||||
print(f"[OK] Has attribute: {attr}")
|
||||
else:
|
||||
print(f"[ERROR] Missing attribute: {attr}")
|
||||
|
||||
print("Test completed successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_multidongle_start()
|
||||
203
tests/test_port_id_config.py
Normal file
203
tests/test_port_id_config.py
Normal file
@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for new series-specific port ID configuration functionality
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the project root to Python path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
try:
|
||||
from core.nodes.exact_nodes import ExactModelNode
|
||||
print("[OK] Successfully imported ExactModelNode")
|
||||
except ImportError as e:
|
||||
print(f"[ERROR] Failed to import ExactModelNode: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def test_port_id_properties():
|
||||
"""Test that new port ID properties are created correctly"""
|
||||
print("\n=== Testing Port ID Properties Creation ===")
|
||||
|
||||
try:
|
||||
node = ExactModelNode()
|
||||
|
||||
# Test that all series port ID properties exist
|
||||
series_properties = ['kl520_port_ids', 'kl720_port_ids', 'kl630_port_ids', 'kl730_port_ids', 'kl540_port_ids']
|
||||
|
||||
for prop in series_properties:
|
||||
if hasattr(node, 'get_property'):
|
||||
try:
|
||||
value = node.get_property(prop)
|
||||
print(f"[OK] Property {prop} exists with value: '{value}'")
|
||||
except:
|
||||
print(f"[ERROR] Property {prop} does not exist or cannot be accessed")
|
||||
else:
|
||||
print(f"[WARN] Node does not have get_property method (NodeGraphQt not available)")
|
||||
break
|
||||
|
||||
# Test property options
|
||||
if hasattr(node, '_property_options'):
|
||||
for prop in series_properties:
|
||||
if prop in node._property_options:
|
||||
options = node._property_options[prop]
|
||||
print(f"[OK] Property options for {prop}: {options}")
|
||||
else:
|
||||
print(f"[ERROR] No property options found for {prop}")
|
||||
else:
|
||||
print("[WARN] Node does not have _property_options")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Error testing port ID properties: {e}")
|
||||
|
||||
def test_display_properties():
|
||||
"""Test that display properties work correctly"""
|
||||
print("\n=== Testing Display Properties ===")
|
||||
|
||||
try:
|
||||
node = ExactModelNode()
|
||||
|
||||
if not hasattr(node, 'get_display_properties'):
|
||||
print("[WARN] Node does not have get_display_properties method (NodeGraphQt not available)")
|
||||
return
|
||||
|
||||
# Test single-series mode
|
||||
if hasattr(node, 'set_property'):
|
||||
node.set_property('multi_series_mode', False)
|
||||
single_props = node.get_display_properties()
|
||||
print(f"[OK] Single-series display properties: {single_props}")
|
||||
|
||||
# Test multi-series mode
|
||||
node.set_property('multi_series_mode', True)
|
||||
node.set_property('enabled_series', ['520', '720'])
|
||||
multi_props = node.get_display_properties()
|
||||
print(f"[OK] Multi-series display properties: {multi_props}")
|
||||
|
||||
# Check if port ID properties are included
|
||||
expected_port_props = ['kl520_port_ids', 'kl720_port_ids']
|
||||
found_port_props = [prop for prop in multi_props if prop in expected_port_props]
|
||||
print(f"[OK] Found port ID properties in display: {found_port_props}")
|
||||
|
||||
# Test with different enabled series
|
||||
node.set_property('enabled_series', ['630', '730'])
|
||||
multi_props_2 = node.get_display_properties()
|
||||
print(f"[OK] Display properties with KL630/730: {multi_props_2}")
|
||||
|
||||
else:
|
||||
print("[WARN] Node does not have set_property method (NodeGraphQt not available)")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Error testing display properties: {e}")
|
||||
|
||||
def test_multi_series_config():
|
||||
"""Test multi-series configuration building"""
|
||||
print("\n=== Testing Multi-Series Config Building ===")
|
||||
|
||||
try:
|
||||
node = ExactModelNode()
|
||||
|
||||
if not hasattr(node, '_build_multi_series_config'):
|
||||
print("[ERROR] Node does not have _build_multi_series_config method")
|
||||
return
|
||||
|
||||
if not hasattr(node, 'set_property'):
|
||||
print("[WARN] Node does not have set_property method (NodeGraphQt not available)")
|
||||
return
|
||||
|
||||
# Test with sample configuration
|
||||
node.set_property('enabled_series', ['520', '720'])
|
||||
node.set_property('kl520_port_ids', '28,32')
|
||||
node.set_property('kl720_port_ids', '30,34')
|
||||
node.set_property('assets_folder', '/fake/assets/path')
|
||||
|
||||
# Build multi-series config
|
||||
config = node._build_multi_series_config()
|
||||
print(f"[OK] Generated multi-series config: {config}")
|
||||
|
||||
# Verify structure
|
||||
if config:
|
||||
expected_keys = ['KL520', 'KL720']
|
||||
for key in expected_keys:
|
||||
if key in config:
|
||||
series_config = config[key]
|
||||
print(f"[OK] {key} config: {series_config}")
|
||||
|
||||
if 'port_ids' in series_config:
|
||||
print(f" - Port IDs: {series_config['port_ids']}")
|
||||
else:
|
||||
print(f" [ERROR] Missing port_ids in {key} config")
|
||||
else:
|
||||
print(f"[ERROR] Missing {key} in config")
|
||||
else:
|
||||
print("[ERROR] Generated config is None or empty")
|
||||
|
||||
# Test with invalid port IDs
|
||||
node.set_property('kl520_port_ids', 'invalid,port,ids')
|
||||
config_invalid = node._build_multi_series_config()
|
||||
print(f"[OK] Config with invalid port IDs: {config_invalid}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Error testing multi-series config: {e}")
|
||||
|
||||
def test_inference_config():
|
||||
"""Test inference configuration"""
|
||||
print("\n=== Testing Inference Config ===")
|
||||
|
||||
try:
|
||||
node = ExactModelNode()
|
||||
|
||||
if not hasattr(node, 'get_inference_config'):
|
||||
print("[ERROR] Node does not have get_inference_config method")
|
||||
return
|
||||
|
||||
if not hasattr(node, 'set_property'):
|
||||
print("[WARN] Node does not have set_property method (NodeGraphQt not available)")
|
||||
return
|
||||
|
||||
# Test multi-series inference config
|
||||
node.set_property('multi_series_mode', True)
|
||||
node.set_property('enabled_series', ['520', '720'])
|
||||
node.set_property('kl520_port_ids', '28,32')
|
||||
node.set_property('kl720_port_ids', '30,34')
|
||||
node.set_property('assets_folder', '/fake/assets')
|
||||
node.set_property('max_queue_size', 50)
|
||||
|
||||
inference_config = node.get_inference_config()
|
||||
print(f"[OK] Inference config: {inference_config}")
|
||||
|
||||
# Check if multi_series_config is included
|
||||
if 'multi_series_config' in inference_config:
|
||||
ms_config = inference_config['multi_series_config']
|
||||
print(f"[OK] Multi-series config included: {ms_config}")
|
||||
else:
|
||||
print("[WARN] Multi-series config not found in inference config")
|
||||
|
||||
# Test single-series mode
|
||||
node.set_property('multi_series_mode', False)
|
||||
node.set_property('model_path', '/fake/model.nef')
|
||||
node.set_property('port_id', '28')
|
||||
|
||||
single_config = node.get_inference_config()
|
||||
print(f"[OK] Single-series config: {single_config}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Error testing inference config: {e}")
|
||||
|
||||
def main():
|
||||
"""Run all tests"""
|
||||
print("Testing Series-Specific Port ID Configuration")
|
||||
print("=" * 50)
|
||||
|
||||
test_port_id_properties()
|
||||
test_display_properties()
|
||||
test_multi_series_config()
|
||||
test_inference_config()
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("Test completed!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
211
tests/test_postprocess_mode.py
Normal file
211
tests/test_postprocess_mode.py
Normal file
@ -0,0 +1,211 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for postprocessing mode switching and visualization.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(parent_dir)
|
||||
|
||||
from core.nodes.exact_nodes import ExactPostprocessNode
|
||||
|
||||
def test_postprocess_node():
|
||||
"""Test the ExactPostprocessNode for mode switching and configuration."""
|
||||
|
||||
print("=== Testing ExactPostprocessNode Mode Switching ===")
|
||||
|
||||
# Create node instance
|
||||
try:
|
||||
node = ExactPostprocessNode()
|
||||
print("✓ ExactPostprocessNode created successfully")
|
||||
|
||||
# Check if NodeGraphQt is available
|
||||
if not hasattr(node, 'set_property'):
|
||||
print("⚠ NodeGraphQt not available - using mock properties")
|
||||
return True # Skip tests that require NodeGraphQt
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error creating node: {e}")
|
||||
return False
|
||||
|
||||
# Test different postprocessing modes
|
||||
test_modes = [
|
||||
('fire_detection', 'No Fire,Fire'),
|
||||
('yolo_v3', 'person,car,bicycle,motorbike,aeroplane'),
|
||||
('yolo_v5', 'person,bicycle,car,motorbike,bus,truck'),
|
||||
('classification', 'cat,dog,bird,fish'),
|
||||
('raw_output', '')
|
||||
]
|
||||
|
||||
print("\n--- Testing Mode Switching ---")
|
||||
for mode, class_names in test_modes:
|
||||
try:
|
||||
# Set properties for this mode
|
||||
node.set_property('postprocess_type', mode)
|
||||
node.set_property('class_names', class_names)
|
||||
node.set_property('confidence_threshold', 0.6)
|
||||
node.set_property('nms_threshold', 0.4)
|
||||
|
||||
# Get configuration
|
||||
config = node.get_postprocessing_config()
|
||||
options = node.get_multidongle_postprocess_options()
|
||||
|
||||
print(f"✓ Mode: {mode}")
|
||||
print(f" - Class names: {class_names}")
|
||||
print(f" - Config: {config['postprocess_type']}")
|
||||
if options:
|
||||
print(f" - PostProcessor options created successfully")
|
||||
else:
|
||||
print(f" - Warning: PostProcessor options not available")
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error testing mode {mode}: {e}")
|
||||
return False
|
||||
|
||||
# Test validation
|
||||
print("\n--- Testing Configuration Validation ---")
|
||||
try:
|
||||
# Valid configuration
|
||||
node.set_property('postprocess_type', 'fire_detection')
|
||||
node.set_property('confidence_threshold', 0.7)
|
||||
node.set_property('nms_threshold', 0.3)
|
||||
node.set_property('max_detections', 50)
|
||||
|
||||
is_valid, message = node.validate_configuration()
|
||||
if is_valid:
|
||||
print("✓ Valid configuration passed validation")
|
||||
else:
|
||||
print(f"✗ Valid configuration failed: {message}")
|
||||
return False
|
||||
|
||||
# Invalid configuration
|
||||
node.set_property('confidence_threshold', 1.5) # Invalid value
|
||||
is_valid, message = node.validate_configuration()
|
||||
if not is_valid:
|
||||
print(f"✓ Invalid configuration caught: {message}")
|
||||
else:
|
||||
print("✗ Invalid configuration not caught")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error testing validation: {e}")
|
||||
return False
|
||||
|
||||
# Test display properties
|
||||
print("\n--- Testing Display Properties ---")
|
||||
try:
|
||||
display_props = node.get_display_properties()
|
||||
expected_props = ['postprocess_type', 'class_names', 'confidence_threshold']
|
||||
|
||||
for prop in expected_props:
|
||||
if prop in display_props:
|
||||
print(f"✓ Display property found: {prop}")
|
||||
else:
|
||||
print(f"✗ Missing display property: {prop}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error testing display properties: {e}")
|
||||
return False
|
||||
|
||||
# Test business properties
|
||||
print("\n--- Testing Business Properties ---")
|
||||
try:
|
||||
business_props = node.get_business_properties()
|
||||
print(f"✓ Business properties retrieved: {len(business_props)} properties")
|
||||
|
||||
# Check key properties exist
|
||||
key_props = ['postprocess_type', 'class_names', 'confidence_threshold', 'nms_threshold']
|
||||
for prop in key_props:
|
||||
if prop in business_props:
|
||||
print(f"✓ Key property found: {prop} = {business_props[prop]}")
|
||||
else:
|
||||
print(f"✗ Missing key property: {prop}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error testing business properties: {e}")
|
||||
return False
|
||||
|
||||
print("\n=== All Tests Passed! ===")
|
||||
return True
|
||||
|
||||
def test_visualization_integration():
|
||||
"""Test visualization integration with different modes."""
|
||||
|
||||
print("\n=== Testing Visualization Integration ===")
|
||||
|
||||
try:
|
||||
node = ExactPostprocessNode()
|
||||
|
||||
# Test each mode for visualization compatibility
|
||||
test_cases = [
|
||||
{
|
||||
'mode': 'fire_detection',
|
||||
'classes': 'No Fire,Fire',
|
||||
'expected_classes': 2,
|
||||
'description': 'Binary fire detection'
|
||||
},
|
||||
{
|
||||
'mode': 'yolo_v3',
|
||||
'classes': 'person,car,bicycle,motorbike,bus',
|
||||
'expected_classes': 5,
|
||||
'description': 'Object detection'
|
||||
},
|
||||
{
|
||||
'mode': 'classification',
|
||||
'classes': 'cat,dog,bird,fish,rabbit',
|
||||
'expected_classes': 5,
|
||||
'description': 'Multi-class classification'
|
||||
}
|
||||
]
|
||||
|
||||
for case in test_cases:
|
||||
print(f"\n--- {case['description']} ---")
|
||||
|
||||
# Configure node
|
||||
node.set_property('postprocess_type', case['mode'])
|
||||
node.set_property('class_names', case['classes'])
|
||||
|
||||
# Get configuration for visualization
|
||||
config = node.get_postprocessing_config()
|
||||
parsed_classes = config['class_names']
|
||||
|
||||
print(f"✓ Mode: {case['mode']}")
|
||||
print(f"✓ Classes: {parsed_classes}")
|
||||
print(f"✓ Expected {case['expected_classes']}, got {len(parsed_classes)}")
|
||||
|
||||
if len(parsed_classes) == case['expected_classes']:
|
||||
print("✓ Class count matches expected")
|
||||
else:
|
||||
print(f"✗ Class count mismatch: expected {case['expected_classes']}, got {len(parsed_classes)}")
|
||||
return False
|
||||
|
||||
print("\n✓ Visualization integration tests passed!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Error in visualization integration test: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting ExactPostprocessNode Tests...\n")
|
||||
|
||||
success = True
|
||||
|
||||
# Run main functionality tests
|
||||
if not test_postprocess_node():
|
||||
success = False
|
||||
|
||||
# Run visualization integration tests
|
||||
if not test_visualization_integration():
|
||||
success = False
|
||||
|
||||
if success:
|
||||
print("\n🎉 All tests completed successfully!")
|
||||
print("ExactPostprocessNode is ready for mode switching and visualization!")
|
||||
else:
|
||||
print("\n❌ Some tests failed. Please check the implementation.")
|
||||
sys.exit(1)
|
||||
172
tests/test_result_formatting_fix.py
Normal file
172
tests/test_result_formatting_fix.py
Normal file
@ -0,0 +1,172 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify result formatting fixes for string probability values
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add UI dialogs to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(os.path.join(parent_dir, 'ui', 'dialogs'))
|
||||
|
||||
def test_probability_formatting():
|
||||
"""Test that probability formatting handles both numeric and string values"""
|
||||
|
||||
print("Testing probability formatting fixes...")
|
||||
|
||||
# Test cases with different probability value types
|
||||
test_cases = [
|
||||
# Numeric probability (should work with :.3f)
|
||||
{"probability": 0.85, "result_string": "Fire", "expected_error": False},
|
||||
|
||||
# String probability that can be converted to float
|
||||
{"probability": "0.75", "result_string": "Fire", "expected_error": False},
|
||||
|
||||
# String probability that cannot be converted to float
|
||||
{"probability": "High", "result_string": "Fire", "expected_error": False},
|
||||
|
||||
# None probability
|
||||
{"probability": None, "result_string": "No result", "expected_error": False},
|
||||
|
||||
# Dict result with numeric probability
|
||||
{"dict_result": {"probability": 0.65, "class_name": "Fire"}, "expected_error": False},
|
||||
|
||||
# Dict result with string probability
|
||||
{"dict_result": {"probability": "Medium", "class_name": "Fire"}, "expected_error": False},
|
||||
]
|
||||
|
||||
all_passed = True
|
||||
|
||||
for i, case in enumerate(test_cases, 1):
|
||||
print(f"\nTest case {i}:")
|
||||
|
||||
try:
|
||||
if "dict_result" in case:
|
||||
# Test dict formatting
|
||||
result = case["dict_result"]
|
||||
for key, value in result.items():
|
||||
if key == 'probability':
|
||||
try:
|
||||
prob_value = float(value)
|
||||
formatted = f" Probability: {prob_value:.3f}"
|
||||
print(f" Dict probability formatted: {formatted}")
|
||||
except (ValueError, TypeError):
|
||||
formatted = f" Probability: {value}"
|
||||
print(f" Dict probability (as string): {formatted}")
|
||||
else:
|
||||
formatted = f" {key}: {value}"
|
||||
print(f" Dict {key}: {formatted}")
|
||||
else:
|
||||
# Test tuple formatting
|
||||
probability = case["probability"]
|
||||
result_string = case["result_string"]
|
||||
|
||||
print(f" Testing probability: {probability} (type: {type(probability)})")
|
||||
|
||||
# Test the formatting logic
|
||||
try:
|
||||
prob_value = float(probability)
|
||||
formatted_prob = f" Probability: {prob_value:.3f}"
|
||||
print(f" Formatted as float: {formatted_prob}")
|
||||
except (ValueError, TypeError):
|
||||
formatted_prob = f" Probability: {probability}"
|
||||
print(f" Formatted as string: {formatted_prob}")
|
||||
|
||||
formatted_result = f" Result: {result_string}"
|
||||
print(f" Formatted result: {formatted_result}")
|
||||
|
||||
print(f" ✓ Test case {i} passed")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ✗ Test case {i} failed: {e}")
|
||||
if not case["expected_error"]:
|
||||
all_passed = False
|
||||
|
||||
return all_passed
|
||||
|
||||
def test_terminal_results_mock():
|
||||
"""Mock test of the terminal results formatting logic"""
|
||||
|
||||
print("\n" + "="*50)
|
||||
print("Testing terminal results formatting logic...")
|
||||
|
||||
# Mock result dictionary with various probability types
|
||||
mock_result_dict = {
|
||||
'timestamp': 1234567890.123,
|
||||
'pipeline_id': 'test-pipeline',
|
||||
'stage_results': {
|
||||
'stage1': (0.85, "Fire Detected"), # Numeric probability
|
||||
'stage2': ("High", "Object Found"), # String probability
|
||||
'stage3': {"probability": 0.65, "result": "Classification"}, # Dict with numeric
|
||||
'stage4': {"probability": "Medium", "result": "Detection"} # Dict with string
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
# Simulate the formatting logic
|
||||
from datetime import datetime
|
||||
|
||||
timestamp = datetime.fromtimestamp(mock_result_dict.get('timestamp', 0)).strftime("%H:%M:%S.%f")[:-3]
|
||||
pipeline_id = mock_result_dict.get('pipeline_id', 'Unknown')
|
||||
|
||||
output_lines = []
|
||||
output_lines.append(f"\nINFERENCE RESULT [{timestamp}]")
|
||||
output_lines.append(f" Pipeline ID: {pipeline_id}")
|
||||
output_lines.append(" " + "="*50)
|
||||
|
||||
stage_results = mock_result_dict.get('stage_results', {})
|
||||
for stage_id, result in stage_results.items():
|
||||
output_lines.append(f" Stage: {stage_id}")
|
||||
|
||||
if isinstance(result, tuple) and len(result) == 2:
|
||||
probability, result_string = result
|
||||
output_lines.append(f" Result: {result_string}")
|
||||
|
||||
# Test the safe formatting
|
||||
try:
|
||||
prob_value = float(probability)
|
||||
output_lines.append(f" Probability: {prob_value:.3f}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" Probability: {probability}")
|
||||
|
||||
elif isinstance(result, dict):
|
||||
for key, value in result.items():
|
||||
if key == 'probability':
|
||||
try:
|
||||
prob_value = float(value)
|
||||
output_lines.append(f" {key.title()}: {prob_value:.3f}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" {key.title()}: {value}")
|
||||
else:
|
||||
output_lines.append(f" {key.title()}: {value}")
|
||||
|
||||
output_lines.append("")
|
||||
|
||||
formatted_output = "\n".join(output_lines)
|
||||
print("Formatted terminal output:")
|
||||
print(formatted_output)
|
||||
print("✓ Terminal formatting test passed")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ Terminal formatting test failed: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Running result formatting fix tests...")
|
||||
|
||||
try:
|
||||
test1_passed = test_probability_formatting()
|
||||
test2_passed = test_terminal_results_mock()
|
||||
|
||||
if test1_passed and test2_passed:
|
||||
print("\n🎉 All formatting fix tests passed! The format string errors should be resolved.")
|
||||
else:
|
||||
print("\n❌ Some tests failed. Please check the output above.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Test suite failed with error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
225
tests/test_yolov5_fixed.py
Normal file
225
tests/test_yolov5_fixed.py
Normal file
@ -0,0 +1,225 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify YOLOv5 postprocessing fixes
|
||||
|
||||
This script tests the improved YOLOv5 postprocessing configuration
|
||||
to ensure positive probabilities and proper bounding box detection.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import numpy as np
|
||||
|
||||
# Add core functions to path
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
sys.path.append(os.path.join(parent_dir, 'core', 'functions'))
|
||||
|
||||
def test_yolov5_postprocessor():
|
||||
"""Test the improved YOLOv5 postprocessor with mock data"""
|
||||
from Multidongle import PostProcessorOptions, PostProcessType, PostProcessor
|
||||
|
||||
print("=" * 60)
|
||||
print("Testing Improved YOLOv5 Postprocessor")
|
||||
print("=" * 60)
|
||||
|
||||
# Create YOLOv5 postprocessor options
|
||||
options = PostProcessorOptions(
|
||||
postprocess_type=PostProcessType.YOLO_V5,
|
||||
threshold=0.3,
|
||||
class_names=["person", "bicycle", "car", "motorbike", "aeroplane", "bus"],
|
||||
nms_threshold=0.5,
|
||||
max_detections_per_class=50
|
||||
)
|
||||
|
||||
postprocessor = PostProcessor(options)
|
||||
|
||||
print(f"✓ Postprocessor created with type: {options.postprocess_type.value}")
|
||||
print(f"✓ Confidence threshold: {options.threshold}")
|
||||
print(f"✓ NMS threshold: {options.nms_threshold}")
|
||||
print(f"✓ Number of classes: {len(options.class_names)}")
|
||||
|
||||
# Create mock YOLOv5 output data - format: [batch, detections, features]
|
||||
# Features: [x_center, y_center, width, height, objectness, class0_prob, class1_prob, ...]
|
||||
mock_output = create_mock_yolov5_output()
|
||||
|
||||
# Test processing
|
||||
try:
|
||||
result = postprocessor.process([mock_output])
|
||||
|
||||
print(f"\n📊 Processing Results:")
|
||||
print(f" Result type: {type(result).__name__}")
|
||||
print(f" Detected objects: {result.box_count}")
|
||||
print(f" Available classes: {result.class_count}")
|
||||
|
||||
if result.box_count > 0:
|
||||
print(f"\n📦 Detection Details:")
|
||||
for i, box in enumerate(result.box_list):
|
||||
print(f" Detection {i+1}:")
|
||||
print(f" Class: {box.class_name} (ID: {box.class_num})")
|
||||
print(f" Confidence: {box.score:.3f}")
|
||||
print(f" Bounding Box: ({box.x1}, {box.y1}) to ({box.x2}, {box.y2})")
|
||||
print(f" Box Size: {box.x2 - box.x1} x {box.y2 - box.y1}")
|
||||
|
||||
# Verify positive probabilities
|
||||
all_positive = all(box.score > 0 for box in result.box_list)
|
||||
print(f"\n✓ All probabilities positive: {all_positive}")
|
||||
|
||||
# Verify reasonable coordinates
|
||||
valid_coords = all(
|
||||
box.x2 > box.x1 and box.y2 > box.y1
|
||||
for box in result.box_list
|
||||
)
|
||||
print(f"✓ All bounding boxes valid: {valid_coords}")
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Postprocessing failed: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
def create_mock_yolov5_output():
|
||||
"""Create mock YOLOv5 output data for testing"""
|
||||
# YOLOv5 output format: [batch_size, num_detections, num_features]
|
||||
# Features: [x_center, y_center, width, height, objectness, class_probs...]
|
||||
|
||||
batch_size = 1
|
||||
num_detections = 25200 # Typical YOLOv5 output size
|
||||
num_classes = 80 # COCO classes
|
||||
num_features = 5 + num_classes # coords + objectness + class probs
|
||||
|
||||
# Create mock output
|
||||
mock_output = np.zeros((batch_size, num_detections, num_features), dtype=np.float32)
|
||||
|
||||
# Add some realistic detections
|
||||
detections = [
|
||||
# Format: [x_center, y_center, width, height, objectness, class_id, class_prob]
|
||||
[320, 240, 100, 150, 0.8, 0, 0.9], # person
|
||||
[500, 300, 80, 60, 0.7, 2, 0.85], # car
|
||||
[150, 100, 60, 120, 0.6, 1, 0.75], # bicycle
|
||||
]
|
||||
|
||||
for i, detection in enumerate(detections):
|
||||
x_center, y_center, width, height, objectness, class_id, class_prob = detection
|
||||
|
||||
# Set coordinates and objectness
|
||||
mock_output[0, i, 0] = x_center
|
||||
mock_output[0, i, 1] = y_center
|
||||
mock_output[0, i, 2] = width
|
||||
mock_output[0, i, 3] = height
|
||||
mock_output[0, i, 4] = objectness
|
||||
|
||||
# Set class probabilities (one-hot style)
|
||||
mock_output[0, i, 5 + int(class_id)] = class_prob
|
||||
|
||||
print(f"✓ Created mock YOLOv5 output: {mock_output.shape}")
|
||||
print(f" Added {len(detections)} test detections")
|
||||
|
||||
# Wrap in mock output object
|
||||
class MockOutput:
|
||||
def __init__(self, data):
|
||||
self.ndarray = data
|
||||
|
||||
return MockOutput(mock_output)
|
||||
|
||||
def test_result_formatting():
|
||||
"""Test the result formatting functions"""
|
||||
from Multidongle import ObjectDetectionResult, BoundingBox
|
||||
|
||||
print(f"\n" + "=" * 60)
|
||||
print("Testing Result Formatting")
|
||||
print("=" * 60)
|
||||
|
||||
# Create mock detection result
|
||||
boxes = [
|
||||
BoundingBox(x1=100, y1=200, x2=200, y2=350, score=0.85, class_num=0, class_name="person"),
|
||||
BoundingBox(x1=300, y1=150, x2=380, y2=210, score=0.75, class_num=2, class_name="car"),
|
||||
BoundingBox(x1=50, y1=100, x2=110, y2=220, score=0.65, class_num=1, class_name="bicycle"),
|
||||
]
|
||||
|
||||
result = ObjectDetectionResult(
|
||||
class_count=80,
|
||||
box_count=len(boxes),
|
||||
box_list=boxes
|
||||
)
|
||||
|
||||
# Test the enhanced result string generation
|
||||
from Multidongle import MultiDongle, PostProcessorOptions, PostProcessType
|
||||
# Create a minimal MultiDongle instance to access the method
|
||||
options = PostProcessorOptions(postprocess_type=PostProcessType.YOLO_V5)
|
||||
multidongle = MultiDongle(port_id=[1], postprocess_options=options) # Dummy port
|
||||
result_string = multidongle._generate_result_string(result)
|
||||
|
||||
print(f"📝 Generated result string: {result_string}")
|
||||
|
||||
# Test individual object summaries
|
||||
print(f"\n📊 Object Summary:")
|
||||
object_counts = {}
|
||||
for box in boxes:
|
||||
if box.class_name in object_counts:
|
||||
object_counts[box.class_name] += 1
|
||||
else:
|
||||
object_counts[box.class_name] = 1
|
||||
|
||||
for class_name, count in sorted(object_counts.items()):
|
||||
print(f" {count} {class_name}{'s' if count > 1 else ''}")
|
||||
|
||||
return result
|
||||
|
||||
def show_configuration_usage():
|
||||
"""Show how to use the fixed configuration"""
|
||||
print(f"\n" + "=" * 60)
|
||||
print("Configuration Usage Instructions")
|
||||
print("=" * 60)
|
||||
|
||||
print(f"\n🔧 Updated Configuration:")
|
||||
print(f" 1. Modified multi_series_example.mflow:")
|
||||
print(f" - Set 'enable_postprocessing': true")
|
||||
print(f" - Added ExactPostprocessNode with YOLOv5 settings")
|
||||
print(f" - Connected Model → Postprocess → Output")
|
||||
|
||||
print(f"\n⚙️ Postprocessing Settings:")
|
||||
print(f" - postprocess_type: 'yolo_v5'")
|
||||
print(f" - confidence_threshold: 0.3")
|
||||
print(f" - nms_threshold: 0.5")
|
||||
print(f" - class_names: Full COCO 80 classes")
|
||||
|
||||
print(f"\n🎯 Expected Improvements:")
|
||||
print(f" ✓ Positive probability values (0.0 to 1.0)")
|
||||
print(f" ✓ Proper object detection with bounding boxes")
|
||||
print(f" ✓ Correct class names (person, car, bicycle, etc.)")
|
||||
print(f" ✓ Enhanced live view with corner markers")
|
||||
print(f" ✓ Detailed terminal output with object counts")
|
||||
print(f" ✓ Non-Maximum Suppression to reduce duplicates")
|
||||
|
||||
print(f"\n📁 Files Modified:")
|
||||
print(f" - core/functions/Multidongle.py (improved YOLO processing)")
|
||||
print(f" - multi_series_example.mflow (added postprocess node)")
|
||||
print(f" - Enhanced live view display and terminal output")
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("YOLOv5 Postprocessing Fix Verification")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
# Test the postprocessor
|
||||
result = test_yolov5_postprocessor()
|
||||
|
||||
if result:
|
||||
# Test result formatting
|
||||
test_result_formatting()
|
||||
|
||||
# Show usage instructions
|
||||
show_configuration_usage()
|
||||
|
||||
print(f"\n🎉 All tests passed! YOLOv5 postprocessing should now work correctly.")
|
||||
print(f" Use the updated multi_series_example.mflow configuration.")
|
||||
else:
|
||||
print(f"\n❌ Tests failed. Please check the error messages above.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n❌ Test suite failed with error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
295
tests/unit/conftest.py
Normal file
295
tests/unit/conftest.py
Normal file
@ -0,0 +1,295 @@
|
||||
"""
|
||||
pytest conftest.py — 單元測試環境設定。
|
||||
|
||||
此測試環境沒有 Kneron NPU 硬體,也沒有 PyQt5 等 GUI 函式庫。
|
||||
為了能夠測試純 Python 的 core/ 和 ui/ 模組,
|
||||
在收集測試前預先注入 Mock 模組,避免 import 時觸發硬體/GUI 初始化。
|
||||
|
||||
UI 元件測試需要 QWidget 等基底類別可被正常繼承與多次實例化,
|
||||
因此使用輕量 Stub 取代 MagicMock 作為 PyQt5 Widget 基底。
|
||||
"""
|
||||
import sys
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def _install_mock(name: str) -> None:
|
||||
"""若模組尚未存在,安裝空 MagicMock 作為替代。"""
|
||||
if name not in sys.modules:
|
||||
sys.modules[name] = MagicMock()
|
||||
|
||||
|
||||
# Kneron KP SDK(需要硬體驅動程式)
|
||||
_install_mock("kp")
|
||||
|
||||
# NumPy(可能未安裝)
|
||||
try:
|
||||
import numpy # noqa: F401
|
||||
except ImportError:
|
||||
_install_mock("numpy")
|
||||
|
||||
# OpenCV(可能未安裝)
|
||||
_install_mock("cv2")
|
||||
|
||||
# NodeGraphQt(依賴 PyQt5)
|
||||
_install_mock("NodeGraphQt")
|
||||
_install_mock("NodeGraphQt.constants")
|
||||
_install_mock("NodeGraphQt.base")
|
||||
_install_mock("NodeGraphQt.base.node")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# PyQt5 Stub — 允許 QWidget/QDialog 子類別被正常繼承並多次實例化。
|
||||
# 使用輕量 Python 類別替代,避免 MagicMock 繼承時的 side_effect 耗盡問題。
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _StubQObject:
|
||||
"""所有 Qt 物件的基底 Stub。"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQWidget(_StubQObject):
|
||||
"""QWidget Stub:可被繼承,支援多次實例化。提供常用 QWidget 方法的空實作。"""
|
||||
|
||||
def setLayout(self, layout):
|
||||
pass
|
||||
|
||||
def setParent(self, parent):
|
||||
pass
|
||||
|
||||
def show(self):
|
||||
pass
|
||||
|
||||
def hide(self):
|
||||
pass
|
||||
|
||||
def setVisible(self, visible: bool):
|
||||
pass
|
||||
|
||||
def setEnabled(self, enabled: bool):
|
||||
pass
|
||||
|
||||
def isEnabled(self) -> bool:
|
||||
return True
|
||||
|
||||
def setObjectName(self, name: str):
|
||||
pass
|
||||
|
||||
def setStyleSheet(self, style: str):
|
||||
pass
|
||||
|
||||
def setMinimumWidth(self, w: int):
|
||||
pass
|
||||
|
||||
def setMinimumHeight(self, h: int):
|
||||
pass
|
||||
|
||||
def setMaximumWidth(self, w: int):
|
||||
pass
|
||||
|
||||
def setMaximumHeight(self, h: int):
|
||||
pass
|
||||
|
||||
def resize(self, *args):
|
||||
pass
|
||||
|
||||
def setWindowTitle(self, title: str):
|
||||
pass
|
||||
|
||||
def setSizePolicy(self, *args):
|
||||
pass
|
||||
|
||||
def update(self):
|
||||
pass
|
||||
|
||||
def repaint(self):
|
||||
pass
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
def font(self):
|
||||
return MagicMock()
|
||||
|
||||
def setFont(self, font):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQDialog(_StubQWidget):
|
||||
"""QDialog Stub。"""
|
||||
|
||||
Accepted = 1
|
||||
Rejected = 0
|
||||
|
||||
def exec_(self):
|
||||
return self.Accepted
|
||||
|
||||
def accept(self):
|
||||
pass
|
||||
|
||||
def reject(self):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQLabel(_StubQWidget):
|
||||
"""QLabel Stub:追蹤 setText 呼叫,可在測試中驗證顯示文字。"""
|
||||
def __init__(self, text: str = "", parent=None):
|
||||
super().__init__(parent)
|
||||
self._text = text
|
||||
self.setText = MagicMock(side_effect=self._set_text)
|
||||
|
||||
def _set_text(self, text: str) -> None:
|
||||
self._text = text
|
||||
|
||||
def text(self) -> str:
|
||||
return self._text
|
||||
|
||||
|
||||
class _StubLayout(_StubQObject):
|
||||
"""QLayout Stub:忽略所有 add* 呼叫。"""
|
||||
def addWidget(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def addLayout(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def addStretch(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def setSpacing(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def setContentsMargins(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQVBoxLayout(_StubLayout):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQHBoxLayout(_StubLayout):
|
||||
pass
|
||||
|
||||
|
||||
class _StubQProgressBar(_StubQWidget):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self._value = 0
|
||||
self._maximum = 100
|
||||
self._minimum = 0
|
||||
self.setValue = MagicMock(side_effect=self._set_value)
|
||||
|
||||
def _set_value(self, v: int) -> None:
|
||||
self._value = v
|
||||
|
||||
def value(self) -> int:
|
||||
return self._value
|
||||
|
||||
def setMaximum(self, v: int) -> None:
|
||||
self._maximum = v
|
||||
|
||||
def setMinimum(self, v: int) -> None:
|
||||
self._minimum = v
|
||||
|
||||
|
||||
class _StubQTableWidget(_StubQWidget):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__()
|
||||
self.setItem = MagicMock()
|
||||
self.setHorizontalHeaderLabels = MagicMock()
|
||||
|
||||
|
||||
class _StubQPushButton(_StubQWidget):
|
||||
def __init__(self, text: str = "", parent=None):
|
||||
super().__init__(parent)
|
||||
self._text = text
|
||||
self._enabled = True
|
||||
self.clicked = MagicMock()
|
||||
self.setEnabled = MagicMock(side_effect=self._set_enabled)
|
||||
|
||||
def _set_enabled(self, enabled: bool) -> None:
|
||||
self._enabled = enabled
|
||||
|
||||
def isEnabled(self) -> bool:
|
||||
return self._enabled
|
||||
|
||||
|
||||
def _make_pyqt_signal(*args, **kwargs):
|
||||
"""pyqtSignal Stub:回傳可 connect/emit 的 MagicMock。"""
|
||||
signal = MagicMock()
|
||||
signal.connect = MagicMock()
|
||||
signal.emit = MagicMock()
|
||||
return signal
|
||||
|
||||
|
||||
def _make_qthread():
|
||||
"""QThread Stub。"""
|
||||
class _StubQThread(_StubQObject):
|
||||
started = MagicMock()
|
||||
finished = MagicMock()
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def isRunning(self):
|
||||
return False
|
||||
|
||||
def wait(self):
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
pass
|
||||
|
||||
def deleteLater(self):
|
||||
pass
|
||||
|
||||
return _StubQThread
|
||||
|
||||
|
||||
# 建立 PyQt5.QtWidgets Mock 模組(保留 MagicMock 為底,覆蓋關鍵類別)
|
||||
_qtwidgets_mock = MagicMock()
|
||||
_qtwidgets_mock.QWidget = _StubQWidget
|
||||
_qtwidgets_mock.QDialog = _StubQDialog
|
||||
_qtwidgets_mock.QLabel = _StubQLabel
|
||||
_qtwidgets_mock.QVBoxLayout = _StubQVBoxLayout
|
||||
_qtwidgets_mock.QHBoxLayout = _StubQHBoxLayout
|
||||
_qtwidgets_mock.QProgressBar = _StubQProgressBar
|
||||
_qtwidgets_mock.QTableWidget = _StubQTableWidget
|
||||
_qtwidgets_mock.QPushButton = _StubQPushButton
|
||||
_qtwidgets_mock.QSizePolicy = MagicMock()
|
||||
_qtwidgets_mock.QTableWidgetItem = MagicMock()
|
||||
_qtwidgets_mock.QHeaderView = MagicMock()
|
||||
_qtwidgets_mock.QMessageBox = MagicMock()
|
||||
_qtwidgets_mock.QApplication = MagicMock()
|
||||
_qtwidgets_mock.QGroupBox = _StubQWidget
|
||||
_qtwidgets_mock.QFrame = _StubQWidget
|
||||
_qtwidgets_mock.QScrollArea = _StubQWidget
|
||||
_qtwidgets_mock.QSpinBox = _StubQWidget
|
||||
_qtwidgets_mock.QComboBox = _StubQWidget
|
||||
_qtwidgets_mock.QCheckBox = _StubQWidget
|
||||
|
||||
# 建立 PyQt5.QtCore Mock 模組
|
||||
_qtcore_mock = MagicMock()
|
||||
_qtcore_mock.pyqtSignal = _make_pyqt_signal
|
||||
_qtcore_mock.QThread = _make_qthread()
|
||||
_qtcore_mock.Qt = MagicMock()
|
||||
_qtcore_mock.QTimer = MagicMock()
|
||||
_qtcore_mock.QObject = _StubQObject
|
||||
|
||||
# 建立 PyQt5.QtGui Mock 模組
|
||||
_qtgui_mock = MagicMock()
|
||||
|
||||
# 建立頂層 PyQt5 Mock
|
||||
_pyqt5_mock = MagicMock()
|
||||
_pyqt5_mock.QtWidgets = _qtwidgets_mock
|
||||
_pyqt5_mock.QtCore = _qtcore_mock
|
||||
_pyqt5_mock.QtGui = _qtgui_mock
|
||||
|
||||
sys.modules["PyQt5"] = _pyqt5_mock
|
||||
sys.modules["PyQt5.QtWidgets"] = _qtwidgets_mock
|
||||
sys.modules["PyQt5.QtCore"] = _qtcore_mock
|
||||
sys.modules["PyQt5.QtGui"] = _qtgui_mock
|
||||
sys.modules["PyQt5.QtChart"] = MagicMock()
|
||||
|
||||
# pyqtgraph(選配)
|
||||
_install_mock("pyqtgraph")
|
||||
134
tests/unit/test_benchmark_dialog.py
Normal file
134
tests/unit/test_benchmark_dialog.py
Normal file
@ -0,0 +1,134 @@
|
||||
"""
|
||||
BenchmarkDialog 的單元測試。
|
||||
|
||||
測試策略:
|
||||
- PyQt5 在 CI 環境中不可用,透過 conftest.py 的 Stub 注入繞過 import。
|
||||
- 測試驗證 BenchmarkDialog 的行為邏輯:
|
||||
- 對話框可正常建立
|
||||
- pipeline_config 為空時開始按鈕被禁用
|
||||
- show_result 正確顯示加速倍數文字
|
||||
- update_progress 更新進度條值
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:BenchmarkDialog 可以建立
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBenchmarkDialogInit:
|
||||
def should_be_importable(self):
|
||||
"""BenchmarkDialog 模組應可匯入(即使 PyQt5 被 Stub)。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
assert BenchmarkDialog is not None
|
||||
|
||||
def should_instantiate_with_valid_config(self):
|
||||
"""提供非空 pipeline_config 時,BenchmarkDialog 應可正常建立。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage_config = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage_config])
|
||||
assert dialog is not None
|
||||
|
||||
def should_instantiate_with_empty_config(self):
|
||||
"""pipeline_config 為空時,BenchmarkDialog 應可建立(不應拋出例外)。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[])
|
||||
assert dialog is not None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:pipeline_config 為空時禁用開始按鈕
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestStartButtonDisabledWhenEmptyConfig:
|
||||
def should_disable_start_button_when_pipeline_config_is_empty(self):
|
||||
"""pipeline_config 為空時,start_button 應被禁用。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[])
|
||||
assert dialog.start_button.isEnabled() is False
|
||||
|
||||
def should_enable_start_button_when_pipeline_config_has_stages(self):
|
||||
"""pipeline_config 有 Stage 時,start_button 應為啟用狀態。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage_config = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage_config])
|
||||
assert dialog.start_button.isEnabled() is True
|
||||
|
||||
def should_show_info_label_when_pipeline_config_is_empty(self):
|
||||
"""pipeline_config 為空時,應有提示訊息 label 顯示。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[])
|
||||
assert hasattr(dialog, "info_label")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:show_result 顯示加速倍數
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestShowResult:
|
||||
def should_display_speedup_text_with_x_suffix(self):
|
||||
"""show_result 後,speedup_label 的文字應包含倍數數值與 'x'。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
seq_result = MagicMock()
|
||||
par_result = MagicMock()
|
||||
dialog.show_result(seq_result, par_result, speedup=3.2)
|
||||
call_arg = dialog.speedup_label.setText.call_args[0][0]
|
||||
assert "3.2" in call_arg
|
||||
assert "x" in call_arg.lower() or "X" in call_arg
|
||||
|
||||
def should_display_faster_in_speedup_text(self):
|
||||
"""show_result 後,speedup_label 文字應包含 'FASTER' 或 'faster'。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
seq_result = MagicMock()
|
||||
par_result = MagicMock()
|
||||
dialog.show_result(seq_result, par_result, speedup=2.5)
|
||||
call_arg = dialog.speedup_label.setText.call_args[0][0]
|
||||
assert "FASTER" in call_arg or "faster" in call_arg
|
||||
|
||||
def should_store_seq_result(self):
|
||||
"""show_result 後,seq_result 應儲存在 dialog 上。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
seq_result = MagicMock()
|
||||
par_result = MagicMock()
|
||||
dialog.show_result(seq_result, par_result, speedup=1.8)
|
||||
assert dialog.seq_result is seq_result
|
||||
|
||||
def should_store_par_result(self):
|
||||
"""show_result 後,par_result 應儲存在 dialog 上。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
seq_result = MagicMock()
|
||||
par_result = MagicMock()
|
||||
dialog.show_result(seq_result, par_result, speedup=1.8)
|
||||
assert dialog.par_result is par_result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:update_progress 更新進度條
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestUpdateProgress:
|
||||
def should_update_progress_bar_value(self):
|
||||
"""update_progress 應將進度條值更新為傳入的 value。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
dialog.progress_bar.setValue.reset_mock()
|
||||
dialog.update_progress("warmup", 42)
|
||||
dialog.progress_bar.setValue.assert_called_once_with(42)
|
||||
|
||||
def should_store_current_phase(self):
|
||||
"""update_progress 應儲存當前 phase 名稱。"""
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
stage = MagicMock()
|
||||
dialog = BenchmarkDialog(parent=None, pipeline_config=[stage])
|
||||
dialog.update_progress("sequential", 70)
|
||||
assert dialog.current_phase == "sequential"
|
||||
282
tests/unit/test_benchmarker.py
Normal file
282
tests/unit/test_benchmarker.py
Normal file
@ -0,0 +1,282 @@
|
||||
"""
|
||||
PerformanceBenchmarker 的單元測試。
|
||||
|
||||
測試策略:
|
||||
- BenchmarkConfig / BenchmarkResult 資料結構驗證
|
||||
- calculate_speedup() 純計算邏輯
|
||||
- run_sequential_benchmark() / run_parallel_benchmark() 透過注入的
|
||||
inference_runner callable 進行 Mock,不需要實際硬體
|
||||
- run_full_benchmark() 整合流程
|
||||
"""
|
||||
import time
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from core.performance.benchmarker import (
|
||||
BenchmarkConfig,
|
||||
BenchmarkResult,
|
||||
PerformanceBenchmarker,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 輔助:建立測試用資料結構
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def make_config(**kwargs) -> BenchmarkConfig:
|
||||
"""建立測試用 BenchmarkConfig,提供合理的預設值。"""
|
||||
defaults = dict(
|
||||
pipeline_config=[],
|
||||
test_duration_seconds=1.0,
|
||||
warmup_frames=2,
|
||||
test_input_source="test_video.mp4",
|
||||
)
|
||||
defaults.update(kwargs)
|
||||
return BenchmarkConfig(**defaults)
|
||||
|
||||
|
||||
def make_result(mode: str = "sequential", fps: float = 30.0) -> BenchmarkResult:
|
||||
"""建立測試用 BenchmarkResult。"""
|
||||
avg_latency_ms = (1000.0 / fps) if fps > 0 else 0.0
|
||||
return BenchmarkResult(
|
||||
mode=mode,
|
||||
fps=fps,
|
||||
avg_latency_ms=avg_latency_ms,
|
||||
p95_latency_ms=avg_latency_ms * 1.5,
|
||||
total_frames=int(fps * 30),
|
||||
timestamp=time.time(),
|
||||
device_config={"KL520": 1},
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:BenchmarkConfig 資料結構
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBenchmarkConfig:
|
||||
def should_have_default_duration_30_seconds(self):
|
||||
"""test_duration_seconds 預設值應為 30.0。"""
|
||||
config = BenchmarkConfig(
|
||||
pipeline_config=[],
|
||||
test_input_source="video.mp4",
|
||||
)
|
||||
assert config.test_duration_seconds == 30.0
|
||||
|
||||
def should_have_default_warmup_50_frames(self):
|
||||
"""warmup_frames 預設值應為 50。"""
|
||||
config = BenchmarkConfig(
|
||||
pipeline_config=[],
|
||||
test_input_source="video.mp4",
|
||||
)
|
||||
assert config.warmup_frames == 50
|
||||
|
||||
def should_allow_custom_duration(self):
|
||||
"""應可自訂 test_duration_seconds。"""
|
||||
config = BenchmarkConfig(
|
||||
pipeline_config=[],
|
||||
test_input_source="video.mp4",
|
||||
test_duration_seconds=10.0,
|
||||
)
|
||||
assert config.test_duration_seconds == 10.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:BenchmarkResult 資料結構
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBenchmarkResult:
|
||||
def should_store_all_required_fields(self):
|
||||
"""BenchmarkResult 應儲存所有規格要求的欄位。"""
|
||||
ts = time.time()
|
||||
result = BenchmarkResult(
|
||||
mode="parallel",
|
||||
fps=45.2,
|
||||
avg_latency_ms=22.1,
|
||||
p95_latency_ms=35.0,
|
||||
total_frames=1356,
|
||||
timestamp=ts,
|
||||
device_config={"KL720": 2},
|
||||
)
|
||||
assert result.mode == "parallel"
|
||||
assert result.fps == pytest.approx(45.2)
|
||||
assert result.avg_latency_ms == pytest.approx(22.1)
|
||||
assert result.p95_latency_ms == pytest.approx(35.0)
|
||||
assert result.total_frames == 1356
|
||||
assert result.timestamp == pytest.approx(ts)
|
||||
assert result.device_config == {"KL720": 2}
|
||||
|
||||
def should_accept_sequential_mode(self):
|
||||
"""mode 欄位應接受 'sequential'。"""
|
||||
result = make_result(mode="sequential")
|
||||
assert result.mode == "sequential"
|
||||
|
||||
def should_accept_parallel_mode(self):
|
||||
"""mode 欄位應接受 'parallel'。"""
|
||||
result = make_result(mode="parallel")
|
||||
assert result.mode == "parallel"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:calculate_speedup(純計算,無外部依賴)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestCalculateSpeedup:
|
||||
def should_return_ratio_of_parallel_to_sequential_fps(self):
|
||||
"""calculate_speedup 應回傳 par.fps / seq.fps。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
seq = make_result(mode="sequential", fps=20.0)
|
||||
par = make_result(mode="parallel", fps=60.0)
|
||||
|
||||
speedup = benchmarker.calculate_speedup(seq, par)
|
||||
assert speedup == pytest.approx(3.0)
|
||||
|
||||
def should_return_one_when_same_fps(self):
|
||||
"""相同 FPS 時 speedup 應為 1.0。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
result = make_result(fps=30.0)
|
||||
|
||||
speedup = benchmarker.calculate_speedup(result, result)
|
||||
assert speedup == pytest.approx(1.0)
|
||||
|
||||
def should_raise_when_sequential_fps_is_zero(self):
|
||||
"""seq.fps 為 0 時應引發 ValueError,避免除以零。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
seq = make_result(fps=0.0)
|
||||
par = make_result(fps=30.0)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
benchmarker.calculate_speedup(seq, par)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:run_sequential_benchmark(Mock inference_runner)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRunSequentialBenchmark:
|
||||
def should_return_benchmark_result_with_sequential_mode(self):
|
||||
"""run_sequential_benchmark() 應回傳 mode='sequential' 的 BenchmarkResult。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.1)
|
||||
|
||||
# Mock inference_runner:每次呼叫模擬 10ms 推論
|
||||
def fake_runner(frame_data):
|
||||
time.sleep(0.01)
|
||||
return {"result": "ok"}
|
||||
|
||||
result = benchmarker.run_sequential_benchmark(config, inference_runner=fake_runner)
|
||||
|
||||
assert isinstance(result, BenchmarkResult)
|
||||
assert result.mode == "sequential"
|
||||
|
||||
def should_report_positive_fps(self):
|
||||
"""FPS 應大於 0。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.1)
|
||||
|
||||
def fake_runner(frame_data):
|
||||
time.sleep(0.01)
|
||||
return {}
|
||||
|
||||
result = benchmarker.run_sequential_benchmark(config, inference_runner=fake_runner)
|
||||
assert result.fps > 0
|
||||
|
||||
def should_report_positive_latency(self):
|
||||
"""avg_latency_ms 和 p95_latency_ms 應大於 0。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.1)
|
||||
|
||||
def fake_runner(frame_data):
|
||||
time.sleep(0.01)
|
||||
return {}
|
||||
|
||||
result = benchmarker.run_sequential_benchmark(config, inference_runner=fake_runner)
|
||||
assert result.avg_latency_ms > 0
|
||||
assert result.p95_latency_ms > 0
|
||||
|
||||
def should_count_frames_excluding_warmup(self):
|
||||
"""total_frames 不應包含暖機幀數。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
call_times = []
|
||||
|
||||
def fake_runner(frame_data):
|
||||
call_times.append(time.time())
|
||||
time.sleep(0.005)
|
||||
return {}
|
||||
|
||||
config = make_config(warmup_frames=3, test_duration_seconds=0.1)
|
||||
result = benchmarker.run_sequential_benchmark(config, inference_runner=fake_runner)
|
||||
|
||||
# warmup 幀不計入 total_frames
|
||||
assert result.total_frames < len(call_times)
|
||||
assert result.total_frames > 0
|
||||
|
||||
def should_use_device_config_from_benchmarker(self):
|
||||
"""BenchmarkResult.device_config 應由 PerformanceBenchmarker 填寫。"""
|
||||
benchmarker = PerformanceBenchmarker(device_config={"KL520": 1})
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.05)
|
||||
|
||||
def fake_runner(frame_data):
|
||||
return {}
|
||||
|
||||
result = benchmarker.run_sequential_benchmark(config, inference_runner=fake_runner)
|
||||
assert result.device_config == {"KL520": 1}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:run_parallel_benchmark(Mock inference_runner)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRunParallelBenchmark:
|
||||
def should_return_benchmark_result_with_parallel_mode(self):
|
||||
"""run_parallel_benchmark() 應回傳 mode='parallel' 的 BenchmarkResult。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.1)
|
||||
|
||||
def fake_runner(frame_data):
|
||||
time.sleep(0.01)
|
||||
return {}
|
||||
|
||||
result = benchmarker.run_parallel_benchmark(config, inference_runner=fake_runner)
|
||||
assert isinstance(result, BenchmarkResult)
|
||||
assert result.mode == "parallel"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:run_full_benchmark
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRunFullBenchmark:
|
||||
def should_return_tuple_of_seq_par_speedup(self):
|
||||
"""run_full_benchmark() 應回傳 (BenchmarkResult, BenchmarkResult, float)。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.05)
|
||||
|
||||
def fast_runner(frame_data):
|
||||
time.sleep(0.005)
|
||||
return {}
|
||||
|
||||
seq_result, par_result, speedup = benchmarker.run_full_benchmark(
|
||||
config, inference_runner=fast_runner
|
||||
)
|
||||
|
||||
assert isinstance(seq_result, BenchmarkResult)
|
||||
assert isinstance(par_result, BenchmarkResult)
|
||||
assert isinstance(speedup, float)
|
||||
assert seq_result.mode == "sequential"
|
||||
assert par_result.mode == "parallel"
|
||||
|
||||
def should_calculate_speedup_consistently(self):
|
||||
"""speedup 應與 calculate_speedup(seq, par) 的結果一致。"""
|
||||
benchmarker = PerformanceBenchmarker()
|
||||
config = make_config(warmup_frames=1, test_duration_seconds=0.05)
|
||||
|
||||
def fake_runner(frame_data):
|
||||
time.sleep(0.005)
|
||||
return {}
|
||||
|
||||
seq_result, par_result, speedup = benchmarker.run_full_benchmark(
|
||||
config, inference_runner=fake_runner
|
||||
)
|
||||
|
||||
expected_speedup = benchmarker.calculate_speedup(seq_result, par_result)
|
||||
assert speedup == pytest.approx(expected_speedup)
|
||||
43
tests/unit/test_bottleneck.py
Normal file
43
tests/unit/test_bottleneck.py
Normal file
@ -0,0 +1,43 @@
|
||||
"""
|
||||
tests/unit/test_bottleneck.py
|
||||
|
||||
Unit tests for the BottleneckAlert dataclass.
|
||||
|
||||
TDD: Red phase — tests written before implementation.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from core.device.bottleneck import BottleneckAlert
|
||||
|
||||
|
||||
class TestBottleneckAlert:
|
||||
def test_fields_accessible(self):
|
||||
alert = BottleneckAlert(
|
||||
stage_id="stage-1",
|
||||
queue_fill_rate=0.85,
|
||||
suggested_action="Add more Dongles to this stage",
|
||||
severity="warning",
|
||||
)
|
||||
assert alert.stage_id == "stage-1"
|
||||
assert alert.queue_fill_rate == 0.85
|
||||
assert alert.suggested_action == "Add more Dongles to this stage"
|
||||
assert alert.severity == "warning"
|
||||
|
||||
def test_severity_critical(self):
|
||||
alert = BottleneckAlert(
|
||||
stage_id="stage-2",
|
||||
queue_fill_rate=0.95,
|
||||
suggested_action="Urgent: add Dongles",
|
||||
severity="critical",
|
||||
)
|
||||
assert alert.severity == "critical"
|
||||
|
||||
def test_dataclass_equality(self):
|
||||
a = BottleneckAlert("s1", 0.9, "action", "warning")
|
||||
b = BottleneckAlert("s1", 0.9, "action", "warning")
|
||||
assert a == b
|
||||
|
||||
def test_dataclass_inequality(self):
|
||||
a = BottleneckAlert("s1", 0.9, "action", "warning")
|
||||
b = BottleneckAlert("s1", 0.5, "action", "warning")
|
||||
assert a != b
|
||||
106
tests/unit/test_device_management_panel.py
Normal file
106
tests/unit/test_device_management_panel.py
Normal file
@ -0,0 +1,106 @@
|
||||
"""
|
||||
tests/unit/test_device_management_panel.py
|
||||
|
||||
Unit tests for DeviceManagementPanel QWidget.
|
||||
|
||||
TDD: Red phase — tests written before implementation.
|
||||
Uses conftest.py Stubs for PyQt5 so no display hardware is needed.
|
||||
"""
|
||||
from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
|
||||
from core.device.device_manager import DeviceInfo, DeviceManager
|
||||
from ui.components.device_management_panel import DeviceManagementPanel
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_device_manager(devices=None):
|
||||
"""Return a DeviceManager-like mock with controllable scan_devices()."""
|
||||
mgr = MagicMock(spec=DeviceManager)
|
||||
if devices is None:
|
||||
devices = [
|
||||
DeviceInfo(
|
||||
device_id="usb-1",
|
||||
series="KL520",
|
||||
product_id=0x100,
|
||||
status="online",
|
||||
gops=2,
|
||||
assigned_stage=None,
|
||||
current_fps=15.0,
|
||||
utilization_pct=50.0,
|
||||
)
|
||||
]
|
||||
mgr.scan_devices.return_value = devices
|
||||
mgr.get_device_statistics.return_value = {d.device_id: d for d in devices}
|
||||
mgr.get_load_balance_recommendation.return_value = {}
|
||||
return mgr
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Panel instantiation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDeviceManagementPanelInit:
|
||||
def test_panel_creates_without_error(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
assert panel is not None
|
||||
|
||||
def test_panel_has_auto_balance_button(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
# auto_balance_button must exist
|
||||
assert hasattr(panel, "auto_balance_button")
|
||||
|
||||
def test_auto_balance_button_text(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
assert panel.auto_balance_button._text == "Auto Balance"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# refresh()
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDeviceManagementPanelRefresh:
|
||||
def test_refresh_calls_scan_devices(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
mgr.scan_devices.reset_mock()
|
||||
panel.refresh()
|
||||
mgr.scan_devices.assert_called_once()
|
||||
|
||||
def test_refresh_updates_known_devices(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
panel.refresh()
|
||||
# After refresh, panel should have device data accessible
|
||||
assert len(panel._devices) == 1
|
||||
assert panel._devices[0].device_id == "usb-1"
|
||||
|
||||
def test_refresh_with_no_devices_sets_empty_list(self):
|
||||
mgr = _make_device_manager(devices=[])
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
panel.refresh()
|
||||
assert panel._devices == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# set_auto_refresh()
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestSetAutoRefresh:
|
||||
def test_set_auto_refresh_stores_interval(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
panel.set_auto_refresh(interval_ms=3000)
|
||||
assert panel._auto_refresh_interval_ms == 3000
|
||||
|
||||
def test_set_auto_refresh_default_interval(self):
|
||||
mgr = _make_device_manager()
|
||||
panel = DeviceManagementPanel(device_manager=mgr)
|
||||
panel.set_auto_refresh()
|
||||
assert panel._auto_refresh_interval_ms == 2000
|
||||
291
tests/unit/test_device_manager.py
Normal file
291
tests/unit/test_device_manager.py
Normal file
@ -0,0 +1,291 @@
|
||||
"""
|
||||
tests/unit/test_device_manager.py
|
||||
|
||||
Unit tests for DeviceManager, DeviceInfo, DeviceHealth.
|
||||
|
||||
TDD: Red phase — tests written before implementation.
|
||||
"""
|
||||
from unittest.mock import MagicMock
|
||||
import pytest
|
||||
|
||||
from core.device.device_manager import DeviceInfo, DeviceHealth, DeviceManager
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_mock_kp_api(devices):
|
||||
"""Build a minimal kp API mock whose scan_devices() returns a descriptor list."""
|
||||
descriptor_list = MagicMock()
|
||||
descriptor_list.device_descriptor_number = len(devices)
|
||||
mock_descs = []
|
||||
for d in devices:
|
||||
desc = MagicMock()
|
||||
desc.usb_port_id = d["port_id"]
|
||||
desc.product_id = d["product_id"]
|
||||
desc.kn_number = d.get("kn_number", 0)
|
||||
mock_descs.append(desc)
|
||||
descriptor_list.device_descriptor_list = mock_descs
|
||||
kp_api = MagicMock()
|
||||
kp_api.core.scan_devices.return_value = descriptor_list
|
||||
return kp_api
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def two_device_kp():
|
||||
"""Mock kp API returning one KL520 and one KL720."""
|
||||
return _make_mock_kp_api([
|
||||
{"port_id": 1, "product_id": 0x100}, # KL520
|
||||
{"port_id": 2, "product_id": 0x720}, # KL720
|
||||
])
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def empty_kp():
|
||||
"""Mock kp API returning no devices."""
|
||||
descriptor_list = MagicMock()
|
||||
descriptor_list.device_descriptor_number = 0
|
||||
descriptor_list.device_descriptor_list = []
|
||||
kp_api = MagicMock()
|
||||
kp_api.core.scan_devices.return_value = descriptor_list
|
||||
return kp_api
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceInfo dataclass
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDeviceInfo:
|
||||
def test_fields_accessible(self):
|
||||
info = DeviceInfo(
|
||||
device_id="usb-1",
|
||||
series="KL520",
|
||||
product_id=0x100,
|
||||
status="online",
|
||||
gops=2,
|
||||
assigned_stage=None,
|
||||
current_fps=0.0,
|
||||
utilization_pct=0.0,
|
||||
)
|
||||
assert info.device_id == "usb-1"
|
||||
assert info.series == "KL520"
|
||||
assert info.product_id == 0x100
|
||||
assert info.status == "online"
|
||||
assert info.gops == 2
|
||||
assert info.assigned_stage is None
|
||||
assert info.current_fps == 0.0
|
||||
assert info.utilization_pct == 0.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceHealth dataclass
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDeviceHealth:
|
||||
def test_fields_accessible(self):
|
||||
health = DeviceHealth(
|
||||
device_id="usb-1",
|
||||
temperature_celsius=None,
|
||||
error_count=0,
|
||||
last_error=None,
|
||||
uptime_seconds=120.0,
|
||||
)
|
||||
assert health.device_id == "usb-1"
|
||||
assert health.temperature_celsius is None
|
||||
assert health.error_count == 0
|
||||
assert health.last_error is None
|
||||
assert health.uptime_seconds == 120.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager.scan_devices
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestScanDevices:
|
||||
def test_returns_list_of_device_info(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
devices = mgr.scan_devices()
|
||||
assert isinstance(devices, list)
|
||||
assert len(devices) == 2
|
||||
assert all(isinstance(d, DeviceInfo) for d in devices)
|
||||
|
||||
def test_kl520_properties(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
devices = mgr.scan_devices()
|
||||
kl520 = next(d for d in devices if d.series == "KL520")
|
||||
assert kl520.product_id == 0x100
|
||||
assert kl520.gops == 2
|
||||
assert kl520.status == "online"
|
||||
|
||||
def test_kl720_properties(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
devices = mgr.scan_devices()
|
||||
kl720 = next(d for d in devices if d.series == "KL720")
|
||||
assert kl720.product_id == 0x720
|
||||
assert kl720.gops == 28
|
||||
assert kl720.status == "online"
|
||||
|
||||
def test_empty_returns_empty_list(self, empty_kp):
|
||||
mgr = DeviceManager(kp_api=empty_kp)
|
||||
devices = mgr.scan_devices()
|
||||
assert devices == []
|
||||
|
||||
def test_device_id_uses_port(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
devices = mgr.scan_devices()
|
||||
ids = {d.device_id for d in devices}
|
||||
assert "usb-1" in ids
|
||||
assert "usb-2" in ids
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager.assign_device / unassign_device
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAssignDevice:
|
||||
def test_assign_online_device_returns_true(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
result = mgr.assign_device("usb-1", "stage-A")
|
||||
assert result is True
|
||||
|
||||
def test_assigned_device_shows_stage(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
devices = mgr.get_device_statistics()
|
||||
assert devices["usb-1"].assigned_stage == "stage-A"
|
||||
|
||||
def test_assign_already_assigned_device_returns_false(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
result = mgr.assign_device("usb-1", "stage-B")
|
||||
assert result is False
|
||||
|
||||
def test_assign_unknown_device_returns_false(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
result = mgr.assign_device("usb-99", "stage-A")
|
||||
assert result is False
|
||||
|
||||
def test_unassign_frees_device(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
result = mgr.unassign_device("usb-1")
|
||||
assert result is True
|
||||
devices = mgr.get_device_statistics()
|
||||
assert devices["usb-1"].assigned_stage is None
|
||||
|
||||
def test_unassign_unknown_device_returns_false(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
result = mgr.unassign_device("usb-99")
|
||||
assert result is False
|
||||
|
||||
def test_reassign_after_unassign_succeeds(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
mgr.unassign_device("usb-1")
|
||||
result = mgr.assign_device("usb-1", "stage-B")
|
||||
assert result is True
|
||||
|
||||
def test_should_reject_assignment_for_offline_device(self):
|
||||
"""assign_device returns False when the device status is offline."""
|
||||
kp_api = _make_mock_kp_api([{"port_id": 5, "product_id": 0x100}])
|
||||
mgr = DeviceManager(kp_api=kp_api)
|
||||
mgr.scan_devices()
|
||||
mgr._devices["usb-5"].status = "offline"
|
||||
result = mgr.assign_device("usb-5", "stage-A")
|
||||
assert result is False
|
||||
|
||||
def test_should_allow_reassignment_to_same_stage(self, two_device_kp):
|
||||
"""Assigning a device to the same stage twice is idempotent and returns True."""
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
result = mgr.assign_device("usb-1", "stage-A")
|
||||
assert result is True
|
||||
|
||||
def test_should_reject_reassignment_to_different_stage(self, two_device_kp):
|
||||
"""Assigning a device already assigned to a different stage returns False."""
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
mgr.assign_device("usb-1", "stage-A")
|
||||
result = mgr.assign_device("usb-1", "stage-B")
|
||||
assert result is False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager.get_load_balance_recommendation
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestLoadBalanceRecommendation:
|
||||
def test_returns_dict_mapping_stage_to_device(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
rec = mgr.get_load_balance_recommendation(["stage-A", "stage-B"])
|
||||
assert isinstance(rec, dict)
|
||||
assert "stage-A" in rec
|
||||
assert "stage-B" in rec
|
||||
|
||||
def test_high_gops_assigned_to_first_stage(self, two_device_kp):
|
||||
"""KL720 (28 GOPS) should be recommended for the first stage."""
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
rec = mgr.get_load_balance_recommendation(["stage-A", "stage-B"])
|
||||
# The device recommended for stage-A should be the higher-gops one
|
||||
stats = mgr.get_device_statistics()
|
||||
first_device_id = rec["stage-A"]
|
||||
assert stats[first_device_id].gops == 28 # KL720
|
||||
|
||||
def test_recommendation_with_more_stages_than_devices(self, two_device_kp):
|
||||
"""Extra stages beyond available devices map to empty string."""
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
rec = mgr.get_load_balance_recommendation(["s1", "s2", "s3"])
|
||||
assert rec["s3"] == ""
|
||||
|
||||
def test_recommendation_with_no_devices(self, empty_kp):
|
||||
mgr = DeviceManager(kp_api=empty_kp)
|
||||
mgr.scan_devices()
|
||||
rec = mgr.get_load_balance_recommendation(["stage-A"])
|
||||
assert rec["stage-A"] == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager.get_device_health
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetDeviceHealth:
|
||||
def test_returns_device_health(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
health = mgr.get_device_health("usb-1")
|
||||
assert isinstance(health, DeviceHealth)
|
||||
assert health.device_id == "usb-1"
|
||||
assert health.temperature_celsius is None # SDK does not support it
|
||||
assert health.error_count == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# DeviceManager.get_device_statistics
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetDeviceStatistics:
|
||||
def test_returns_all_known_devices(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
stats = mgr.get_device_statistics()
|
||||
assert isinstance(stats, dict)
|
||||
assert "usb-1" in stats
|
||||
assert "usb-2" in stats
|
||||
|
||||
def test_values_are_device_info(self, two_device_kp):
|
||||
mgr = DeviceManager(kp_api=two_device_kp)
|
||||
mgr.scan_devices()
|
||||
stats = mgr.get_device_statistics()
|
||||
assert all(isinstance(v, DeviceInfo) for v in stats.values())
|
||||
179
tests/unit/test_export_report_dialog.py
Normal file
179
tests/unit/test_export_report_dialog.py
Normal file
@ -0,0 +1,179 @@
|
||||
"""
|
||||
tests/unit/test_export_report_dialog.py — ExportReportDialog 單元測試。
|
||||
|
||||
在無 PyQt5 環境下,使用 conftest.py 中的 Stub 進行測試。
|
||||
"""
|
||||
from unittest.mock import MagicMock, patch
|
||||
import pytest
|
||||
|
||||
from core.performance.benchmarker import BenchmarkResult
|
||||
from core.performance.report_exporter import DeviceSummary, ReportData
|
||||
from ui.dialogs.export_report_dialog import ExportReportDialog
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_benchmark_result(mode: str = "sequential", fps: float = 14.2) -> BenchmarkResult:
|
||||
return BenchmarkResult(
|
||||
mode=mode,
|
||||
fps=fps,
|
||||
avg_latency_ms=70.4,
|
||||
p95_latency_ms=95.0,
|
||||
total_frames=426,
|
||||
timestamp=1743856222.0,
|
||||
device_config={"KL720": 1},
|
||||
id=f"benchmark_20260405_143022_{mode}",
|
||||
)
|
||||
|
||||
|
||||
def _make_dialog(
|
||||
benchmarker=None,
|
||||
history=None,
|
||||
device_manager=None,
|
||||
dashboard=None,
|
||||
) -> ExportReportDialog:
|
||||
"""建立 ExportReportDialog,所有依賴預設為 MagicMock。"""
|
||||
if benchmarker is None:
|
||||
benchmarker = MagicMock()
|
||||
benchmarker.history = []
|
||||
if history is None:
|
||||
history = MagicMock()
|
||||
history.get_history.return_value = []
|
||||
if device_manager is None:
|
||||
device_manager = MagicMock()
|
||||
device_manager.scan_devices.return_value = []
|
||||
if dashboard is None:
|
||||
dashboard = MagicMock()
|
||||
|
||||
return ExportReportDialog(
|
||||
parent=None,
|
||||
benchmarker=benchmarker,
|
||||
history=history,
|
||||
device_manager=device_manager,
|
||||
dashboard=dashboard,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 基本建立
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestExportReportDialogCreation:
|
||||
def test_dialog_can_be_created(self):
|
||||
"""ExportReportDialog 應可正常建立"""
|
||||
dialog = _make_dialog()
|
||||
assert dialog is not None
|
||||
|
||||
def test_dialog_is_instance_of_qdialog(self):
|
||||
"""ExportReportDialog 應繼承自 QDialog(或其 Stub)"""
|
||||
from PyQt5.QtWidgets import QDialog
|
||||
dialog = _make_dialog()
|
||||
assert isinstance(dialog, QDialog)
|
||||
|
||||
def test_dialog_default_format_is_pdf(self):
|
||||
"""格式選擇預設應為 PDF"""
|
||||
dialog = _make_dialog()
|
||||
assert dialog._selected_format == "pdf"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _collect_report_data
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestCollectReportData:
|
||||
def test_returns_report_data_instance(self):
|
||||
"""_collect_report_data() 應回傳 ReportData 型別"""
|
||||
dialog = _make_dialog()
|
||||
result = dialog._collect_report_data()
|
||||
assert isinstance(result, ReportData)
|
||||
|
||||
def test_uses_history_records(self):
|
||||
"""_collect_report_data() 應使用 history.get_history() 的結果"""
|
||||
history = MagicMock()
|
||||
records = [_make_benchmark_result("parallel")]
|
||||
history.get_history.return_value = records
|
||||
|
||||
dialog = _make_dialog(history=history)
|
||||
result = dialog._collect_report_data()
|
||||
|
||||
history.get_history.assert_called_once()
|
||||
assert result.history_records == records
|
||||
|
||||
def test_uses_device_manager_scan(self):
|
||||
"""_collect_report_data() 應呼叫 device_manager.scan_devices()"""
|
||||
device_manager = MagicMock()
|
||||
device_manager.scan_devices.return_value = []
|
||||
|
||||
dialog = _make_dialog(device_manager=device_manager)
|
||||
dialog._collect_report_data()
|
||||
|
||||
device_manager.scan_devices.assert_called_once()
|
||||
|
||||
def test_handles_history_failure_gracefully(self):
|
||||
"""history.get_history() 拋出例外時,應回傳空的 history_records"""
|
||||
history = MagicMock()
|
||||
history.get_history.side_effect = Exception("history error")
|
||||
|
||||
dialog = _make_dialog(history=history)
|
||||
result = dialog._collect_report_data()
|
||||
|
||||
assert result.history_records == []
|
||||
|
||||
def test_handles_device_manager_failure_gracefully(self):
|
||||
"""device_manager.scan_devices() 拋出例外時,devices 應為空列表"""
|
||||
device_manager = MagicMock()
|
||||
device_manager.scan_devices.side_effect = Exception("device error")
|
||||
|
||||
dialog = _make_dialog(device_manager=device_manager)
|
||||
result = dialog._collect_report_data()
|
||||
|
||||
assert result.devices == []
|
||||
|
||||
def test_uses_latest_benchmark_from_history_as_parallel_result(self):
|
||||
"""benchmarker.history 有記錄時,應使用最新一筆作為 parallel_result"""
|
||||
benchmarker = MagicMock()
|
||||
latest = _make_benchmark_result("parallel", fps=45.6)
|
||||
benchmarker.history = [_make_benchmark_result("sequential"), latest]
|
||||
|
||||
dialog = _make_dialog(benchmarker=benchmarker)
|
||||
result = dialog._collect_report_data()
|
||||
|
||||
# parallel_result 應為最新一筆(index -1)
|
||||
assert result.parallel_result == latest
|
||||
|
||||
def test_parallel_result_is_none_when_history_empty(self):
|
||||
"""benchmarker.history 為空時,parallel_result 應為 None"""
|
||||
benchmarker = MagicMock()
|
||||
benchmarker.history = []
|
||||
|
||||
dialog = _make_dialog(benchmarker=benchmarker)
|
||||
result = dialog._collect_report_data()
|
||||
|
||||
assert result.parallel_result is None
|
||||
|
||||
def test_chart_image_bytes_is_none(self):
|
||||
"""chart_image_bytes 應為 None(截圖整合留未來)"""
|
||||
dialog = _make_dialog()
|
||||
result = dialog._collect_report_data()
|
||||
assert result.chart_image_bytes is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 格式選擇
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestFormatSelection:
|
||||
def test_set_format_to_csv(self):
|
||||
"""可將格式設為 CSV"""
|
||||
dialog = _make_dialog()
|
||||
dialog._set_format("csv")
|
||||
assert dialog._selected_format == "csv"
|
||||
|
||||
def test_set_format_to_pdf(self):
|
||||
"""可將格式設回 PDF"""
|
||||
dialog = _make_dialog()
|
||||
dialog._set_format("csv")
|
||||
dialog._set_format("pdf")
|
||||
assert dialog._selected_format == "pdf"
|
||||
224
tests/unit/test_history.py
Normal file
224
tests/unit/test_history.py
Normal file
@ -0,0 +1,224 @@
|
||||
"""
|
||||
PerformanceHistory 的單元測試。
|
||||
|
||||
測試覆蓋:
|
||||
- 記錄 BenchmarkResult
|
||||
- 依條件查詢歷史記錄(limit / mode 過濾)
|
||||
- 回歸比較報告
|
||||
- 持久化(JSON 讀寫)
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
from core.performance.benchmarker import BenchmarkResult
|
||||
from core.performance.history import PerformanceHistory
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 輔助函式
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def make_result(mode: str = "sequential", fps: float = 30.0, avg_latency_ms: float = 33.3,
|
||||
p95_latency_ms: float = 50.0, total_frames: int = 900) -> BenchmarkResult:
|
||||
"""建立測試用的 BenchmarkResult。"""
|
||||
return BenchmarkResult(
|
||||
mode=mode,
|
||||
fps=fps,
|
||||
avg_latency_ms=avg_latency_ms,
|
||||
p95_latency_ms=p95_latency_ms,
|
||||
total_frames=total_frames,
|
||||
timestamp=time.time(),
|
||||
device_config={"KL520": 1},
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixture
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_history(tmp_path):
|
||||
"""回傳一個使用暫存路徑的 PerformanceHistory 實例。"""
|
||||
storage_path = str(tmp_path / "benchmark_history.json")
|
||||
return PerformanceHistory(storage_path=storage_path)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:基本記錄功能
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestRecord:
|
||||
def should_record_result_to_storage(self, tmp_history):
|
||||
"""record() 應將結果寫入 JSON 儲存。"""
|
||||
result = make_result()
|
||||
tmp_history.record(result)
|
||||
|
||||
records = tmp_history.get_history()
|
||||
assert len(records) == 1
|
||||
|
||||
def should_persist_across_instances(self, tmp_path):
|
||||
"""record() 應將資料持久化,重新建立實例後仍可讀取。"""
|
||||
storage_path = str(tmp_path / "benchmark_history.json")
|
||||
history1 = PerformanceHistory(storage_path=storage_path)
|
||||
result = make_result(fps=42.0)
|
||||
history1.record(result)
|
||||
|
||||
history2 = PerformanceHistory(storage_path=storage_path)
|
||||
records = history2.get_history()
|
||||
assert len(records) == 1
|
||||
assert records[0].fps == 42.0
|
||||
|
||||
def should_assign_unique_id_to_each_record(self, tmp_history):
|
||||
"""每筆記錄應有唯一的 id。"""
|
||||
tmp_history.record(make_result())
|
||||
time.sleep(0.01)
|
||||
tmp_history.record(make_result())
|
||||
|
||||
records = tmp_history.get_history()
|
||||
ids = [r.id for r in records]
|
||||
assert len(set(ids)) == 2
|
||||
|
||||
def should_store_all_benchmark_fields(self, tmp_history):
|
||||
"""record() 應完整儲存所有欄位。"""
|
||||
result = make_result(
|
||||
mode="parallel",
|
||||
fps=60.5,
|
||||
avg_latency_ms=16.5,
|
||||
p95_latency_ms=25.0,
|
||||
total_frames=1815,
|
||||
)
|
||||
tmp_history.record(result)
|
||||
|
||||
saved = tmp_history.get_history()[0]
|
||||
assert saved.mode == "parallel"
|
||||
assert saved.fps == pytest.approx(60.5)
|
||||
assert saved.avg_latency_ms == pytest.approx(16.5)
|
||||
assert saved.p95_latency_ms == pytest.approx(25.0)
|
||||
assert saved.total_frames == 1815
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:get_history 查詢
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetHistory:
|
||||
def should_return_records_in_reverse_chronological_order(self, tmp_history):
|
||||
"""get_history() 應以最新優先的順序回傳記錄。"""
|
||||
base_time = 1000000.0
|
||||
for i, fps in enumerate([10.0, 20.0, 30.0]):
|
||||
result = make_result(fps=fps)
|
||||
result.timestamp = base_time + i # 確保時間戳遞增
|
||||
tmp_history.record(result)
|
||||
|
||||
records = tmp_history.get_history()
|
||||
fps_values = [r.fps for r in records]
|
||||
# 最新優先:fps=30 (timestamp最大) 排第一
|
||||
assert fps_values == [30.0, 20.0, 10.0]
|
||||
|
||||
def should_respect_limit_parameter(self, tmp_history):
|
||||
"""get_history(limit=N) 應只回傳最新的 N 筆記錄。"""
|
||||
for i in range(5):
|
||||
tmp_history.record(make_result(fps=float(i + 1)))
|
||||
|
||||
records = tmp_history.get_history(limit=3)
|
||||
assert len(records) == 3
|
||||
|
||||
def should_filter_by_mode(self, tmp_history):
|
||||
"""get_history(mode='parallel') 應只回傳 parallel 模式的記錄。"""
|
||||
tmp_history.record(make_result(mode="sequential"))
|
||||
tmp_history.record(make_result(mode="parallel"))
|
||||
tmp_history.record(make_result(mode="sequential"))
|
||||
|
||||
records = tmp_history.get_history(mode="parallel")
|
||||
assert len(records) == 1
|
||||
assert records[0].mode == "parallel"
|
||||
|
||||
def should_return_empty_list_when_no_records(self, tmp_history):
|
||||
"""空儲存應回傳空列表。"""
|
||||
records = tmp_history.get_history()
|
||||
assert records == []
|
||||
|
||||
def should_apply_limit_after_mode_filter(self, tmp_history):
|
||||
"""limit 應在 mode 過濾之後套用。"""
|
||||
for _ in range(4):
|
||||
tmp_history.record(make_result(mode="sequential"))
|
||||
for _ in range(4):
|
||||
tmp_history.record(make_result(mode="parallel"))
|
||||
|
||||
records = tmp_history.get_history(limit=2, mode="parallel")
|
||||
assert len(records) == 2
|
||||
assert all(r.mode == "parallel" for r in records)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:回歸報告
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetRegressionReport:
|
||||
def should_report_fps_improvement(self, tmp_history):
|
||||
"""get_regression_report() 應計算 FPS 改善百分比。"""
|
||||
baseline = make_result(fps=30.0, avg_latency_ms=33.3, p95_latency_ms=50.0)
|
||||
tmp_history.record(baseline)
|
||||
baseline_id = tmp_history.get_history()[0].id
|
||||
|
||||
compare = make_result(fps=45.0, avg_latency_ms=22.2, p95_latency_ms=35.0)
|
||||
tmp_history.record(compare)
|
||||
compare_id = tmp_history.get_history()[0].id # 最新一筆
|
||||
|
||||
report = tmp_history.get_regression_report(baseline_id, compare_id)
|
||||
|
||||
assert "fps_change_pct" in report
|
||||
assert report["fps_change_pct"] == pytest.approx(50.0, rel=1e-2)
|
||||
|
||||
def should_report_latency_change(self, tmp_history):
|
||||
"""get_regression_report() 應計算延遲變化百分比。"""
|
||||
baseline = make_result(avg_latency_ms=40.0, p95_latency_ms=60.0)
|
||||
tmp_history.record(baseline)
|
||||
baseline_id = tmp_history.get_history()[0].id
|
||||
|
||||
compare = make_result(avg_latency_ms=20.0, p95_latency_ms=30.0)
|
||||
tmp_history.record(compare)
|
||||
compare_id = tmp_history.get_history()[0].id
|
||||
|
||||
report = tmp_history.get_regression_report(baseline_id, compare_id)
|
||||
|
||||
assert "avg_latency_change_pct" in report
|
||||
assert report["avg_latency_change_pct"] == pytest.approx(-50.0, rel=1e-2)
|
||||
|
||||
def should_raise_error_for_invalid_id(self, tmp_history):
|
||||
"""無效的 id 應引發 ValueError。"""
|
||||
with pytest.raises(ValueError):
|
||||
tmp_history.get_regression_report("nonexistent_baseline", "nonexistent_compare")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:JSON 檔案格式
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestStorageFormat:
|
||||
def should_produce_valid_json_file(self, tmp_path):
|
||||
"""儲存的檔案應為合法的 JSON 並符合規格格式。"""
|
||||
storage_path = str(tmp_path / "benchmark_history.json")
|
||||
history = PerformanceHistory(storage_path=storage_path)
|
||||
history.record(make_result(mode="parallel", fps=45.2))
|
||||
|
||||
with open(storage_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
assert "records" in data
|
||||
assert len(data["records"]) == 1
|
||||
record = data["records"][0]
|
||||
for field in ("id", "mode", "fps", "avg_latency_ms", "p95_latency_ms",
|
||||
"total_frames", "timestamp", "device_config"):
|
||||
assert field in record, f"缺少欄位:{field}"
|
||||
|
||||
def should_create_parent_directory_if_not_exists(self, tmp_path):
|
||||
"""若父目錄不存在,應自動建立。"""
|
||||
storage_path = str(tmp_path / "deep" / "nested" / "history.json")
|
||||
history = PerformanceHistory(storage_path=storage_path)
|
||||
history.record(make_result())
|
||||
|
||||
assert os.path.exists(storage_path)
|
||||
364
tests/unit/test_optimization_engine.py
Normal file
364
tests/unit/test_optimization_engine.py
Normal file
@ -0,0 +1,364 @@
|
||||
"""
|
||||
tests/unit/test_optimization_engine.py
|
||||
|
||||
TDD Phase 3.3.1 — OptimizationEngine 單元測試。
|
||||
|
||||
覆蓋範圍:
|
||||
- analyze_pipeline 的三條優化規則(含邊界值測試)
|
||||
- predict_performance 計算邏輯
|
||||
- apply_suggestion 對 rebalance_devices 呼叫 device_manager
|
||||
"""
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, call
|
||||
|
||||
from core.optimization.engine import OptimizationEngine, OptimizationSuggestion
|
||||
from core.device.device_manager import DeviceInfo
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.fixture
|
||||
def engine():
|
||||
return OptimizationEngine()
|
||||
|
||||
|
||||
def _make_stats(
|
||||
stage_fill_rates=None,
|
||||
stage_avg_times=None,
|
||||
device_utilizations=None,
|
||||
):
|
||||
"""建立 analyze_pipeline 接受的 stats 字典。"""
|
||||
stage_fill_rates = stage_fill_rates or {}
|
||||
stage_avg_times = stage_avg_times or {}
|
||||
device_utilizations = device_utilizations or {}
|
||||
|
||||
stages = {}
|
||||
all_stage_ids = set(stage_fill_rates) | set(stage_avg_times)
|
||||
for sid in all_stage_ids:
|
||||
stages[sid] = {
|
||||
"queue_fill_rate": stage_fill_rates.get(sid, 0.0),
|
||||
"avg_processing_time": stage_avg_times.get(sid, 10.0),
|
||||
"fps": 30.0,
|
||||
}
|
||||
|
||||
devices = {}
|
||||
for did, util in device_utilizations.items():
|
||||
devices[did] = {
|
||||
"utilization_pct": util,
|
||||
"series": "KL720",
|
||||
}
|
||||
|
||||
return {"stages": stages, "devices": devices}
|
||||
|
||||
|
||||
def _make_device_info(device_id="usb-1", gops=28, series="KL720"):
|
||||
return DeviceInfo(
|
||||
device_id=device_id,
|
||||
series=series,
|
||||
product_id=0x720,
|
||||
status="online",
|
||||
gops=gops,
|
||||
assigned_stage=None,
|
||||
current_fps=0.0,
|
||||
utilization_pct=0.0,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# analyze_pipeline — rule 1: rebalance_devices
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAnalyzePipelineRebalanceDevices:
|
||||
"""queue_fill_rate > 0.70 應觸發 rebalance_devices 建議。"""
|
||||
|
||||
def test_should_suggest_rebalance_when_fill_rate_above_threshold(self, engine):
|
||||
stats = _make_stats(stage_fill_rates={"stage_0": 0.71})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "rebalance_devices" in types
|
||||
|
||||
def test_should_not_suggest_rebalance_when_fill_rate_at_threshold(self, engine):
|
||||
"""恰好等於 0.70 不觸發(需 > 0.70)。"""
|
||||
stats = _make_stats(stage_fill_rates={"stage_0": 0.70})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "rebalance_devices" not in types
|
||||
|
||||
def test_should_not_suggest_rebalance_when_fill_rate_below_threshold(self, engine):
|
||||
stats = _make_stats(stage_fill_rates={"stage_0": 0.50})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "rebalance_devices" not in types
|
||||
|
||||
def test_rebalance_suggestion_has_required_fields(self, engine):
|
||||
stats = _make_stats(stage_fill_rates={"stage_0": 0.85})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
rebalance = next(s for s in suggestions if s.type == "rebalance_devices")
|
||||
assert rebalance.suggestion_id
|
||||
assert rebalance.description
|
||||
assert 0.0 <= rebalance.estimated_improvement_pct
|
||||
assert rebalance.confidence in ("high", "medium", "low")
|
||||
assert isinstance(rebalance.action_params, dict)
|
||||
|
||||
def test_rebalance_action_params_includes_stage_id(self, engine):
|
||||
stats = _make_stats(stage_fill_rates={"stage_0": 0.85})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
rebalance = next(s for s in suggestions if s.type == "rebalance_devices")
|
||||
assert "stage_id" in rebalance.action_params
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# analyze_pipeline — rule 2: adjust_queue
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAnalyzePipelineAdjustQueue:
|
||||
"""avg_processing_time 最大/最小比值 > 2.0 應觸發 adjust_queue 建議。"""
|
||||
|
||||
def test_should_suggest_adjust_queue_when_ratio_above_threshold(self, engine):
|
||||
stats = _make_stats(
|
||||
stage_avg_times={"stage_0": 10.0, "stage_1": 25.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "adjust_queue" in types
|
||||
|
||||
def test_should_not_suggest_adjust_queue_when_ratio_at_threshold(self, engine):
|
||||
"""恰好等於 2.0 不觸發(需 > 2.0)。"""
|
||||
stats = _make_stats(
|
||||
stage_avg_times={"stage_0": 10.0, "stage_1": 20.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "adjust_queue" not in types
|
||||
|
||||
def test_should_not_suggest_adjust_queue_when_ratio_below_threshold(self, engine):
|
||||
stats = _make_stats(
|
||||
stage_avg_times={"stage_0": 10.0, "stage_1": 15.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "adjust_queue" not in types
|
||||
|
||||
def test_should_not_suggest_adjust_queue_with_single_stage(self, engine):
|
||||
"""只有一個 Stage 時無法計算比值,不觸發。"""
|
||||
stats = _make_stats(stage_avg_times={"stage_0": 100.0})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "adjust_queue" not in types
|
||||
|
||||
def test_adjust_queue_suggestion_has_required_fields(self, engine):
|
||||
stats = _make_stats(
|
||||
stage_avg_times={"stage_0": 10.0, "stage_1": 25.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
adj = next(s for s in suggestions if s.type == "adjust_queue")
|
||||
assert adj.suggestion_id
|
||||
assert adj.description
|
||||
assert adj.confidence in ("high", "medium", "low")
|
||||
assert isinstance(adj.action_params, dict)
|
||||
|
||||
def should_not_suggest_adjust_queue_when_min_processing_time_is_zero(self, engine):
|
||||
# stage avg_processing_time 為 0 時,比值計算無意義,不應觸發規則
|
||||
stats = _make_stats(stage_avg_times={"stage_0": 0.0, "stage_1": 50.0})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
adjust = [s for s in suggestions if s.type == "adjust_queue"]
|
||||
assert len(adjust) == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# analyze_pipeline — rule 3: add_devices
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAnalyzePipelineAddDevices:
|
||||
"""所有 Dongle 使用率 > 85% 應觸發 add_devices 建議。"""
|
||||
|
||||
def test_should_suggest_add_devices_when_all_above_threshold(self, engine):
|
||||
stats = _make_stats(
|
||||
device_utilizations={"usb-1": 86.0, "usb-2": 90.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "add_devices" in types
|
||||
|
||||
def test_should_not_suggest_add_devices_when_one_device_below_threshold(self, engine):
|
||||
stats = _make_stats(
|
||||
device_utilizations={"usb-1": 90.0, "usb-2": 80.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "add_devices" not in types
|
||||
|
||||
def test_should_not_suggest_add_devices_when_all_at_threshold(self, engine):
|
||||
"""恰好等於 85% 不觸發(需 > 85%)。"""
|
||||
stats = _make_stats(
|
||||
device_utilizations={"usb-1": 85.0, "usb-2": 85.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "add_devices" not in types
|
||||
|
||||
def test_should_not_suggest_add_devices_when_no_devices(self, engine):
|
||||
"""沒有裝置資訊時不觸發。"""
|
||||
stats = _make_stats(device_utilizations={})
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
types = [s.type for s in suggestions]
|
||||
assert "add_devices" not in types
|
||||
|
||||
def test_add_devices_suggestion_has_required_fields(self, engine):
|
||||
stats = _make_stats(
|
||||
device_utilizations={"usb-1": 90.0, "usb-2": 92.0}
|
||||
)
|
||||
suggestions = engine.analyze_pipeline(stats)
|
||||
add = next(s for s in suggestions if s.type == "add_devices")
|
||||
assert add.suggestion_id
|
||||
assert add.description
|
||||
assert add.confidence in ("high", "medium", "low")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# analyze_pipeline — empty stats
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAnalyzePipelineEmptyStats:
|
||||
def test_should_return_empty_list_when_stats_empty(self, engine):
|
||||
suggestions = engine.analyze_pipeline({"stages": {}, "devices": {}})
|
||||
assert suggestions == []
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# predict_performance
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestPredictPerformance:
|
||||
"""predict_performance 使用 sum(gops) / num_stages * 0.6 計算 FPS。"""
|
||||
|
||||
def test_should_return_expected_fps_with_single_device_single_stage(self, engine):
|
||||
devices = [_make_device_info(gops=28)]
|
||||
# estimated_fps = 28 / 1 * 0.6 = 16.8
|
||||
config = [MagicMock()] # 1 stage
|
||||
result = engine.predict_performance(config, devices)
|
||||
assert result["estimated_fps"] == pytest.approx(16.8)
|
||||
|
||||
def test_should_return_expected_latency(self, engine):
|
||||
devices = [_make_device_info(gops=28)]
|
||||
config = [MagicMock()] # 1 stage
|
||||
result = engine.predict_performance(config, devices)
|
||||
# estimated_latency_ms = 1000 / 16.8
|
||||
assert result["estimated_latency_ms"] == pytest.approx(1000.0 / 16.8, rel=1e-4)
|
||||
|
||||
def test_should_return_confidence_range_as_tuple(self, engine):
|
||||
devices = [_make_device_info(gops=28)]
|
||||
config = [MagicMock()] # 1 stage
|
||||
result = engine.predict_performance(config, devices)
|
||||
low, high = result["confidence_range"]
|
||||
fps = result["estimated_fps"]
|
||||
assert low == pytest.approx(fps * 0.8)
|
||||
assert high == pytest.approx(fps * 1.2)
|
||||
|
||||
def test_should_scale_fps_with_multiple_devices(self, engine):
|
||||
devices = [
|
||||
_make_device_info("usb-1", gops=28),
|
||||
_make_device_info("usb-2", gops=28),
|
||||
]
|
||||
config = [MagicMock(), MagicMock()] # 2 stages
|
||||
result = engine.predict_performance(config, devices)
|
||||
# estimated_fps = (28 + 28) / 2 * 0.6 = 16.8
|
||||
assert result["estimated_fps"] == pytest.approx(16.8)
|
||||
|
||||
def test_should_decrease_fps_with_more_stages(self, engine):
|
||||
devices = [_make_device_info(gops=28)]
|
||||
config_1 = [MagicMock()] # 1 stage
|
||||
config_4 = [MagicMock()] * 4 # 4 stages
|
||||
result_1 = engine.predict_performance(config_1, devices)
|
||||
result_4 = engine.predict_performance(config_4, devices)
|
||||
assert result_4["estimated_fps"] < result_1["estimated_fps"]
|
||||
|
||||
def test_should_handle_zero_stages_without_crash(self, engine):
|
||||
"""num_stages = 0 時回傳 0 FPS(不拋錯)。"""
|
||||
devices = [_make_device_info(gops=28)]
|
||||
result = engine.predict_performance([], devices)
|
||||
assert result["estimated_fps"] == 0.0
|
||||
|
||||
def test_should_return_zero_fps_with_no_devices(self, engine):
|
||||
config = [MagicMock()]
|
||||
result = engine.predict_performance(config, [])
|
||||
assert result["estimated_fps"] == 0.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# apply_suggestion
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestApplySuggestion:
|
||||
def _make_rebalance_suggestion(self, stage_id="stage_0", device_id="usb-1"):
|
||||
return OptimizationSuggestion(
|
||||
suggestion_id="test-001",
|
||||
type="rebalance_devices",
|
||||
description="Rebalance test",
|
||||
estimated_improvement_pct=10.0,
|
||||
confidence="medium",
|
||||
action_params={"stage_id": stage_id, "device_id": device_id},
|
||||
)
|
||||
|
||||
def test_should_call_assign_device_for_rebalance_suggestion(self, engine):
|
||||
dm = MagicMock()
|
||||
dm.assign_device.return_value = True
|
||||
suggestion = self._make_rebalance_suggestion("stage_0", "usb-1")
|
||||
result = engine.apply_suggestion(suggestion, dm)
|
||||
dm.assign_device.assert_called_once_with("usb-1", "stage_0")
|
||||
assert result is True
|
||||
|
||||
def test_should_return_false_when_assign_device_fails(self, engine):
|
||||
dm = MagicMock()
|
||||
dm.assign_device.return_value = False
|
||||
suggestion = self._make_rebalance_suggestion()
|
||||
result = engine.apply_suggestion(suggestion, dm)
|
||||
assert result is False
|
||||
|
||||
def test_should_return_true_for_add_devices_without_calling_assign(self, engine):
|
||||
dm = MagicMock()
|
||||
suggestion = OptimizationSuggestion(
|
||||
suggestion_id="test-002",
|
||||
type="add_devices",
|
||||
description="Add more dongles",
|
||||
estimated_improvement_pct=20.0,
|
||||
confidence="high",
|
||||
action_params={},
|
||||
)
|
||||
result = engine.apply_suggestion(suggestion, dm)
|
||||
dm.assign_device.assert_not_called()
|
||||
assert result is True
|
||||
|
||||
def test_should_return_true_for_adjust_queue_without_calling_assign(self, engine):
|
||||
dm = MagicMock()
|
||||
suggestion = OptimizationSuggestion(
|
||||
suggestion_id="test-003",
|
||||
type="adjust_queue",
|
||||
description="Adjust queue size",
|
||||
estimated_improvement_pct=5.0,
|
||||
confidence="low",
|
||||
action_params={},
|
||||
)
|
||||
result = engine.apply_suggestion(suggestion, dm)
|
||||
dm.assign_device.assert_not_called()
|
||||
assert result is True
|
||||
|
||||
def should_call_assign_device_with_empty_device_id_when_not_populated(self, engine):
|
||||
# analyze_pipeline 產生的 rebalance 建議 device_id 預設為空字串
|
||||
# apply_suggestion 應如實傳遞空字串給 device_manager(行為可預期)
|
||||
suggestion = OptimizationSuggestion(
|
||||
suggestion_id="test",
|
||||
type="rebalance_devices",
|
||||
description="test",
|
||||
estimated_improvement_pct=10.0,
|
||||
confidence="medium",
|
||||
action_params={"device_id": "", "stage_id": "stage_0"}
|
||||
)
|
||||
mock_dm = MagicMock()
|
||||
mock_dm.assign_device.return_value = False # 空 device_id 通常回傳 False
|
||||
result = engine.apply_suggestion(suggestion, mock_dm)
|
||||
mock_dm.assign_device.assert_called_once_with("", "stage_0")
|
||||
# result 取決於 assign_device 回傳值
|
||||
assert result == False
|
||||
152
tests/unit/test_performance_dashboard.py
Normal file
152
tests/unit/test_performance_dashboard.py
Normal file
@ -0,0 +1,152 @@
|
||||
"""
|
||||
PerformanceDashboard 的單元測試。
|
||||
|
||||
測試策略:
|
||||
- PyQt5 在 CI 環境中不可用,透過 conftest.py 的 Mock 注入繞過 import。
|
||||
- 測試驗證 PerformanceDashboard 的行為邏輯:
|
||||
update_stats 是否更新顯示值、reset 是否歸零、set_display_window 是否儲存設定。
|
||||
- 使用 MagicMock 取代真實 QLabel,透過記錄 setText 呼叫來驗證。
|
||||
"""
|
||||
import sys
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch, call
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:PerformanceDashboard 可以建立
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestPerformanceDashboardInit:
|
||||
def should_be_importable(self):
|
||||
"""PerformanceDashboard 模組應可匯入(即使 PyQt5 被 Mock)。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
assert PerformanceDashboard is not None
|
||||
|
||||
def should_instantiate_without_error(self):
|
||||
"""PerformanceDashboard() 應可無錯誤地建立實例。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
assert dashboard is not None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:update_stats 更新顯示值
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestUpdateStats:
|
||||
def should_store_fps_after_update(self):
|
||||
"""update_stats 後,current_fps 屬性應更新為傳入的值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 30.5, "avg_latency_ms": 10.0, "p95_latency_ms": 15.0})
|
||||
assert dashboard.current_fps == pytest.approx(30.5)
|
||||
|
||||
def should_store_avg_latency_after_update(self):
|
||||
"""update_stats 後,current_avg_latency_ms 屬性應更新。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 12.3, "p95_latency_ms": 20.0})
|
||||
assert dashboard.current_avg_latency_ms == pytest.approx(12.3)
|
||||
|
||||
def should_store_p95_latency_after_update(self):
|
||||
"""update_stats 後,current_p95_latency_ms 屬性應更新。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 12.0, "p95_latency_ms": 25.7})
|
||||
assert dashboard.current_p95_latency_ms == pytest.approx(25.7)
|
||||
|
||||
def should_call_fps_label_setText(self):
|
||||
"""update_stats 應對 fps_label 呼叫 setText,包含 fps 數值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.fps_label.setText.reset_mock()
|
||||
dashboard.update_stats({"fps": 45.0, "avg_latency_ms": 10.0, "p95_latency_ms": 15.0})
|
||||
dashboard.fps_label.setText.assert_called_once()
|
||||
call_arg = dashboard.fps_label.setText.call_args[0][0]
|
||||
assert "45" in call_arg
|
||||
|
||||
def should_call_avg_latency_label_setText(self):
|
||||
"""update_stats 應對 avg_latency_label 呼叫 setText,包含延遲數值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.avg_latency_label.setText.reset_mock()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 8.5, "p95_latency_ms": 12.0})
|
||||
dashboard.avg_latency_label.setText.assert_called_once()
|
||||
call_arg = dashboard.avg_latency_label.setText.call_args[0][0]
|
||||
assert "8.5" in call_arg or "8" in call_arg
|
||||
|
||||
def should_call_p95_latency_label_setText(self):
|
||||
"""update_stats 應對 p95_latency_label 呼叫 setText,包含 p95 數值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.p95_latency_label.setText.reset_mock()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 8.0, "p95_latency_ms": 19.2})
|
||||
dashboard.p95_latency_label.setText.assert_called_once()
|
||||
call_arg = dashboard.p95_latency_label.setText.call_args[0][0]
|
||||
assert "19" in call_arg
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:reset 歸零
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestReset:
|
||||
def should_reset_fps_to_zero(self):
|
||||
"""reset() 後 current_fps 應歸零。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 55.0, "avg_latency_ms": 5.0, "p95_latency_ms": 8.0})
|
||||
dashboard.reset()
|
||||
assert dashboard.current_fps == 0.0
|
||||
|
||||
def should_reset_avg_latency_to_zero(self):
|
||||
"""reset() 後 current_avg_latency_ms 應歸零。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 12.0, "p95_latency_ms": 18.0})
|
||||
dashboard.reset()
|
||||
assert dashboard.current_avg_latency_ms == 0.0
|
||||
|
||||
def should_reset_p95_latency_to_zero(self):
|
||||
"""reset() 後 current_p95_latency_ms 應歸零。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.update_stats({"fps": 30.0, "avg_latency_ms": 12.0, "p95_latency_ms": 18.0})
|
||||
dashboard.reset()
|
||||
assert dashboard.current_p95_latency_ms == 0.0
|
||||
|
||||
def should_call_label_setText_with_zero_on_reset(self):
|
||||
"""reset() 應對 fps_label 呼叫 setText,更新為 0 值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.fps_label.setText.reset_mock()
|
||||
dashboard.reset()
|
||||
dashboard.fps_label.setText.assert_called_once()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 測試:set_display_window 儲存設定
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestSetDisplayWindow:
|
||||
def should_store_display_window_seconds(self):
|
||||
"""set_display_window(120) 後,display_window_seconds 應為 120。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.set_display_window(120)
|
||||
assert dashboard.display_window_seconds == 120
|
||||
|
||||
def should_default_to_60_seconds(self):
|
||||
"""不傳參數時 display_window_seconds 預設應為 60。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.set_display_window()
|
||||
assert dashboard.display_window_seconds == 60
|
||||
|
||||
def should_update_display_window_on_second_call(self):
|
||||
"""連續呼叫 set_display_window 應覆蓋舊值。"""
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
dashboard = PerformanceDashboard()
|
||||
dashboard.set_display_window(30)
|
||||
dashboard.set_display_window(90)
|
||||
assert dashboard.display_window_seconds == 90
|
||||
250
tests/unit/test_report_exporter.py
Normal file
250
tests/unit/test_report_exporter.py
Normal file
@ -0,0 +1,250 @@
|
||||
"""
|
||||
tests/unit/test_report_exporter.py — ReportExporter 單元測試。
|
||||
|
||||
按照 TDD 3.4.9 的測試清單實作。
|
||||
"""
|
||||
import csv
|
||||
import io
|
||||
import time
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from core.performance.benchmarker import BenchmarkResult
|
||||
from core.performance.report_exporter import DeviceSummary, ReportData, ReportExporter
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _make_benchmark_result(mode: str = "sequential", fps: float = 14.2) -> BenchmarkResult:
|
||||
return BenchmarkResult(
|
||||
mode=mode,
|
||||
fps=fps,
|
||||
avg_latency_ms=70.4,
|
||||
p95_latency_ms=95.0,
|
||||
total_frames=426,
|
||||
timestamp=1743856222.0,
|
||||
device_config={"KL720": 1},
|
||||
id=f"benchmark_20260405_143022_{mode}",
|
||||
)
|
||||
|
||||
|
||||
def _make_report_data_with_benchmark() -> ReportData:
|
||||
seq = _make_benchmark_result("sequential", fps=14.2)
|
||||
par = _make_benchmark_result("parallel", fps=45.6)
|
||||
return ReportData(
|
||||
report_title="Test Report",
|
||||
pipeline_name="test_pipeline",
|
||||
sequential_result=seq,
|
||||
parallel_result=par,
|
||||
speedup=45.6 / 14.2,
|
||||
history_records=[seq, par],
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _get_timestamp_str
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetTimestampStr:
|
||||
def test_format_is_yyyy_mm_dd_hh_mm_ss(self):
|
||||
"""_get_timestamp_str 應回傳 'YYYY-MM-DD HH:MM:SS' 格式的字串"""
|
||||
ts = 1743856222.0
|
||||
result = ReportExporter._get_timestamp_str(ts)
|
||||
# 驗證格式:長度固定為 19,包含 '-' 和 ':'
|
||||
assert len(result) == 19
|
||||
assert result[4] == "-"
|
||||
assert result[7] == "-"
|
||||
assert result[10] == " "
|
||||
assert result[13] == ":"
|
||||
assert result[16] == ":"
|
||||
|
||||
def test_all_parts_are_digits(self):
|
||||
"""timestamp 各欄位均應為數字"""
|
||||
ts = 1743856222.0
|
||||
result = ReportExporter._get_timestamp_str(ts)
|
||||
parts = result.replace("-", "").replace(":", "").replace(" ", "")
|
||||
assert parts.isdigit()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ReportData 預設值
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestReportDataDefaults:
|
||||
def test_report_title_is_non_empty(self):
|
||||
"""ReportData 預設 report_title 應非空"""
|
||||
data = ReportData()
|
||||
assert data.report_title
|
||||
assert len(data.report_title) > 0
|
||||
|
||||
def test_generated_at_is_close_to_now(self):
|
||||
"""ReportData 預設 generated_at 應接近當下時間(誤差 < 5 秒)"""
|
||||
before = time.time()
|
||||
data = ReportData()
|
||||
after = time.time()
|
||||
assert before <= data.generated_at <= after + 5
|
||||
|
||||
def test_history_records_defaults_to_empty_list(self):
|
||||
"""ReportData 預設 history_records 應為空列表"""
|
||||
data = ReportData()
|
||||
assert data.history_records == []
|
||||
|
||||
def test_devices_defaults_to_empty_list(self):
|
||||
"""ReportData 預設 devices 應為空列表"""
|
||||
data = ReportData()
|
||||
assert data.devices == []
|
||||
|
||||
def test_sequential_result_defaults_to_none(self):
|
||||
data = ReportData()
|
||||
assert data.sequential_result is None
|
||||
|
||||
def test_parallel_result_defaults_to_none(self):
|
||||
data = ReportData()
|
||||
assert data.parallel_result is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# export_csv
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestExportCsv:
|
||||
def test_creates_file_at_given_path(self, tmp_path):
|
||||
"""export_csv() 應在指定路徑建立 CSV 檔案"""
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
result = exporter.export_csv(data, output_path)
|
||||
assert output_path.exists()
|
||||
assert result == output_path
|
||||
|
||||
def test_contains_benchmark_summary_section(self, tmp_path):
|
||||
"""CSV 應包含完整的 benchmark_summary header 行"""
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_csv(data, output_path)
|
||||
|
||||
content = output_path.read_text(encoding="utf-8")
|
||||
assert "section,metric,sequential,parallel,diff_pct" in content
|
||||
|
||||
def test_contains_history_section(self, tmp_path):
|
||||
"""CSV 應包含完整的歷史記錄 header 行"""
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_csv(data, output_path)
|
||||
|
||||
content = output_path.read_text(encoding="utf-8")
|
||||
assert "id,timestamp,mode,fps,avg_latency_ms,p95_latency_ms,total_frames" in content
|
||||
|
||||
# 歷史記錄有 2 筆,驗證資料行數
|
||||
lines = [l for l in content.splitlines() if l.strip()]
|
||||
history_data_lines = [l for l in lines if l.startswith("benchmark_2")]
|
||||
assert len(history_data_lines) == len(data.history_records)
|
||||
|
||||
def test_two_sections_separated_by_blank_line(self, tmp_path):
|
||||
"""CSV 的兩個 header 行之間恰有一行空行"""
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_csv(data, output_path)
|
||||
|
||||
content = output_path.read_text(encoding="utf-8")
|
||||
lines = content.splitlines()
|
||||
|
||||
summary_header = "section,metric,sequential,parallel,diff_pct"
|
||||
history_header = "id,timestamp,mode,fps,avg_latency_ms,p95_latency_ms,total_frames"
|
||||
|
||||
idx_summary = next(i for i, l in enumerate(lines) if l == summary_header)
|
||||
idx_history = next(i for i, l in enumerate(lines) if l == history_header)
|
||||
|
||||
# 兩個 header 行之間,緊鄰 history header 的前一行必須是空行
|
||||
assert idx_history > idx_summary + 1
|
||||
assert lines[idx_history - 1] == ""
|
||||
|
||||
def test_no_benchmark_result_raises_value_error(self, tmp_path):
|
||||
"""sequential_result 或 parallel_result 為 None 時,應拋出 ValueError"""
|
||||
data = ReportData() # sequential_result=None, parallel_result=None
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
with pytest.raises(ValueError):
|
||||
exporter.export_csv(data, output_path)
|
||||
|
||||
def test_empty_history_produces_only_summary(self, tmp_path):
|
||||
"""history_records 為空時,CSV 只輸出 Benchmark 摘要區塊,歷史記錄表為空"""
|
||||
seq = _make_benchmark_result("sequential", fps=14.2)
|
||||
par = _make_benchmark_result("parallel", fps=45.6)
|
||||
data = ReportData(
|
||||
sequential_result=seq,
|
||||
parallel_result=par,
|
||||
speedup=45.6 / 14.2,
|
||||
history_records=[],
|
||||
)
|
||||
output_path = tmp_path / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_csv(data, output_path)
|
||||
|
||||
content = output_path.read_text(encoding="utf-8")
|
||||
assert "benchmark_summary" in content
|
||||
# 沒有歷史資料行(id 開頭的行)
|
||||
data_lines = [l for l in content.splitlines() if l.startswith("benchmark_2")]
|
||||
assert len(data_lines) == 0
|
||||
|
||||
def test_auto_creates_parent_directory(self, tmp_path):
|
||||
"""若輸出路徑的父目錄不存在,export_csv() 應自動建立"""
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "subdir" / "report.csv"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_csv(data, output_path)
|
||||
assert output_path.exists()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# export_pdf
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestExportPdf:
|
||||
def test_creates_file_at_given_path(self, tmp_path):
|
||||
"""export_pdf() 應在指定路徑建立 PDF 檔案(不驗證內容,只驗證存在)"""
|
||||
reportlab = pytest.importorskip("reportlab")
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.pdf"
|
||||
exporter = ReportExporter()
|
||||
result = exporter.export_pdf(data, output_path)
|
||||
assert output_path.exists()
|
||||
assert result == output_path
|
||||
|
||||
def test_auto_creates_parent_directory(self, tmp_path):
|
||||
"""若輸出路徑的父目錄不存在,export_pdf() 應自動建立"""
|
||||
pytest.importorskip("reportlab")
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "subdir" / "report.pdf"
|
||||
exporter = ReportExporter()
|
||||
exporter.export_pdf(data, output_path)
|
||||
assert output_path.exists()
|
||||
|
||||
def test_without_chart_image_does_not_raise(self, tmp_path):
|
||||
"""chart_image_bytes 為 None 時,PDF 匯出不應拋出例外"""
|
||||
pytest.importorskip("reportlab")
|
||||
data = _make_report_data_with_benchmark()
|
||||
data.chart_image_bytes = None
|
||||
output_path = tmp_path / "report.pdf"
|
||||
exporter = ReportExporter()
|
||||
# 不應拋出例外
|
||||
exporter.export_pdf(data, output_path)
|
||||
|
||||
def test_raises_import_error_when_reportlab_missing(self, tmp_path):
|
||||
"""reportlab 未安裝時,export_pdf() 應拋出 ImportError"""
|
||||
import core.performance.report_exporter as re_mod
|
||||
|
||||
data = _make_report_data_with_benchmark()
|
||||
output_path = tmp_path / "report.pdf"
|
||||
exporter = ReportExporter()
|
||||
|
||||
with patch.object(re_mod, "_REPORTLAB_AVAILABLE", False):
|
||||
with pytest.raises(ImportError, match="reportlab"):
|
||||
exporter.export_pdf(data, output_path)
|
||||
88
tests/unit/test_result_serializer.py
Normal file
88
tests/unit/test_result_serializer.py
Normal file
@ -0,0 +1,88 @@
|
||||
"""
|
||||
Tests for ResultSerializer — JSON serialization of inference result objects.
|
||||
"""
|
||||
import dataclasses
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from core.functions.result_handler import ResultSerializer
|
||||
|
||||
|
||||
# Minimal stand-ins for the SDK dataclasses (no kp import needed)
|
||||
@dataclasses.dataclass
|
||||
class FakeBoundingBox:
|
||||
x1: int = 0
|
||||
y1: int = 0
|
||||
x2: int = 100
|
||||
y2: int = 100
|
||||
class_name: str = "fire"
|
||||
score: float = 0.9
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class FakeObjectDetectionResult:
|
||||
class_count: int = 1
|
||||
box_count: int = 1
|
||||
box_list: list = dataclasses.field(default_factory=list)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class FakeClassificationResult:
|
||||
probability: float = 0.85
|
||||
class_name: str = "fire"
|
||||
class_num: int = 0
|
||||
|
||||
|
||||
class TestResultSerializerToJson:
|
||||
def setup_method(self):
|
||||
self.serializer = ResultSerializer()
|
||||
|
||||
def should_serialize_plain_dict(self):
|
||||
data = {"fps": 30.0, "pipeline_id": "p1"}
|
||||
result = self.serializer.to_json(data)
|
||||
assert '"fps"' in result
|
||||
assert "30.0" in result
|
||||
|
||||
def should_serialize_dict_containing_dataclass_object(self):
|
||||
"""Bug reproduction: ObjectDetectionResult in result dict caused TypeError."""
|
||||
det = FakeObjectDetectionResult(
|
||||
class_count=1,
|
||||
box_count=1,
|
||||
box_list=[FakeBoundingBox()]
|
||||
)
|
||||
data = {"stage_results": {"stage_0": det}}
|
||||
# Should NOT raise TypeError: Object of type FakeObjectDetectionResult is not JSON serializable
|
||||
result = self.serializer.to_json(data)
|
||||
assert result is not None
|
||||
assert "stage_0" in result
|
||||
|
||||
def should_serialize_dict_containing_classification_result(self):
|
||||
"""ClassificationResult must also be handled."""
|
||||
clf = FakeClassificationResult(probability=0.85, class_name="fire")
|
||||
data = {"stage_results": {"stage_0": clf}}
|
||||
result = self.serializer.to_json(data)
|
||||
assert "stage_0" in result
|
||||
|
||||
def should_serialize_nested_dataclass_in_list(self):
|
||||
"""box_list inside ObjectDetectionResult contains BoundingBox dataclasses."""
|
||||
det = FakeObjectDetectionResult(
|
||||
box_count=1,
|
||||
box_list=[FakeBoundingBox(x1=10, y1=20, x2=110, y2=120, class_name="fire")]
|
||||
)
|
||||
data = {"detections": det}
|
||||
result = self.serializer.to_json(data)
|
||||
assert "fire" in result
|
||||
|
||||
def should_preserve_primitive_values_unchanged(self):
|
||||
data = {"fps": 45.2, "count": 3, "name": "test", "flag": True}
|
||||
import json
|
||||
result = json.loads(self.serializer.to_json(data))
|
||||
assert result["fps"] == 45.2
|
||||
assert result["count"] == 3
|
||||
assert result["name"] == "test"
|
||||
assert result["flag"] is True
|
||||
|
||||
def should_handle_none_values(self):
|
||||
data = {"result": None, "stage": "stage_0"}
|
||||
result = self.serializer.to_json(data)
|
||||
assert "null" in result
|
||||
231
tests/unit/test_template_manager.py
Normal file
231
tests/unit/test_template_manager.py
Normal file
@ -0,0 +1,231 @@
|
||||
"""
|
||||
tests/unit/test_template_manager.py
|
||||
|
||||
TDD Phase 3.3.2 — TemplateManager 單元測試。
|
||||
|
||||
覆蓋範圍:
|
||||
- get_builtin_templates 回傳 3 個範本
|
||||
- load_template 正確載入內建範本
|
||||
- load_template 對不存在的 ID 拋出 ValueError
|
||||
- save_as_template 建立新範本並可被 load_template 讀取
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from core.templates.manager import TemplateManager, PipelineTemplate
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.fixture
|
||||
def manager():
|
||||
return TemplateManager()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_builtin_templates
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetBuiltinTemplates:
|
||||
def test_should_return_exactly_three_builtin_templates(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
assert len(templates) == 3
|
||||
|
||||
def test_should_return_list_of_pipeline_template_instances(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
for t in templates:
|
||||
assert isinstance(t, PipelineTemplate)
|
||||
|
||||
def test_should_include_yolov5_detection_template(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
ids = [t.template_id for t in templates]
|
||||
assert "yolov5_detection" in ids
|
||||
|
||||
def test_should_include_fire_detection_template(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
ids = [t.template_id for t in templates]
|
||||
assert "fire_detection" in ids
|
||||
|
||||
def test_should_include_dual_model_cascade_template(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
ids = [t.template_id for t in templates]
|
||||
assert "dual_model_cascade" in ids
|
||||
|
||||
def test_each_template_has_non_empty_name_and_description(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
for t in templates:
|
||||
assert t.name
|
||||
assert t.description
|
||||
|
||||
def test_each_template_has_nodes_list(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
for t in templates:
|
||||
assert isinstance(t.nodes, list)
|
||||
assert len(t.nodes) >= 2
|
||||
|
||||
def test_each_template_has_connections_list(self, manager):
|
||||
templates = manager.get_builtin_templates()
|
||||
for t in templates:
|
||||
assert isinstance(t.connections, list)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# load_template — 內建範本
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestLoadTemplate:
|
||||
def test_should_load_yolov5_detection_by_id(self, manager):
|
||||
t = manager.load_template("yolov5_detection")
|
||||
assert isinstance(t, PipelineTemplate)
|
||||
assert t.template_id == "yolov5_detection"
|
||||
|
||||
def test_should_load_fire_detection_by_id(self, manager):
|
||||
t = manager.load_template("fire_detection")
|
||||
assert t.template_id == "fire_detection"
|
||||
|
||||
def test_should_load_dual_model_cascade_by_id(self, manager):
|
||||
t = manager.load_template("dual_model_cascade")
|
||||
assert t.template_id == "dual_model_cascade"
|
||||
|
||||
def test_should_raise_value_error_for_unknown_id(self, manager):
|
||||
with pytest.raises(ValueError, match="not found"):
|
||||
manager.load_template("nonexistent_template_xyz")
|
||||
|
||||
def test_should_raise_value_error_with_template_id_in_message(self, manager):
|
||||
bad_id = "totally_unknown_id"
|
||||
with pytest.raises(ValueError, match=bad_id):
|
||||
manager.load_template(bad_id)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# yolov5_detection 節點結構驗證
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestYolov5DetectionStructure:
|
||||
"""Input → Preprocess → Model → Postprocess → Output 順序。"""
|
||||
|
||||
def test_should_have_five_nodes(self, manager):
|
||||
t = manager.load_template("yolov5_detection")
|
||||
assert len(t.nodes) == 5
|
||||
|
||||
def test_nodes_should_include_input_and_output(self, manager):
|
||||
t = manager.load_template("yolov5_detection")
|
||||
node_types = [n["type"] for n in t.nodes]
|
||||
assert "Input" in node_types
|
||||
assert "Output" in node_types
|
||||
|
||||
def test_nodes_should_include_model_and_preprocess_postprocess(self, manager):
|
||||
t = manager.load_template("yolov5_detection")
|
||||
node_types = [n["type"] for n in t.nodes]
|
||||
assert "Model" in node_types
|
||||
assert "Preprocess" in node_types
|
||||
assert "Postprocess" in node_types
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# fire_detection 節點結構驗證
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestFireDetectionStructure:
|
||||
"""Input → Model → Postprocess → Output 順序。"""
|
||||
|
||||
def test_should_have_four_nodes(self, manager):
|
||||
t = manager.load_template("fire_detection")
|
||||
assert len(t.nodes) == 4
|
||||
|
||||
def test_nodes_should_include_input_model_postprocess_output(self, manager):
|
||||
t = manager.load_template("fire_detection")
|
||||
node_types = [n["type"] for n in t.nodes]
|
||||
assert "Input" in node_types
|
||||
assert "Model" in node_types
|
||||
assert "Postprocess" in node_types
|
||||
assert "Output" in node_types
|
||||
|
||||
def test_nodes_should_not_include_preprocess(self, manager):
|
||||
t = manager.load_template("fire_detection")
|
||||
node_types = [n["type"] for n in t.nodes]
|
||||
assert "Preprocess" not in node_types
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# dual_model_cascade 節點結構驗證
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDualModelCascadeStructure:
|
||||
"""Input → Model1 → Postprocess1 → Model2 → Postprocess2 → Output 順序。"""
|
||||
|
||||
def test_should_have_six_nodes(self, manager):
|
||||
t = manager.load_template("dual_model_cascade")
|
||||
assert len(t.nodes) == 6
|
||||
|
||||
def test_should_have_two_model_nodes(self, manager):
|
||||
t = manager.load_template("dual_model_cascade")
|
||||
model_nodes = [n for n in t.nodes if n["type"] == "Model"]
|
||||
assert len(model_nodes) == 2
|
||||
|
||||
def test_should_have_two_postprocess_nodes(self, manager):
|
||||
t = manager.load_template("dual_model_cascade")
|
||||
pp_nodes = [n for n in t.nodes if n["type"] == "Postprocess"]
|
||||
assert len(pp_nodes) == 2
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# save_as_template
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestSaveAsTemplate:
|
||||
def _sample_config(self):
|
||||
return {
|
||||
"nodes": [
|
||||
{"id": "n1", "type": "Input"},
|
||||
{"id": "n2", "type": "Output"},
|
||||
],
|
||||
"connections": [
|
||||
{"from": "n1", "to": "n2"},
|
||||
],
|
||||
}
|
||||
|
||||
def test_should_return_pipeline_template_instance(self, manager):
|
||||
t = manager.save_as_template(
|
||||
self._sample_config(), "My Template", "A test template"
|
||||
)
|
||||
assert isinstance(t, PipelineTemplate)
|
||||
|
||||
def test_returned_template_has_correct_name(self, manager):
|
||||
t = manager.save_as_template(self._sample_config(), "Custom Pipeline", "desc")
|
||||
assert t.name == "Custom Pipeline"
|
||||
|
||||
def test_returned_template_has_correct_description(self, manager):
|
||||
t = manager.save_as_template(self._sample_config(), "name", "My description")
|
||||
assert t.description == "My description"
|
||||
|
||||
def test_returned_template_has_unique_id(self, manager):
|
||||
t1 = manager.save_as_template(self._sample_config(), "T1", "desc")
|
||||
t2 = manager.save_as_template(self._sample_config(), "T2", "desc")
|
||||
assert t1.template_id != t2.template_id
|
||||
|
||||
def test_returned_template_id_starts_with_custom(self, manager):
|
||||
t = manager.save_as_template(self._sample_config(), "My Template", "desc")
|
||||
assert t.template_id.startswith("custom_")
|
||||
|
||||
def test_saved_template_can_be_loaded_by_id(self, manager):
|
||||
saved = manager.save_as_template(self._sample_config(), "Loadable", "desc")
|
||||
loaded = manager.load_template(saved.template_id)
|
||||
assert loaded.template_id == saved.template_id
|
||||
assert loaded.name == "Loadable"
|
||||
|
||||
def test_saved_template_nodes_match_pipeline_config(self, manager):
|
||||
config = self._sample_config()
|
||||
saved = manager.save_as_template(config, "Node Test", "desc")
|
||||
assert saved.nodes == config["nodes"]
|
||||
|
||||
def test_saved_template_connections_match_pipeline_config(self, manager):
|
||||
config = self._sample_config()
|
||||
saved = manager.save_as_template(config, "Conn Test", "desc")
|
||||
assert saved.connections == config["connections"]
|
||||
|
||||
def test_saving_does_not_affect_builtin_templates(self, manager):
|
||||
manager.save_as_template(self._sample_config(), "Extra", "desc")
|
||||
builtins = manager.get_builtin_templates()
|
||||
assert len(builtins) == 3
|
||||
123
ui/components/device_management_panel.py
Normal file
123
ui/components/device_management_panel.py
Normal file
@ -0,0 +1,123 @@
|
||||
"""
|
||||
ui/components/device_management_panel.py
|
||||
|
||||
DeviceManagementPanel — QWidget that displays the status of all connected
|
||||
NPU Dongles and provides manual/automatic assignment controls.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from PyQt5.QtCore import QTimer, pyqtSignal
|
||||
from PyQt5.QtWidgets import (
|
||||
QHBoxLayout,
|
||||
QLabel,
|
||||
QPushButton,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from core.device.device_manager import DeviceInfo, DeviceManager
|
||||
|
||||
|
||||
class DeviceManagementPanel(QWidget):
|
||||
"""Displays real-time NPU Dongle status and assignment controls.
|
||||
|
||||
Signals
|
||||
-------
|
||||
device_assignment_changed(device_id, stage_id):
|
||||
Emitted when the user changes a device's stage assignment.
|
||||
"""
|
||||
|
||||
device_assignment_changed = pyqtSignal(str, str)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
device_manager: DeviceManager,
|
||||
parent: Optional[QWidget] = None,
|
||||
) -> None:
|
||||
super().__init__(parent)
|
||||
self._device_manager = device_manager
|
||||
self._devices: List[DeviceInfo] = []
|
||||
self._auto_refresh_interval_ms: int = 0
|
||||
self._timer: Optional[QTimer] = None
|
||||
|
||||
self._setup_ui()
|
||||
self.refresh()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# UI construction
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
layout = QVBoxLayout()
|
||||
|
||||
# Toolbar row: Auto Balance button
|
||||
toolbar = QHBoxLayout()
|
||||
self.auto_balance_button = QPushButton("Auto Balance")
|
||||
self.auto_balance_button.clicked.connect(self._on_auto_balance)
|
||||
toolbar.addWidget(self.auto_balance_button)
|
||||
toolbar.addStretch()
|
||||
|
||||
# Device cards area
|
||||
self._cards_layout = QVBoxLayout()
|
||||
self._no_device_label = QLabel("No devices connected")
|
||||
|
||||
layout.addLayout(toolbar)
|
||||
layout.addWidget(self._no_device_label)
|
||||
layout.addLayout(self._cards_layout)
|
||||
self.setLayout(layout)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def refresh(self) -> None:
|
||||
"""Re-scan devices and update the displayed cards."""
|
||||
self._devices = self._device_manager.scan_devices()
|
||||
self._rebuild_cards()
|
||||
|
||||
def set_auto_refresh(self, interval_ms: int = 2000) -> None:
|
||||
"""Configure periodic auto-refresh using a QTimer.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
interval_ms:
|
||||
Refresh interval in milliseconds. Defaults to 2 000 ms.
|
||||
"""
|
||||
if interval_ms <= 0:
|
||||
if self._timer is not None:
|
||||
self._timer.stop()
|
||||
return
|
||||
self._auto_refresh_interval_ms = interval_ms
|
||||
if self._timer is None:
|
||||
self._timer = QTimer(self)
|
||||
self._timer.timeout.connect(self.refresh)
|
||||
self._timer.start(interval_ms)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Private helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _rebuild_cards(self) -> None:
|
||||
"""Recreate device card widgets from the current device list."""
|
||||
if not self._devices:
|
||||
self._no_device_label.setVisible(True)
|
||||
return
|
||||
self._no_device_label.setVisible(False)
|
||||
|
||||
def _on_auto_balance(self) -> None:
|
||||
"""Handle Auto Balance button click."""
|
||||
if not self._devices:
|
||||
return
|
||||
stage_ids = [
|
||||
d.assigned_stage for d in self._devices if d.assigned_stage
|
||||
]
|
||||
if not stage_ids:
|
||||
return
|
||||
recommendations = self._device_manager.get_load_balance_recommendation(
|
||||
stage_ids
|
||||
)
|
||||
for stage_id, device_id in recommendations.items():
|
||||
if device_id:
|
||||
self.device_assignment_changed.emit(device_id, stage_id)
|
||||
97
ui/components/performance_dashboard.py
Normal file
97
ui/components/performance_dashboard.py
Normal file
@ -0,0 +1,97 @@
|
||||
"""
|
||||
ui/components/performance_dashboard.py
|
||||
|
||||
PerformanceDashboard — 顯示即時 FPS 與延遲數值的 QWidget。
|
||||
|
||||
使用 pyqtgraph 繪製折線圖(如可用),否則降級為純 QLabel 顯示數值,
|
||||
避免 import error 導致應用崩潰。
|
||||
"""
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from PyQt5.QtCore import pyqtSignal
|
||||
from PyQt5.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QWidget
|
||||
|
||||
try:
|
||||
import pyqtgraph as pg # type: ignore
|
||||
_PYQTGRAPH_AVAILABLE = True
|
||||
except ImportError:
|
||||
_PYQTGRAPH_AVAILABLE = False
|
||||
# TODO: Phase 2 - 當 pyqtgraph 可用時,改用折線圖顯示歷史 FPS/Latency
|
||||
|
||||
|
||||
class PerformanceDashboard(QWidget):
|
||||
"""即時效能儀錶板元件。
|
||||
|
||||
顯示當前 FPS、平均延遲與 p95 延遲。
|
||||
接受 update_stats(stats) 推送的數據並更新 QLabel 顯示值。
|
||||
"""
|
||||
|
||||
update_requested = pyqtSignal(dict)
|
||||
|
||||
def __init__(self, parent: Optional[QWidget] = None) -> None:
|
||||
super().__init__(parent)
|
||||
|
||||
# 內部狀態
|
||||
self.current_fps: float = 0.0
|
||||
self.current_avg_latency_ms: float = 0.0
|
||||
self.current_p95_latency_ms: float = 0.0
|
||||
self.display_window_seconds: int = 60
|
||||
|
||||
# UI 元件(動態值 label,前綴由靜態 label 負責)
|
||||
self.fps_label = QLabel("0.0")
|
||||
self.avg_latency_label = QLabel("0.0")
|
||||
self.p95_latency_label = QLabel("0.0")
|
||||
|
||||
self._setup_ui()
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
layout = QVBoxLayout()
|
||||
|
||||
fps_row = QHBoxLayout()
|
||||
fps_row.addWidget(QLabel("FPS:"))
|
||||
fps_row.addWidget(self.fps_label)
|
||||
|
||||
avg_row = QHBoxLayout()
|
||||
avg_row.addWidget(QLabel("Avg Latency:"))
|
||||
avg_row.addWidget(self.avg_latency_label)
|
||||
|
||||
p95_row = QHBoxLayout()
|
||||
p95_row.addWidget(QLabel("P95 Latency:"))
|
||||
p95_row.addWidget(self.p95_latency_label)
|
||||
|
||||
layout.addLayout(fps_row)
|
||||
layout.addLayout(avg_row)
|
||||
layout.addLayout(p95_row)
|
||||
self.setLayout(layout)
|
||||
|
||||
def update_stats(self, stats: Dict[str, Any]) -> None:
|
||||
"""接收效能數據並更新顯示。
|
||||
|
||||
Args:
|
||||
stats: 包含 "fps"、"avg_latency_ms"、"p95_latency_ms" 的字典。
|
||||
"""
|
||||
self.current_fps = float(stats.get("fps", 0.0))
|
||||
self.current_avg_latency_ms = float(stats.get("avg_latency_ms", 0.0))
|
||||
self.current_p95_latency_ms = float(stats.get("p95_latency_ms", 0.0))
|
||||
|
||||
self.fps_label.setText(f"{self.current_fps:.1f} FPS")
|
||||
self.avg_latency_label.setText(f"{self.current_avg_latency_ms:.1f} ms")
|
||||
self.p95_latency_label.setText(f"{self.current_p95_latency_ms:.1f} ms")
|
||||
|
||||
def reset(self) -> None:
|
||||
"""清空所有顯示值,回到初始狀態(0)。"""
|
||||
self.current_fps = 0.0
|
||||
self.current_avg_latency_ms = 0.0
|
||||
self.current_p95_latency_ms = 0.0
|
||||
|
||||
self.fps_label.setText("0.0 FPS")
|
||||
self.avg_latency_label.setText("0.0 ms")
|
||||
self.p95_latency_label.setText("0.0 ms")
|
||||
|
||||
def set_display_window(self, seconds: int = 60) -> None:
|
||||
"""設定圖表顯示的時間視窗(秒)。
|
||||
|
||||
Args:
|
||||
seconds: 要顯示的歷史時間範圍,預設 60 秒。
|
||||
"""
|
||||
self.display_window_seconds = seconds
|
||||
207
ui/dialogs/benchmark_dialog.py
Normal file
207
ui/dialogs/benchmark_dialog.py
Normal file
@ -0,0 +1,207 @@
|
||||
"""
|
||||
ui/dialogs/benchmark_dialog.py
|
||||
|
||||
BenchmarkDialog — 一鍵啟動 Benchmark 的 QDialog。
|
||||
|
||||
顯示三階段進度條(熱機/循序/平行)、即時 FPS、完成後加速倍數大字體
|
||||
以及循序 vs 平行的 FPS 與延遲對比表。
|
||||
|
||||
Benchmark 執行透過 QThread 進行,避免 UI 凍結。
|
||||
若 pipeline_config 為空,顯示提示訊息並禁用開始按鈕。
|
||||
"""
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from PyQt5.QtCore import QThread, pyqtSignal
|
||||
from PyQt5.QtWidgets import (
|
||||
QDialog,
|
||||
QHBoxLayout,
|
||||
QLabel,
|
||||
QProgressBar,
|
||||
QPushButton,
|
||||
QTableWidget,
|
||||
QTableWidgetItem,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
|
||||
class _BenchmarkWorker(QThread):
|
||||
"""在背景執行緒執行 benchmark,避免 UI 凍結。"""
|
||||
|
||||
progress_updated = pyqtSignal(str, int)
|
||||
result_ready = pyqtSignal(object, object, float)
|
||||
error_occurred = pyqtSignal(str)
|
||||
|
||||
def __init__(self, benchmarker: Any) -> None:
|
||||
super().__init__()
|
||||
self._benchmarker = benchmarker
|
||||
|
||||
def run(self) -> None:
|
||||
try:
|
||||
seq_result, par_result, speedup = self._benchmarker.run_full_benchmark(
|
||||
progress_callback=self._on_progress
|
||||
)
|
||||
self.result_ready.emit(seq_result, par_result, speedup)
|
||||
except Exception as exc:
|
||||
self.error_occurred.emit(str(exc))
|
||||
|
||||
def _on_progress(self, phase: str, value: int) -> None:
|
||||
self.progress_updated.emit(phase, value)
|
||||
|
||||
|
||||
class BenchmarkDialog(QDialog):
|
||||
"""Benchmark 觸發與結果顯示對話框。
|
||||
|
||||
Args:
|
||||
parent: 父視窗。
|
||||
pipeline_config: 目前的 pipeline Stage 設定列表。若為空,禁用開始按鈕。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent: Optional[QWidget],
|
||||
pipeline_config: List[Any],
|
||||
) -> None:
|
||||
super().__init__(parent)
|
||||
|
||||
self._pipeline_config = pipeline_config
|
||||
self.seq_result: Optional[Any] = None
|
||||
self.par_result: Optional[Any] = None
|
||||
self.current_phase: str = ""
|
||||
self._worker: Optional[_BenchmarkWorker] = None
|
||||
|
||||
self.setWindowTitle("Performance Benchmark")
|
||||
|
||||
# UI 元件
|
||||
self.info_label = QLabel("")
|
||||
self.progress_bar = QProgressBar()
|
||||
self.progress_bar.setMinimum(0)
|
||||
self.progress_bar.setMaximum(100)
|
||||
|
||||
self.fps_label = QLabel("FPS: —")
|
||||
self.phase_label = QLabel("")
|
||||
self.speedup_label = QLabel("")
|
||||
|
||||
self.result_table = QTableWidget(2, 3)
|
||||
self.result_table.setHorizontalHeaderLabels(["模式", "FPS", "Avg Latency (ms)"])
|
||||
|
||||
self.start_button = QPushButton("開始 Benchmark")
|
||||
self.close_button = QPushButton("關閉")
|
||||
|
||||
self._setup_ui()
|
||||
self._apply_initial_state()
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
layout = QVBoxLayout()
|
||||
|
||||
layout.addWidget(self.info_label)
|
||||
|
||||
progress_row = QHBoxLayout()
|
||||
progress_row.addWidget(self.progress_bar)
|
||||
progress_row.addWidget(self.phase_label)
|
||||
layout.addLayout(progress_row)
|
||||
|
||||
fps_row = QHBoxLayout()
|
||||
fps_row.addWidget(QLabel("即時 FPS:"))
|
||||
fps_row.addWidget(self.fps_label)
|
||||
layout.addLayout(fps_row)
|
||||
|
||||
layout.addWidget(self.speedup_label)
|
||||
layout.addWidget(self.result_table)
|
||||
|
||||
btn_row = QHBoxLayout()
|
||||
btn_row.addWidget(self.start_button)
|
||||
btn_row.addWidget(self.close_button)
|
||||
layout.addLayout(btn_row)
|
||||
|
||||
self.setLayout(layout)
|
||||
|
||||
def _apply_initial_state(self) -> None:
|
||||
if not self._pipeline_config:
|
||||
self.info_label.setText("尚未設定 Pipeline,請先在 Pipeline Editor 中建立 Stage。")
|
||||
self.start_button.setEnabled(False)
|
||||
else:
|
||||
self.info_label.setText(f"已載入 {len(self._pipeline_config)} 個 Stage,可開始 Benchmark。")
|
||||
self.start_button.setEnabled(True)
|
||||
|
||||
def start_benchmark(self, benchmarker: Any) -> None:
|
||||
"""在 QThread 中執行 benchmark,避免 UI 凍結。
|
||||
|
||||
Args:
|
||||
benchmarker: PerformanceBenchmarker 實例。
|
||||
"""
|
||||
self._worker = _BenchmarkWorker(benchmarker)
|
||||
self._worker.progress_updated.connect(self.update_progress)
|
||||
self._worker.result_ready.connect(self._on_result_ready)
|
||||
self._worker.error_occurred.connect(self._on_error)
|
||||
self._worker.finished.connect(self._worker.deleteLater)
|
||||
self.start_button.setEnabled(False)
|
||||
self._worker.start()
|
||||
|
||||
def update_progress(self, phase: str, value: int) -> None:
|
||||
"""更新進度條與當前階段。
|
||||
|
||||
Args:
|
||||
phase: 當前階段名稱("warmup" / "sequential" / "parallel")。
|
||||
value: 進度值(0–100)。
|
||||
"""
|
||||
_PHASE_LABELS = {
|
||||
"warmup": "熱機中...",
|
||||
"sequential": "循序測試...",
|
||||
"parallel": "平行測試...",
|
||||
}
|
||||
self.current_phase = phase
|
||||
self.progress_bar.setValue(value)
|
||||
self.phase_label.setText(_PHASE_LABELS.get(phase, phase))
|
||||
|
||||
def show_result(
|
||||
self,
|
||||
seq_result: Any,
|
||||
par_result: Any,
|
||||
speedup: float,
|
||||
) -> None:
|
||||
"""顯示 benchmark 結果。
|
||||
|
||||
Args:
|
||||
seq_result: 循序模式的 BenchmarkResult。
|
||||
par_result: 平行模式的 BenchmarkResult。
|
||||
speedup: 加速倍數(par.fps / seq.fps)。
|
||||
"""
|
||||
self.seq_result = seq_result
|
||||
self.par_result = par_result
|
||||
|
||||
font = self.speedup_label.font()
|
||||
font.setPointSize(20)
|
||||
font.setBold(True)
|
||||
self.speedup_label.setFont(font)
|
||||
self.speedup_label.setText(f"{speedup:.1f}x FASTER")
|
||||
self._populate_table(seq_result, par_result)
|
||||
|
||||
def _populate_table(self, seq_result: Any, par_result: Any) -> None:
|
||||
rows = [
|
||||
("循序", seq_result),
|
||||
("平行", par_result),
|
||||
]
|
||||
for row_idx, (mode_label, result) in enumerate(rows):
|
||||
self.result_table.setItem(row_idx, 0, QTableWidgetItem(mode_label))
|
||||
try:
|
||||
self.result_table.setItem(row_idx, 1, QTableWidgetItem(f"{result.fps:.1f}"))
|
||||
self.result_table.setItem(
|
||||
row_idx, 2, QTableWidgetItem(f"{result.avg_latency_ms:.1f}")
|
||||
)
|
||||
except (AttributeError, TypeError):
|
||||
pass
|
||||
|
||||
def _on_result_ready(
|
||||
self,
|
||||
seq_result: Any,
|
||||
par_result: Any,
|
||||
speedup: float,
|
||||
) -> None:
|
||||
self.show_result(seq_result, par_result, speedup)
|
||||
|
||||
def _on_error(self, message: str) -> None:
|
||||
self.info_label.setText(f"Benchmark 失敗:{message}")
|
||||
self.progress_bar.setValue(0)
|
||||
self._worker = None
|
||||
self.start_button.setEnabled(True)
|
||||
@ -21,6 +21,7 @@ Usage:
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import queue
|
||||
import threading
|
||||
import traceback
|
||||
import io
|
||||
@ -35,8 +36,10 @@ from PyQt5.QtWidgets import (
|
||||
from PyQt5.QtCore import Qt, QThread, pyqtSignal, QTimer
|
||||
from PyQt5.QtGui import QFont, QColor, QPalette, QImage, QPixmap
|
||||
|
||||
# Import our converter and pipeline system
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'core', 'functions'))
|
||||
# Ensure project root is on sys.path so that 'core.functions' package imports work
|
||||
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
|
||||
if PROJECT_ROOT not in sys.path:
|
||||
sys.path.insert(0, PROJECT_ROOT)
|
||||
|
||||
try:
|
||||
from core.functions.mflow_converter import MFlowConverter, PipelineConfig
|
||||
@ -57,50 +60,48 @@ except ImportError as e:
|
||||
|
||||
|
||||
class StdoutCapture:
|
||||
"""Context manager to capture stdout/stderr and emit to signal."""
|
||||
"""Context manager to capture stdout/stderr into a thread-safe queue.
|
||||
|
||||
def __init__(self, signal_emitter):
|
||||
self.signal_emitter = signal_emitter
|
||||
Uses queue.Queue instead of directly emitting a Qt signal, so that
|
||||
any thread (including non-Qt SDK threads) can safely write output
|
||||
without triggering cross-thread signal warnings.
|
||||
The dialog drains the queue from the main thread via QTimer.
|
||||
"""
|
||||
|
||||
def __init__(self, output_queue: queue.Queue):
|
||||
self.output_queue = output_queue
|
||||
self.original_stdout = None
|
||||
self.original_stderr = None
|
||||
self.captured_output = io.StringIO()
|
||||
|
||||
def __enter__(self):
|
||||
self.original_stdout = sys.stdout
|
||||
self.original_stderr = sys.stderr
|
||||
|
||||
# Create a custom write function that both prints to original and captures
|
||||
output_queue = self.output_queue
|
||||
|
||||
class TeeWriter:
|
||||
def __init__(self, original, captured, emitter):
|
||||
def __init__(self, original):
|
||||
self.original = original
|
||||
self.captured = captured
|
||||
self.emitter = emitter
|
||||
self._emitting = False # Prevent recursion
|
||||
|
||||
def write(self, text):
|
||||
# Write to original stdout/stderr (so it still appears in terminal)
|
||||
self.original.write(text)
|
||||
self.original.flush()
|
||||
|
||||
# Capture for GUI if it's a substantial message and not already emitting
|
||||
if text.strip() and not self._emitting:
|
||||
self._emitting = True
|
||||
if self.original is not None:
|
||||
self.original.write(text)
|
||||
self.original.flush()
|
||||
if text.strip():
|
||||
try:
|
||||
self.emitter(text)
|
||||
finally:
|
||||
self._emitting = False
|
||||
output_queue.put_nowait(text)
|
||||
except queue.Full:
|
||||
pass # Drop if queue full; non-fatal
|
||||
|
||||
def flush(self):
|
||||
self.original.flush()
|
||||
|
||||
# Replace stdout and stderr with our tee writers
|
||||
sys.stdout = TeeWriter(self.original_stdout, self.captured_output, self.signal_emitter)
|
||||
sys.stderr = TeeWriter(self.original_stderr, self.captured_output, self.signal_emitter)
|
||||
if self.original is not None:
|
||||
self.original.flush()
|
||||
|
||||
sys.stdout = TeeWriter(self.original_stdout)
|
||||
sys.stderr = TeeWriter(self.original_stderr)
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
# Restore original stdout/stderr
|
||||
sys.stdout = self.original_stdout
|
||||
sys.stderr = self.original_stderr
|
||||
|
||||
@ -118,11 +119,11 @@ class DeploymentWorker(QThread):
|
||||
frame_updated = pyqtSignal('PyQt_PyObject') # For live view
|
||||
result_updated = pyqtSignal(dict) # For inference results
|
||||
terminal_output = pyqtSignal(str) # For terminal output in GUI
|
||||
stdout_captured = pyqtSignal(str) # For captured stdout/stderr
|
||||
|
||||
def __init__(self, pipeline_data: Dict[str, Any]):
|
||||
def __init__(self, pipeline_data: Dict[str, Any], stdout_queue: queue.Queue):
|
||||
super().__init__()
|
||||
self.pipeline_data = pipeline_data
|
||||
self.stdout_queue = stdout_queue # thread-safe queue; drained by dialog's QTimer
|
||||
self.should_stop = False
|
||||
self.orchestrator = None
|
||||
|
||||
@ -178,7 +179,7 @@ class DeploymentWorker(QThread):
|
||||
# Create InferencePipeline instance with stdout capture
|
||||
try:
|
||||
# Capture all stdout/stderr during pipeline operations
|
||||
with StdoutCapture(self.stdout_captured.emit):
|
||||
with StdoutCapture(self.stdout_queue):
|
||||
pipeline = converter.create_inference_pipeline(config)
|
||||
|
||||
self.progress_updated.emit(80, "Initializing workflow orchestrator...")
|
||||
@ -211,7 +212,8 @@ class DeploymentWorker(QThread):
|
||||
# Add current FPS from pipeline to result_dict
|
||||
current_fps = pipeline.get_current_fps()
|
||||
result_dict['current_pipeline_fps'] = current_fps
|
||||
print(f"DEBUG: Pipeline FPS = {current_fps:.2f}") # Debug info
|
||||
if os.getenv('C4NPU_DEBUG', '0') == '1':
|
||||
print(f"DEBUG: Pipeline FPS = {current_fps:.2f}") # Debug info
|
||||
|
||||
# Send to GUI terminal and results display
|
||||
terminal_output = self._format_terminal_results(result_dict)
|
||||
@ -263,42 +265,89 @@ class DeploymentWorker(QThread):
|
||||
output_lines.append(f" Stage: {stage_id}")
|
||||
|
||||
if isinstance(result, tuple) and len(result) == 2:
|
||||
# Handle tuple results (probability, result_string) - matching actual format
|
||||
probability, result_string = result
|
||||
# Handle tuple results (may be (ObjectDetectionResult, result_string) or (float, result_string))
|
||||
probability_or_obj, result_string = result
|
||||
output_lines.append(f" Result: {result_string}")
|
||||
output_lines.append(f" Probability: {probability:.3f}")
|
||||
|
||||
# Add confidence level
|
||||
if probability > 0.8:
|
||||
confidence = "Very High"
|
||||
elif probability > 0.6:
|
||||
confidence = "High"
|
||||
elif probability > 0.4:
|
||||
confidence = "Medium"
|
||||
# If first element is an object detection result, summarize detections
|
||||
if hasattr(probability_or_obj, 'box_count') and hasattr(probability_or_obj, 'box_list'):
|
||||
det = probability_or_obj
|
||||
output_lines.append(f" Detections: {int(getattr(det, 'box_count', 0))}")
|
||||
# Optional short class summary
|
||||
class_counts = {}
|
||||
for b in getattr(det, 'box_list', [])[:5]:
|
||||
name = getattr(b, 'class_name', 'object')
|
||||
class_counts[name] = class_counts.get(name, 0) + 1
|
||||
if class_counts:
|
||||
summary = ", ".join(f"{k} x{v}" for k, v in class_counts.items())
|
||||
output_lines.append(f" Classes: {summary}")
|
||||
else:
|
||||
confidence = "Low"
|
||||
output_lines.append(f" Confidence: {confidence}")
|
||||
# Safely format numeric probability
|
||||
try:
|
||||
prob_value = float(probability_or_obj)
|
||||
output_lines.append(f" Probability: {prob_value:.3f}")
|
||||
# Add confidence level
|
||||
if prob_value > 0.8:
|
||||
confidence = "Very High"
|
||||
elif prob_value > 0.6:
|
||||
confidence = "High"
|
||||
elif prob_value > 0.4:
|
||||
confidence = "Medium"
|
||||
else:
|
||||
confidence = "Low"
|
||||
output_lines.append(f" Confidence: {confidence}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" Probability: {probability_or_obj}")
|
||||
|
||||
elif isinstance(result, dict):
|
||||
# Handle dict results
|
||||
for key, value in result.items():
|
||||
if key == 'probability':
|
||||
output_lines.append(f" {key.title()}: {value:.3f}")
|
||||
try:
|
||||
prob_value = float(value)
|
||||
output_lines.append(f" {key.title()}: {prob_value:.3f}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" {key.title()}: {value}")
|
||||
elif key == 'result':
|
||||
output_lines.append(f" {key.title()}: {value}")
|
||||
elif key == 'confidence':
|
||||
output_lines.append(f" {key.title()}: {value}")
|
||||
elif key == 'fused_probability':
|
||||
output_lines.append(f" Fused Probability: {value:.3f}")
|
||||
try:
|
||||
prob_value = float(value)
|
||||
output_lines.append(f" Fused Probability: {prob_value:.3f}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" Fused Probability: {value}")
|
||||
elif key == 'individual_probs':
|
||||
output_lines.append(f" Individual Probabilities:")
|
||||
for prob_key, prob_value in value.items():
|
||||
output_lines.append(f" {prob_key}: {prob_value:.3f}")
|
||||
try:
|
||||
float_prob = float(prob_value)
|
||||
output_lines.append(f" {prob_key}: {float_prob:.3f}")
|
||||
except (ValueError, TypeError):
|
||||
output_lines.append(f" {prob_key}: {prob_value}")
|
||||
else:
|
||||
output_lines.append(f" {key}: {value}")
|
||||
else:
|
||||
# Handle other result types
|
||||
output_lines.append(f" Raw Result: {result}")
|
||||
# Handle other result types, including detection objects
|
||||
# Try to pretty-print ObjectDetectionResult-like objects
|
||||
try:
|
||||
if hasattr(result, 'box_count') and hasattr(result, 'box_list'):
|
||||
# Summarize detections
|
||||
count = int(getattr(result, 'box_count', 0))
|
||||
output_lines.append(f" Detections: {count}")
|
||||
# Optional: top classes summary
|
||||
class_counts = {}
|
||||
for b in getattr(result, 'box_list', [])[:5]:
|
||||
name = getattr(b, 'class_name', 'object')
|
||||
class_counts[name] = class_counts.get(name, 0) + 1
|
||||
if class_counts:
|
||||
summary = ", ".join(f"{k} x{v}" for k, v in class_counts.items())
|
||||
output_lines.append(f" Classes: {summary}")
|
||||
else:
|
||||
output_lines.append(f" Raw Result: {result}")
|
||||
except Exception:
|
||||
output_lines.append(f" Raw Result: {result}")
|
||||
|
||||
output_lines.append("") # Blank line between stages
|
||||
else:
|
||||
@ -335,12 +384,17 @@ class DeploymentWorker(QThread):
|
||||
class DeploymentDialog(QDialog):
|
||||
"""Main deployment dialog with comprehensive deployment management."""
|
||||
|
||||
# Emitted from the wait_for_stop background thread; connected slot runs on main thread
|
||||
_stop_done = pyqtSignal(bool) # True = stopped cleanly
|
||||
|
||||
def __init__(self, pipeline_data: Dict[str, Any], parent=None):
|
||||
super().__init__(parent)
|
||||
|
||||
self.pipeline_data = pipeline_data
|
||||
self.deployment_worker = None
|
||||
self.pipeline_config = None
|
||||
self._latest_boxes = [] # cached detection boxes for live overlay
|
||||
self._latest_letterbox = None # cached letterbox mapping for overlay
|
||||
|
||||
self.setWindowTitle("Deploy Pipeline to Dongles")
|
||||
self.setMinimumSize(800, 600)
|
||||
@ -558,6 +612,20 @@ class DeploymentDialog(QDialog):
|
||||
self.live_view_label.setAlignment(Qt.AlignCenter)
|
||||
self.live_view_label.setMinimumSize(640, 480)
|
||||
video_layout.addWidget(self.live_view_label)
|
||||
|
||||
# Display threshold control
|
||||
from PyQt5.QtWidgets import QDoubleSpinBox
|
||||
thresh_row = QHBoxLayout()
|
||||
thresh_label = QLabel("Min Conf:")
|
||||
self.display_threshold_spin = QDoubleSpinBox()
|
||||
self.display_threshold_spin.setRange(0.0, 1.0)
|
||||
self.display_threshold_spin.setSingleStep(0.05)
|
||||
self.display_threshold_spin.setValue(getattr(self, '_display_threshold', 0.5))
|
||||
self.display_threshold_spin.valueChanged.connect(self.on_display_threshold_changed)
|
||||
thresh_row.addWidget(thresh_label)
|
||||
thresh_row.addWidget(self.display_threshold_spin)
|
||||
thresh_row.addStretch()
|
||||
video_layout.addLayout(thresh_row)
|
||||
layout.addWidget(video_group, 2)
|
||||
|
||||
# Inference results
|
||||
@ -570,6 +638,13 @@ class DeploymentDialog(QDialog):
|
||||
|
||||
return widget
|
||||
|
||||
def on_display_threshold_changed(self, val: float):
|
||||
"""Update in-UI display confidence threshold for overlays and summaries."""
|
||||
try:
|
||||
self._display_threshold = float(val)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def populate_overview(self):
|
||||
"""Populate overview tab with pipeline data."""
|
||||
self.name_label.setText(self.pipeline_data.get('project_name', 'Untitled'))
|
||||
@ -622,10 +697,32 @@ Stage Configurations:
|
||||
|
||||
for i, stage_config in enumerate(config.stage_configs, 1):
|
||||
analysis_text += f"\nStage {i}: {stage_config.stage_id}\n"
|
||||
analysis_text += f" Port IDs: {stage_config.port_ids}\n"
|
||||
analysis_text += f" Model Path: {stage_config.model_path}\n"
|
||||
analysis_text += f" SCPU Firmware: {stage_config.scpu_fw_path}\n"
|
||||
analysis_text += f" NCPU Firmware: {stage_config.ncpu_fw_path}\n"
|
||||
|
||||
# Check if this is multi-series configuration
|
||||
if stage_config.multi_series_config:
|
||||
analysis_text += f" Mode: Multi-Series\n"
|
||||
analysis_text += f" Series Configured: {list(stage_config.multi_series_config.keys())}\n"
|
||||
|
||||
# Show details for each series
|
||||
for series_name, series_config in stage_config.multi_series_config.items():
|
||||
analysis_text += f" \n {series_name} Configuration:\n"
|
||||
analysis_text += f" Port IDs: {series_config.get('port_ids', [])}\n"
|
||||
model_path = series_config.get('model_path', 'Not specified')
|
||||
analysis_text += f" Model: {model_path}\n"
|
||||
firmware_paths = series_config.get('firmware_paths', {})
|
||||
if firmware_paths:
|
||||
analysis_text += f" SCPU Firmware: {firmware_paths.get('scpu', 'Not specified')}\n"
|
||||
analysis_text += f" NCPU Firmware: {firmware_paths.get('ncpu', 'Not specified')}\n"
|
||||
else:
|
||||
analysis_text += f" Firmware: Not specified\n"
|
||||
else:
|
||||
# Single-series (legacy) configuration
|
||||
analysis_text += f" Mode: Single-Series\n"
|
||||
analysis_text += f" Port IDs: {stage_config.port_ids}\n"
|
||||
analysis_text += f" Model Path: {stage_config.model_path}\n"
|
||||
analysis_text += f" SCPU Firmware: {stage_config.scpu_fw_path}\n"
|
||||
analysis_text += f" NCPU Firmware: {stage_config.ncpu_fw_path}\n"
|
||||
|
||||
analysis_text += f" Upload Firmware: {stage_config.upload_fw}\n"
|
||||
analysis_text += f" Max Queue Size: {stage_config.max_queue_size}\n"
|
||||
|
||||
@ -663,23 +760,66 @@ Stage Configurations:
|
||||
stage_group = QGroupBox(f"Stage {i}: {stage_config.stage_id}")
|
||||
stage_layout = QFormLayout(stage_group)
|
||||
|
||||
# Create read-only fields for stage configuration
|
||||
model_path_edit = QLineEdit(stage_config.model_path)
|
||||
model_path_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Model Path:", model_path_edit)
|
||||
# Check if this is multi-series configuration
|
||||
if stage_config.multi_series_config:
|
||||
# Multi-series configuration display
|
||||
mode_edit = QLineEdit("Multi-Series")
|
||||
mode_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Mode:", mode_edit)
|
||||
|
||||
scpu_fw_edit = QLineEdit(stage_config.scpu_fw_path)
|
||||
scpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow("SCPU Firmware:", scpu_fw_edit)
|
||||
series_edit = QLineEdit(str(list(stage_config.multi_series_config.keys())))
|
||||
series_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Series:", series_edit)
|
||||
|
||||
ncpu_fw_edit = QLineEdit(stage_config.ncpu_fw_path)
|
||||
ncpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow("NCPU Firmware:", ncpu_fw_edit)
|
||||
# Show details for each series
|
||||
for series_name, series_config in stage_config.multi_series_config.items():
|
||||
series_label = QLabel(f"--- {series_name} ---")
|
||||
series_label.setStyleSheet("font-weight: bold; color: #89b4fa;")
|
||||
stage_layout.addRow(series_label)
|
||||
|
||||
port_ids_edit = QLineEdit(str(stage_config.port_ids))
|
||||
port_ids_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Port IDs:", port_ids_edit)
|
||||
port_ids_edit = QLineEdit(str(series_config.get('port_ids', [])))
|
||||
port_ids_edit.setReadOnly(True)
|
||||
stage_layout.addRow(f"{series_name} Port IDs:", port_ids_edit)
|
||||
|
||||
model_path = series_config.get('model_path', 'Not specified')
|
||||
model_path_edit = QLineEdit(model_path)
|
||||
model_path_edit.setReadOnly(True)
|
||||
stage_layout.addRow(f"{series_name} Model:", model_path_edit)
|
||||
|
||||
firmware_paths = series_config.get('firmware_paths', {})
|
||||
if firmware_paths:
|
||||
scpu_path = firmware_paths.get('scpu', 'Not specified')
|
||||
scpu_fw_edit = QLineEdit(scpu_path)
|
||||
scpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow(f"{series_name} SCPU FW:", scpu_fw_edit)
|
||||
|
||||
ncpu_path = firmware_paths.get('ncpu', 'Not specified')
|
||||
ncpu_fw_edit = QLineEdit(ncpu_path)
|
||||
ncpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow(f"{series_name} NCPU FW:", ncpu_fw_edit)
|
||||
else:
|
||||
# Single-series configuration display
|
||||
mode_edit = QLineEdit("Single-Series")
|
||||
mode_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Mode:", mode_edit)
|
||||
|
||||
model_path_edit = QLineEdit(stage_config.model_path)
|
||||
model_path_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Model Path:", model_path_edit)
|
||||
|
||||
scpu_fw_edit = QLineEdit(stage_config.scpu_fw_path)
|
||||
scpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow("SCPU Firmware:", scpu_fw_edit)
|
||||
|
||||
ncpu_fw_edit = QLineEdit(stage_config.ncpu_fw_path)
|
||||
ncpu_fw_edit.setReadOnly(True)
|
||||
stage_layout.addRow("NCPU Firmware:", ncpu_fw_edit)
|
||||
|
||||
port_ids_edit = QLineEdit(str(stage_config.port_ids))
|
||||
port_ids_edit.setReadOnly(True)
|
||||
stage_layout.addRow("Port IDs:", port_ids_edit)
|
||||
|
||||
# Common fields
|
||||
queue_size_spin = QSpinBox()
|
||||
queue_size_spin.setValue(stage_config.max_queue_size)
|
||||
queue_size_spin.setReadOnly(True)
|
||||
@ -725,8 +865,11 @@ Stage Configurations:
|
||||
self.terminal_output_display.clear()
|
||||
self.terminal_output_display.append("Pipeline deployment started - terminal output will appear here...")
|
||||
|
||||
# Create thread-safe queue for stdout captured from non-Qt threads
|
||||
self._stdout_queue: queue.Queue = queue.Queue(maxsize=1000)
|
||||
|
||||
# Create and start deployment worker
|
||||
self.deployment_worker = DeploymentWorker(self.pipeline_data)
|
||||
self.deployment_worker = DeploymentWorker(self.pipeline_data, self._stdout_queue)
|
||||
self.deployment_worker.progress_updated.connect(self.update_progress)
|
||||
self.deployment_worker.topology_analyzed.connect(self.update_topology_results)
|
||||
self.deployment_worker.conversion_completed.connect(self.on_conversion_completed)
|
||||
@ -736,8 +879,14 @@ Stage Configurations:
|
||||
self.deployment_worker.frame_updated.connect(self.update_live_view)
|
||||
self.deployment_worker.result_updated.connect(self.update_inference_results)
|
||||
self.deployment_worker.terminal_output.connect(self.update_terminal_output)
|
||||
self.deployment_worker.stdout_captured.connect(self.update_terminal_output)
|
||||
|
||||
# Drain stdout queue from main thread every 100 ms to avoid cross-thread signal warnings
|
||||
self._stdout_drain_timer = QTimer(self)
|
||||
self._stdout_drain_timer.timeout.connect(self._drain_stdout_queue)
|
||||
self._stdout_drain_timer.start(100)
|
||||
|
||||
# Stop timer and flush queue when worker finishes (runs on main thread via signal)
|
||||
self.deployment_worker.finished.connect(self._on_worker_finished)
|
||||
|
||||
self.deployment_worker.start()
|
||||
|
||||
@ -756,24 +905,54 @@ Stage Configurations:
|
||||
|
||||
self.deployment_worker.stop()
|
||||
|
||||
# Wait for worker to finish in a separate thread to avoid blocking UI
|
||||
def wait_for_stop():
|
||||
if self.deployment_worker.wait(5000): # Wait up to 5 seconds
|
||||
self.deployment_log.append("Inference stopped successfully.")
|
||||
else:
|
||||
self.deployment_log.append("Warning: Inference may not have stopped cleanly.")
|
||||
# Wait for worker to finish in a separate thread to avoid blocking UI.
|
||||
# All UI updates happen in _on_stop_done (main thread, via signal).
|
||||
self._stop_done.connect(self._on_stop_done)
|
||||
|
||||
# Update UI on main thread
|
||||
self.stop_button.setVisible(False)
|
||||
self.deploy_button.setEnabled(True)
|
||||
self.close_button.setText("Close")
|
||||
self.progress_bar.setVisible(False)
|
||||
self.status_label.setText("Inference stopped")
|
||||
self.dongle_status.setText("Pipeline stopped")
|
||||
def wait_for_stop():
|
||||
success = self.deployment_worker.wait(5000) # Wait up to 5 seconds
|
||||
self._stop_done.emit(success)
|
||||
|
||||
import threading
|
||||
threading.Thread(target=wait_for_stop, daemon=True).start()
|
||||
|
||||
def _on_stop_done(self, success: bool):
|
||||
"""Called on the main thread after the stop background thread has waited for the worker."""
|
||||
# Disconnect so re-running won't accumulate connections
|
||||
try:
|
||||
self._stop_done.disconnect(self._on_stop_done)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
if success:
|
||||
self.deployment_log.append("Inference stopped successfully.")
|
||||
else:
|
||||
self.deployment_log.append("Warning: Inference may not have stopped cleanly.")
|
||||
|
||||
self.stop_button.setVisible(False)
|
||||
self.deploy_button.setEnabled(True)
|
||||
self.close_button.setText("Close")
|
||||
self.progress_bar.setVisible(False)
|
||||
self.status_label.setText("Inference stopped")
|
||||
self.dongle_status.setText("Pipeline stopped")
|
||||
|
||||
def _on_worker_finished(self):
|
||||
"""Called on the main thread when the deployment worker thread exits."""
|
||||
if hasattr(self, '_stdout_drain_timer'):
|
||||
self._stdout_drain_timer.stop()
|
||||
self._drain_stdout_queue() # flush any remaining output
|
||||
|
||||
def _drain_stdout_queue(self):
|
||||
"""Drain the stdout queue and forward lines to the terminal display (main thread only)."""
|
||||
if not hasattr(self, '_stdout_queue'):
|
||||
return
|
||||
try:
|
||||
while True:
|
||||
text = self._stdout_queue.get_nowait()
|
||||
self.update_terminal_output(text)
|
||||
except queue.Empty:
|
||||
pass
|
||||
|
||||
def update_progress(self, value: int, message: str):
|
||||
"""Update deployment progress."""
|
||||
self.progress_bar.setValue(value)
|
||||
@ -837,6 +1016,57 @@ Stage Configurations:
|
||||
def update_live_view(self, frame):
|
||||
"""Update the live view with a new frame."""
|
||||
try:
|
||||
# Optionally overlay latest detections before display
|
||||
if hasattr(self, '_latest_boxes') and self._latest_boxes:
|
||||
import cv2
|
||||
H, W = frame.shape[0], frame.shape[1]
|
||||
# Letterbox mapping
|
||||
letter = getattr(self, '_latest_letterbox', None)
|
||||
for box in self._latest_boxes:
|
||||
# Filter by display threshold
|
||||
sc = box.get('score', None)
|
||||
try:
|
||||
if sc is not None and float(sc) < getattr(self, '_display_threshold', 0.5):
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
x1 = float(box.get('x1', 0)); y1 = float(box.get('y1', 0))
|
||||
x2 = float(box.get('x2', 0)); y2 = float(box.get('y2', 0))
|
||||
|
||||
mapped = False
|
||||
if letter and all(k in letter for k in ('model_w','model_h','resized_w','resized_h','pad_left','pad_top')):
|
||||
mw = int(letter.get('model_w', 0))
|
||||
mh = int(letter.get('model_h', 0))
|
||||
rw = int(letter.get('resized_w', 0))
|
||||
rh = int(letter.get('resized_h', 0))
|
||||
pl = int(letter.get('pad_left', 0)); pt = int(letter.get('pad_top', 0))
|
||||
if rw > 0 and rh > 0:
|
||||
# Reverse letterbox: remove padding, then scale to original
|
||||
x1 = (x1 - pl) / rw * W; x2 = (x2 - pl) / rw * W
|
||||
y1 = (y1 - pt) / rh * H; y2 = (y2 - pt) / rh * H
|
||||
mapped = True
|
||||
elif mw > 0 and mh > 0:
|
||||
# Fallback: simple proportional mapping from model space
|
||||
x1 = x1 / mw * W; x2 = x2 / mw * W
|
||||
y1 = y1 / mh * H; y2 = y2 / mh * H
|
||||
mapped = True
|
||||
if not mapped:
|
||||
# Last resort proportional mapping using typical 640 baseline
|
||||
baseline = 640.0
|
||||
x1 = x1 / baseline * W; x2 = x2 / baseline * W
|
||||
y1 = y1 / baseline * H; y2 = y2 / baseline * H
|
||||
|
||||
# Clamp
|
||||
xi1 = max(0, min(int(x1), W - 1)); yi1 = max(0, min(int(y1), H - 1))
|
||||
xi2 = max(xi1 + 1, min(int(x2), W)); yi2 = max(yi1 + 1, min(int(y2), H))
|
||||
color = (0, 255, 0)
|
||||
cv2.rectangle(frame, (xi1, yi1), (xi2, yi2), color, 2)
|
||||
label = box.get('class_name', 'obj')
|
||||
score = box.get('score', None)
|
||||
if score is not None:
|
||||
label = f"{label} {score:.2f}"
|
||||
cv2.putText(frame, label, (xi1, max(0, yi1 - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
|
||||
|
||||
# Convert the OpenCV frame to a QImage
|
||||
height, width, channel = frame.shape
|
||||
bytes_per_line = 3 * width
|
||||
@ -862,20 +1092,92 @@ Stage Configurations:
|
||||
# Display results from each stage
|
||||
for stage_id, result in stage_results.items():
|
||||
result_text += f" {stage_id}:\n"
|
||||
# Cache latest detection boxes for live overlay if available
|
||||
source_obj = None
|
||||
if hasattr(result, 'box_count') and hasattr(result, 'box_list'):
|
||||
source_obj = result
|
||||
elif isinstance(result, tuple) and len(result) == 2 and hasattr(result[0], 'box_list'):
|
||||
source_obj = result[0]
|
||||
|
||||
if source_obj is not None:
|
||||
boxes = []
|
||||
for b in getattr(source_obj, 'box_list', [])[:50]:
|
||||
boxes.append({
|
||||
'x1': getattr(b, 'x1', 0), 'y1': getattr(b, 'y1', 0),
|
||||
'x2': getattr(b, 'x2', 0), 'y2': getattr(b, 'y2', 0),
|
||||
'class_name': getattr(b, 'class_name', 'obj'),
|
||||
'score': float(getattr(b, 'score', 0.0)) if hasattr(b, 'score') else None,
|
||||
})
|
||||
self._latest_boxes = boxes
|
||||
# Cache letterbox mapping from result object if available
|
||||
try:
|
||||
self._latest_letterbox = {
|
||||
'model_w': int(getattr(source_obj, 'model_input_width', 0)),
|
||||
'model_h': int(getattr(source_obj, 'model_input_height', 0)),
|
||||
'resized_w': int(getattr(source_obj, 'resized_img_width', 0)),
|
||||
'resized_h': int(getattr(source_obj, 'resized_img_height', 0)),
|
||||
'pad_left': int(getattr(source_obj, 'pad_left', 0)),
|
||||
'pad_top': int(getattr(source_obj, 'pad_top', 0)),
|
||||
'pad_right': int(getattr(source_obj, 'pad_right', 0)),
|
||||
'pad_bottom': int(getattr(source_obj, 'pad_bottom', 0)),
|
||||
}
|
||||
except Exception:
|
||||
self._latest_letterbox = None
|
||||
|
||||
if isinstance(result, tuple) and len(result) == 2:
|
||||
# Handle tuple results (probability, result_string)
|
||||
probability, result_string = result
|
||||
# Handle tuple results which may be (ClassificationResult|ObjectDetectionResult|float, result_string)
|
||||
prob_or_obj, result_string = result
|
||||
result_text += f" Result: {result_string}\n"
|
||||
result_text += f" Probability: {probability:.3f}\n"
|
||||
|
||||
# Object detection summary
|
||||
if hasattr(prob_or_obj, 'box_list'):
|
||||
filtered = [b for b in getattr(prob_or_obj, 'box_list', [])
|
||||
if not hasattr(b, 'score') or float(getattr(b, 'score', 0.0)) >= getattr(self, '_display_threshold', 0.5)]
|
||||
thresh = getattr(self, '_display_threshold', 0.5)
|
||||
result_text += f" Detections (>= {thresh:.2f}): {len(filtered)}\n"
|
||||
# Classification summary (e.g., Fire detection)
|
||||
elif hasattr(prob_or_obj, 'probability') and hasattr(prob_or_obj, 'class_name'):
|
||||
try:
|
||||
p = float(getattr(prob_or_obj, 'probability', 0.0))
|
||||
result_text += f" Probability: {p:.3f}\n"
|
||||
except Exception:
|
||||
result_text += f" Probability: {getattr(prob_or_obj, 'probability', 'N/A')}\n"
|
||||
else:
|
||||
# Numeric probability fallback
|
||||
try:
|
||||
prob_value = float(prob_or_obj)
|
||||
result_text += f" Probability: {prob_value:.3f}\n"
|
||||
except (ValueError, TypeError):
|
||||
result_text += f" Probability: {prob_or_obj}\n"
|
||||
elif isinstance(result, dict):
|
||||
# Handle dict results
|
||||
for key, value in result.items():
|
||||
if key == 'probability':
|
||||
result_text += f" Probability: {value:.3f}\n"
|
||||
try:
|
||||
prob_value = float(value)
|
||||
result_text += f" Probability: {prob_value:.3f}\n"
|
||||
except (ValueError, TypeError):
|
||||
result_text += f" Probability: {value}\n"
|
||||
else:
|
||||
result_text += f" {key}: {value}\n"
|
||||
else:
|
||||
result_text += f" {result}\n"
|
||||
# Pretty-print detection objects
|
||||
try:
|
||||
if hasattr(result, 'box_count') and hasattr(result, 'box_list'):
|
||||
filtered = [b for b in getattr(result, 'box_list', [])
|
||||
if not hasattr(b, 'score') or float(getattr(b, 'score', 0.0)) >= getattr(self, '_display_threshold', 0.5)]
|
||||
thresh = getattr(self, '_display_threshold', 0.5)
|
||||
result_text += f" Detections (>= {thresh:.2f}): {len(filtered)}\n"
|
||||
elif hasattr(result, 'probability') and hasattr(result, 'class_name'):
|
||||
try:
|
||||
p = float(getattr(result, 'probability', 0.0))
|
||||
result_text += f" Probability: {p:.3f}\n"
|
||||
except Exception:
|
||||
result_text += f" Probability: {getattr(result, 'probability', 'N/A')}\n"
|
||||
else:
|
||||
result_text += f" {result}\n"
|
||||
except Exception:
|
||||
result_text += f" {result}\n"
|
||||
|
||||
result_text += "-" * 50 + "\n"
|
||||
|
||||
@ -898,21 +1200,21 @@ Stage Configurations:
|
||||
def update_terminal_output(self, terminal_text: str):
|
||||
"""Update the terminal output display with new text."""
|
||||
try:
|
||||
# Use append() instead of setPlainText() for better performance and no truncation
|
||||
self.terminal_output_display.append(terminal_text.rstrip('\n'))
|
||||
|
||||
# Auto-scroll to bottom
|
||||
scrollbar = self.terminal_output_display.verticalScrollBar()
|
||||
scrollbar.setValue(scrollbar.maximum())
|
||||
|
||||
# Optional: Limit total lines to prevent excessive memory usage
|
||||
# Only trim if we have way too many lines (e.g., > 1000)
|
||||
# Limit total lines to prevent excessive memory usage.
|
||||
# Use toPlainText/setPlainText to avoid QTextCursor cross-thread warnings.
|
||||
document = self.terminal_output_display.document()
|
||||
if document.lineCount() > 1000:
|
||||
cursor = self.terminal_output_display.textCursor()
|
||||
cursor.movePosition(cursor.Start)
|
||||
cursor.movePosition(cursor.Down, cursor.KeepAnchor, 200) # Select first 200 lines
|
||||
cursor.removeSelectedText()
|
||||
lines = self.terminal_output_display.toPlainText().split('\n')
|
||||
trimmed = '\n'.join(lines[-800:]) # Keep last 800 lines
|
||||
self.terminal_output_display.setPlainText(trimmed)
|
||||
# Restore scroll to bottom after setPlainText resets it
|
||||
scrollbar.setValue(scrollbar.maximum())
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error updating terminal output: {e}")
|
||||
|
||||
238
ui/dialogs/export_report_dialog.py
Normal file
238
ui/dialogs/export_report_dialog.py
Normal file
@ -0,0 +1,238 @@
|
||||
"""
|
||||
ui/dialogs/export_report_dialog.py — 效能報告匯出對話框。
|
||||
|
||||
提供 ExportReportDialog(QDialog),讓使用者選擇報告格式(PDF/CSV)與儲存路徑,
|
||||
然後觸發 ReportExporter 執行匯出。
|
||||
|
||||
設計重點:
|
||||
- _collect_report_data() 從各模組收集資料,每個來源都用 try/except 保護。
|
||||
- 不在此模組執行實際 benchmark,只使用 history 的最新一筆作為 parallel_result。
|
||||
- chart_image_bytes 留 None(截圖整合留未來)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, List, Optional
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QDialog,
|
||||
QFileDialog,
|
||||
QHBoxLayout,
|
||||
QLabel,
|
||||
QPushButton,
|
||||
QRadioButton,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
QLineEdit,
|
||||
QGroupBox,
|
||||
QProgressBar,
|
||||
)
|
||||
from PyQt5.QtCore import Qt
|
||||
|
||||
from core.performance.report_exporter import DeviceSummary, ReportData, ReportExporter
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.performance.benchmarker import PerformanceBenchmarker
|
||||
from core.performance.history import PerformanceHistory
|
||||
|
||||
|
||||
class ExportReportDialog(QDialog):
|
||||
"""
|
||||
效能報告匯出對話框。
|
||||
|
||||
使用者可選擇格式(PDF / CSV),指定儲存路徑後按匯出,
|
||||
對話框會呼叫 ReportExporter 產出檔案並顯示結果。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent: Optional[QWidget],
|
||||
benchmarker, # PerformanceBenchmarker | None
|
||||
history, # PerformanceHistory | None
|
||||
device_manager, # DeviceManager | None
|
||||
dashboard, # PerformanceDashboard | None
|
||||
) -> None:
|
||||
super().__init__(parent)
|
||||
|
||||
self._benchmarker = benchmarker
|
||||
self._history = history
|
||||
self._device_manager = device_manager
|
||||
self._dashboard = dashboard
|
||||
self._exporter = ReportExporter()
|
||||
|
||||
# 預設格式為 PDF
|
||||
self._selected_format: str = "pdf"
|
||||
|
||||
self._setup_ui()
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# UI 建立
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _setup_ui(self) -> None:
|
||||
"""建立對話框 UI。"""
|
||||
self.setWindowTitle("匯出效能報告")
|
||||
|
||||
main_layout = QVBoxLayout()
|
||||
|
||||
# 格式選擇
|
||||
format_group = QGroupBox("匯出格式")
|
||||
format_layout = QHBoxLayout()
|
||||
|
||||
self._pdf_radio = QRadioButton("PDF")
|
||||
self._csv_radio = QRadioButton("CSV")
|
||||
self._pdf_radio.setChecked(True)
|
||||
self._pdf_radio.clicked.connect(lambda: self._set_format("pdf"))
|
||||
self._csv_radio.clicked.connect(lambda: self._set_format("csv"))
|
||||
|
||||
format_layout.addWidget(self._pdf_radio)
|
||||
format_layout.addWidget(self._csv_radio)
|
||||
format_group.setLayout(format_layout)
|
||||
main_layout.addWidget(format_group)
|
||||
|
||||
# 儲存路徑
|
||||
path_layout = QHBoxLayout()
|
||||
self._path_input = QLineEdit()
|
||||
self._path_input.setPlaceholderText("儲存路徑…")
|
||||
self._browse_btn = QPushButton("瀏覽")
|
||||
self._browse_btn.clicked.connect(self._on_browse)
|
||||
path_layout.addWidget(self._path_input)
|
||||
path_layout.addWidget(self._browse_btn)
|
||||
main_layout.addLayout(path_layout)
|
||||
|
||||
# 進度條
|
||||
self._progress_bar = QProgressBar()
|
||||
self._progress_bar.setVisible(False)
|
||||
main_layout.addWidget(self._progress_bar)
|
||||
|
||||
# 匯出按鈕
|
||||
self._export_btn = QPushButton("匯出")
|
||||
self._export_btn.clicked.connect(self._on_export)
|
||||
main_layout.addWidget(self._export_btn)
|
||||
|
||||
# 狀態標籤
|
||||
self._status_label = QLabel("")
|
||||
main_layout.addWidget(self._status_label)
|
||||
|
||||
self.setLayout(main_layout)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 格式設定
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _set_format(self, fmt: str) -> None:
|
||||
"""設定匯出格式('pdf' 或 'csv')。"""
|
||||
self._selected_format = fmt
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 事件處理
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _on_browse(self) -> None:
|
||||
"""開啟 QFileDialog 讓使用者選擇儲存路徑。"""
|
||||
if self._selected_format == "pdf":
|
||||
file_filter = "PDF 檔案 (*.pdf)"
|
||||
default_suffix = ".pdf"
|
||||
else:
|
||||
file_filter = "CSV 檔案 (*.csv)"
|
||||
default_suffix = ".csv"
|
||||
|
||||
path, _ = QFileDialog.getSaveFileName(
|
||||
self,
|
||||
"選擇儲存位置",
|
||||
f"performance_report{default_suffix}",
|
||||
file_filter,
|
||||
)
|
||||
if path:
|
||||
self._path_input.setText(path)
|
||||
|
||||
def _on_export(self) -> None:
|
||||
"""執行匯出:收集資料 -> 呼叫 ReportExporter。"""
|
||||
output_path = self._path_input.text().strip()
|
||||
if not output_path:
|
||||
self._status_label.setText("請先指定儲存路徑。")
|
||||
return
|
||||
|
||||
data = self._collect_report_data()
|
||||
|
||||
try:
|
||||
if self._selected_format == "pdf":
|
||||
result = self._exporter.export_pdf(data, output_path)
|
||||
else:
|
||||
result = self._exporter.export_csv(data, output_path)
|
||||
self._status_label.setText(f"匯出成功:{result}")
|
||||
except ImportError as e:
|
||||
self._status_label.setText(f"匯出失敗(缺少函式庫):{e}")
|
||||
except ValueError as e:
|
||||
self._status_label.setText(f"匯出失敗(資料不足):{e}")
|
||||
except Exception as e:
|
||||
self._status_label.setText(f"匯出失敗:{e}")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 資料收集
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _collect_report_data(self) -> ReportData:
|
||||
"""
|
||||
從各模組收集資料,組裝 ReportData。
|
||||
每個來源都用 try/except 保護,失敗時使用 None / 空值。
|
||||
不實際執行 benchmark,只使用 history 的最新一筆作為 parallel_result。
|
||||
"""
|
||||
# 歷史記錄,同時從中取最近一筆 sequential / parallel 作為 result
|
||||
history_records: list = []
|
||||
seq_result = None
|
||||
par_result = None
|
||||
try:
|
||||
records = self._history.get_history(limit=20) if self._history else []
|
||||
history_records = list(records) if records else []
|
||||
seq_result = next((r for r in history_records if r.mode == "sequential"), None)
|
||||
par_result = next((r for r in history_records if r.mode == "parallel"), None)
|
||||
except Exception:
|
||||
history_records, seq_result, par_result = [], None, None
|
||||
|
||||
# 從 benchmarker.history 取最新一筆作為 parallel_result(fallback,不執行新的 benchmark)
|
||||
if par_result is None:
|
||||
try:
|
||||
if self._benchmarker is not None:
|
||||
hist = self._benchmarker.history
|
||||
if hist:
|
||||
par_result = hist[-1]
|
||||
except Exception:
|
||||
par_result = None
|
||||
|
||||
# 裝置資訊
|
||||
devices: List[DeviceSummary] = []
|
||||
try:
|
||||
if self._device_manager is not None:
|
||||
raw_devices = self._device_manager.scan_devices() or []
|
||||
devices = self._convert_devices(raw_devices)
|
||||
except Exception:
|
||||
devices = []
|
||||
|
||||
return ReportData(
|
||||
sequential_result=seq_result,
|
||||
parallel_result=par_result,
|
||||
speedup=None,
|
||||
history_records=history_records,
|
||||
devices=devices,
|
||||
chart_image_bytes=None, # 截圖整合留未來
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _convert_devices(raw_devices: list) -> List[DeviceSummary]:
|
||||
"""
|
||||
將 DeviceManager 回傳的裝置列表轉換為 DeviceSummary 列表。
|
||||
若轉換失敗,略過該裝置。
|
||||
"""
|
||||
result: List[DeviceSummary] = []
|
||||
for dev in raw_devices:
|
||||
try:
|
||||
result.append(DeviceSummary(
|
||||
device_id=str(getattr(dev, "device_id", getattr(dev, "id", "unknown"))),
|
||||
product_name=str(getattr(dev, "product_name", getattr(dev, "model", "unknown"))),
|
||||
firmware_version=str(getattr(dev, "firmware_version", "unknown")),
|
||||
is_active=bool(getattr(dev, "is_active", True)),
|
||||
))
|
||||
except Exception:
|
||||
continue
|
||||
return result
|
||||
@ -43,6 +43,7 @@ except ImportError:
|
||||
|
||||
from config.theme import HARMONIOUS_THEME_STYLESHEET
|
||||
from config.settings import get_settings
|
||||
from utils.folder_dialog import select_assets_folder
|
||||
try:
|
||||
from core.nodes import (
|
||||
InputNode, ModelNode, PreprocessNode, PostprocessNode, OutputNode,
|
||||
@ -58,6 +59,25 @@ from core.nodes.exact_nodes import (
|
||||
ExactPostprocessNode, ExactOutputNode, EXACT_NODE_TYPES
|
||||
)
|
||||
|
||||
try:
|
||||
from ui.components.performance_dashboard import PerformanceDashboard
|
||||
PERFORMANCE_DASHBOARD_AVAILABLE = True
|
||||
except ImportError:
|
||||
PERFORMANCE_DASHBOARD_AVAILABLE = False
|
||||
|
||||
try:
|
||||
from ui.components.device_management_panel import DeviceManagementPanel
|
||||
from core.device.device_manager import DeviceManager
|
||||
DEVICE_MANAGEMENT_AVAILABLE = True
|
||||
except ImportError:
|
||||
DEVICE_MANAGEMENT_AVAILABLE = False
|
||||
|
||||
try:
|
||||
from ui.dialogs.export_report_dialog import ExportReportDialog
|
||||
EXPORT_REPORT_AVAILABLE = True
|
||||
except ImportError:
|
||||
EXPORT_REPORT_AVAILABLE = False
|
||||
|
||||
# Import pipeline analysis functions
|
||||
try:
|
||||
from core.pipeline import get_stage_count, analyze_pipeline_stages, get_pipeline_summary
|
||||
@ -157,6 +177,8 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
self.props_instructions = None
|
||||
self.node_props_container = None
|
||||
self.node_props_layout = None
|
||||
self.device_manager = None
|
||||
self.device_management_panel = None
|
||||
self.fps_label = None
|
||||
self.latency_label = None
|
||||
self.memory_label = None
|
||||
@ -895,6 +917,19 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
|
||||
layout.addWidget(metrics_group)
|
||||
|
||||
# Real-time performance monitor
|
||||
perf_dashboard_group = QGroupBox("即時效能監控")
|
||||
perf_dashboard_layout = QVBoxLayout(perf_dashboard_group)
|
||||
if PERFORMANCE_DASHBOARD_AVAILABLE:
|
||||
self.performance_dashboard = PerformanceDashboard()
|
||||
else:
|
||||
self.performance_dashboard = None
|
||||
if self.performance_dashboard:
|
||||
perf_dashboard_layout.addWidget(self.performance_dashboard)
|
||||
else:
|
||||
perf_dashboard_layout.addWidget(QLabel("PerformanceDashboard 不可用"))
|
||||
layout.addWidget(perf_dashboard_group)
|
||||
|
||||
# Suggestions
|
||||
suggestions_group = QGroupBox("Optimization Suggestions")
|
||||
suggestions_layout = QVBoxLayout(suggestions_group)
|
||||
@ -906,6 +941,26 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
|
||||
layout.addWidget(suggestions_group)
|
||||
|
||||
# Benchmark section
|
||||
benchmark_group = QGroupBox("效能 Benchmark")
|
||||
benchmark_layout = QVBoxLayout(benchmark_group)
|
||||
|
||||
self.benchmark_button = QPushButton("執行 Benchmark")
|
||||
self.benchmark_button.setToolTip("比較單 Dongle vs 多 Dongle 的效能差異")
|
||||
self.benchmark_button.clicked.connect(self.open_benchmark_dialog)
|
||||
benchmark_layout.addWidget(self.benchmark_button)
|
||||
|
||||
layout.addWidget(benchmark_group)
|
||||
|
||||
if EXPORT_REPORT_AVAILABLE:
|
||||
export_group = QGroupBox("報告匯出")
|
||||
export_layout = QVBoxLayout(export_group)
|
||||
self.export_report_button = QPushButton("匯出效能報告(PDF/CSV)")
|
||||
self.export_report_button.setToolTip("將 Benchmark 結果與歷史記錄匯出為 PDF 或 CSV")
|
||||
self.export_report_button.clicked.connect(self.open_export_report_dialog)
|
||||
export_layout.addWidget(self.export_report_button)
|
||||
layout.addWidget(export_group)
|
||||
|
||||
# Deploy section
|
||||
deploy_group = QGroupBox("Pipeline Deployment")
|
||||
deploy_layout = QVBoxLayout(deploy_group)
|
||||
@ -976,6 +1031,17 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
self.dongles_list.addItem("No dongles detected. Click 'Detect Dongles' to scan.")
|
||||
layout.addWidget(self.dongles_list)
|
||||
|
||||
if DEVICE_MANAGEMENT_AVAILABLE:
|
||||
try:
|
||||
self.device_manager = DeviceManager()
|
||||
self.device_management_panel = DeviceManagementPanel(self.device_manager)
|
||||
self.device_management_panel.set_auto_refresh(3000)
|
||||
layout.addWidget(self.device_management_panel)
|
||||
except Exception as e:
|
||||
err_label = QLabel(f"裝置管理面板初始化失敗:{e}")
|
||||
err_label.setStyleSheet("color: #f38ba8; font-size: 11px;")
|
||||
layout.addWidget(err_label)
|
||||
|
||||
layout.addStretch()
|
||||
widget.setWidget(content)
|
||||
widget.setWidgetResizable(True)
|
||||
@ -1199,10 +1265,16 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
elif 'Postprocess' in node_type:
|
||||
# Exact PostprocessNode properties from original
|
||||
properties = {
|
||||
'postprocess_type': node.get_property('postprocess_type') if hasattr(node, 'get_property') else 'fire_detection',
|
||||
'class_names': node.get_property('class_names') if hasattr(node, 'get_property') else 'No Fire,Fire',
|
||||
'output_format': node.get_property('output_format') if hasattr(node, 'get_property') else 'JSON',
|
||||
'confidence_threshold': node.get_property('confidence_threshold') if hasattr(node, 'get_property') else 0.5,
|
||||
'nms_threshold': node.get_property('nms_threshold') if hasattr(node, 'get_property') else 0.4,
|
||||
'max_detections': node.get_property('max_detections') if hasattr(node, 'get_property') else 100
|
||||
'max_detections': node.get_property('max_detections') if hasattr(node, 'get_property') else 100,
|
||||
'enable_confidence_filter': node.get_property('enable_confidence_filter') if hasattr(node, 'get_property') else True,
|
||||
'enable_nms': node.get_property('enable_nms') if hasattr(node, 'get_property') else True,
|
||||
'coordinate_system': node.get_property('coordinate_system') if hasattr(node, 'get_property') else 'relative',
|
||||
'operations': node.get_property('operations') if hasattr(node, 'get_property') else 'filter,nms,format'
|
||||
}
|
||||
elif 'Output' in node_type:
|
||||
# Exact OutputNode properties from original
|
||||
@ -1323,8 +1395,74 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
if hasattr(node, '_property_options') and prop_name in node._property_options:
|
||||
prop_options = node._property_options[prop_name]
|
||||
|
||||
# Check for file path properties first (from prop_options or name pattern)
|
||||
if (prop_options and isinstance(prop_options, dict) and prop_options.get('type') == 'file_path') or \
|
||||
# Special handling for assets_folder property
|
||||
if prop_name == 'assets_folder':
|
||||
# Assets folder property with validation and improved dialog
|
||||
display_text = self.truncate_path_smart(str(prop_value)) if prop_value else 'Select Assets Folder...'
|
||||
widget = QPushButton(display_text)
|
||||
|
||||
# Set fixed width and styling to prevent expansion
|
||||
widget.setMaximumWidth(250)
|
||||
widget.setMinimumWidth(200)
|
||||
widget.setStyleSheet("""
|
||||
QPushButton {
|
||||
text-align: left;
|
||||
padding: 5px 8px;
|
||||
background-color: #45475a;
|
||||
color: #cdd6f4;
|
||||
border: 1px solid #585b70;
|
||||
border-radius: 4px;
|
||||
font-size: 10px;
|
||||
}
|
||||
QPushButton:hover {
|
||||
background-color: #585b70;
|
||||
border-color: #a6e3a1;
|
||||
}
|
||||
QPushButton:pressed {
|
||||
background-color: #313244;
|
||||
}
|
||||
""")
|
||||
|
||||
# Store full path for tooltip and internal use
|
||||
full_path = str(prop_value) if prop_value else ''
|
||||
widget.setToolTip(f"Full path: {full_path}\n\nClick to browse for Assets folder\n(Should contain Firmware/ and Models/ subfolders)")
|
||||
|
||||
def browse_assets_folder():
|
||||
# Use the specialized assets folder dialog with validation
|
||||
result = select_assets_folder(initial_dir=full_path or '')
|
||||
|
||||
if result['path']:
|
||||
# Update button text with truncated path
|
||||
truncated_text = self.truncate_path_smart(result['path'])
|
||||
widget.setText(truncated_text)
|
||||
|
||||
# Create detailed tooltip with validation results
|
||||
tooltip_lines = [f"Full path: {result['path']}"]
|
||||
if result['valid']:
|
||||
tooltip_lines.append("✓ Valid Assets folder structure detected")
|
||||
if 'details' in result and 'available_series' in result['details']:
|
||||
series = result['details']['available_series']
|
||||
tooltip_lines.append(f"Available series: {', '.join(series)}")
|
||||
else:
|
||||
tooltip_lines.append(f"⚠ {result['message']}")
|
||||
|
||||
tooltip_lines.append("\nClick to browse for Assets folder")
|
||||
widget.setToolTip('\n'.join(tooltip_lines))
|
||||
|
||||
# Set property with full path
|
||||
if hasattr(node, 'set_property'):
|
||||
node.set_property(prop_name, result['path'])
|
||||
|
||||
# Show validation message to user
|
||||
if not result['valid']:
|
||||
QMessageBox.warning(self, "Assets Folder Validation",
|
||||
f"Selected folder may not have the expected structure:\n\n{result['message']}\n\n"
|
||||
"Expected structure:\nAssets/\n├── Firmware/\n│ └── KL520/, KL720/, etc.\n└── Models/\n └── KL520/, KL720/, etc.")
|
||||
|
||||
widget.clicked.connect(browse_assets_folder)
|
||||
|
||||
# Check for file path properties (from prop_options or name pattern)
|
||||
elif (prop_options and isinstance(prop_options, dict) and prop_options.get('type') == 'file_path') or \
|
||||
prop_name in ['model_path', 'source_path', 'destination']:
|
||||
# File path property with smart truncation and width limits
|
||||
display_text = self.truncate_path_smart(str(prop_value)) if prop_value else 'Select File...'
|
||||
@ -1853,6 +1991,57 @@ class IntegratedPipelineDashboard(QMainWindow):
|
||||
|
||||
self.suggestions_text.setPlainText("\n".join(suggestions))
|
||||
|
||||
# Update PerformanceDashboard (if available)
|
||||
if hasattr(self, 'performance_dashboard') and self.performance_dashboard:
|
||||
self.performance_dashboard.update_stats({
|
||||
"fps": float(estimated_fps),
|
||||
"avg_latency_ms": float(estimated_latency),
|
||||
"p95_latency_ms": float(estimated_latency * 1.5) # 估算 p95
|
||||
})
|
||||
|
||||
def open_benchmark_dialog(self):
|
||||
"""開啟 Benchmark 對話框。"""
|
||||
try:
|
||||
from ui.dialogs.benchmark_dialog import BenchmarkDialog
|
||||
from core.pipeline import analyze_pipeline_stages
|
||||
|
||||
if not self.graph:
|
||||
QMessageBox.warning(self, "無 Pipeline", "請先建立 Pipeline 再執行 Benchmark。")
|
||||
return
|
||||
|
||||
stages = analyze_pipeline_stages(self.graph)
|
||||
# analyze_pipeline_stages 回傳 List[PipelineStage]
|
||||
pipeline_config = stages if stages else []
|
||||
|
||||
dialog = BenchmarkDialog(self, pipeline_config)
|
||||
dialog.exec_()
|
||||
except ImportError as e:
|
||||
QMessageBox.warning(self, "功能未啟用", f"Benchmark 功能暫不可用:{e}")
|
||||
|
||||
def open_export_report_dialog(self):
|
||||
"""開啟效能報告匯出對話框。"""
|
||||
try:
|
||||
from ui.dialogs.export_report_dialog import ExportReportDialog
|
||||
from core.performance.benchmarker import PerformanceBenchmarker
|
||||
from core.performance.history import PerformanceHistory
|
||||
from core.device.device_manager import DeviceManager
|
||||
|
||||
benchmarker = getattr(self, '_benchmarker', None)
|
||||
history = getattr(self, '_perf_history', None)
|
||||
device_manager = getattr(self, 'device_manager', None)
|
||||
dashboard = getattr(self, 'performance_dashboard', None)
|
||||
|
||||
dialog = ExportReportDialog(
|
||||
parent=self,
|
||||
benchmarker=benchmarker,
|
||||
history=history,
|
||||
device_manager=device_manager,
|
||||
dashboard=dashboard,
|
||||
)
|
||||
dialog.exec_()
|
||||
except Exception as e:
|
||||
QMessageBox.warning(self, "匯出功能", f"無法開啟報告匯出:{e}")
|
||||
|
||||
def delete_selected_nodes(self):
|
||||
"""Delete selected nodes from the graph."""
|
||||
if not self.graph:
|
||||
|
||||
@ -21,8 +21,12 @@ Usage:
|
||||
# Import utilities as they are implemented
|
||||
# from . import file_utils
|
||||
# from . import ui_utils
|
||||
from .folder_dialog import select_folder, select_assets_folder, validate_assets_folder_structure
|
||||
|
||||
__all__ = [
|
||||
# "file_utils",
|
||||
# "ui_utils"
|
||||
"select_folder",
|
||||
"select_assets_folder",
|
||||
"validate_assets_folder_structure"
|
||||
]
|
||||
252
utils/folder_dialog.py
Normal file
252
utils/folder_dialog.py
Normal file
@ -0,0 +1,252 @@
|
||||
"""
|
||||
Folder selection utilities using PyQt5 as primary, tkinter as fallback
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
def select_folder(title="Select Folder", initial_dir="", must_exist=True):
|
||||
"""
|
||||
Open a folder selection dialog using PyQt5 (preferred) or tkinter (fallback)
|
||||
|
||||
Args:
|
||||
title (str): Dialog window title
|
||||
initial_dir (str): Initial directory to open
|
||||
must_exist (bool): Whether the folder must already exist
|
||||
|
||||
Returns:
|
||||
str: Selected folder path, or empty string if cancelled
|
||||
"""
|
||||
# Try PyQt5 first (more reliable on macOS)
|
||||
try:
|
||||
from PyQt5.QtWidgets import QApplication, QFileDialog
|
||||
import sys
|
||||
|
||||
# Create QApplication if it doesn't exist
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
# Set initial directory
|
||||
if not initial_dir:
|
||||
initial_dir = os.getcwd()
|
||||
elif not os.path.exists(initial_dir):
|
||||
initial_dir = os.getcwd()
|
||||
|
||||
# Open folder selection dialog
|
||||
folder_path = QFileDialog.getExistingDirectory(
|
||||
None,
|
||||
title,
|
||||
initial_dir,
|
||||
QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks
|
||||
)
|
||||
|
||||
return folder_path if folder_path else ""
|
||||
|
||||
except ImportError:
|
||||
print("PyQt5 not available, trying tkinter...")
|
||||
|
||||
# Fallback to tkinter
|
||||
try:
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
|
||||
# Create a root window but keep it hidden
|
||||
root = tk.Tk()
|
||||
root.withdraw() # Hide the main window
|
||||
root.attributes('-topmost', True) # Bring dialog to front
|
||||
|
||||
# Set initial directory
|
||||
if not initial_dir:
|
||||
initial_dir = os.getcwd()
|
||||
|
||||
# Open folder selection dialog
|
||||
folder_path = filedialog.askdirectory(
|
||||
title=title,
|
||||
initialdir=initial_dir,
|
||||
mustexist=must_exist
|
||||
)
|
||||
|
||||
# Destroy the root window
|
||||
root.destroy()
|
||||
|
||||
return folder_path if folder_path else ""
|
||||
|
||||
except ImportError:
|
||||
print("tkinter also not available")
|
||||
return ""
|
||||
except Exception as e:
|
||||
print(f"Error opening tkinter folder dialog: {e}")
|
||||
return ""
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error opening PyQt5 folder dialog: {e}")
|
||||
return ""
|
||||
|
||||
def select_assets_folder(initial_dir=""):
|
||||
"""
|
||||
Specialized function for selecting Assets folder with validation
|
||||
|
||||
Args:
|
||||
initial_dir (str): Initial directory to open
|
||||
|
||||
Returns:
|
||||
dict: Result with 'path', 'valid', and 'message' keys
|
||||
"""
|
||||
folder_path = select_folder(
|
||||
title="Select Assets Folder (containing Firmware/ and Models/)",
|
||||
initial_dir=initial_dir
|
||||
)
|
||||
|
||||
if not folder_path:
|
||||
return {'path': '', 'valid': False, 'message': 'No folder selected'}
|
||||
|
||||
# Validate folder structure
|
||||
validation_result = validate_assets_folder_structure(folder_path)
|
||||
|
||||
return {
|
||||
'path': folder_path,
|
||||
'valid': validation_result['valid'],
|
||||
'message': validation_result['message'],
|
||||
'details': validation_result.get('details', {})
|
||||
}
|
||||
|
||||
def validate_assets_folder_structure(folder_path):
|
||||
"""
|
||||
Validate that a folder has the expected Assets structure
|
||||
|
||||
Expected structure:
|
||||
Assets/
|
||||
├── Firmware/
|
||||
│ ├── KL520/
|
||||
│ │ ├── fw_scpu.bin
|
||||
│ │ └── fw_ncpu.bin
|
||||
│ └── KL720/
|
||||
│ ├── fw_scpu.bin
|
||||
│ └── fw_ncpu.bin
|
||||
└── Models/
|
||||
├── KL520/
|
||||
│ └── model.nef
|
||||
└── KL720/
|
||||
└── model.nef
|
||||
|
||||
Args:
|
||||
folder_path (str): Path to validate
|
||||
|
||||
Returns:
|
||||
dict: Validation result with 'valid', 'message', and 'details' keys
|
||||
"""
|
||||
if not os.path.exists(folder_path):
|
||||
return {'valid': False, 'message': 'Folder does not exist'}
|
||||
|
||||
if not os.path.isdir(folder_path):
|
||||
return {'valid': False, 'message': 'Path is not a directory'}
|
||||
|
||||
details = {}
|
||||
issues = []
|
||||
|
||||
# Check for Firmware and Models folders
|
||||
firmware_path = os.path.join(folder_path, 'Firmware')
|
||||
models_path = os.path.join(folder_path, 'Models')
|
||||
|
||||
has_firmware = os.path.exists(firmware_path) and os.path.isdir(firmware_path)
|
||||
has_models = os.path.exists(models_path) and os.path.isdir(models_path)
|
||||
|
||||
details['has_firmware_folder'] = has_firmware
|
||||
details['has_models_folder'] = has_models
|
||||
|
||||
if not has_firmware:
|
||||
issues.append("Missing 'Firmware' folder")
|
||||
|
||||
if not has_models:
|
||||
issues.append("Missing 'Models' folder")
|
||||
|
||||
if not (has_firmware and has_models):
|
||||
return {
|
||||
'valid': False,
|
||||
'message': f"Invalid folder structure: {', '.join(issues)}",
|
||||
'details': details
|
||||
}
|
||||
|
||||
# Check for series subfolders
|
||||
expected_series = ['KL520', 'KL720', 'KL630', 'KL730', 'KL540']
|
||||
|
||||
firmware_series = []
|
||||
models_series = []
|
||||
|
||||
try:
|
||||
firmware_dirs = [d for d in os.listdir(firmware_path)
|
||||
if os.path.isdir(os.path.join(firmware_path, d))]
|
||||
firmware_series = [d for d in firmware_dirs if d in expected_series]
|
||||
|
||||
models_dirs = [d for d in os.listdir(models_path)
|
||||
if os.path.isdir(os.path.join(models_path, d))]
|
||||
models_series = [d for d in models_dirs if d in expected_series]
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
'valid': False,
|
||||
'message': f"Error reading folder contents: {e}",
|
||||
'details': details
|
||||
}
|
||||
|
||||
details['firmware_series'] = firmware_series
|
||||
details['models_series'] = models_series
|
||||
|
||||
# Find common series (have both firmware and models)
|
||||
common_series = list(set(firmware_series) & set(models_series))
|
||||
details['available_series'] = common_series
|
||||
|
||||
if not common_series:
|
||||
return {
|
||||
'valid': False,
|
||||
'message': "No series found with both firmware and models folders",
|
||||
'details': details
|
||||
}
|
||||
|
||||
# Check for actual files in series folders
|
||||
series_with_files = []
|
||||
for series in common_series:
|
||||
has_files = False
|
||||
|
||||
# Check firmware files
|
||||
fw_series_path = os.path.join(firmware_path, series)
|
||||
if os.path.exists(fw_series_path):
|
||||
fw_files = [f for f in os.listdir(fw_series_path)
|
||||
if f.endswith('.bin')]
|
||||
if fw_files:
|
||||
has_files = True
|
||||
|
||||
# Check model files
|
||||
model_series_path = os.path.join(models_path, series)
|
||||
if os.path.exists(model_series_path):
|
||||
model_files = [f for f in os.listdir(model_series_path)
|
||||
if f.endswith('.nef')]
|
||||
if model_files and has_files:
|
||||
series_with_files.append(series)
|
||||
|
||||
details['series_with_files'] = series_with_files
|
||||
|
||||
if not series_with_files:
|
||||
return {
|
||||
'valid': False,
|
||||
'message': "No series found with actual firmware and model files",
|
||||
'details': details
|
||||
}
|
||||
|
||||
return {
|
||||
'valid': True,
|
||||
'message': f"Valid Assets folder with {len(series_with_files)} series: {', '.join(series_with_files)}",
|
||||
'details': details
|
||||
}
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
print("Testing folder selection...")
|
||||
|
||||
# Test basic folder selection
|
||||
folder = select_folder("Select any folder")
|
||||
print(f"Selected: {folder}")
|
||||
|
||||
# Test Assets folder selection with validation
|
||||
result = select_assets_folder()
|
||||
print(f"Assets folder result: {result}")
|
||||
41
verify_properties.py
Normal file
41
verify_properties.py
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Verify that properties are correctly set for multi-series
|
||||
"""
|
||||
|
||||
def verify_properties():
|
||||
"""Check the expected multi-series properties"""
|
||||
|
||||
print("Multi-Series Configuration Checklist:")
|
||||
print("=" * 50)
|
||||
|
||||
print("\n1. In your Dashboard, Model Node properties should have:")
|
||||
print(" ✓ multi_series_mode = True")
|
||||
print(" ✓ enabled_series = ['520', '720']")
|
||||
print(" ✓ kl520_port_ids = '28,32'")
|
||||
print(" ✓ kl720_port_ids = '4'")
|
||||
print(" ✓ assets_folder = (optional, for auto model/firmware detection)")
|
||||
|
||||
print("\n2. After setting these properties, when you deploy:")
|
||||
print(" Expected output should show:")
|
||||
print(" '[stage_1_Model_Node] Using multi-series mode with config: ...'")
|
||||
print(" NOT: 'Single-series config converted to multi-series format'")
|
||||
|
||||
print("\n3. If you still see single-series behavior:")
|
||||
print(" a) Double-check property names (they should be lowercase)")
|
||||
print(" b) Make sure multi_series_mode is checked/enabled")
|
||||
print(" c) Verify port IDs are comma-separated strings")
|
||||
print(" d) Save the .mflow file and re-deploy")
|
||||
|
||||
print("\n4. Property format reference:")
|
||||
print(" - kl520_port_ids: '28,32' (string, comma-separated)")
|
||||
print(" - kl720_port_ids: '4' (string)")
|
||||
print(" - enabled_series: ['520', '720'] (list)")
|
||||
print(" - multi_series_mode: True (boolean)")
|
||||
|
||||
print("\n" + "=" * 50)
|
||||
print("If properties are set correctly, your deployment should use")
|
||||
print("true multi-series load balancing across KL520 and KL720 dongles!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
verify_properties()
|
||||
Loading…
x
Reference in New Issue
Block a user