Datasets:

Modalities:
Image
ArXiv:
IMT-CXR / data_prepare.py
MedHK23's picture
Upload 18 files
572f8a9
raw
history blame
5.64 kB
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', 'where is {}?', 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)