CoolFace
Modelpublic

Angelakeke/RaTE-NER-Deberta

sourceHugging Facemitupdated 2y agoView on Hugging Face
6likes14kdownloads
Model Card

RaTE-NER-Deberta

This model is a fine-tuned version of DeBERTa on the RaTE-NER dataset.

Model description

This model is trained to serve the RaTEScore metric, if you are interested in our pipeline, please refer to our paper and Github.

This model also can be used to extract Abnormality, Non-Abnormality, Anatomy, Disease, Non-Disease in medical radiology reports.

Usage

<details> <summary> Click to expand the usage of this model. </summary> <pre><code> from transformers import AutoTokenizer, AutoModelForTokenClassification import torch def postprocess(tokenizedtext, predictedentities, tokenizer): entityspans = [] start = end = None entitytype = None for i, (token, label) in enumerate(zip(tokenizedtext, predictedentities[:len(tokenizedtext)])): if token in ["[CLS]", "[SEP]"]: continue if label != "O" and i < len(predictedentities) - 1: if label.startswith("B-") and predictedentities[i+1].startswith("I-"): start = i entitytype = label[2:] elif label.startswith("B-") and predictedentities[i+1].startswith("B-"): start = i end = i entityspans.append((start, end, label[2:])) start = i entitytype = label[2:] elif label.startswith("B-") and predictedentities[i+1].startswith("O"): start = i end = i entityspans.append((start, end, label[2:])) start = end = None entitytype = None elif label.startswith("I-") and predictedentities[i+1].startswith("B-"): end = i if start is not None: entityspans.append((start, end, entitytype)) start = i entitytype = label[2:] elif label.startswith("I-") and predictedentities[i+1].startswith("O"): end = i if start is not None: entityspans.append((start, end, entitytype)) start = end = None entitytype = None if start is not None and end is None: end = len(tokenizedtext) - 2 entityspans.append((start, end, entitytype)) savepair = [] for start, end, entitytype in entityspans: entitystr = tokenizer.converttokenstostring(tokenizedtext[start:end+1]) savepair.append((entitystr, entitytype)) return savepair

def runner(texts, idx2label, tokenizer, model, device): inputs = tokenizer(texts, maxlength=512, padding=True, truncation=True, returntensors="pt").to(device) with torch.nograd(): outputs = model(**inputs) predictedlabels = torch.argmax(outputs.logits, dim=2).tolist() savepairs = [] for i in range(len(texts)): predictedentities = [idx2label[label] for label in predictedlabels[i]] nonpadmask = inputs["inputids"][i] != tokenizer.padtokenid nonpadlength = nonpadmask.sum().item() nonpadinputids = inputs["inputids"][i][:nonpadlength] tokenizedtext = tokenizer.convertidstotokens(nonpadinputids) savepair = postprocess(tokenizedtext, predictedentities, tokenizer) if i == 0: savepairs = savepair else: savepairs.extend(savepair) return save_pairs

nerlabels = ['B-ABNORMALITY', 'I-ABNORMALITY', 'B-NON-ABNORMALITY', 'I-NON-ABNORMALITY', 'B-DISEASE', 'I-DISEASE', 'B-NON-DISEASE', 'I-NON-DISEASE', 'B-ANATOMY', 'I-ANATOMY', 'O'] idx2label = {i: label for i, label in enumerate(nerlabels)}

tokenizer = AutoTokenizer.frompretrained('Angelakeke/RaTE-NER-Deberta') model = AutoModelForTokenClassification.frompretrained('Angelakeke/RaTE-NER-Deberta')

device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) model.eval()

We recommend to inference by sentences.

text = ""

texts = text.split('. ') savepair = runner(texts, idx2label, tokenizer, model, device)

</code></pre>

</details>

Author

Author: Weike Zhao

If you have any questions, please feel free to contact zwk0629@sjtu.edu.cn.

Citation

bibtex
@inproceedings{zhao2024ratescore,
  title={RaTEScore: A Metric for Radiology Report Generation},
  author={Zhao, Weike and Wu, Chaoyi and Zhang, Xiaoman and Zhang, Ya and Wang, Yanfeng and Xie, Weidi},
  booktitle={Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing},
  pages={15004--15019},
  year={2024}
}