Instructions to use ianpan/mri-brain-sequence-bert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ianpan/mri-brain-sequence-bert with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-feature-extraction", model="ianpan/mri-brain-sequence-bert", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ianpan/mri-brain-sequence-bert", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Upload MRIBrainSequenceBERT
Browse files- README.md +199 -0
- config.json +15 -0
- configuration.py +11 -0
- modeling.py +144 -0
README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|
config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"MRIBrainSequenceBERT"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration.MRIBrainSequenceBERTConfig",
|
| 7 |
+
"AutoModel": "modeling.MRIBrainSequenceBERT"
|
| 8 |
+
},
|
| 9 |
+
"dropout": 0.2,
|
| 10 |
+
"max_len": 512,
|
| 11 |
+
"model_type": "mri_brain_sequence_bert",
|
| 12 |
+
"num_classes": 16,
|
| 13 |
+
"torch_dtype": "float32",
|
| 14 |
+
"transformers_version": "4.47.0"
|
| 15 |
+
}
|
configuration.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class MRIBrainSequenceBERTConfig(PretrainedConfig):
|
| 5 |
+
model_type = "mri_brain_sequence_bert"
|
| 6 |
+
|
| 7 |
+
def __init__(self, max_len=512, dropout=0.2, num_classes=16, **kwargs):
|
| 8 |
+
self.max_len = max_len
|
| 9 |
+
self.dropout = dropout
|
| 10 |
+
self.num_classes = num_classes
|
| 11 |
+
super().__init__(**kwargs)
|
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utoModelForSequenceClassification,
|
| 9 |
+
PreTrainedModel,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
from .configuration import MRIBrainSequenceBERTConfig
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class MRIBrainSequenceBERT(PreTrainedModel):
|
| 16 |
+
config_class = MRIBrainSequenceBERTConfig
|
| 17 |
+
|
| 18 |
+
def __init__(self, config):
|
| 19 |
+
super().__init__(config)
|
| 20 |
+
self.llm = AutoModelForSequenceClassification.from_pretrained(
|
| 21 |
+
"google/mobilebert-uncased"
|
| 22 |
+
)
|
| 23 |
+
self.dim_feats = self.llm.classifier.in_features
|
| 24 |
+
self.dropout = nn.Dropout(p=config.dropout)
|
| 25 |
+
self.classifier = nn.Linear(self.dim_feats, config.num_classes)
|
| 26 |
+
self.llm.dropout = nn.Identity()
|
| 27 |
+
self.llm.classifier = nn.Identity()
|
| 28 |
+
|
| 29 |
+
self.tokenizer = AutoTokenizer.from_pretrained("google/mobilebert-uncased")
|
| 30 |
+
self.max_len = config.max_len
|
| 31 |
+
|
| 32 |
+
self.metadata_elements = [
|
| 33 |
+
"SeriesDescription",
|
| 34 |
+
"ImageType",
|
| 35 |
+
"Manufacturer",
|
| 36 |
+
"ManufacturerModelName",
|
| 37 |
+
"ContrastBolusAgent",
|
| 38 |
+
"ScanningSequence",
|
| 39 |
+
"SequenceVariant",
|
| 40 |
+
"ScanOptions",
|
| 41 |
+
"MRAcquisitionType",
|
| 42 |
+
"SequenceName",
|
| 43 |
+
"AngioFlag",
|
| 44 |
+
"SliceThickness",
|
| 45 |
+
"RepetitionTime",
|
| 46 |
+
"EchoTime",
|
| 47 |
+
"InversionTime",
|
| 48 |
+
"NumberOfAverages",
|
| 49 |
+
"ImagingFrequency",
|
| 50 |
+
"ImagedNucleus",
|
| 51 |
+
"EchoNumbers",
|
| 52 |
+
"SpacingBetweenSlices",
|
| 53 |
+
"NumberOfPhaseEncodingSteps",
|
| 54 |
+
"EchoTrainLength",
|
| 55 |
+
"PercentSampling",
|
| 56 |
+
"PercentPhaseFieldOfView",
|
| 57 |
+
"PixelBandwidth",
|
| 58 |
+
"ContrastBolusVolume",
|
| 59 |
+
"ContrastBolusTotalDose",
|
| 60 |
+
"AcquisitionMatrix",
|
| 61 |
+
"InPlanePhaseEncodingDirection",
|
| 62 |
+
"FlipAngle",
|
| 63 |
+
"VariableFlipAngleFlag",
|
| 64 |
+
"SAR",
|
| 65 |
+
"dBdt",
|
| 66 |
+
"SeriesNumber",
|
| 67 |
+
"AcquisitionNumber",
|
| 68 |
+
"PhotometricInterpretation",
|
| 69 |
+
"PixelSpacing",
|
| 70 |
+
"ImagesInAcquisition",
|
| 71 |
+
"SmallestImagePixelValue",
|
| 72 |
+
"LargestImagePixelValue",
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
self.label2index = {
|
| 76 |
+
"t1": 0, # T1 precontrast
|
| 77 |
+
"t1c": 1, # T1 postcontrast
|
| 78 |
+
"t2": 2, # T2
|
| 79 |
+
"flair": 3, # T2-FLAIR
|
| 80 |
+
"dwi": 4, # DWI trace
|
| 81 |
+
"adc": 5, # ADC map
|
| 82 |
+
"dti": 6, # DTI
|
| 83 |
+
"swi": 7, # SWI
|
| 84 |
+
"swi_mip": 8, # SWI MinIP
|
| 85 |
+
"phase": 9, # SWI phase images
|
| 86 |
+
"mag": 10, # SWI mag images
|
| 87 |
+
"gre": 11, # T2* GRE
|
| 88 |
+
"perf": 12, # Perfusion-related images
|
| 89 |
+
"pd": 13, # Proton density
|
| 90 |
+
"loc": 14, # Localizers
|
| 91 |
+
"other": 15, # Other, NOS
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
self.index2label = {v: k for k, v in self.label2index.items()}
|
| 95 |
+
|
| 96 |
+
def forward(
|
| 97 |
+
self, x: str, device: str | torch.device = "cpu", apply_softmax: bool = True
|
| 98 |
+
):
|
| 99 |
+
x = self.tokenizer(
|
| 100 |
+
x,
|
| 101 |
+
return_tensors="pt",
|
| 102 |
+
padding="max_length",
|
| 103 |
+
truncation=True,
|
| 104 |
+
max_length=self.max_len,
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
for k, v in x.items():
|
| 108 |
+
x[k] = v.to(device)
|
| 109 |
+
|
| 110 |
+
features = self.llm(**x)["logits"]
|
| 111 |
+
logits = self.classifier(self.dropout(features))
|
| 112 |
+
if apply_softmax:
|
| 113 |
+
logits = torch.softmax(logits, dim=1)
|
| 114 |
+
return logits
|
| 115 |
+
|
| 116 |
+
def create_string_from_dicom(
|
| 117 |
+
self, ds: pydicom.Dataset | dict, exclude_elements: list[str] = []
|
| 118 |
+
):
|
| 119 |
+
# Sometimes we may want to exclude specific elements from being used for prediction
|
| 120 |
+
x = []
|
| 121 |
+
for each_element in self.metadata_elements:
|
| 122 |
+
# Only include elements which are present
|
| 123 |
+
if each_element in ds and each_element not in exclude_elements:
|
| 124 |
+
if ds[each_element] is not None and str(ds[each_element]) != "nan":
|
| 125 |
+
x.append(f"{each_element} {ds[each_element]}")
|
| 126 |
+
|
| 127 |
+
x = " | ".join(x)
|
| 128 |
+
x = x.replace("[", "").replace("]", "").replace(",", "").replace("'", "")
|
| 129 |
+
return x
|
| 130 |
+
|
| 131 |
+
@staticmethod
|
| 132 |
+
def determine_plane_from_dicom(ds: pydicom.Dataset):
|
| 133 |
+
iop = np.asarray(ds.ImageOrientationPatient)
|
| 134 |
+
# Calculate the direction cosine for the normal vector of the plane
|
| 135 |
+
normal_vector = np.cross(iop[:3], iop[3:])
|
| 136 |
+
|
| 137 |
+
# Determine the plane based on the largest component of the normal vector
|
| 138 |
+
abs_normal = np.abs(normal_vector)
|
| 139 |
+
if abs_normal[0] > abs_normal[1] and abs_normal[0] > abs_normal[2]:
|
| 140 |
+
return "SAG"
|
| 141 |
+
elif abs_normal[1] > abs_normal[0] and abs_normal[1] > abs_normal[2]:
|
| 142 |
+
return "COR"
|
| 143 |
+
else:
|
| 144 |
+
return "AX"
|