Update README.md
Browse files
README.md
CHANGED
|
@@ -1,4 +1,57 @@
|
|
| 1 |
-
Usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
Label Mapping:
|
| 4 |
```
|
|
|
|
| 1 |
+
Usage:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
import json
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 7 |
+
from datasets import load_dataset
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
# Configuration
|
| 11 |
+
MODEL_PATH = "StanfordAIMI/SRR-BERT-Upper"
|
| 12 |
+
MAPPING_URL = "https://raw.githubusercontent.com/jbdel/StructEval/refs/heads/main/structeval/upper_mapping.json"
|
| 13 |
+
MAX_LENGTH = 128
|
| 14 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
+
|
| 16 |
+
# Fetch mapping from GitHub
|
| 17 |
+
resp = requests.get(MAPPING_URL)
|
| 18 |
+
resp.raise_for_status()
|
| 19 |
+
label_map = resp.json()
|
| 20 |
+
idx2label = {v: k for k, v in label_map.items()}
|
| 21 |
+
|
| 22 |
+
# Load tokenizer & model
|
| 23 |
+
tokenizer = BertTokenizer.from_pretrained("microsoft/BiomedVLP-CXR-BERT-general")
|
| 24 |
+
model = BertForSequenceClassification.from_pretrained(MODEL_PATH, num_labels=len(label_map))
|
| 25 |
+
model.to(DEVICE).eval()
|
| 26 |
+
|
| 27 |
+
# Grab one test sentence
|
| 28 |
+
dataset = load_dataset("StanfordAIMI/StructUtterances", split="test_reviewed")
|
| 29 |
+
sentence = dataset[35]["utterance"]
|
| 30 |
+
|
| 31 |
+
# Tokenize and infer
|
| 32 |
+
inputs = tokenizer(
|
| 33 |
+
sentence,
|
| 34 |
+
padding="max_length",
|
| 35 |
+
truncation=True,
|
| 36 |
+
max_length=MAX_LENGTH,
|
| 37 |
+
return_tensors="pt"
|
| 38 |
+
).to(DEVICE)
|
| 39 |
+
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
logits = model(**inputs).logits
|
| 42 |
+
preds = (torch.sigmoid(logits)[0].cpu().numpy() > 0.5).astype(int)
|
| 43 |
+
|
| 44 |
+
pred_labels = [idx2label[i] for i, flag in enumerate(preds) if flag]
|
| 45 |
+
|
| 46 |
+
print(f"Sentence: {sentence}")
|
| 47 |
+
print("Predicted labels:", pred_labels)
|
| 48 |
+
```
|
| 49 |
+
Output:
|
| 50 |
+
```
|
| 51 |
+
Sentence: Patchy consolidation in the left retrocardiac area, suggestive of atelectasis or early airspace disease.
|
| 52 |
+
Predicted labels: ['Consolidation', 'Air space opacity']
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
|
| 56 |
Label Mapping:
|
| 57 |
```
|