Wen1201 commited on
Commit
b54de6a
·
verified ·
1 Parent(s): 50d52ea

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +175 -175
  2. app.py +5 -1
  3. requirements.txt +1 -1
README.md CHANGED
@@ -1,225 +1,225 @@
1
  ---
2
- title: BERT 二次微調平台
3
  emoji: 🥼
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- # 🥼 BERT 乳癌存活預測 - 二次微調完整平台
13
 
14
- 完整的 BERT 二次微調系統,支援從第一次微調到二次微調的完整流程,並可在新數據上比較多個模型的表現。
15
 
16
- ## 🌟 核心功能
17
 
18
- ### 1️⃣ 第一次微調
19
- - 從純 BERT 開始訓練
20
- - 支援三種微調方法:
21
- - **Full Fine-tuning**: 訓練所有參數
22
- - **LoRA**: 低秩適配,參數高效
23
- - **AdaLoRA**: 自適應 LoRA,動態調整秩
24
- - 自動比較純 BERT vs 第一次微調的表現
25
 
26
- ### 2️⃣ 二次微調
27
- - 基於第一次微調模型繼續訓練
28
- - 使用新的訓練數據
29
- - 自動繼承第一次的微調方法
30
- - 適合增量學習和領域適應
31
 
32
- ### 3️⃣ 新數據測試
33
- - 上傳新測試數據
34
- - 同時比較最多 3 個模型:
35
- - BERT (Baseline)
36
- - 第一次微調模型
37
- - 第二次微調模型
38
- - 並排顯示所有評估指標
39
 
40
- ### 4️⃣ 模型預測
41
- - 選擇任一已訓練模型
42
- - 輸入病歷文本進行預測
43
- - 同時顯示未微調和微調模型的預測結果
44
 
45
- ## 📋 資料格式
46
 
47
- CSV 檔案必須包含以下欄位:
48
- - **Text**: 病歷文本 (英文)
49
- - **label**: 標籤 (0=存活, 1=死亡)
50
 
51
- 範例:
52
  ```csv
53
  Text,label
54
  "Patient is a 45-year-old female with stage II breast cancer...",0
55
  "65-year-old woman diagnosed with triple-negative breast cancer...",1
56
  ```
57
 
58
- ## 🚀 使用流程
59
-
60
- ### 步驟 1: 第一次微調
61
- 1. 進入「1️⃣ 第一次微調」頁面
62
- 2. 上傳訓練數據 A (CSV)
63
- 3. 選擇微調方法 (建議先用 Full Fine-tuning)
64
- 4. 調整訓練參數:
65
- - 權重倍數: 0.8 (處理不平衡數據)
66
- - 訓練輪數: 8-10
67
- - 學習率: 2e-5
68
- 5. 點擊「開始第一次微調」
69
- 6. 等待訓練完成,查看結果
70
-
71
- ### 步驟 2: 二次微調
72
- 1. 進入「2️⃣ 二次微調」頁面
73
- 2. 點擊「🔄 重新整理模型列表」
74
- 3. 選擇第一次微調的模型
75
- 4. 上傳新的訓練數據 B
76
- 5. 調整訓練參數 (建議):
77
- - 訓練輪數: 3-5 (比第一次少)
78
- - 學習率: 1e-5 (比第一次小)
79
- 6. 點擊「開始二次微調」
80
- 7. 等待訓練完成
81
-
82
- ### 步驟 3: 新數據測試
83
- 1. 進入「3️⃣ 新數據測試」頁面
84
- 2. 上傳測試數據 C
85
- 3. 選擇要比較的模型:
86
- - BERT: 選擇「評估純 BERT
87
- - 第一次微調: 從下拉選單選擇
88
- - 第二次微調: 從下拉選單選擇
89
- 4. 點擊「開始測試」
90
- 5. 查看三個模型的比較結果
91
-
92
- ### 步驟 4: 預測
93
- 1. 進入「4️⃣ 模型預測」頁面
94
- 2. 選擇要使用的模型
95
- 3. 輸入病歷文本
96
- 4. 點擊「開始預測」
97
- 5. 查看預測結果
98
-
99
- ## 🎯 微調方法比較
100
-
101
- | 方法 | 參數量 | 訓練速度 | 記憶體使用 | 效果 |
102
- |------|--------|---------|-----------|------|
103
- | **Full Fine-tuning** | 100% | 1x (基準) | | 最佳 |
104
- | **LoRA** | ~1% | 3-5x | | 良好 |
105
- | **AdaLoRA** | ~1% | 3-5x | | 良好 |
106
-
107
- ## 💡 二次微調最佳實踐
108
-
109
- ### 何時使用二次微調?
110
-
111
- 1. **領域適應**
112
- - 第一次: 使用通用醫療數據
113
- - 第二次: 使用特定醫院/科別數據
114
-
115
- 2. **增量學習**
116
- - 第一次: 使用歷史數據
117
- - 第二次: 加入新收集的數據
118
-
119
- 3. **數據稀缺**
120
- - 第一次: 使用大量相關領域數據
121
- - 第二次: 使用少量目標領域數據
122
-
123
- ### 參數調整建議
124
-
125
- | 參數 | 第一次微調 | 第二次微調 | 原因 |
126
- |------|----------|----------|------|
127
- | **Epochs** | 8-10 | 3-5 | 避免過度擬合 |
128
- | **Learning Rate** | 2e-5 | 1e-5 | 保護已���習知識 |
129
- | **Warmup Steps** | 200 | 100 | 較少的預熱 |
130
- | **權重倍數** | 根據數據調整 | 根據新數據調整 | 處理不平衡 |
131
-
132
- ### 注意事項
133
-
134
- ⚠️ **重要提醒**:
135
- - 第二次微調會自動使用第一次的微調方法,無法更換
136
- - 建議第二次的學習率比第一次小,避免「災難性遺忘」
137
- - 如果第二次數據與第一次差異很大,可能需要更多輪數
138
- - 始終在新數據上測試,確保沒有性能下降
139
-
140
- ## 📊 評估指標說明
141
-
142
- | 指標 | 說明 | 適用場景 |
143
- |------|------|---------|
144
- | **F1 Score** | 精確率和召回率的調和平均 | 平衡評估,通用指標 |
145
- | **Accuracy** | 整體準確率 | 數據平衡時使用 |
146
- | **Precision** | 預測為死亡中的準確率 | 避免誤報時優化 |
147
- | **Recall** | 實際死亡中被識別的比例 | 避免漏診時優化 |
148
- | **Sensitivity** | 等同於 Recall | 醫療場景常用 |
149
- | **Specificity** | 實際存活中被識別的比例 | 避免過度治療 |
150
- | **AUC** | ROC 曲線下面積 | 整體分類能力 |
151
-
152
- ## 🔧 技術細節
153
-
154
- ### 訓練流程
155
-
156
- 1. **數據準備**
157
- - 載入 CSV
158
- - 保持原始類別比例
159
  - Tokenization (max_length=256)
160
- - 80/20 訓練/驗證分割
161
 
162
- 2. **模型初始化**
163
- - 第一次: `bert-base-uncased` 載入
164
- - 第二次: 從第一次���調模型載入
165
- - 應用 PEFT 配置 (如果使用 LoRA/AdaLoRA)
166
 
167
- 3. **訓練**
168
- - 使用類別權重處理不平衡
169
- - Early stopping (基於驗證集)
170
- - 保存最佳模型
171
 
172
- 4. **評估**
173
- - 在驗證集上評估
174
- - 計算所有指標
175
- - 生成混淆矩陣
176
 
177
- ### 模型儲存
178
 
179
- - 模型檔案: `./breast_cancer_bert_{method}_{type}_{timestamp}/`
180
- - 模型清單: `./saved_models_list.json`
181
- - 包含所有訓練資訊和超參數
182
 
183
- ## 🐛 常見問題
184
 
185
- ### Q1: 為什麼二次微調不能更換方法?
186
- **A**: 因為不同方法的參數結構不同。例如 LoRA 添加了低秩矩陣,如果切換到 Full Fine-tuning,這些參數會遺失。
187
 
188
- ### Q2: 第二次微調的數據量應該多少?
189
- **A**: 建議至少 100 ,但可以比第一次少。如果數據太少,可能會過度擬合。
190
 
191
- ### Q3: 如何選擇最佳化指標?
192
  **A**:
193
- - 醫療場景通常優先 **Recall** (避免漏診)
194
- - 如果誤報代價高, **Precision**
195
- - 平衡場景選 **F1 Score**
196
 
197
- ### Q4: GPU 記憶體不足怎麼辦?
198
  **A**:
199
- - 使用 LoRA AdaLoRA (減少 90% 記憶體)
200
- - 減小 batch size
201
- - 減少 max_length
202
 
203
- ### Q5: 訓練時間太長?
204
  **A**:
205
- - 使用 LoRA/AdaLoRA (3-5 )
206
- - 減少 epochs
207
- - 增加 batch size (如果記憶體允許)
208
 
209
- ## 📝 版本資訊
210
 
211
  - **Version**: 1.0.0
212
  - **Python**: 3.10+
213
- - **主要依賴**:
214
  - transformers 4.36.0
215
  - torch 2.1.0
216
  - peft 0.7.1
217
- - gradio 4.44.0
218
 
219
- ## 📄 授權
220
 
221
- 本專案完全保留您的原始程式邏輯,僅新增二次微調和測試功能。
222
 
223
- ## 🙏 致謝
224
 
225
- 基於 BERT 模型和 Hugging Face Transformers 庫開發。
 
1
  ---
2
+ title: BERT Second Fine-tuning Platform
3
  emoji: 🥼
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.36.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ # 🥼 BERT Breast Cancer Survival Prediction - Complete Second Fine-tuning Platform
13
 
14
+ Complete BERT second fine-tuning system supporting the full workflow from first fine-tuning to second fine-tuning, with multi-model comparison on new data.
15
 
16
+ ## 🌟 Core Features
17
 
18
+ ### 1️⃣ First Fine-tuning
19
+ - Train from pure BERT
20
+ - Supports three fine-tuning methods:
21
+ - **Full Fine-tuning**: Train all parameters
22
+ - **LoRA**: Low-rank adaptation, parameter efficient
23
+ - **AdaLoRA**: Adaptive LoRA, dynamically adjusts rank
24
+ - Automatically compare pure BERT vs first fine-tuning performance
25
 
26
+ ### 2️⃣ Second Fine-tuning
27
+ - Continue training based on first fine-tuning model
28
+ - Use new training data
29
+ - Automatically inherit first fine-tuning method
30
+ - Suitable for incremental learning and domain adaptation
31
 
32
+ ### 3️⃣ Test on New Data
33
+ - Upload new test data
34
+ - Compare up to 3 models simultaneously:
35
+ - Pure BERT (Baseline)
36
+ - First fine-tuning model
37
+ - Second fine-tuning model
38
+ - Display all evaluation metrics side by side
39
 
40
+ ### 4️⃣ Model Prediction
41
+ - Select any trained model
42
+ - Input medical text for prediction
43
+ - Display predictions from both non-finetuned and finetuned models
44
 
45
+ ## 📋 Data Format
46
 
47
+ CSV file must contain the following columns:
48
+ - **Text**: Medical record text (English)
49
+ - **label**: Label (0=Survival, 1=Death)
50
 
51
+ Example:
52
  ```csv
53
  Text,label
54
  "Patient is a 45-year-old female with stage II breast cancer...",0
55
  "65-year-old woman diagnosed with triple-negative breast cancer...",1
56
  ```
57
 
58
+ ## 🚀 Usage Workflow
59
+
60
+ ### Step 1: First Fine-tuning
61
+ 1. Go to "1️⃣ First Fine-tuning" page
62
+ 2. Upload training data A (CSV)
63
+ 3. Select fine-tuning method (recommend starting with Full Fine-tuning)
64
+ 4. Adjust training parameters:
65
+ - Weight Multiplier: 0.8 (handle imbalanced data)
66
+ - Training Epochs: 8-10
67
+ - Learning Rate: 2e-5
68
+ 5. Click "Start First Fine-tuning"
69
+ 6. Wait for training to complete, review results
70
+
71
+ ### Step 2: Second Fine-tuning
72
+ 1. Go to "2️⃣ Second Fine-tuning" page
73
+ 2. Click "🔄 Refresh Model List"
74
+ 3. Select first fine-tuning model
75
+ 4. Upload new training data B
76
+ 5. Adjust training parameters (recommended):
77
+ - Training Epochs: 3-5 (fewer than first)
78
+ - Learning Rate: 1e-5 (smaller than first)
79
+ 6. Click "Start Second Fine-tuning"
80
+ 7. Wait for training to complete
81
+
82
+ ### Step 3: Test on New Data
83
+ 1. Go to "3️⃣ Test on New Data" page
84
+ 2. Upload test data C
85
+ 3. Select models to compare:
86
+ - Pure BERT: Select "Evaluate Pure BERT"
87
+ - First fine-tuning: Select from dropdown
88
+ - Second fine-tuning: Select from dropdown
89
+ 4. Click "Start Testing"
90
+ 5. View comparison results for all three models
91
+
92
+ ### Step 4: Prediction
93
+ 1. Go to "4️⃣ Model Prediction" page
94
+ 2. Select model to use
95
+ 3. Input medical text
96
+ 4. Click "Start Prediction"
97
+ 5. View prediction results
98
+
99
+ ## 🎯 Fine-tuning Method Comparison
100
+
101
+ | Method | Parameters | Training Speed | Memory Usage | Performance |
102
+ |--------|-----------|----------------|--------------|-------------|
103
+ | **Full Fine-tuning** | 100% | 1x (baseline) | High | Best |
104
+ | **LoRA** | ~1% | 3-5x faster | Low | Good |
105
+ | **AdaLoRA** | ~1% | 3-5x faster | Low | Good |
106
+
107
+ ## 💡 Second Fine-tuning Best Practices
108
+
109
+ ### When to Use Second Fine-tuning?
110
+
111
+ 1. **Domain Adaptation**
112
+ - First: Use general medical data
113
+ - Second: Use specific hospital/department data
114
+
115
+ 2. **Incremental Learning**
116
+ - First: Use historical data
117
+ - Second: Add newly collected data
118
+
119
+ 3. **Data Scarcity**
120
+ - First: Use large amount of related domain data
121
+ - Second: Use small amount of target domain data
122
+
123
+ ### Parameter Adjustment Recommendations
124
+
125
+ | Parameter | First Fine-tuning | Second Fine-tuning | Reason |
126
+ |-----------|------------------|-------------------|--------|
127
+ | **Epochs** | 8-10 | 3-5 | Avoid overfitting |
128
+ | **Learning Rate** | 2e-5 | 1e-5 | Preserve learned knowledge |
129
+ | **Warmup Steps** | 200 | 100 | Less warmup needed |
130
+ | **Weight Multiplier** | Adjust based on data | Adjust based on new data | Handle imbalance |
131
+
132
+ ### Important Notes
133
+
134
+ ⚠️ **Critical Reminders**:
135
+ - Second fine-tuning automatically uses first fine-tuning method, cannot change
136
+ - Recommend smaller learning rate for second fine-tuning to avoid "catastrophic forgetting"
137
+ - If second data differs greatly from first, may need more epochs
138
+ - Always test on new data to ensure no performance degradation
139
+
140
+ ## 📊 Evaluation Metrics Explanation
141
+
142
+ | Metric | Description | Use Case |
143
+ |--------|-------------|----------|
144
+ | **F1 Score** | Harmonic mean of precision and recall | Balanced evaluation, general metric |
145
+ | **Accuracy** | Overall accuracy | Use when data is balanced |
146
+ | **Precision** | Accuracy of death predictions | Optimize to avoid false positives |
147
+ | **Recall** | Proportion of actual deaths identified | Optimize to avoid missed diagnoses |
148
+ | **Sensitivity** | Same as Recall | Commonly used in medical scenarios |
149
+ | **Specificity** | Proportion of actual survivals identified | Avoid overtreatment |
150
+ | **AUC** | Area under ROC curve | Overall classification ability |
151
+
152
+ ## 🔧 Technical Details
153
+
154
+ ### Training Process
155
+
156
+ 1. **Data Preparation**
157
+ - Load CSV
158
+ - Maintain original class ratio
159
  - Tokenization (max_length=256)
160
+ - 80/20 train/validation split
161
 
162
+ 2. **Model Initialization**
163
+ - First: Load from `bert-base-uncased`
164
+ - Second: Load from first fine-tuning model
165
+ - Apply PEFT configuration (if using LoRA/AdaLoRA)
166
 
167
+ 3. **Training**
168
+ - Use class weights to handle imbalance
169
+ - Early stopping (based on validation set)
170
+ - Save best model
171
 
172
+ 4. **Evaluation**
173
+ - Evaluate on validation set
174
+ - Calculate all metrics
175
+ - Generate confusion matrix
176
 
177
+ ### Model Storage
178
 
179
+ - Model files: `./breast_cancer_bert_{method}_{type}_{timestamp}/`
180
+ - Model list: `./saved_models_list.json`
181
+ - Includes all training information and hyperparameters
182
 
183
+ ## 🐛 Common Questions
184
 
185
+ ### Q1: Why can't I change methods in second fine-tuning?
186
+ **A**: Because different methods have different parameter structures. For example, LoRA adds low-rank matrices; if you switch to Full Fine-tuning, these parameters would be lost.
187
 
188
+ ### Q2: How much data should second fine-tuning have?
189
+ **A**: Recommend at least 100 samples, but can be less than first. If data is too scarce, may overfit.
190
 
191
+ ### Q3: How to choose optimization metric?
192
  **A**:
193
+ - Medical scenarios usually prioritize **Recall** (avoid missed diagnoses)
194
+ - If false positives have high cost, choose **Precision**
195
+ - For balanced scenarios, choose **F1 Score**
196
 
197
+ ### Q4: What if GPU memory insufficient?
198
  **A**:
199
+ - Use LoRA or AdaLoRA (reduce 90% memory)
200
+ - Reduce batch size
201
+ - Reduce max_length
202
 
203
+ ### Q5: Training takes too long?
204
  **A**:
205
+ - Use LoRA/AdaLoRA (3-5x faster)
206
+ - Reduce epochs
207
+ - Increase batch size (if memory allows)
208
 
209
+ ## 📝 Version Information
210
 
211
  - **Version**: 1.0.0
212
  - **Python**: 3.10+
213
+ - **Main Dependencies**:
214
  - transformers 4.36.0
215
  - torch 2.1.0
216
  - peft 0.7.1
217
+ - gradio 4.36.0
218
 
219
+ ## 📄 License
220
 
221
+ This project completely preserves your original program logic, only adding second fine-tuning and testing features.
222
 
223
+ ## 🙏 Acknowledgments
224
 
225
+ Developed based on BERT model and Hugging Face Transformers library.
app.py CHANGED
@@ -1682,4 +1682,8 @@ with gr.Blocks(title="BERT Second Fine-tuningplatform", theme=gr.themes.Soft())
1682
  )
1683
 
1684
  if __name__ == "__main__":
1685
- demo.launch()
 
 
 
 
 
1682
  )
1683
 
1684
  if __name__ == "__main__":
1685
+ demo.launch(
1686
+ server_name="0.0.0.0",
1687
+ server_port=7860,
1688
+ share=False
1689
+ )
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==4.44.0
2
  pandas==2.0.3
3
  torch==2.1.0
4
  transformers==4.36.0
 
1
+ gradio==4.36.0
2
  pandas==2.0.3
3
  torch==2.1.0
4
  transformers==4.36.0