Datasets:

Modalities:
Image
ArXiv:
File size: 5,653 Bytes
ceb159f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random
import pandas as pd


def get_training_batch(root):
        """
        the proportional distribution of training data across each batch for classification,
        disease localization, report generation, and segmentation tasks to be 0.15/0.2/0.5/0.15
        """
        classification_label = ['Atelectasis', 'Calcification of the Aorta', 'Cardiomegaly', 'Consolidation', 'Edema', \
            'Emphysema', 'Enlarged Cardiomediastinum', 'Fibrosis', 'Fracture', 'Hernia', 'Infiltration', 'Lung Lesion', \
            'Lung Opacity', 'Mass', 'No Finding', 'Nodule', 'Pleural Effusion', 'Pleural Other', 'Pleural Thickening', \
            'Pneumomediastinum', 'Pneumonia', 'Pneumoperitoneum', 'Pneumothorax', 'Subcutaneous Emphysema', 'Support Devices', 'Tortuous Aorta']

        batch_size = 256
        cla_num = int(batch_size * 0.15)
        loc_num = int(batch_size * 0.2)
        report_num = int(batch_size * 0.5)
        seg_num = batch_size - (cla_num + loc_num + report_num)

        read = lambda x, y : pd.read_csv(x, sep='\t', header=None, chunksize=y)

        ### classification and report generation
        # Instruction: 
        #   what disease does this image have?
        #   is {} in this image?
        mimic = f'{root}/MIMIC_classification_report-generation_train.tsv'
        mimic_chunck = read(mimic, max(cla_num, report_num))
        cla_mimic_info = []
        report_mimic_info = []
        for chunck in mimic_chunck:
            for info in chunck.values.tolist():
                report = info[1]
                label = info[2]
                dicom_id = info[-1]
                if len(report_mimic_info) < report_num:
                    report_mimic_info.append(
                        ['describe the image', report, dicom_id, 'report generation']
                    )
                if len(cla_mimic_info) < int(cla_num*0.6):
                    if random.randint(0, 1):
                        cur_info = ['what disease does this image have?', f"there are {', '.join(label.split('&&'))}", dicom_id, 'classification']
                    else:
                        vqa_label = classification_label[random.randint(0, len(classification_label)-1)]
                        if vqa_label in label:
                            cur_info = [f'Is {vqa_label} in this image?', f'yes, there is {vqa_label}.', dicom_id, 'classification']
                        else:
                            cur_info = [f'Is {vqa_label} in this image?', f'no {vqa_label}.', dicom_id, 'classification']
                    cla_mimic_info.append(cur_info)
            break
        del mimic_chunck

        def organize_data(file, chunck_size, task, instruction, label_index, image_index, instruction_index=None, label_format=None):
            res = []
            chunck_size = max(chunck_size, 1)
            chuncks = read(file, chunck_size)
            for chunck in chuncks:
                for info in chunck.values.tolist():
                    if instruction_index is not None:
                        instruction = instruction.format(info[instruction_index])
                        ans = info[label_index]
                    elif label_format is not None:
                        label_ans_list = label_format(info[label_index])
                        label_ans = label_ans_list[random.randint(0, len(label_ans_list)-1)].split(',')
                        if len(label_ans) == 1:
                            label, ans = label_ans_list[0].split(',')[0], label_ans[0]
                        else:
                            label, ans = label_ans
                        label = label.strip()
                        instruction =  instruction.format(label)
                        ans = ans.strip()
                    if len(res) < chunck_size:
                        res.append(
                            [instruction, ans, info[image_index], task]
                        )
                break
            return res

        ### classification: severity
        # Instruction: 
        #   what is the level of {}?
        mimic_severity = f'{root}/MIMIC_classification-severity_train.tsv'
        cla_sev_mimic_info = organize_data(
            mimic_severity, int(cla_num*0.2), 'classification_sev', 'what is the level of {}?', 1, -1, label_format=lambda x:x.split('&&')
        )

        ### classification: location
        # Instruction: 
        #   where is {}?
        mimic_location = f'{root}/MIMIC_classification-location_train.tsv'
        cla_loc_mimic_info = organize_data(
            mimic_location, int(cla_num*0.2), 'classification_loc', 'where is {}?', 1, -1, label_format=lambda x:x.split('&')
        )

        ### localization
        # Instruction: 
        #   give the accurate bbox of {}.
        chestX_det = f'{root}/ChestX_Det_localization.tsv'
        chestX_det_info = organize_data(
            chestX_det, loc_num, 'localization', 'Give the accurate bbox of {}', 2, -1, instruction_index=1, 
        )

        ### segmentation
        # Instruction: 
        #   describe the image.
        cheXmask_heart = f'{root}/CheXmask_heart_segmentation.tsv'
        cheXmask_heart_info = organize_data(
            cheXmask_heart, seg_num, 'segmentation', 'please segment the {} from the given image.', 2, -1, instruction_index=1
        )

        batch_info = cla_mimic_info + report_mimic_info + cla_loc_mimic_info + cla_sev_mimic_info + chestX_det_info + cheXmask_heart_info
        random.shuffle(batch_info)
        batch_df = pd.DataFrame(batch_info)
        return batch_df


if __name__ == '__main__':
    root = ''
    get_training_batch(root)