{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Imports\nimport numpy as np, pandas as pd, SimpleITK as sitk, matplotlib.pyplot as plt, os\nimport tensorflow as tf, pydicom as dicom, cv2, matplotlib as mpl, nibabel as nib\nfrom sklearn.model_selection import train_test_split\nfrom scipy import ndimage\nfrom PIL import Image\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-12T10:04:08.753168Z","iopub.execute_input":"2023-10-12T10:04:08.753585Z","iopub.status.idle":"2023-10-12T10:04:18.656357Z","shell.execute_reply.started":"2023-10-12T10:04:08.753552Z","shell.execute_reply":"2023-10-12T10:04:18.655292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Some constants intialisation\nTRAIN_IMG_PATH = '/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images'\nPICS_PER_PACIENT = 20\nIMG_SIZE = [256,256]\nTARGET_COLS  = [\n        \"bowel_injury\", \"extravasation_injury\",\n        \"kidney_healthy\", \"kidney_low\", \"kidney_high\",\n        \"liver_healthy\", \"liver_low\", \"liver_high\",\n        \"spleen_healthy\", \"spleen_low\", \"spleen_high\",\n    ]","metadata":{"execution":{"iopub.status.busy":"2023-10-12T10:04:18.658336Z","iopub.execute_input":"2023-10-12T10:04:18.659233Z","iopub.status.idle":"2023-10-12T10:04:18.664872Z","shell.execute_reply.started":"2023-10-12T10:04:18.659189Z","shell.execute_reply":"2023-10-12T10:04:18.66366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting dataframe linking series_id of segmented scans with respective patient_id\nmask_ser = [x[:-4] for x in os.listdir(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations\")]\nmasks_img = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_series_meta.csv')\nmasks_img['series_id'] = masks_img['series_id'].astype(str)\nmasks_img['patient_id'] = masks_img['patient_id'].astype(str)\n#masks_red = masks_img[masks_img['series_id'].isin(masks)]\nmasks_red = masks_img.drop(['aortic_hu', 'incomplete_organ'], axis = 1)\ndisplay(masks_red) # there are in total 206 segmented images","metadata":{"execution":{"iopub.status.busy":"2023-10-12T10:04:18.666249Z","iopub.execute_input":"2023-10-12T10:04:18.666551Z","iopub.status.idle":"2023-10-12T10:04:18.767271Z","shell.execute_reply.started":"2023-10-12T10:04:18.666519Z","shell.execute_reply":"2023-10-12T10:04:18.766236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_scan_with_mask(number):\n    # plotting stuff\n    image_shape = (512,512)\n    num_images = 12\n    \n    #reading list of dicoms for respective patient_id and ordering them\n    patient_id = str(masks_red.iloc[number][0])\n    series_id = str(masks_red.iloc[number][1])\n    should_predict=False if series_id in masks else True\n    print(should_predict)\n    if should_predict==False : img_data = nib.load('/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations/'+series_id+'.nii').get_fdata()\n    dicoms = [int(x[:-4]) for x in os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'+patient_id+'/'+series_id)]\n    dicoms = [str(x)+'.dcm' for x in sorted(dicoms)]\n    step = int((len(dicoms)/(PICS_PER_PACIENT)))\n    #index_to_load = list(range(step,len(dicoms)-step,step))\n    #print(index_to_load)\n    step = int((len(dicoms)/(PICS_PER_PACIENT)))\n    for i, img in enumerate(range(step,len(dicoms)-step,int((len(dicoms)/(PICS_PER_PACIENT))))):\n        alpha = 0.7\n        mask = []\n        ds = dicom.dcmread('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'+patient_id+'/'+series_id+'/'+dicoms[img])\n        scan = (np.stack((ds.pixel_array,) * 3, axis=-1) - np.min(ds.pixel_array))/(np.max(ds.pixel_array) - np.min(ds.pixel_array)) #normalising\n        if should_predict:\n            model = tf.keras.models.load_model('/kaggle/input/rsna-segmentation/rsna_segmentation.keras')\n            tf_scan = tf.image.resize(scan, (128,128), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n            tf_scan = tf.expand_dims(tf_scan, axis=0)\n            mask = model.predict(tf_scan)\n            mask = tf.argmax(mask, axis=-1)\n            NORM = mpl.colors.Normalize(vmin=0, vmax=6)\n            mask = mpl.colormaps['turbo'](mask[0]/5)[:, :, :3]\n            mask = cv2.resize(mask, (512, 512))\n            mask = mask.astype(np.float64)\n            combined = cv2.addWeighted(scan, alpha, mask, 1 - alpha, 0)\n            plt.imshow(combined)\n            plt.show()\n        else:\n            alpha = 0.7\n            ds = dicom.dcmread('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'+patient_id+'/'+series_id+'/'+dicoms[img])\n            scan = (np.stack((ds.pixel_array,) * 3, axis=-1) - np.min(ds.pixel_array))/(np.max(ds.pixel_array) - np.min(ds.pixel_array)) #normalising\n            mask = mpl.colormaps['turbo'](img_data[:, :, -img] /5)[:, :, :3]\n            combined = cv2.addWeighted(scan, alpha, np.rot90(mask), 1 - alpha, 0)\n            plt.imshow(combined)\n            plt.show()\n        \n        #axes[i].imshow(combined)\n        #axes[i].axis('off')\n    #plt.tight_layout()\n    #plt.show()\n\n#for i in range(1,200,20):\n #   display_scan_with_mask(i) # enter any number up to 205","metadata":{"execution":{"iopub.status.busy":"2023-10-12T10:04:18.769255Z","iopub.execute_input":"2023-10-12T10:04:18.769685Z","iopub.status.idle":"2023-10-12T10:04:18.783798Z","shell.execute_reply.started":"2023-10-12T10:04:18.769657Z","shell.execute_reply":"2023-10-12T10:04:18.782548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dataset_maker(full_df, part_to_use):\n    how_many = int(len(full_df)*part_to_use)\n    smalltrain = train.head(how_many)\n    full_df['patient_id'] = full_df['patient_id'].astype(str)\n    X_train, X_valid, y_train, y_valid = train_test_split(full_df.patient_id, full_df.drop(['patient_id'], axis = 1), test_size = 0.2)\n    y_train['patient_id'] = X_train\n    y_valid['patient_id'] = X_valid\n    train_dataset = y_train\n    valid_dataset = y_valid\n    return train_dataset, valid_dataset\n\ntrain = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\nmasks_patient = masks_red.patient_id.to_list()\ntrain['patient_id'] = train['patient_id'].astype(str)\ntrain = train[train['patient_id'].isin(masks_patient)]\n#train = pd.merge(train, masks_red, on='patient_id', how='outer')\ntrain_dataset, valid_dataset = dataset_maker(train, 0.3)  #cant use full dataset since I dont have enough gpu left\n\ntrain_dataset = pd.merge(train_dataset, masks_red, on='patient_id', how='left')\nvalid_dataset = pd.merge(valid_dataset, masks_red, on='patient_id', how='left')\ndisplay(train_dataset)","metadata":{"execution":{"iopub.status.busy":"2023-10-12T10:04:18.785277Z","iopub.execute_input":"2023-10-12T10:04:18.78598Z","iopub.status.idle":"2023-10-12T10:04:18.851846Z","shell.execute_reply.started":"2023-10-12T10:04:18.78594Z","shell.execute_reply":"2023-10-12T10:04:18.850622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_img(row, output_path, new_df):\n    patient_id = row['patient_id']\n    series_id = row['series_id']\n    masks = [x[:-4] for x in os.listdir(\"/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations\")]\n    should_predict=False if series_id in masks else True\n    if should_predict==False : img_data = nib.load('/kaggle/input/rsna-2023-abdominal-trauma-detection/segmentations/'+series_id+'.nii').get_fdata()\n    labels = row[TARGET_COLS]#.values\n    dicom_files = [int(x[:-4]) for x in os.listdir('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'+patient_id+'/'+series_id)]\n    dicoms = [str(x)+'.dcm' for x in sorted(dicom_files)]\n    step = int((len(dicoms)/(PICS_PER_PACIENT)))\n    dicoms = [dicom.dcmread('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images/'+patient_id+'/'+series_id+'/'+dicoms[i]) for i in range(step,len(dicoms)-step, step)]\n    dicoms = [ds.pixel_array for ds in dicoms]\n    scans = [(np.stack((dicom,) * 3, axis=-1) - np.min(dicom))/(np.max(dicom) - np.min(dicom)) for dicom in dicoms]\n    masks = []\n    if should_predict:\n            model = tf.keras.models.load_model('/kaggle/input/rsna-segmentation/rsna_segmentation.keras')\n            tf_scan = tf.image.resize(scans, (128,128), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n            #tf_scan = tf.expand_dims(tf_scan, axis=0)\n            masks = model.predict(tf_scan)\n    for i, img in enumerate(range(step,len(dicom_files)-step,step)):\n        alpha = 0.7\n        combined = []\n        scan = scans[i]  #normalising\n        if should_predict:\n            mask = tf.argmax(masks[i], axis=-1)\n            NORM = mpl.colors.Normalize(vmin=0, vmax=6)\n            mask = mpl.colormaps['turbo'](mask/5)[:, :, :3]\n            mask = cv2.resize(mask, (512, 512))\n            mask = mask.astype(np.float64)\n        else:\n            mask = np.rot90(mpl.colormaps['turbo'](img_data[:, :, -img] /5)[:, :, :3])\n        try:\n            combined = cv2.addWeighted(scan, alpha,mask, 1 - alpha, 0)\n            res = (cv2.resize(combined, dsize=IMG_SIZE, interpolation=cv2.INTER_CUBIC)*255).astype(np.uint8)\n            image = Image.fromarray(res)\n            os.chdir(output_path)\n            image.save(patient_id+'_'+series_id+'_'+str(i)+'.png')\n            labels['file'] = patient_id+'_'+series_id+'_'+str(i)+'.png'\n            new_df = pd.concat([new_df, labels.to_frame().T], ignore_index=True)\n        except:\n            print('UNSUCCESFUL')\n            print('Series:'+series_id+', Patient:'+patient_id)\n    return new_df\n\ndef save_img_to_numpy(dataset, output_dir, limit):\n    new_df = pd.DataFrame(columns = TARGET_COLS)\n    if not os.path.isdir('/kaggle/working'+output_dir): os.mkdir('/kaggle/working'+output_dir)\n    image_array = []\n    label_array = []\n    os.chdir('/kaggle/working'+output_dir)\n    for index, row in dataset.iterrows():\n        new_df = save_img(row, '/kaggle/working'+output_dir, new_df)\n        end_time = time.time()\n        elapsed_time = end_time - start_time\n        #print(elapsed_time)\n        if elapsed_time > limit:\n            #print('/kaggle/working'+output_dir[:-7]+'.csv')\n            new_df.to_csv('/kaggle/working'+output_dir[:-7]+'.csv')\n            break\n\n#print(type(train_dataset))\n#save_img_to_numpy(train_dataset.head(), '/smalltrain_images')\n#save_img_to_numpy(valid_dataset.head(), '/smallvalid_images')\nprint('run')\nstart_time = time.time()\nsave_img_to_numpy(train_dataset, '/train_images', limit = 23000)\nsave_img_to_numpy(valid_dataset, '/valid_images', limit = 28800)\n\"\"\"\nsave_img_to_numpy(smalltrain_dataset, '/kaggle/working/smalltrain_images')\nsave_img_to_numpy(smallvalid_dataset, '/kaggle/working/smallvalid_images')\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-10-13T04:06:36.165684Z","iopub.execute_input":"2023-10-13T04:06:36.166009Z","iopub.status.idle":"2023-10-13T04:06:36.525548Z","shell.execute_reply.started":"2023-10-13T04:06:36.165986Z","shell.execute_reply":"2023-10-13T04:06:36.524424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Credits\n\nThis notebook was forked from https://www.kaggle.com/code/awsaf49/rsna-atd-cnn-tpu-train\n\nEvaluation metric from https://www.kaggle.com/code/metric/rsna-trauma-metric/notebook","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}