mansour94 commited on
Commit
214520e
·
verified ·
1 Parent(s): 6853797

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +172 -192
README.md CHANGED
@@ -1,199 +1,179 @@
1
  ---
2
- library_name: transformers
3
- tags: []
 
 
 
 
 
 
 
 
4
  ---
5
 
6
- # Model Card for Model ID
7
-
8
- <!-- Provide a quick summary of what the model is/does. -->
9
-
10
 
 
11
 
12
  ## Model Details
13
 
14
- ### Model Description
15
-
16
- <!-- Provide a longer summary of what this model is. -->
17
-
18
- This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
-
20
- - **Developed by:** [More Information Needed]
21
- - **Funded by [optional]:** [More Information Needed]
22
- - **Shared by [optional]:** [More Information Needed]
23
- - **Model type:** [More Information Needed]
24
- - **Language(s) (NLP):** [More Information Needed]
25
- - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
-
28
- ### Model Sources [optional]
29
-
30
- <!-- Provide the basic links for the model. -->
31
-
32
- - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
- - **Demo [optional]:** [More Information Needed]
35
-
36
- ## Uses
37
-
38
- <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
-
40
- ### Direct Use
41
-
42
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
-
44
- [More Information Needed]
45
-
46
- ### Downstream Use [optional]
47
-
48
- <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
-
50
- [More Information Needed]
51
-
52
- ### Out-of-Scope Use
53
-
54
- <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
-
56
- [More Information Needed]
57
-
58
- ## Bias, Risks, and Limitations
59
-
60
- <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
-
62
- [More Information Needed]
63
-
64
- ### Recommendations
65
-
66
- <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
-
68
- Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
-
70
- ## How to Get Started with the Model
71
-
72
- Use the code below to get started with the model.
73
-
74
- [More Information Needed]
75
-
76
- ## Training Details
77
-
78
- ### Training Data
79
-
80
- <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
-
82
- [More Information Needed]
83
-
84
- ### Training Procedure
85
-
86
- <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
-
88
- #### Preprocessing [optional]
89
-
90
- [More Information Needed]
91
-
92
-
93
- #### Training Hyperparameters
94
-
95
- - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
-
97
- #### Speeds, Sizes, Times [optional]
98
-
99
- <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
-
101
- [More Information Needed]
102
-
103
- ## Evaluation
104
-
105
- <!-- This section describes the evaluation protocols and provides the results. -->
106
-
107
- ### Testing Data, Factors & Metrics
108
-
109
- #### Testing Data
110
-
111
- <!-- This should link to a Dataset Card if possible. -->
112
-
113
- [More Information Needed]
114
-
115
- #### Factors
116
-
117
- <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
-
119
- [More Information Needed]
120
-
121
- #### Metrics
122
-
123
- <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
-
125
- [More Information Needed]
126
-
127
- ### Results
128
-
129
- [More Information Needed]
130
-
131
- #### Summary
132
-
133
-
134
-
135
- ## Model Examination [optional]
136
-
137
- <!-- Relevant interpretability work for the model goes here -->
138
-
139
- [More Information Needed]
140
-
141
- ## Environmental Impact
142
-
143
- <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
-
145
- Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
-
147
- - **Hardware Type:** [More Information Needed]
148
- - **Hours used:** [More Information Needed]
149
- - **Cloud Provider:** [More Information Needed]
150
- - **Compute Region:** [More Information Needed]
151
- - **Carbon Emitted:** [More Information Needed]
152
-
153
- ## Technical Specifications [optional]
154
-
155
- ### Model Architecture and Objective
156
-
157
- [More Information Needed]
158
-
159
- ### Compute Infrastructure
160
-
161
- [More Information Needed]
162
-
163
- #### Hardware
164
-
165
- [More Information Needed]
166
-
167
- #### Software
168
-
169
- [More Information Needed]
170
-
171
- ## Citation [optional]
172
-
173
- <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
-
175
- **BibTeX:**
176
-
177
- [More Information Needed]
178
-
179
- **APA:**
180
-
181
- [More Information Needed]
182
-
183
- ## Glossary [optional]
184
-
185
- <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
-
187
- [More Information Needed]
188
-
189
- ## More Information [optional]
190
-
191
- [More Information Needed]
192
-
193
- ## Model Card Authors [optional]
194
-
195
- [More Information Needed]
196
-
197
- ## Model Card Contact
198
-
199
- [More Information Needed]
 
1
  ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ tags:
6
+ - text-classification
7
+ - medical
8
+ - nhs
9
+ - clinical-letters
10
+ - distilbert
11
+ pipeline_tag: text-classification
12
  ---
13
 
14
+ # NHS Medical Letter Classifier
 
 
 
15
 
16
+ Fine-tuned **DistilBERT** (`distilbert-base-uncased`) for classifying OCR'd NHS medical clinic letters into 49 letter type categories.
17
 
18
  ## Model Details
19
 
20
+ | Parameter | Value |
21
+ |---|---|
22
+ | Base model | `distilbert-base-uncased` |
23
+ | Training samples | 13,672 |
24
+ | Classes | 49 |
25
+ | Epochs | 6 |
26
+ | Batch size | 16 |
27
+ | Learning rate | 2e-5 |
28
+ | Max sequence length | 512 tokens |
29
+ | Cleanlab corrections | 212 labels relabeled (1.6% of dataset) |
30
+
31
+ ## How We Got Here: Experiment Journey
32
+
33
+ ### 1. Baseline: TF-IDF + LinearSVC
34
+ - **Approach:** TfidfVectorizer (unigram+bigram, 50k features) with CalibratedClassifierCV(LinearSVC)
35
+ - **Result:** ~91% accuracy on the original label set
36
+ - **Takeaway:** Strong baseline, but limited by bag-of-words representation
37
+
38
+ ### 2. Label Merging (Critical Improvement)
39
+ - **Approach:** Consolidated synonymous labels (e.g., "Nephrology" to "Renal", "Minor Illness Consultation" to "Pharmacy") and dropped ambiguous/administrative labels
40
+ - **Result:** Accuracy jumped from ~91% to ~96%
41
+ - **Takeaway:** Label quality matters more than model architecture. Reduced label set from ~51 to 49 meaningful categories
42
+
43
+ ### 3. DistilBERT Baseline (Our Core Model)
44
+ - **Approach:** Fine-tuned `distilbert-base-uncased`, 4 epochs, 512 tokens, 70/10/20 stratified split
45
+ - **Result:** Top-1: 95.76% | Top-3: 98.06% | Top-5: 98.61%
46
+ - **Takeaway:** Strong performance, established as the baseline for all further experiments
47
+
48
+ ### 4. ClinicalBERT & BioClinicalBERT
49
+ - **Approach:** Tested domain-specific models (`medicalai/ClinicalBERT`, `emilyalsentzer/Bio_ClinicalBERT`)
50
+ - **Result:** Similar to DistilBERT (~95-96%), no meaningful improvement
51
+ - **Takeaway:** General-purpose DistilBERT captures enough for this task; domain pre-training didn't help
52
+
53
+ ### 5. Longformer (1024 tokens)
54
+ - **Approach:** `allenai/longformer-base-4096` at 1024 tokens with global attention on CLS, case-sensitive
55
+ - **Result:** Comparable to DistilBERT at 512 tokens
56
+ - **Takeaway:** Most discriminative information is in the first 512 tokens; longer context doesn't help
57
+
58
+ ### 6. Hierarchical Architecture
59
+ - **Approach:** Two-stage: DistilBERT body for CLS embeddings, per-clinic LogisticRegression heads. 51 fine labels mapped to 25 broad categories
60
+ - **Result:** Did not outperform flat DistilBERT
61
+ - **Takeaway:** The flat classification space works well; hierarchical routing adds complexity without benefit
62
+
63
+ ### 7. LLM Relabeling (GPT-5-mini)
64
+ - **Approach:** Used OpenAI Batch API to get GPT-5-mini to reclassify all 13,672 samples. Trained DistilBERT on LLM-assigned labels
65
+ - **Result:** 86.22% vs original labels | 93.24% vs LLM labels (Top-1)
66
+ - **Takeaway:** LLM agrees with original labels ~85.7% of the time. LLM labels are different but not better — the original clinical labels carry domain knowledge the LLM lacks
67
+
68
+ ### 8. Consensus Relabeling
69
+ - **Approach:** Only change labels where both BERT and GPT-5-mini agree the original label is wrong
70
+ - **Result:** Only 4 out of 9,569 samples met the consensus criteria
71
+ - **Takeaway:** BERT memorizes its training labels, so it almost never disagrees with originals on training data. Consensus is too strict
72
+
73
+ ### 9. Soft Knowledge Distillation
74
+ - **Approach:** Got GPT-5-mini top-5 predictions with confidence scores as soft labels. Trained with blended loss: alpha * CE(hard) + (1-alpha) * KL(soft || student), alpha=0.5
75
+ - **Result:** Top-1: 95.32% (-0.44pp) | Top-3: 97.48% (-0.58pp)
76
+ - **Takeaway:** LLM self-reported confidence scores are too noisy/uniform. Soft KL loss stayed flat at ~3.5. Would need actual logprobs for this to work
77
+
78
+ ### 10. Cleanlab: Remove Mislabeled Samples
79
+ - **Approach:** Confident learning (Northcutt et al. 2021). 3-fold cross-validation for out-of-sample probabilities, then `find_label_issues()` to detect mislabeled samples. Removed 142 flagged training samples and retrained
80
+ - **Result:** Top-1: 95.90% (+0.14pp) | Top-3: 97.70% (-0.36pp)
81
+ - **Takeaway:** Small top-1 gain, but removing ambiguous samples hurt ranked predictions. Manual inspection confirmed ~99% of flagged samples were genuinely mislabeled
82
+
83
+ ### 11. Cleanlab: Relabel Instead of Remove
84
+ - **Approach:** Same cleanlab detection, but replaced wrong labels with model's predicted label instead of removing samples
85
+ - **Result (vs original test labels):** Top-1: 95.80% | Top-3: 97.92% | Top-5: 98.46%
86
+ - **Result (vs corrected test labels):** Top-1: 98.06% | Top-3: 99.09% | Top-5: 99.38%
87
+ - **Takeaway:** The ~2pp gap between original and corrected evaluation reveals that the remaining "errors" are mostly test set noise, not model mistakes. True model performance is ~98% top-1
88
+
89
+ ### 12. Production Model (This Model)
90
+ - **Approach:** Fresh 3-fold cleanlab on the **entire** dataset (13,672 samples). Found 212 mislabeled samples (1.6%), relabeled all. Trained on full corrected dataset for 6 epochs
91
+ - **Sanity check:** 99.74% accuracy on training data (expected, since model saw all data)
92
+ - **Estimated true accuracy:** ~98% top-1, ~99% top-3 based on corrected-label evaluation
93
+
94
+ ## Key Findings
95
+
96
+ 1. **Label quality > model architecture.** Label merging (+5pp) and cleanlab corrections (+2pp true accuracy) had more impact than any model change
97
+ 2. **DistilBERT is sufficient.** Domain-specific models (ClinicalBERT, BioClinicalBERT) and longer context (Longformer) didn't help
98
+ 3. **~1.6% of labels are wrong.** Discharge summary (9.1%), Paediatrics (7.2%), and Physiotherapy (6.8%) are the noisiest classes
99
+ 4. **The model is better than naive metrics suggest.** When evaluated against corrected labels, top-1 jumps from ~96% to ~98%
100
+
101
+ ## Labels (49 classes)
102
+
103
+ - `A&E`
104
+ - `Ambulance Notification`
105
+ - `Audiology`
106
+ - `Bowel Cancer Screening`
107
+ - `Breast Clinic`
108
+ - `Cancer Screening`
109
+ - `Cardiology`
110
+ - `Colposcopy`
111
+ - `Dermatology`
112
+ - `Diabetes & Endocrine`
113
+ - `Diet Services`
114
+ - `Discharge summary`
115
+ - `ENT`
116
+ - `Echocardiogram`
117
+ - `Elderly Care`
118
+ - `Gastroenterology`
119
+ - `General Surgery`
120
+ - `Genetics`
121
+ - `Haematology`
122
+ - `INR`
123
+ - `Immunology`
124
+ - `Mammogram`
125
+ - `Maternity`
126
+ - `Maxillofacial`
127
+ - `Mental Health`
128
+ - `Neurology`
129
+ - `Neurosurgery`
130
+ - `Obstetrics & Gynaecology`
131
+ - `Oncology`
132
+ - `Ophthalmology`
133
+ - `Orthopaedics`
134
+ - `Out of Hours`
135
+ - `Paediatrics`
136
+ - `Pain Management`
137
+ - `Pharmacy`
138
+ - `Physiotherapy`
139
+ - `Plastic Surgery`
140
+ - `Radiology`
141
+ - `Renal`
142
+ - `Respiratory`
143
+ - `Retinal Screening`
144
+ - `Rheumatology`
145
+ - `Sexual Health`
146
+ - `Speech and Language Therapy`
147
+ - `Stroke Services`
148
+ - `Urgent Care Centre`
149
+ - `Urology`
150
+ - `Vascular`
151
+ - `Walk in Centre`
152
+
153
+ ## Usage
154
+
155
+ ```python
156
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
157
+ import torch, json
158
+
159
+ model = AutoModelForSequenceClassification.from_pretrained("mansour94/kynoby-william-bert-classifier")
160
+ tokenizer = AutoTokenizer.from_pretrained("mansour94/kynoby-william-bert-classifier")
161
+
162
+ # Load label map
163
+ from huggingface_hub import hf_hub_download
164
+ label_map = json.load(open(hf_hub_download("mansour94/kynoby-william-bert-classifier", "label_map.json")))
165
+ id2label = {int(k): v for k, v in label_map["id2label"].items()}
166
+
167
+ text = "Dear Dr Smith, I am writing to inform you about the patient's ophthalmology appointment..."
168
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
169
+ with torch.no_grad():
170
+ logits = model(**inputs).logits
171
+ probs = torch.softmax(logits, dim=-1)
172
+
173
+ # Top-3 predictions
174
+ top3 = torch.topk(probs, 3)
175
+ for i in range(3):
176
+ idx = top3.indices[0][i].item()
177
+ conf = top3.values[0][i].item()
178
+ print(f" {id2label[idx]}: {conf:.1%}")
179
+ ```