import pandas as pd
import time
import os
from collections import Counter
import numpy as np
import cv2
import SimpleITK
from tqdm import tqdm
def getFiles(path):
Filelist = []
for home, dirs, files in os.walk(path):
for file in files:
Filelist.append(os.path.join(home, file))
return Filelist
root_path = '/home/tione/notebook/taop-2021/100003/'
train_files = pd.read_csv('/home/tione/notebook/taop-2021/100003/test2_data_info.csv')
from mmdet.apis import init_detector, inference_detector, show_result_pyplot, show_result_ins
import mmcv
import os
import numpy as np
import skimage.io
import cv2
from PIL import Image
import sys
from scipy import ndimage
import csv
import matplotlib.pyplot as plt
config_file = '/home/tione/notebook/code/configs/solov2/solov2_r101_dcn_fpn_8gpu_3x.py'
checkpoint_file = '/home/tione/notebook/code/work_dirs/solov2_release_r101_dcn_fpn_8gpu_step1/epoch_25.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
step2_1_config_file = '/home/tione/notebook/code/configs/solov2/solov2_r101_dcn_fpn_8gpu_3x1.py'
step2_1_checkpoint_file = '/home/tione/notebook/code/work_dirs/solov2_release_r101_dcn_fpn_8gpu_step2_1/epoch_6.pth'
step2_1_model = init_detector(step2_1_config_file, step2_1_checkpoint_file, device='cuda:0')
step2_2_config_file = '/home/tione/notebook/code/configs/solov2/solov2_r101_dcn_fpn_8gpu_3x2.py'
step2_2_checkpoint_file = '/home/tione/notebook/code/work_dirs/solov2_release_r101_dcn_fpn_8gpu_step2_2/epoch_11.pth'
step2_2_model = init_detector(step2_2_config_file, step2_2_checkpoint_file, device='cuda:0')
step2_3_config_file = '/home/tione/notebook/code/configs/solov2/solov2_r101_dcn_fpn_8gpu_3x3.py'
step2_3_checkpoint_file = '/home/tione/notebook/code/work_dirs/solov2_release_r101_dcn_fpn_8gpu_step2_3/epoch_10.pth'
step2_3_model = init_detector(step2_3_config_file, step2_3_checkpoint_file, device='cuda:0')
step2_4_config_file = '/home/tione/notebook/code/configs/solov2/solov2_r101_dcn_fpn_8gpu_3x4.py'
step2_4_checkpoint_file = '/home/tione/notebook/code/work_dirs/solov2_release_r101_dcn_fpn_8gpu_step2_4/epoch_10.pth'
step2_4_model = init_detector(step2_4_config_file, step2_4_checkpoint_file, device='cuda:0')
c = Counter(train_files['id_patient'])
idx = 0
image_id = 1
segmentation_id = 1
csv_flag = False
for i in tqdm(c):
idx_each_patient = idx
nums_per_patient = int(c[i] / 3)
Flair_path = train_files.iloc[idx_each_patient]['id_series']
Flair_path = Flair_path.split('_')[0] + '_' + Flair_path.split('_')[1].capitalize()
CET1_path = train_files.iloc[idx_each_patient+nums_per_patient]['id_series']
if CET1_path.split('_')[1] == 't1ce':
CET1_path = CET1_path.split('_')[0] + '_CET1'
T2_path = train_files.iloc[idx_each_patient+2*nums_per_patient]['id_series']
T2_path = T2_path.split('_')[0] + '_' + T2_path.split('_')[1].upper()
dicoms_flair_path = root_path + Flair_path.split('_')[0] + '/' + Flair_path
dicoms_cet1_path = root_path + Flair_path.split('_')[0] + '/' + CET1_path
dicoms_t2_path = root_path + Flair_path.split('_')[0] + '/' + T2_path
flair_dicoms = getFiles(dicoms_flair_path)
flair_ds = SimpleITK.ReadImage(flair_dicoms[0])
flair_ds = SimpleITK.GetArrayFromImage(flair_ds)
h, w = flair_ds[0].shape
mask_label0 = np.zeros((len(flair_dicoms), h, w))
mask_label1 = np.zeros((len(flair_dicoms), h, w))
mask_label2 = np.zeros((len(flair_dicoms), h, w))
mask_label3 = np.zeros((len(flair_dicoms), h, w))
mask_labelflag_0 = False
mask_labelflag_1 = False
mask_labelflag_2 = False
mask_labelflag_3 = False
for flair_dicom in flair_dicoms:
pic_name = os.path.split(flair_dicom)[1]
if pic_name == '0.dcm' or pic_name == '1.dcm':
continue
cet1_dicom = dicoms_cet1_path + '/' + pic_name
t2_dicom = dicoms_t2_path + '/' + pic_name
flair_ds = SimpleITK.ReadImage(flair_dicom)
flair_ds = SimpleITK.GetArrayFromImage(flair_ds)
cet1_ds = SimpleITK.ReadImage(cet1_dicom)
cet1_ds = SimpleITK.GetArrayFromImage(cet1_ds)
t2_ds = SimpleITK.ReadImage(t2_dicom)
t2_ds = SimpleITK.GetArrayFromImage(t2_ds)
flair_array = np.reshape(flair_ds, (flair_ds.shape[1], flair_ds.shape[2]))
cet1_array = np.reshape(cet1_ds, (cet1_ds.shape[1], cet1_ds.shape[2]))
t2_array = np.reshape(t2_ds, (t2_ds.shape[1], t2_ds.shape[2]))
flair_array = (flair_array - np.mean(flair_array)) / np.std(flair_array)
cet1_array = (cet1_array - np.mean(cet1_array)) / np.std(cet1_array)
t2_array = (t2_array - np.mean(t2_array)) / np.std(t2_array)
flair_array = ((flair_array - flair_array.min())/(flair_array.max()-flair_array.min()))*255
cet1_array = ((cet1_array - cet1_array.min())/(cet1_array.max()-cet1_array.min()))*255
t2_array = ((t2_array - t2_array.min())/(t2_array.max()-t2_array.min()))*255
flair_array = np.asarray(flair_array).astype(np.float32)
cet1_array = np.asarray(cet1_array).astype(np.float32)
t2_array = np.asarray(t2_array).astype(np.float32)
stacked_img = np.stack((cet1_array, flair_array, t2_array), axis=-1)
image_path = '/home/tione/notebook/img-2021-test-step1/'
if not os.path.exists(image_path):
os.makedirs(image_path)
image_name = pic_name.split('.')[0] + '.png'
saved_imgpath = image_path + Flair_path.split('_')[0] + '_' + image_name
cv2.imwrite(saved_imgpath, stacked_img)
result = inference_detector(model, stacked_img)
if result[0] != None:
cur_result = result[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
score = cur_result[2].cpu().numpy()
score_thr = 0.3
vis_inds = score > score_thr
seg_label = seg_label[vis_inds]
num_mask = seg_label.shape[0]
cate_label = cate_label[vis_inds]
cate_score = score[vis_inds]
if num_mask == 0:
continue
stacked_img[seg_label[0] == False] = 0
image_path = '/home/tione/notebook/img-2021-test-step2/'
if not os.path.exists(image_path):
os.makedirs(image_path)
image_name = pic_name.split('.')[0] + '.png'
cv2.imwrite(image_path + Flair_path.split('_')[0] + '_' + image_name, stacked_img)
result_1 = inference_detector(step2_1_model, stacked_img)
result_2 = inference_detector(step2_2_model, stacked_img)
result_3 = inference_detector(step2_3_model, stacked_img)
result_4 = inference_detector(step2_4_model, stacked_img)
if result_1[0] != None:
cur_result = result_1[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
score = cur_result[2].cpu().numpy()
score_thr = 0.3
vis_inds = score > score_thr
seg_label = seg_label[vis_inds]
num_mask = seg_label.shape[0]
cate_label = cate_label[vis_inds]
cate_score = score[vis_inds]
if num_mask == 0:
continue
mask_label0[int(pic_name.split('.')[0]), :, :] = seg_label
mask_labelflag_0 = True
if result_2[0] != None:
cur_result = result_2[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
score = cur_result[2].cpu().numpy()
score_thr = 0.3
vis_inds = score > score_thr
seg_label = seg_label[vis_inds]
num_mask = seg_label.shape[0]
cate_label = cate_label[vis_inds]
cate_score = score[vis_inds]
if num_mask == 0:
continue
mask_label1[int(pic_name.split('.')[0]), :, :] = seg_label
mask_labelflag_1 = True
if result_3[0] != None:
cur_result = result_3[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
score = cur_result[2].cpu().numpy()
score_thr = 0.3
vis_inds = score > score_thr
seg_label = seg_label[vis_inds]
num_mask = seg_label.shape[0]
cate_label = cate_label[vis_inds]
cate_score = score[vis_inds]
if num_mask == 0:
continue
mask_label2[int(pic_name.split('.')[0]), :, :] = seg_label
mask_labelflag_2 = True
if result_4[0] != None:
cur_result = result_4[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
score = cur_result[2].cpu().numpy()
score_thr = 0.3
vis_inds = score > score_thr
seg_label = seg_label[vis_inds]
num_mask = seg_label.shape[0]
cate_label = cate_label[vis_inds]
cate_score = score[vis_inds]
if num_mask == 0:
continue
mask_label3[int(pic_name.split('.')[0]), :, :] = seg_label
mask_labelflag_3 = True
record_time = int(time.time())
file_path = '/home/tione/notebook/03_predicts/' + Flair_path.split('_')[0] + '/mask/'
if not os.path.exists(file_path):
os.makedirs(file_path)
nums_label = []
if mask_labelflag_0 == True:
out = SimpleITK.GetImageFromArray(mask_label0)
SimpleITK.WriteImage(out, file_path + '1.nii.gz')
nums_label.append(1)
if mask_labelflag_1 == True:
out = SimpleITK.GetImageFromArray(mask_label1)
SimpleITK.WriteImage(out, file_path + '2.nii.gz')
nums_label.append(2)
if mask_labelflag_2 == True:
out = SimpleITK.GetImageFromArray(mask_label2)
SimpleITK.WriteImage(out, file_path + '3.nii.gz')
nums_label.append(3)
if mask_labelflag_3 == True:
out = SimpleITK.GetImageFromArray(mask_label3)
SimpleITK.WriteImage(out, file_path + '4.nii.gz')
nums_label.append(4)
print('nums_label', len(nums_label), nums_label)
field_order = ['id_type', 'id_project', 'id_patient', 'id_exam', 'id_series', 'id_image', 'id_doctor', 'id_task', 'id_area', 'label_type', 'mask_path', 'class(1:LBSA;2:EA;3:NA;4:CA)']
with open("03_predicts.csv", 'a+', encoding="utf-8", newline='') as csvfile:
writer = csv.DictWriter(csvfile, field_order)
if csv_flag == False:
writer.writeheader()
csv_flag = True
id_type = train_files.iloc[idx_each_patient]['id_type']
id_project = train_files.iloc[idx_each_patient]['id_project']
id_patient = train_files.iloc[idx_each_patient]['id_patient']
id_exam = train_files.iloc[idx_each_patient]['id_exam']
id_image = train_files.iloc[idx_each_patient]['id_image']
id_doctor = train_files.iloc[idx_each_patient]['id_doctor']
id_task = train_files.iloc[idx_each_patient]['id_task']
label_type = train_files.iloc[idx_each_patient]['label_type']
if len(nums_label) == 0:
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 1, label_type, None, None])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 1, label_type, None, None])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 1, label_type, None, None])))
elif len(nums_label) == 1:
if mask_labelflag_0 == True:
mask_path = Flair_path.split('_')[0] + '/mask/1.nii.gz'
mask_class = 1
elif mask_labelflag_1 == True:
mask_path = Flair_path.split('_')[0] + '/mask/2.nii.gz'
mask_class = 2
elif mask_labelflag_2 == True:
mask_path = Flair_path.split('_')[0] + '/mask/3.nii.gz'
mask_class = 3
elif mask_labelflag_3 == True:
mask_path = Flair_path.split('_')[0] + '/mask/4.nii.gz'
mask_class = 4
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
elif len(nums_label) == 2:
mask_class = nums_label[0]
mask_path = Flair_path.split('_')[0] + '/mask/' + str(mask_class) + '.nii.gz'
mask_class1 = nums_label[1]
mask_path1 = Flair_path.split('_')[0] + '/mask/' + str(mask_class1) + '.nii.gz'
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
elif len(nums_label) == 3:
mask_class = nums_label[0]
mask_path = Flair_path.split('_')[0] + '/mask/' + str(mask_class) + '.nii.gz'
mask_class1 = nums_label[1]
mask_path1 = Flair_path.split('_')[0] + '/mask/' + str(mask_class1) + '.nii.gz'
mask_class2 = nums_label[2]
mask_path2 = Flair_path.split('_')[0] + '/mask/' + str(mask_class2) + '.nii.gz'
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
elif len(nums_label) == 4:
mask_class = nums_label[0]
mask_path = Flair_path.split('_')[0] + '/mask/' + str(mask_class) + '.nii.gz'
mask_class1 = nums_label[1]
mask_path1 = Flair_path.split('_')[0] + '/mask/' + str(mask_class1) + '.nii.gz'
mask_class2 = nums_label[2]
mask_path2 = Flair_path.split('_')[0] + '/mask/' + str(mask_class2) + '.nii.gz'
mask_class3 = nums_label[3]
mask_path3 = Flair_path.split('_')[0] + '/mask/' + str(mask_class3) + '.nii.gz'
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_flair', id_image, id_doctor, id_task, 4, label_type, mask_path3, mask_class3])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t1ce', id_image, id_doctor, id_task, 4, label_type, mask_path3, mask_class3])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 1, label_type, mask_path, mask_class])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 2, label_type, mask_path1, mask_class1])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 3, label_type, mask_path2, mask_class2])))
writer.writerow(dict(zip(field_order, [id_type, id_project, id_patient, id_exam, str(id_exam) + '_t2', id_image, id_doctor, id_task, 4, label_type, mask_path3, mask_class3])))
mask_labelflag_0 = False
mask_labelflag_1 = False
mask_labelflag_2 = False
mask_labelflag_3 = False
idx = idx + c[i]
print('it is ok!')