Update README.md
Browse files
README.md
CHANGED
@@ -5,4 +5,94 @@ datasets:
|
|
5 |
base_model:
|
6 |
- microsoft/unixcoder-base
|
7 |
library_name: transformers
|
8 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
base_model:
|
6 |
- microsoft/unixcoder-base
|
7 |
library_name: transformers
|
8 |
+
---
|
9 |
+
|
10 |
+
# Fine-Tuned UnixCoder for Vulnerability and CWE Classification
|
11 |
+
|
12 |
+
## Model Overview
|
13 |
+
This model is a fine-tuned version of **microsoft/unixcoder-base** on a curated and enriched dataset for vulnerability detection and CWE classification. It is capable of predicting whether a given code snippet is vulnerable and, if vulnerable, identifying the specific CWE ID associated with it.
|
14 |
+
|
15 |
+
## Dataset
|
16 |
+
The model was fine-tuned using the dataset [mahdin70/cwe_enriched_balanced_bigvul_primevul](https://huggingface.co/datasets/mahdin70/cwe_enriched_balanced_bigvul_primevul). The dataset contains both vulnerable and non-vulnerable code samples and is enriched with CWE metadata.
|
17 |
+
|
18 |
+
### CWE IDs Covered:
|
19 |
+
1. **CWE-119**: Improper Restriction of Operations within the Bounds of a Memory Buffer
|
20 |
+
2. **CWE-20**: Improper Input Validation
|
21 |
+
3. **CWE-125**: Out-of-bounds Read
|
22 |
+
4. **CWE-399**: Resource Management Errors
|
23 |
+
5. **CWE-200**: Information Exposure
|
24 |
+
6. **CWE-787**: Out-of-bounds Write
|
25 |
+
7. **CWE-264**: Permissions, Privileges, and Access Controls
|
26 |
+
8. **CWE-416**: Use After Free
|
27 |
+
9. **CWE-476**: NULL Pointer Dereference
|
28 |
+
10. **CWE-190**: Integer Overflow or Wraparound
|
29 |
+
11. **CWE-189**: Numeric Errors
|
30 |
+
12. **CWE-362**: Concurrent Execution using Shared Resource with Improper Synchronization
|
31 |
+
|
32 |
+
---
|
33 |
+
|
34 |
+
## Model Training
|
35 |
+
The model was trained for **3 epochs** with the following configuration:
|
36 |
+
- **Learning Rate**: 2e-5
|
37 |
+
- **Weight Decay**: 0.01
|
38 |
+
- **Batch Size**: 8
|
39 |
+
- **Optimizer**: AdamW
|
40 |
+
- **Scheduler**: Linear
|
41 |
+
|
42 |
+
### Training Loss and Validation Loss Per Epoch:
|
43 |
+
| Epoch | Training Loss | Validation Loss | Vul Accuracy | Vul Precision | Vul Recall | Vul F1 | CWE Accuracy |
|
44 |
+
|------|---------------|----------------|--------------|---------------|-----------|-------|---------------|
|
45 |
+
| 1 | 1.3732 | 1.2689 | 0.8220 | 0.8831 | 0.6231 | 0.7307| 0.4032 |
|
46 |
+
| 2 | 1.0318 | 1.1613 | 0.8229 | 0.8238 | 0.6907 | 0.7514| 0.4903 |
|
47 |
+
| 3 | 0.8192 | 1.1871 | 0.8158 | 0.7997 | 0.6999 | 0.7465| 0.5326 |
|
48 |
+
|
49 |
+
#### Training Summary:
|
50 |
+
- **Total Training Steps**: 2958
|
51 |
+
- **Training Loss**: 1.1267
|
52 |
+
- **Training Time**: 2687.8 seconds (~45 minutes)
|
53 |
+
- **Training Speed**: 17.6 samples per second
|
54 |
+
- **Steps Per Second**: 1.1
|
55 |
+
|
56 |
+
---
|
57 |
+
|
58 |
+
## Model Evaluation (Test Set Results)
|
59 |
+
The model was evaluated on the test set with the following metrics:
|
60 |
+
|
61 |
+
### Vulnerability Detection Metrics:
|
62 |
+
- **Accuracy**: 82.73%
|
63 |
+
- **Precision**: 82.15%
|
64 |
+
- **Recall**: 70.86%
|
65 |
+
- **F1-Score**: 76.09%
|
66 |
+
|
67 |
+
### CWE Classification Metrics:
|
68 |
+
- **Accuracy**: 51.46%
|
69 |
+
- **Precision**: 51.11%
|
70 |
+
- **Recall**: 51.46%
|
71 |
+
- **F1-Score**: 50.65%
|
72 |
+
|
73 |
+
---
|
74 |
+
|
75 |
+
## How to Use the Model
|
76 |
+
```python
|
77 |
+
from transformers import AutoModel, AutoTokenizer
|
78 |
+
|
79 |
+
model = AutoModel.from_pretrained("mahdin70/UnixCoder-VulnCWE", trust_remote_code=True)
|
80 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/unixcoder-base")
|
81 |
+
|
82 |
+
code_snippet = "int main() { int arr[10]; arr[11] = 5; return 0; }"
|
83 |
+
inputs = tokenizer(code_snippet, return_tensors="pt")
|
84 |
+
outputs = model(**inputs)
|
85 |
+
|
86 |
+
vul_logits = outputs["vul_logits"]
|
87 |
+
cwe_logits = outputs["cwe_logits"]
|
88 |
+
|
89 |
+
vul_pred = vul_logits.argmax(dim=1).item()
|
90 |
+
cwe_pred = cwe_logits.argmax(dim=1).item()
|
91 |
+
|
92 |
+
print(f"Vulnerability: {'Vulnerable' if vul_pred == 1 else 'Non-vulnerable'}")
|
93 |
+
print(f"CWE ID: {cwe_pred if vul_pred == 1 else 'N/A'}")
|
94 |
+
```
|
95 |
+
|
96 |
+
## Limitations and Future Improvements
|
97 |
+
- The model has limited accuracy on CWE classification (51.46%). Improving the model with advanced architectures or better data balancing could yield better results.
|
98 |
+
- The model might not perform well on edge cases or unseen CWEs.
|