File size: 8,826 Bytes
0d4b564
 
5ec6567
 
 
 
 
 
 
0d4b564
5ec6567
 
 
 
 
 
 
 
 
8d300d3
 
5ec6567
8d300d3
5ec6567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d300d3
5ec6567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d300d3
5ec6567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
---
license: apache-2.0
tags:
- medical
- 3D medical image caption
- image-text pair
- medical report
size_categories:
- 100K<n<1M
---

## Dataset Description
Large-scale 3D medical multi-modal dataset - Image-Text Pair Dataset (M3D-Cap)


### Dataset Introduction
Medical institutions, such as hospitals, store vast amounts of multi-modal data, 
including medical images and diagnostic reports. 
However, disclosing these multi-modal datasets involving patient data faces challenges due to sensitivity and privacy concerns. 
To circumvent these limitations, we collected medical images and reports from publicly accessible professional medical websites [Radiopaedia](https://radiopaedia.org/). 
Specifically, each patient case in our dataset includes multiple images and corresponding reports, which experts from the Radiopaedia platform meticulously review. 
Given the crucial role of 3D CT in medical image analysis, particularly in the diagnosis, localization, and measurement of systemic lesions, 
we focus on 3D CT data. We successfully constructed the largest-scale 3D medical image-text paired dataset, M3D-Cap, 
comprising 120K pairs of image-text data. Overall, it is divided into two data folders named ct_case and ct_quizze. 
ct_quizze is used for medical exams and has higher quality. Each folder contains some image folders and one text file. 
The image folders contain multiple 2D slices of 3D images, while the text files provide English reports describing the corresponding 3D images, 
including types of abnormalities and lesions. M3D_Cap.json provides the split scheme.


### Supported Tasks
M3D-Cap supports various image-text multimodal tasks in 3D medical scenarios, 
including image-text retrieval, report generation, and image generation.

## Dataset Format and Structure

### Data Format
<pre>
    M3D_Seg/
        ct_case/
            000006/
                Axial_non_contrast/
                    0.jpeg
                    1.jpeg
                    ......
                text.txt
            ......
        ct_quizze/
            000007/
                Axial_non_contrast/
                    0.png
                    1.png
                    ......
                text.txt
            ......
        ......
</pre>

### Dataset Download
#### Clone with HTTP
```bash
git clone 
```
#### Manual Download
Download all files from the dataset manually, which can be done using batch download tools. 
Note: Due to the large size of the overall dataset, it is divided into subfiles of 20G each. 
After downloading all files, extract them together to obtain the complete data.


### Dataset Loading Method
#### 1. Preprocessing
Combine slices under each folder in the dataset to form 3D images and name them according to the image file names (retain plane and phase information), 
saving them as npy files. Filter the text reports in the dataset to obtain high-quality descriptions.

#### 2. Build Dataset 
We provide sample code for building the dataset

```python
class CapDataset(Dataset):
    def __init__(self, args, tokenizer, mode="train"):
        self.args = args
        self.data_root = args.data_root
        self.tokenizer = tokenizer
        self.mode = mode

        self.image_tokens = "<im_patch>" * args.proj_out_num

        with open(args.cap_data_path, 'r') as file:
            self.json_file = json.load(file)
        self.data_list = self.json_file[mode]

        self.caption_prompts = [
            "Can you provide a caption consists of findings for this medical image?",
            "Describe the findings of the medical image you see.",
            "Please caption this medical scan with findings.",
            "What is the findings of this image?",
            "Describe this medical scan with findings.",
            "Please write a caption consists of findings for this image.",
            "Can you summarize with findings the images presented?",
            "Please caption this scan with findings.",
            "Please provide a caption consists of findings for this medical image.",
            "Can you provide a summary consists of findings of this radiograph?",
            "What are the findings presented in this medical scan?",
            "Please write a caption consists of findings for this scan.",
            "Can you provide a description consists of findings of this medical scan?",
            "Please caption this medical scan with findings.",
            "Can you provide a caption consists of findings for this medical scan?"
        ]

        train_transform = mtf.Compose(
            [
                mtf.RandRotate90(prob=0.5, spatial_axes=(1, 2)),
                mtf.RandFlip(prob=0.10, spatial_axis=0),
                mtf.RandFlip(prob=0.10, spatial_axis=1),
                mtf.RandFlip(prob=0.10, spatial_axis=2),
                mtf.RandScaleIntensity(factors=0.1, prob=0.5),
                mtf.RandShiftIntensity(offsets=0.1, prob=0.5),

                mtf.ToTensor(dtype=torch.float),
            ]
        )

        val_transform = mtf.Compose(
                [
                    mtf.ToTensor(dtype=torch.float),
                ]
            )
        set_track_meta(False)

        if mode == 'train':
            self.transform = train_transform
        elif mode == 'validation':
            self.transform = val_transform
        elif mode == 'test':
            self.transform = val_transform

    def __len__(self):
        return len(self.data_list)

    def __getitem__(self, idx):
        max_attempts = 100
        for _ in range(max_attempts):
            try:
                data = self.data_list[idx]
                image_path = data["image"]
                image_abs_path = os.path.join(self.data_root, image_path)
                image = np.load(image_abs_path)  # nomalized 0-1, C,D,H,W
                image = self.transform(image)

                text_path = data["text"]
                text_abs_path = os.path.join(self.data_root, text_path)
                with open(text_abs_path, 'r') as text_file:
                    raw_text = text_file.read()
                answer = raw_text

                prompt_question = random.choice(self.caption_prompts)

                question = self.image_tokens + prompt_question

                text_tensor = self.tokenizer(
                    question + ' ' + answer, max_length=self.args.max_length, truncation=True, padding="max_length", return_tensors="pt"
                )

                input_id = text_tensor["input_ids"][0]
                attention_mask = text_tensor["attention_mask"][0]

                valid_len = torch.sum(attention_mask)
                if valid_len < len(input_id):
                    input_id[valid_len] = self.tokenizer.eos_token_id

                question_tensor = self.tokenizer(
                    question, max_length=self.args.max_length, truncation=True, padding="max_length", return_tensors="pt"
                )
                question_len = torch.sum(question_tensor["attention_mask"][0])

                label = input_id.clone()
                label[label == self.tokenizer.pad_token_id] = -100
                label[:question_len] = -100

                ret = {
                    'image': image,
                    'input_id': input_id,
                    'label': label,
                    'attention_mask': attention_mask,
                    'question': question,
                    'answer': answer,
                    'question_type': "Caption",
                }
                return ret

            except Exception as e:
                print(f"Error in __getitem__ at index {idx}: {e}")
                idx = random.randint(0, len(self.data_list) - 1)
```


### Data Splitting
The entire dataset is split into
‘train, validation, test100, test500, test1k, and test’ using a JSON file. 
considering testing costs, we provide different quantities of test samples from 100 to 2k, with the number of ‘test‘ being 2k.

## Dataset Copyright Information

All images and reports involved in this dataset are publicly available data. 
For detailed copyright information, please refer to the corresponding links.

## Citation
If you use this dataset, please cite the following works:

```BibTeX
@misc{bai2024m3d,
      title={M3D: Advancing 3D Medical Image Analysis with Multi-Modal Large Language Models}, 
      author={Fan Bai and Yuxin Du and Tiejun Huang and Max Q. -H. Meng and Bo Zhao},
      year={2024},
      eprint={2404.00578},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
@misc{du2024segvol,
      title={SegVol: Universal and Interactive Volumetric Medical Image Segmentation}, 
      author={Yuxin Du and Fan Bai and Tiejun Huang and Bo Zhao},
      year={2024},
      eprint={2311.13385},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
```