anicolson commited on
Commit
677dccc
1 Parent(s): c3b7214

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +49 -93
README.md CHANGED
@@ -1,33 +1,34 @@
1
- ---
2
- language:
3
- - en
4
- license: apache-2.0
5
- library_name: transformers
6
- tags:
7
- - chest X-ray report generation
8
- - radiology report generation
9
- - image captioning
10
- - chest X-ray
11
- - X-ray
12
- - radiology
13
- - cxrmate
14
- - cxrmate-ed
15
- - report
16
- - radiology report
17
- - multimodal
18
- - patient data
19
- - patient records
20
- - mimic-cxr
21
- - mimic-iv-ed
22
- ---
23
-
24
- # CXRMate-ED: The Impact of Auxiliary Patient Data on Automated Chest X-Ray Report Generation and How to Incorporate It
 
25
 
26
  This is the model and data pipeline for the CXRMate-ED model from: https://arxiv.org/pdf/2406.13181.
27
 
28
  The abstract from the paper:
29
 
30
- "This study investigates the integration of diverse patient data sources into multimodal language models for automated chest X-ray (CXR) report generation. Traditionally, CXR report generation relies solely on CXR images and limited radiology data, overlooking valuable information from patient health records, particularly from emergency departments. Utilising the MIMIC-CXR and MIMIC-IV-ED datasets, we incorporate detailed patient information such as aperiodic vital signs, medications, and clinical history to enhance diagnostic accuracy. We introduce a novel approach to transform these heterogeneous data sources into embeddings that prompt a multimodal language model, significantly enhancing the diagnostic accuracy of generated radiology reports. Our comprehensive evaluation demonstrates the benefits of using a broader set of patient data, underscoring the potential for enhanced diagnostic capabilities and better patient outcomes through the integration of multimodal data in CXR report generation."
31
 
32
  ## MIMIC-CXR & MIMIC-IV-ED Dataset:
33
 
@@ -39,6 +40,7 @@ mimic-cxr mimic-cxr-jpg mimic-iv-ed
39
  ```
40
 
41
  ### Download MIMIC-CXR-JPG:
 
42
  Download the MIMIC-CXR-JPG dataset from https://physionet.org/content/mimic-cxr-jpg, e.g.,
43
  ```shell
44
  wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/mimic-cxr-jpg/2.1.0/
@@ -46,6 +48,7 @@ wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/m
46
  Note that you must be a credentialised user to access this dataset.
47
 
48
  ### Download the reports from MIMIC-CXR:
 
49
  MIMIC-CXR-JPG does not include the radiology reports and are instead included with MIMIC-CXR (the DICOM version of the dataset). To download this dataset and avoid downloading the DICOM files (which are very large), use `--reject dcm` with the wget command from https://physionet.org/content/mimic-cxr, e.g,
50
  ```shell
51
  wget -r -N -c -np --reject dcm --user <username> --ask-password https://physionet.org/files/mimic-cxr/2.0.0/
@@ -53,6 +56,7 @@ wget -r -N -c -np --reject dcm --user <username> --ask-password https://physione
53
  Note that you must be a credentialised user to access this dataset.
54
 
55
  ### Download MIMIC-IV-ED:
 
56
  Download the MIMIC-IV-ED dataset from https://physionet.org/content/mimic-iv-ed, e.g.,
57
  ```shell
58
  wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/mimic-iv-ed/2.2/
@@ -60,92 +64,45 @@ wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/m
60
  Note that you must be a credentialised user to access this dataset.
61
 
62
  ### Prepare the dataset:
 
63
  ```python
64
  import transformers
65
 
66
  # Paths:
67
  physionet_dir = '/.../physionet.org/files' # Where MIMIC-CXR, MIMIC-CXR-JPG, and MIMIC-IV-ED are stored.
68
- database_dir = '/.../database/cxrmate_ed' # The LMDB database for the JPGs and the DuckDB database for the tables will be saved here.
69
 
70
- # Prepare the MIMIC-CXR & MIMIC-IV-ED dataset:
71
  model = transformers.AutoModel.from_pretrained('aehrc/cxrmate-ed', trust_remote_code=True)
72
- model.prepare_data(
73
- physionet_dir=physionet_dir,
74
- database_dir=database_dir,
75
- )
76
  ```
77
- Note: dataset preperation should take roughly 2-3 hours.
78
-
79
 
80
  ## Generate a report
81
 
82
  ```python
83
  import torch
84
  import transformers
85
- from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
86
- from torch.utils.data import DataLoader
87
- from torchvision.transforms import v2
88
- import os
89
- import pprint
90
- import matplotlib.pyplot as plt
91
- from torchvision.utils import make_grid
92
 
93
  # Device and paths:
94
  device = 'cuda'
95
- physionet_dir = '/.../physionet.org/files' # Where MIMIC-CXR, MIMIC-CXR-JPG, and MIMIC-IV-ED are stored.
96
- database_dir = '/.../database/cxrmate_ed' # The LMDB database for the JPGs and the DuckDB database for the tables will be saved here.
97
 
98
  # Download model checkpoint:
99
- model = transformers.AutoModel.from_pretrained('aehrc/cxrmate-ed', trust_remote_code=True).to(device=device)
100
- model.eval()
101
-
102
- # Download tokenizer:
103
  tokenizer = transformers.PreTrainedTokenizerFast.from_pretrained('aehrc/cxrmate-ed')
104
- os.environ['TOKENIZERS_PARALLELISM'] = 'false'
105
-
106
- # Image transforms:
107
- image_size = 384
108
- test_transforms = v2.Compose(
109
- [
110
- v2.Grayscale(num_output_channels=3),
111
- v2.Resize(
112
- size=image_size,
113
- antialias=True,
114
- interpolation=v2.InterpolationMode.BICUBIC,
115
- ),
116
- v2.CenterCrop(size=[image_size, image_size]),
117
- v2.ToDtype(torch.float32, scale=True),
118
- v2.Normalize(mean=IMAGENET_DEFAULT_MEAN, std=IMAGENET_DEFAULT_STD),
119
- ]
120
- )
121
-
122
- # Prepare the MIMIC-CXR & MIMIC-IV-ED dataset:
123
- model.prepare_data(
124
- physionet_dir=physionet_dir,
125
- database_dir=database_dir,
126
- )
127
-
128
- # Get the test set dataset & dataloader:
129
- test_set = model.get_dataset(split='test', transforms=test_transforms, database_dir=database_dir)
130
- test_dataloader = DataLoader(
131
- test_set,
132
- batch_size=1,
133
- num_workers=5,
134
- shuffle=True,
135
- collate_fn=model.collate_fn,
136
- pin_memory=True,
137
- )
138
-
139
- # Get an example:
140
- batch = next(iter(test_dataloader))
141
-
142
- # Move tensors in the batch to the device:
143
- for key, value in batch.items():
144
- if isinstance(value, torch.Tensor):
145
- batch[key] = value.to(device)
146
 
147
  # Convert the patient data in the batch into embeddings:
148
- inputs_embeds, attention_mask, token_type_ids, position_ids, bos_token_ids = model.prepare_inputs(tokenizer=tokenizer, **batch)
149
 
150
  # Generate reports:
151
  output_ids = model.generate(
@@ -155,7 +112,6 @@ output_ids = model.generate(
155
  prompt_attention_mask=attention_mask,
156
  prompt_position_ids=position_ids,
157
  special_token_ids=[tokenizer.sep_token_id],
158
- token_type_id_sections=model.decoder.config.section_ids,
159
  max_length=256,
160
  num_beams=4,
161
  return_dict_in_generate=True,
@@ -165,13 +121,13 @@ output_ids = model.generate(
165
  findings, impression = model.split_and_decode_sections(output_ids, [tokenizer.sep_token_id, tokenizer.eos_token_id], tokenizer)
166
  for i,j in zip(findings, impression):
167
  print(f'Findings:\t{i}\nImpression:\t{j}\n\n')
168
-
169
  ```
170
 
171
  # Environment requirements
172
 
173
- Environment requirements can be found here: https://github.com/aehrc/anon/blob/main/requirements.txt.
174
 
175
 
176
  # Code repository
177
- The code repository, which includes the training pipeline for CXRMate-ED, is available at: https://github.com/aehrc/anon.
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ license: apache-2.0
5
+ library_name: transformers
6
+ tags:
7
+ - chest X-ray report generation
8
+ - radiology report generation
9
+ - image captioning
10
+ - chest X-ray
11
+ - X-ray
12
+ - radiology
13
+ - cxrmate
14
+ - cxrmate-ed
15
+ - report
16
+ - radiology report
17
+ - multimodal
18
+ - patient data
19
+ - patient records
20
+ - mimic-cxr
21
+ - mimic-iv-ed
22
+ pipeline_tag: image-text-to-text
23
+ ---
24
+
25
+ # **CXRMate-ED**: The Impact of Auxiliary Patient Data on Automated Chest X-Ray Report Generation and How to Incorporate It
26
 
27
  This is the model and data pipeline for the CXRMate-ED model from: https://arxiv.org/pdf/2406.13181.
28
 
29
  The abstract from the paper:
30
 
31
+ "This study investigates the integration of diverse patient data sources into multimodal language models for automated chest X-ray (CXR) report generation. Traditionally, CXR report generation relies solely on CXR images and limited radiology data, overlooking valuable information from patient health records, particularly from emergency departments. Utilising the MIMIC-CXR and MIMIC-IV-ED datasets, we incorporate detailed patient information such as vital signs, medicines, and clinical history to enhance diagnostic accuracy. We introduce a novel approach to transform these heterogeneous data sources into embeddings that prompt a multimodal language model; this significantly enhances the diagnostic accuracy of generated radiology reports. Our comprehensive evaluation demonstrates the benefits of using a broader set of patient data, underscoring the potential for enhanced diagnostic capabilities and better patient outcomes through the integration of multimodal data in CXR report generation."
32
 
33
  ## MIMIC-CXR & MIMIC-IV-ED Dataset:
34
 
 
40
  ```
41
 
42
  ### Download MIMIC-CXR-JPG:
43
+
44
  Download the MIMIC-CXR-JPG dataset from https://physionet.org/content/mimic-cxr-jpg, e.g.,
45
  ```shell
46
  wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/mimic-cxr-jpg/2.1.0/
 
48
  Note that you must be a credentialised user to access this dataset.
49
 
50
  ### Download the reports from MIMIC-CXR:
51
+
52
  MIMIC-CXR-JPG does not include the radiology reports and are instead included with MIMIC-CXR (the DICOM version of the dataset). To download this dataset and avoid downloading the DICOM files (which are very large), use `--reject dcm` with the wget command from https://physionet.org/content/mimic-cxr, e.g,
53
  ```shell
54
  wget -r -N -c -np --reject dcm --user <username> --ask-password https://physionet.org/files/mimic-cxr/2.0.0/
 
56
  Note that you must be a credentialised user to access this dataset.
57
 
58
  ### Download MIMIC-IV-ED:
59
+
60
  Download the MIMIC-IV-ED dataset from https://physionet.org/content/mimic-iv-ed, e.g.,
61
  ```shell
62
  wget -r -N -c -np --user <username> --ask-password https://physionet.org/files/mimic-iv-ed/2.2/
 
64
  Note that you must be a credentialised user to access this dataset.
65
 
66
  ### Prepare the dataset:
67
+
68
  ```python
69
  import transformers
70
 
71
  # Paths:
72
  physionet_dir = '/.../physionet.org/files' # Where MIMIC-CXR, MIMIC-CXR-JPG, and MIMIC-IV-ED are stored.
73
+ database_dir = '/.../database/cxrmate_ed' # The Hugging Face dataset will be saved here.
74
 
75
+ # Prepare the Hugging Face MIMIC-CXR & MIMIC-IV-ED dataset:
76
  model = transformers.AutoModel.from_pretrained('aehrc/cxrmate-ed', trust_remote_code=True)
77
+ model.prepare_data(physionet_dir=physionet_dir, database_dir=database_dir)
 
 
 
78
  ```
 
 
79
 
80
  ## Generate a report
81
 
82
  ```python
83
  import torch
84
  import transformers
 
 
 
 
 
 
 
85
 
86
  # Device and paths:
87
  device = 'cuda'
 
 
88
 
89
  # Download model checkpoint:
90
+ model = transformers.AutoModelForCausalLM.from_pretrained('aehrc/cxrmate-ed', trust_remote_code=True).to(device=device)
 
 
 
91
  tokenizer = transformers.PreTrainedTokenizerFast.from_pretrained('aehrc/cxrmate-ed')
92
+
93
+ # Get the Hugging Face MIMIC-CXR & MIMIC-IV-ED test set:
94
+ test_set = model.get_dataset(database_dir=database_dir, test_set_only=True)
95
+
96
+ # Get an example, add mini-batch dimension and move to device:
97
+ example = test_set[0]
98
+ for k, v in example.items():
99
+ if isinstance(v, torch.Tensor):
100
+ example[k] = v.to(device).unsqueeze(0)
101
+ else:
102
+ example[k] = [v]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  # Convert the patient data in the batch into embeddings:
105
+ inputs_embeds, attention_mask, token_type_ids, position_ids, bos_token_ids = model.prepare_inputs(tokenizer=tokenizer, **example)
106
 
107
  # Generate reports:
108
  output_ids = model.generate(
 
112
  prompt_attention_mask=attention_mask,
113
  prompt_position_ids=position_ids,
114
  special_token_ids=[tokenizer.sep_token_id],
 
115
  max_length=256,
116
  num_beams=4,
117
  return_dict_in_generate=True,
 
121
  findings, impression = model.split_and_decode_sections(output_ids, [tokenizer.sep_token_id, tokenizer.eos_token_id], tokenizer)
122
  for i,j in zip(findings, impression):
123
  print(f'Findings:\t{i}\nImpression:\t{j}\n\n')
 
124
  ```
125
 
126
  # Environment requirements
127
 
128
+ Environment requirements can be found here: https://github.com/aehrc/cxrmate-ed/blob/main/requirements.txt.
129
 
130
 
131
  # Code repository
132
+
133
+ The code repository, which includes the training pipeline for CXRMate-ED, is available at: https://github.com/aehrc/cxrmate-ed.