Delete data_prepare.py
Browse files- data_prepare.py +0 -120
data_prepare.py
DELETED
@@ -1,120 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import random
|
3 |
-
import pandas as pd
|
4 |
-
|
5 |
-
|
6 |
-
def get_training_batch(root):
|
7 |
-
"""
|
8 |
-
the proportional distribution of training data across each batch for classification,
|
9 |
-
disease localization, report generation, and segmentation tasks to be 0.15/0.2/0.5/0.15
|
10 |
-
"""
|
11 |
-
classification_label = ['Atelectasis', 'Calcification of the Aorta', 'Cardiomegaly', 'Consolidation', 'Edema', \
|
12 |
-
'Emphysema', 'Enlarged Cardiomediastinum', 'Fibrosis', 'Fracture', 'Hernia', 'Infiltration', 'Lung Lesion', \
|
13 |
-
'Lung Opacity', 'Mass', 'No Finding', 'Nodule', 'Pleural Effusion', 'Pleural Other', 'Pleural Thickening', \
|
14 |
-
'Pneumomediastinum', 'Pneumonia', 'Pneumoperitoneum', 'Pneumothorax', 'Subcutaneous Emphysema', 'Support Devices', 'Tortuous Aorta']
|
15 |
-
|
16 |
-
batch_size = 256
|
17 |
-
cla_num = int(batch_size * 0.15)
|
18 |
-
loc_num = int(batch_size * 0.2)
|
19 |
-
report_num = int(batch_size * 0.5)
|
20 |
-
seg_num = batch_size - (cla_num + loc_num + report_num)
|
21 |
-
|
22 |
-
read = lambda x, y : pd.read_csv(x, sep='\t', header=None, chunksize=y)
|
23 |
-
|
24 |
-
### classification and report generation
|
25 |
-
# Instruction:
|
26 |
-
# what disease does this image have?
|
27 |
-
# is {} in this image?
|
28 |
-
mimic = f'{root}/MIMIC_classification_report-generation_train.tsv'
|
29 |
-
mimic_chunck = read(mimic, max(cla_num, report_num))
|
30 |
-
cla_mimic_info = []
|
31 |
-
report_mimic_info = []
|
32 |
-
for chunck in mimic_chunck:
|
33 |
-
for info in chunck.values.tolist():
|
34 |
-
report = info[1]
|
35 |
-
label = info[2]
|
36 |
-
dicom_id = info[-1]
|
37 |
-
if len(report_mimic_info) < report_num:
|
38 |
-
report_mimic_info.append(
|
39 |
-
['describe the image', report, dicom_id, 'report generation']
|
40 |
-
)
|
41 |
-
if len(cla_mimic_info) < int(cla_num*0.6):
|
42 |
-
if random.randint(0, 1):
|
43 |
-
cur_info = ['what disease does this image have?', f"there are {', '.join(label.split('&&'))}", dicom_id, 'classification']
|
44 |
-
else:
|
45 |
-
vqa_label = classification_label[random.randint(0, len(classification_label)-1)]
|
46 |
-
if vqa_label in label:
|
47 |
-
cur_info = [f'Is {vqa_label} in this image?', f'yes, there is {vqa_label}.', dicom_id, 'classification']
|
48 |
-
else:
|
49 |
-
cur_info = [f'Is {vqa_label} in this image?', f'no {vqa_label}.', dicom_id, 'classification']
|
50 |
-
cla_mimic_info.append(cur_info)
|
51 |
-
break
|
52 |
-
del mimic_chunck
|
53 |
-
|
54 |
-
def organize_data(file, chunck_size, task, instruction, label_index, image_index, instruction_index=None, label_format=None):
|
55 |
-
res = []
|
56 |
-
chunck_size = max(chunck_size, 1)
|
57 |
-
chuncks = read(file, chunck_size)
|
58 |
-
for chunck in chuncks:
|
59 |
-
for info in chunck.values.tolist():
|
60 |
-
if instruction_index is not None:
|
61 |
-
instruction = instruction.format(info[instruction_index])
|
62 |
-
ans = info[label_index]
|
63 |
-
elif label_format is not None:
|
64 |
-
label_ans_list = label_format(info[label_index])
|
65 |
-
label_ans = label_ans_list[random.randint(0, len(label_ans_list)-1)].split(',')
|
66 |
-
if len(label_ans) == 1:
|
67 |
-
label, ans = label_ans_list[0].split(',')[0], label_ans[0]
|
68 |
-
else:
|
69 |
-
label, ans = label_ans
|
70 |
-
label = label.strip()
|
71 |
-
instruction = instruction.format(label)
|
72 |
-
ans = ans.strip()
|
73 |
-
if len(res) < chunck_size:
|
74 |
-
res.append(
|
75 |
-
[instruction, ans, info[image_index], task]
|
76 |
-
)
|
77 |
-
break
|
78 |
-
return res
|
79 |
-
|
80 |
-
### classification: severity
|
81 |
-
# Instruction:
|
82 |
-
# what is the level of {}?
|
83 |
-
mimic_severity = f'{root}/MIMIC_classification-severity_train.tsv'
|
84 |
-
cla_sev_mimic_info = organize_data(
|
85 |
-
mimic_severity, int(cla_num*0.2), 'classification_sev', 'what is the level of {}?', 1, -1, label_format=lambda x:x.split('&&')
|
86 |
-
)
|
87 |
-
|
88 |
-
### classification: location
|
89 |
-
# Instruction:
|
90 |
-
# where is {}?
|
91 |
-
mimic_location = f'{root}/MIMIC_classification-location_train.tsv'
|
92 |
-
cla_loc_mimic_info = organize_data(
|
93 |
-
mimic_location, int(cla_num*0.2), 'classification_loc', 'where is {}?', 1, -1, label_format=lambda x:x.split('&')
|
94 |
-
)
|
95 |
-
|
96 |
-
### localization
|
97 |
-
# Instruction:
|
98 |
-
# give the accurate bbox of {}.
|
99 |
-
chestX_det = f'{root}/ChestX_Det_localization.tsv'
|
100 |
-
chestX_det_info = organize_data(
|
101 |
-
chestX_det, loc_num, 'localization', 'where is {}?', 2, -1, instruction_index=1,
|
102 |
-
)
|
103 |
-
|
104 |
-
### segmentation
|
105 |
-
# Instruction:
|
106 |
-
# describe the image.
|
107 |
-
cheXmask_heart = f'{root}/CheXmask_heart_segmentation.tsv'
|
108 |
-
cheXmask_heart_info = organize_data(
|
109 |
-
cheXmask_heart, seg_num, 'segmentation', 'please segment the {} from the given image.', 2, -1, instruction_index=1
|
110 |
-
)
|
111 |
-
|
112 |
-
batch_info = cla_mimic_info + report_mimic_info + cla_loc_mimic_info + cla_sev_mimic_info + chestX_det_info + cheXmask_heart_info
|
113 |
-
random.shuffle(batch_info)
|
114 |
-
batch_df = pd.DataFrame(batch_info)
|
115 |
-
return batch_df
|
116 |
-
|
117 |
-
|
118 |
-
if __name__ == '__main__':
|
119 |
-
root = ''
|
120 |
-
get_training_batch(root)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|