FORTE 完整教學
Repository: https://github.com/charlierabea/FORTE Stars: 49 | Fork: 6 | License: MIT Tags: multimodal, LLM, brain-CT, report 論文: Nature Communications 16, 2258 (2025) 最後更新: 2026-05-27 Zenodo DOI: 10.5281/zenodo.14852686
2. 核心架構
2.1 系統全貌
flowchart TB
subgraph INPUT["輸入層"]
CT["3D Brain CT
(N 張 2D Slice)"]
INST["Structured Instruction
(MIIT 格式)"]
end
subgraph MODEL["BrainGPT 模型"]
CLIP["CLIP Vision Encoder
(影像編碼器)"]
PERC["Perceiver Resampler
(跨模態橋接)"]
MPT["MPT-7B LLM
(語言生成器)"]
CLIP --> PERC
PERC --> MPT
end
subgraph EVAL["FORTE 評估框架"]
AUTO["Automatic Evaluation
(BLEU / METEOR / ROUGE / CIDEr)"]
SP["Sentence Pairing
(語義相似度配對)"]
KW["FORTE Keyword Extraction
(4 類關鍵字擷取)"]
NEG["Negation Removal
(否定句移除)"]
SP --> KW
KW --> NEG
end
CT --> CLIP
INST --> MPT
MPT -->|"Generated Report"| EVAL
AUTO -.->|"NLG Metrics"| RESULT["Evaluation Result
(Excel)"]
NEG -->|"Clinical Precision/Recall"| RESULT
style INPUT fill:#e1f5fe,stroke:#0288d1
style MODEL fill:#fff3e0,stroke:#f57c00
style EVAL fill:#e8f5e9,stroke:#388e3c
2.2 目錄結構
1FORTE/
2├── MIIT/ # 訓練框架(基於 Otter)
3│ ├── flamingo/ # OpenFlamingo 模型定義
4│ │ ├── modeling_flamingo.py # Flamingo 條件生成模型
5│ │ └── mpt/ # MPT backbone
6│ ├── otter/ # Otter 模型(BrainGPT 基底)
7│ │ └── modeling_otter.py # OtterForConditionalGeneration
8│ └── pipeline/
9│ ├── train/
10│ │ ├── instruction_following.py # 主訓練腳本
11│ │ ├── data.py # 資料載入器
12│ │ └── train_utils.py # 訓練工具
13│ ├── eval/ # COCO 風格評估
14│ ├── serve/ # Gradio demo 伺服器
15│ └── mimicit_utils/ # MIIT 資料集工具
16├── evaluation/ # 推論框架(結構同 MIIT,用於 eval mode)
17│ └── pipeline/train/eval.py # 推論主腳本
18├── data/
19│ ├── CQ500p_instruction.json # CQ500 外部驗證指令
20│ ├── FORTE_brain.json # 腦 CT 關鍵字詞典(4 類 × N 同義詞組)
21│ ├── FORTE_chestCT.json # 胸 CT 關鍵字詞典
22│ ├── FORTE_abdomen.json # 腹 CT 關鍵字詞典
23│ └── FORTE_CXR.json # 胸 X 光關鍵字詞典
24├── Automatic_evaluation.py # Step 1: NLG 自動評估
25├── Sentence_pairing.py # Step 2: 語義句子配對
26├── FORTE.py # Step 3: FORTE 關鍵字評估
27├── Negation_removal.py # Step 4: 否定移除
28├── train.sh # 訓練腳本入口
29├── eval.sh # 推論腳本入口
30├── environment.yml # Conda 環境
31└── requirements.txt # pip 依賴
2.3 FORTE 評估 Pipeline 流程
flowchart LR
A["Generated Reports
(Excel: gt + parsed_output)"] --> B["Step 1
Automatic Evaluation
(BLEU/METEOR/ROUGE/CIDEr)"]
B --> C["Step 2
Sentence Pairing
(all-mpnet-base-v2
cosine similarity)"]
C --> D["Step 3
FORTE Keyword Extraction
(4 類: degree/landmark/
feature/impression)"]
D --> E["Step 4
Negation Removal
(移除含 'no' 的 degree 句)"]
E --> F["Final Metrics
per-category
Precision & Recall"]
style A fill:#fff9c4,stroke:#f9a825
style F fill:#c8e6c9,stroke:#2e7d32
四類 FORTE 關鍵字(以 Brain CT 為例):
| 類別 | 範例 | 同義詞組數 |
|---|---|---|
| Degree (程度) | normal/intact/healthy; mild/slight/subtle/faint; acute; chronic | ~15 組 |
| Landmark (解剖位置) | parenchyma; midline/falx; sulci/fissure; basal ganglia; thalamus | ~50 組 |
| Feature (影像特徵) | hemorrhage/hematoma; atrophy/degeneration; calcification/arteriosclerosis | ~25 組 |
| Impression (臨床印象) | ICH/intracerebral hemorrhage; SDH/subdural hematoma; dementia; meningioma | ~35 組 |
3. 安裝與設定
3.1 系統需求
| 項目 | 最低需求 | 建議配置 |
|---|---|---|
| GPU | NVIDIA GPU with >= 36 GB VRAM | A100 80GB / H100 |
| CUDA | 11.1+ | 11.7+ |
| Python | 3.9 | 3.9 |
| 磁碟空間 | ~40 GB (模型 checkpoint 32.54 GB + 資料) | 60 GB+ |
| RAM | 32 GB | 64 GB+ |
3.2 環境建置
1# Step 1: Clone 專案
2git clone https://github.com/charlierabea/FORTE.git
3cd FORTE
4
5# Step 2: 建立 Conda 環境
6conda env create -f environment.yml
7conda activate forte
8
9# Step 3: 安裝 PyTorch(須匹配 CUDA 版本)
10# 範例:CUDA 11.8
11pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
12
13# Step 4: 安裝其餘依賴
14pip install -r requirements.txt
15
16# 注意:Horovod 安裝可能因 cmake 版本過舊而失敗
17# 推論不需要 Horovod,可略過此錯誤
3.3 下載模型與資料
1# 建立目錄
2mkdir -p checkpoints data
3
4# 1. 下載 Base Model(Otter-MPT-7B-Init)
5# 來源:https://huggingface.co/luodian/OTTER-MPT7B-Init
6# 放置於 ./checkpoints/OTTER-MPT7B-Init/
7
8# 2. 下載 BrainGPT Fine-tuned Model
9# 來源:https://huggingface.co/Charliebear/BrainGPT
10# 放置於 ./checkpoints/OTTER_CLIP_BRAINGPT_hf/
11
12# 3. 下載 CQ500 外部驗證影像
13# 來源:Google Drive (見 README)
14# 放置於 ./data/CQ500p.json (影像索引)
15
16# 4. 下載 FORTE Keyword File
17# 來源:Google Drive (見 README)
18# 已內建:data/FORTE_brain.json 等 4 個 JSON
19
20# 目錄結構驗證
21ls checkpoints/
22# OTTER-MPT7B-Init/ OTTER_CLIP_BRAINGPT_hf/
23
24ls data/
25# CQ500p_instruction.json CQ500p.json FORTE_brain.json ...
4. 使用方式與程式碼範例
4.1 範例一:使用 BrainGPT 生成放射科報告(推論)
這是最核心的使用情境:給定一組 Brain CT Slice,生成結構化報告。
1# 執行推論腳本
2# 需要 GPU,使用 FSDP(Fully Sharded Data Parallel)加速
3bash ./eval.sh
eval.sh 的內容拆解:
1# 設定 Python 路徑以正確引入 evaluation/ 模組
2export PYHTONPATH="./evaluation/"
3
4# 使用 Accelerate + FSDP 啟動推論
5accelerate launch \
6 --config_file=./evaluation/pipeline/accelerate_configs/accelerate_config_fsdp.yaml \
7 ./evaluation/pipeline/train/eval.py \
8 --pretrained_model_name_or_path="./checkpoints/OTTER_CLIP_BRAINGPT_hf/" \
9 --mimicit_path="./data/CQ500p_instruction.json" \
10 --images_path="./data/CQ500p.json" \
11 --batch_size=1 \
12 --warmup_steps_ratio=0.01 \
13 --workers=1
輸入格式(CQ500p_instruction.json):
1{
2 "meta": {"version": "0.0.2", "time": "2024-02", "author": "big_data_center"},
3 "data": {
4 "CQ500p_INS_00001": {
5 "instruction": "You are an AI assistant specialized in radiology topics. You are provided with brain CT slices from a single study. The number of slices is 24. Please generate medical descriptions based on the images in a consistent style. Use the following guidelines: - Degree: Indicate the intensity or state (e.g., normal, mild, chronic, old, etc). - Landmark: Specify the area of interest (e.g., intracerebral, midline, parenchyma, sulci, etc). - Feature: Describe any observed abnormalities (e.g., hemorrhage, atrophy, infarcts, etc). - Impression: Conclude with a clinical impression...",
6 "answer": "",
7 "image_ids": [
8 "CQ500p_IMG_460_3_9",
9 "CQ500p_IMG_460_3_10",
10 "...共 24 張 slice..."
11 ],
12 "rel_ins_ids": []
13 }
14 }
15}
輸出:Excel 檔案,位於 ./Evaluation/pipeline/train/output/,每列包含 id、gt (ground truth)、parsed_output (模型生成)。
4.2 範例二:FORTE 四步驟評估 Pipeline
產生報告後,執行 FORTE 評估框架。這四個步驟須依序執行:
1# ===== Step 1: Automatic Evaluation (NLG 自動指標) =====
2# 輸入:excel_files/evaluation_examples.xlsx (含 gt 與 parsed_output 欄位)
3# 輸出:excel_files/automated_evaluation.xlsx (附加 BLEU/METEOR/ROUGE/CIDEr 分數)
4
5# 注意:須先移除 pycocoevalcap/eval.py 中的 Spice scorer 以避免錯誤
6# 將以下行註解掉:
7# (Spice(), "SPICE")
8
9python3 Automatic_evaluation.py
10
11
12# ===== Step 2: Sentence Pairing (語義句子配對) =====
13# 使用 all-mpnet-base-v2 計算 GT 與 Output 各句子的 cosine similarity
14# 將最相似的句子配對,方便後續逐句比較
15# 輸入:excel_files/evaluation_examples.xlsx
16# 輸出:excel_files/sentencepaired_reports.xlsx
17
18python3 Sentence_pairing.py
19
20
21# ===== Step 3: FORTE Keyword Evaluation (領域關鍵字評估) =====
22# 從配對句子中擷取 4 類 FORTE 關鍵字,計算 Precision/Recall
23# 輸入:excel_files/sentencepaired_reports.xlsx + data/FORTE_brain.json
24# 輸出:excel_files/FORTE_evaluated.xlsx
25
26python3 FORTE.py
27
28
29# ===== Step 4: Negation Removal (否定移除) =====
30# 過濾掉 GT 或 Output 中 degree 欄含 "no" 的列
31# 避免 "no hemorrhage" 被誤計為 "hemorrhage" 的假陽性
32# 輸入:excel_files/FORTE_evaluated.xlsx
33# 輸出:excel_files/FORTE_negationremoval.xlsx
34
35python3 Negation_removal.py
4.3 範例三:使用 MIIT 框架自行訓練(Fine-tuning)
若要在自己的醫學影像資料集上訓練 BrainGPT 或同類模型:
1# 準備訓練資料:
2# 1. MED_instruction.json — 指令格式同 CQ500p_instruction.json
3# 2. MED.json — 影像路徑對應檔
4
5# 修改 train.sh 中的路徑
6export PYTHONPATH=.
7
8CUDA_VISIBLE_DEVICES=0,1 accelerate launch \
9 --config_file=./MIIT/pipeline/accelerate_configs/accelerate_config_fsdp.yaml \
10 ./MIIT/pipeline/train/instruction_following.py \
11 --pretrained_model_name_or_path="./checkpoints/OTTER-MPT7B-Init" \
12 --mimicit_path="./data/MED_instruction.json" \
13 --images_path="./data/MED.json" \
14 --batch_size=1 \
15 --gradient_accumulation_steps=4 \
16 --num_epochs=3 \
17 --report_to_wandb \
18 --wandb_entity="your_institution" \
19 --run_name=BrainGPT_custom \
20 --wandb_project=BrainGPT_custom \
21 --workers=1 \
22 --lr_scheduler=cosine \
23 --learning_rate=1e-5 \
24 --warmup_steps_ratio=0.01
自訂 Instruction JSON 的格式要求:
1# 每筆資料須包含:
2{
3 "instruction": "...", # 提示模型生成報告的指令
4 "answer": "GT 報告文本", # Ground truth(訓練時使用)
5 "image_ids": ["img_001", "img_002", ...], # 對應影像 ID 清單
6 "rel_ins_ids": [] # 相關指令 ID(in-context learning 用)
7}
關鍵訓練參數:
| 參數 | 預設值 | 說明 |
|---|---|---|
batch_size | 1 | 因模型大(~32 GB),建議維持 1 |
gradient_accumulation_steps | 4 | 等效 batch = 4 |
num_epochs | 3 | 醫學資料集通常 3-5 epochs 即收斂 |
learning_rate | 1e-5 | Instruction Tuning 常用範圍 |
lr_scheduler | cosine | Cosine Annealing 學習率排程 |
5. 在醫療 LLM / 文本 SDG 生態系中的定位
5.1 Domain 6-C: Radiology Report Generation
FORTE 屬於 Bio-SDG 生態系 Domain 6 的 Sub-domain C (Radiology Report Generation),與以下專案形成互補:
| 專案 | 模態 | 模型基底 | 評估特色 |
|---|---|---|---|
| R2GenGPT | Chest X-ray | LLaMA + ViT | 標準 NLG metrics |
| RadFact | 多模態 | GPT-4 judge | Factual grounding |
| LLM-RG4 | Chest X-ray | LLM + retrieval | Region-guided |
| FORTE | 3D Brain CT | Otter/MPT-7B | 領域關鍵字 + 同義詞 + 否定處理 |
FORTE 的獨特價值在於:
- 3D 多切面:其他專案多處理 2D X-ray 單張影像,FORTE 處理同一病例的 N 張 CT Slice
- 結構化關鍵字評估:不只看 BLEU/ROUGE 等 n-gram 重疊,而是針對放射科四大類別做 semantic-aware 比對
- 跨模態可擴展:FORTE Keyword JSON 已涵蓋 Brain CT、Chest CT、Abdomen CT、Chest X-ray 四種模態
5.2 作為 Synthetic Data Engine 的潛力
在 Medical Text SDG 的架構中,FORTE 可扮演以下角色:
1真實 CT 影像 ─→ BrainGPT ─→ 合成放射科報告 ─→ 下游應用
2 │
3 ┌───────────────┼───────────────┐
4 ▼ ▼ ▼
5 NLP 模型訓練 CDSS 標註輔助 品質控管 benchmark
6 (RadBERT 等) (半自動報告) (FORTE metrics)
- 大規模合成語料:對小型醫院缺乏足夠報告文本的場景,可用 BrainGPT 從 CT 影像批量生成訓練語料
- 資料增強 (Data Augmentation):同一組影像搭配不同 instruction prompt,產生多樣化報告描述
- 跨語言合成:修改 instruction 可引導模型輸出不同語言的報告(需額外微調)
5.3 與 AIKT Pipeline 的連接點
對生物資訊分析而言,FORTE 的價值在於:
- Imaging + Text 雙模態管線:若組織有 Brain Imaging 資料,可用 FORTE 自動化報告生成,減輕放射科醫師工作負擔
- FORTE Keyword JSON 可獨立使用:即使不用 BrainGPT 模型,FORTE 的 keyword dictionary + synonym mapping 邏輯可直接用於任何放射科報告的品質評估
- Pre-IND 文件中的影像描述:在監管文件準備中,若需引用影像學資料摘要,可用 FORTE 評估框架驗證描述的一致性
6. 與其他工具的整合
6.1 與 RadFact 互補評估
RadFact 提供 LLM-as-Judge 的 factual grounding 評估;FORTE 提供 keyword-level precision/recall。兩者結合可從不同角度驗證報告品質:
1# 概念範例:同時使用 FORTE keyword metrics + RadFact factual score
2
3# Step 1: FORTE keyword evaluation
4import json
5import pandas as pd
6
7with open('data/FORTE_brain.json', 'r') as f:
8 forte_keywords = json.load(f)
9
10# 擷取 GT 與 Output 的關鍵字
11def extract_forte_keywords(text, keyword_dict):
12 """從報告文本中擷取 FORTE 四類關鍵字"""
13 text = f" {text.lower()} "
14 found = {}
15 for category in ['degree', 'landmark', 'feature', 'impression']:
16 found[category] = [k.strip() for k in keyword_dict[category] if k in text]
17 return found
18
19gt_keywords = extract_forte_keywords(gt_report, forte_keywords)
20pred_keywords = extract_forte_keywords(pred_report, forte_keywords)
21
22# Step 2: 計算 FORTE Precision / Recall per category
23# (詳見 FORTE.py 的 calculate_precision_recall 函式)
24
25# Step 3: 同時收集 RadFact factual consistency score
26# radfact_score = radfact_evaluate(gt_report, pred_report)
27
28# Step 4: 綜合報告
29# combined_score = weighted_average(forte_f1, radfact_score)
6.2 與 R2GenGPT / LLM-RG4 的模型替換
FORTE 的評估框架可獨立於 BrainGPT 模型使用。若改用 R2GenGPT 或 LLM-RG4 生成報告,只要輸出格式為文本,即可直接套用 FORTE 評估流程:
1# 任何模型的輸出,只要存為 Excel (id / gt / parsed_output) 即可
2# 套用 FORTE 四步驟評估
3
4# 1. 存為 Excel
5import pandas as pd
6results = pd.DataFrame({
7 'id': case_ids,
8 'gt': ground_truth_reports,
9 'parsed_output': any_model_generated_reports
10})
11results.to_excel('excel_files/evaluation_examples.xlsx', index=False)
12
13# 2. 依序執行四步驟
14# python3 Automatic_evaluation.py
15# python3 Sentence_pairing.py
16# python3 FORTE.py
17# python3 Negation_removal.py
6.3 與 distilabel / sdg_hub 的合成資料管線
將 FORTE 模型嵌入 SDG 框架,大規模產生合成報告:
1distilabel Pipeline
2 └── Step 1: 收集 CT 影像 metadata
3 └── Step 2: 構建 MIIT instruction JSON
4 └── Step 3: 呼叫 BrainGPT 生成報告
5 └── Step 4: FORTE 評估品質過濾
6 └── Step 5: 匯出高品質合成報告語料
6.4 FORTE Keyword JSON 的獨立應用
FORTE 提供的四套 Keyword JSON(Brain CT / Chest CT / Abdomen CT / Chest X-ray)本身就是高價值的領域資源,可用於:
- 報告品質自動審查:醫院 QA 系統自動檢查報告是否涵蓋必要解剖結構
- NER 標註輔助:作為放射科 Named Entity Recognition (命名實體辨識) 的種子詞典
- Prompt Engineering:在設計 instruction 時參考四類關鍵字的分類體系
7. 優缺點分析
7.1 優點
| 項目 | 說明 |
|---|---|
| Nature Communications 等級驗證 | 經同儕審查的學術品質保證,方法論經得起檢驗 |
| 3D 多切面處理 | 同類工具多限於 2D 單張影像,FORTE 處理完整 CT study 的多張 slice |
| 領域特化評估 | FORTE 關鍵字 + 同義詞 + 否定處理,比純 BLEU/ROUGE 更貼近臨床實際 |
| 跨模態 Keyword 覆蓋 | 已建好 4 種影像模態的關鍵字詞典,可直接用於其他專案 |
| MIT License | 商業與學術皆可自由使用 |
| 完整 Pipeline | 從訓練到推論到評估,端到端可復現 |
| Zenodo 存檔 | 有 DOI 可引用,資料可追溯 |
7.2 缺點與限制
| 項目 | 說明 |
|---|---|
| GPU 門檻極高 | 模型 checkpoint 32.54 GB,需 >= 36 GB VRAM,排除大部分消費級 GPU |
| 模型基底偏舊 | 基於 Otter/MPT-7B (2023),未使用 LLaVA-Med、Med-PaLM 等更新架構 |
| 文件偏簡略 | README 缺乏詳細 API 文件與中間產物格式說明 |
| 外部資料取得不易 | CQ500 影像需另外下載,Google Drive 連結可能失效 |
| 僅英文報告 | 目前指令與輸出皆為英文,中文報告需額外微調 |
| Keyword JSON 為手工維護 | 同義詞映射表需領域專家持續維護,缺乏自動擴展機制 |
| 無 Docker 容器 | 缺少 Dockerfile,環境建置依賴 Conda + pip 多步驟手動操作 |
| Horovod 相容性問題 | README 提及可能遇到安裝錯誤,增加部署障礙 |
7.3 適用場景 vs 不適用場景
| 適用 | 不適用 |
|---|---|
| 有 A100/H100 GPU 資源的研究機構 | 僅有消費級 GPU 或 CPU-only 環境 |
| 需要腦 CT 自動報告生成 | 需要即時診斷(模型推論速度未優化) |
| 需要評估任意模型生成的放射科報告品質 | 處理非影像模態的醫學文本(如 EHR 敘述) |
| 建構 Radiology NLP 的合成訓練資料 | 需要 FDA 等級臨床部署(未經醫療器材認證) |
| 學術研究與論文復現 | 需要 real-time API 服務(未提供生產級部署方案) |
7.4 Blue Ocean 觀察
以 Medical Text SDG 的視角,FORTE 所揭示的幾個藍海方向:
- 非英語放射科報告 SDG:以 FORTE 架構搭配多語言 LLM,產生中文 / 日文放射科報告
- FORTE Keyword → 自動化 ICD Coding:從 FORTE 擷取的 impression 關鍵字可進一步映射至 ICD-10 編碼
- 3D 影像 → 結構化 CRF/eCRF 資料:將 FORTE 的報告生成能力延伸至臨床試驗 Case Report Form 的自動填寫
- Keyword JSON 自動擴展:以 LLM 輔助擴展 FORTE 同義詞詞典,降低人工維護成本
一句話總結:FORTE 是一套發表於 Nature Communications 的 3D 腦 CT 多模態 LLM 報告生成與評估框架,其 BrainGPT 模型能從多切面 CT 影像自動生成結構化放射科報告,而其 FORTE Keyword Evaluation 四步驟流程與四模態關鍵字詞典更可作為所有放射科報告生成模型的通用品質評估標準。
Comments