Upload 3 files
Browse files- README.md +175 -175
- app.py +5 -1
- 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.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# 🥼 BERT
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
## 🌟
|
| 17 |
|
| 18 |
-
### 1️⃣
|
| 19 |
-
-
|
| 20 |
-
-
|
| 21 |
-
- **Full Fine-tuning**:
|
| 22 |
-
- **LoRA**:
|
| 23 |
-
- **AdaLoRA**:
|
| 24 |
-
-
|
| 25 |
|
| 26 |
-
### 2️⃣
|
| 27 |
-
-
|
| 28 |
-
-
|
| 29 |
-
-
|
| 30 |
-
-
|
| 31 |
|
| 32 |
-
### 3️⃣
|
| 33 |
-
-
|
| 34 |
-
-
|
| 35 |
-
-
|
| 36 |
-
-
|
| 37 |
-
-
|
| 38 |
-
-
|
| 39 |
|
| 40 |
-
### 4️⃣
|
| 41 |
-
-
|
| 42 |
-
-
|
| 43 |
-
-
|
| 44 |
|
| 45 |
-
## 📋
|
| 46 |
|
| 47 |
-
CSV
|
| 48 |
-
- **Text**:
|
| 49 |
-
- **label**:
|
| 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 |
-
###
|
| 61 |
-
1.
|
| 62 |
-
2.
|
| 63 |
-
3.
|
| 64 |
-
4.
|
| 65 |
-
-
|
| 66 |
-
-
|
| 67 |
-
-
|
| 68 |
-
5.
|
| 69 |
-
6.
|
| 70 |
-
|
| 71 |
-
###
|
| 72 |
-
1.
|
| 73 |
-
2.
|
| 74 |
-
3.
|
| 75 |
-
4.
|
| 76 |
-
5.
|
| 77 |
-
-
|
| 78 |
-
-
|
| 79 |
-
6.
|
| 80 |
-
7.
|
| 81 |
-
|
| 82 |
-
###
|
| 83 |
-
1.
|
| 84 |
-
2.
|
| 85 |
-
3.
|
| 86 |
-
-
|
| 87 |
-
-
|
| 88 |
-
-
|
| 89 |
-
4.
|
| 90 |
-
5.
|
| 91 |
-
|
| 92 |
-
###
|
| 93 |
-
1.
|
| 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** |
|
| 149 |
-
| **Specificity** |
|
| 150 |
-
| **AUC** | ROC
|
| 151 |
-
|
| 152 |
-
## 🔧
|
| 153 |
-
|
| 154 |
-
###
|
| 155 |
-
|
| 156 |
-
1. **
|
| 157 |
-
-
|
| 158 |
-
-
|
| 159 |
- Tokenization (max_length=256)
|
| 160 |
-
- 80/20
|
| 161 |
|
| 162 |
-
2. **
|
| 163 |
-
-
|
| 164 |
-
-
|
| 165 |
-
-
|
| 166 |
|
| 167 |
-
3. **
|
| 168 |
-
-
|
| 169 |
-
- Early stopping (
|
| 170 |
-
-
|
| 171 |
|
| 172 |
-
4. **
|
| 173 |
-
-
|
| 174 |
-
-
|
| 175 |
-
-
|
| 176 |
|
| 177 |
-
###
|
| 178 |
|
| 179 |
-
-
|
| 180 |
-
-
|
| 181 |
-
-
|
| 182 |
|
| 183 |
-
## 🐛
|
| 184 |
|
| 185 |
-
### Q1:
|
| 186 |
-
**A**:
|
| 187 |
|
| 188 |
-
### Q2:
|
| 189 |
-
**A**:
|
| 190 |
|
| 191 |
-
### Q3:
|
| 192 |
**A**:
|
| 193 |
-
-
|
| 194 |
-
-
|
| 195 |
-
-
|
| 196 |
|
| 197 |
-
### Q4: GPU
|
| 198 |
**A**:
|
| 199 |
-
-
|
| 200 |
-
-
|
| 201 |
-
-
|
| 202 |
|
| 203 |
-
### Q5:
|
| 204 |
**A**:
|
| 205 |
-
-
|
| 206 |
-
-
|
| 207 |
-
-
|
| 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.
|
| 218 |
|
| 219 |
-
## 📄
|
| 220 |
|
| 221 |
-
|
| 222 |
|
| 223 |
-
## 🙏
|
| 224 |
|
| 225 |
-
|
|
|
|
| 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.
|
| 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
|