{"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":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt \nimport seaborn as sns\nimport cv2 as cv\nfrom path import Path\nimport os \nimport glob\nimport tensorflow_hub as hub\nimport os \nimport pydicom as dicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers\nfrom tqdm import tqdm\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom pydicom import dcmread\nimport nibabel as nib","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-01T13:00:42.870705Z","iopub.execute_input":"2022-08-01T13:00:42.871047Z","iopub.status.idle":"2022-08-01T13:00:42.877786Z","shell.execute_reply.started":"2022-08-01T13:00:42.871021Z","shell.execute_reply":"2022-08-01T13:00:42.877004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.879401Z","iopub.execute_input":"2022-08-01T13:00:42.880223Z","iopub.status.idle":"2022-08-01T13:00:42.90751Z","shell.execute_reply.started":"2022-08-01T13:00:42.880181Z","shell.execute_reply":"2022-08-01T13:00:42.906104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.909316Z","iopub.execute_input":"2022-08-01T13:00:42.909644Z","iopub.status.idle":"2022-08-01T13:00:42.924097Z","shell.execute_reply.started":"2022-08-01T13:00:42.909614Z","shell.execute_reply":"2022-08-01T13:00:42.922867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.925467Z","iopub.execute_input":"2022-08-01T13:00:42.926439Z","iopub.status.idle":"2022-08-01T13:00:42.947474Z","shell.execute_reply.started":"2022-08-01T13:00:42.926397Z","shell.execute_reply":"2022-08-01T13:00:42.94644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.949829Z","iopub.execute_input":"2022-08-01T13:00:42.950159Z","iopub.status.idle":"2022-08-01T13:00:42.959881Z","shell.execute_reply.started":"2022-08-01T13:00:42.950131Z","shell.execute_reply":"2022-08-01T13:00:42.958183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cor=train.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.962561Z","iopub.execute_input":"2022-08-01T13:00:42.963777Z","iopub.status.idle":"2022-08-01T13:00:42.972487Z","shell.execute_reply.started":"2022-08-01T13:00:42.963711Z","shell.execute_reply":"2022-08-01T13:00:42.971517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(cor,annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:42.97363Z","iopub.execute_input":"2022-08-01T13:00:42.974399Z","iopub.status.idle":"2022-08-01T13:00:43.496548Z","shell.execute_reply.started":"2022-08-01T13:00:42.974364Z","shell.execute_reply":"2022-08-01T13:00:43.495769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(train)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:43.497713Z","iopub.execute_input":"2022-08-01T13:00:43.498168Z","iopub.status.idle":"2022-08-01T13:00:56.085856Z","shell.execute_reply.started":"2022-08-01T13:00:43.49814Z","shell.execute_reply":"2022-08-01T13:00:56.084689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boun_box = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.087815Z","iopub.execute_input":"2022-08-01T13:00:56.088521Z","iopub.status.idle":"2022-08-01T13:00:56.114796Z","shell.execute_reply.started":"2022-08-01T13:00:56.088477Z","shell.execute_reply":"2022-08-01T13:00:56.113951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boun_box.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.117327Z","iopub.execute_input":"2022-08-01T13:00:56.117928Z","iopub.status.idle":"2022-08-01T13:00:56.13103Z","shell.execute_reply.started":"2022-08-01T13:00:56.117892Z","shell.execute_reply":"2022-08-01T13:00:56.129696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boun_box.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.13271Z","iopub.execute_input":"2022-08-01T13:00:56.133176Z","iopub.status.idle":"2022-08-01T13:00:56.147935Z","shell.execute_reply.started":"2022-08-01T13:00:56.133132Z","shell.execute_reply":"2022-08-01T13:00:56.146661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"boun_box.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.149784Z","iopub.execute_input":"2022-08-01T13:00:56.150254Z","iopub.status.idle":"2022-08-01T13:00:56.160882Z","shell.execute_reply.started":"2022-08-01T13:00:56.150209Z","shell.execute_reply":"2022-08-01T13:00:56.160061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cor2 = boun_box.corr()","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.162023Z","iopub.execute_input":"2022-08-01T13:00:56.1625Z","iopub.status.idle":"2022-08-01T13:00:56.172201Z","shell.execute_reply.started":"2022-08-01T13:00:56.16247Z","shell.execute_reply":"2022-08-01T13:00:56.171081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(cor2,annot=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.173799Z","iopub.execute_input":"2022-08-01T13:00:56.174368Z","iopub.status.idle":"2022-08-01T13:00:56.464924Z","shell.execute_reply.started":"2022-08-01T13:00:56.174337Z","shell.execute_reply":"2022-08-01T13:00:56.463482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.pairplot(boun_box)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:00:56.467064Z","iopub.execute_input":"2022-08-01T13:00:56.467431Z","iopub.status.idle":"2022-08-01T13:01:01.772528Z","shell.execute_reply.started":"2022-08-01T13:00:56.4674Z","shell.execute_reply":"2022-08-01T13:01:01.770836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    img=dicom.dcmread(path)\n    data=img.pixel_array\n    data=data-np.min(data)\n    if np.max(data) != 0:\n        data=data/np.max(data)\n    data=(data*255).astype(np.uint8)\n    return data","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:01.774329Z","iopub.execute_input":"2022-08-01T13:01:01.774849Z","iopub.status.idle":"2022-08-01T13:01:01.783407Z","shell.execute_reply.started":"2022-08-01T13:01:01.774803Z","shell.execute_reply":"2022-08-01T13:01:01.782354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def listdirs(folder):\n    return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:01.785645Z","iopub.execute_input":"2022-08-01T13:01:01.786478Z","iopub.status.idle":"2022-08-01T13:01:01.800349Z","shell.execute_reply.started":"2022-08-01T13:01:01.786441Z","shell.execute_reply":"2022-08-01T13:01:01.799032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images'\ntest_dir = '../input/rsna-2022-cervical-spine-fracture-detection/test_images'\npatients = sorted(os.listdir(train_dir))\npatients[:5]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:01.802222Z","iopub.execute_input":"2022-08-01T13:01:01.803159Z","iopub.status.idle":"2022-08-01T13:01:01.818589Z","shell.execute_reply.started":"2022-08-01T13:01:01.803109Z","shell.execute_reply":"2022-08-01T13:01:01.817672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_file = glob.glob(\"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/*.dcm\")\nplt.figure(figsize=(20, 20))\n\nfor i in range(28):\n    ax = plt.subplot(7, 7, i + 1)\n    # specify your dcm image path\n    image_path = image_file[i]\n\n    image = load_dicom(image_path)\n\n    plt.axis('off')\n    \n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:01.819943Z","iopub.execute_input":"2022-08-01T13:01:01.821211Z","iopub.status.idle":"2022-08-01T13:01:04.501993Z","shell.execute_reply.started":"2022-08-01T13:01:01.821162Z","shell.execute_reply":"2022-08-01T13:01:04.500724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nibabel as nib\n\nimage_file = glob.glob(\"../input/rsna-2022-cervical-spine-fracture-detection/segmentations/*.nii\")\nplt.figure(figsize=(20, 20))\n\nfor i in range(28):\n    ax = plt.subplot(7, 7, i + 1)\n    # specify your nii image path\n    image_path = image_file[i]\n    nii_img = nib.load(image_path).get_fdata()\n    nib_image = nii_img[:,:,59]\n    plt.axis('off')\n    plt.imshow(nib_image)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:04.503572Z","iopub.execute_input":"2022-08-01T13:01:04.503998Z","iopub.status.idle":"2022-08-01T13:01:47.814353Z","shell.execute_reply.started":"2022-08-01T13:01:04.503959Z","shell.execute_reply":"2022-08-01T13:01:47.813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pydicom.data import get_testdata_files\ntrainset=[]\ntrainlabel=[]\ntrainidt=[]\nlimit = 10 \nfor i in tqdm(range(len(train))): #there are 2019 rows, need much times, so just process 10 rows to test\n    idt=train.loc[i,'StudyInstanceUID']\n    \n#     idt2=('00000'+str(idt))[-5:]\n    path=os.path.join(train_dir,idt)   \n    \n    for im in os.listdir(path):\n        \n        \n        dc = dicom.read_file(os.path.join(path,im))\n        if dc.file_meta.TransferSyntaxUID.name =='JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])':\n            continue\n        \n        img=load_dicom(os.path.join(path,im)) \n#         ds = decode(os.path.join(path,im))\n#         print(ds)\n#         ds.decompress(\"pylibjpeg\")\n\n        img=cv.resize(img,(64,64)) \n        image=img_to_array(img)\n        image=image/255.0\n\n        trainset+=[image]\n        cur_label=[]\n        cur_label.append(train.loc[i, 'patient_overall'])\n        cur_label.append(train.loc[i,'C1'])\n        cur_label.append(train.loc[i,'C2'])\n        cur_label.append(train.loc[i,'C3'])\n        cur_label.append(train.loc[i,'C4'])\n        cur_label.append(train.loc[i,'C5'])\n        cur_label.append(train.loc[i,'C6'])\n        cur_label.append(train.loc[i,'C7'])\n        trainlabel+=[cur_label]\n        trainidt+=[idt]\n    i+=1\n    if i==limit:\n        break","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:01:47.816161Z","iopub.execute_input":"2022-08-01T13:01:47.816632Z","iopub.status.idle":"2022-08-01T13:02:13.647876Z","shell.execute_reply.started":"2022-08-01T13:01:47.816572Z","shell.execute_reply":"2022-08-01T13:02:13.646323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=np.array(trainlabel)\nY_train=y\nX_train=np.array(trainset)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:13.653741Z","iopub.execute_input":"2022-08-01T13:02:13.654128Z","iopub.status.idle":"2022-08-01T13:02:13.684903Z","shell.execute_reply.started":"2022-08-01T13:02:13.654095Z","shell.execute_reply":"2022-08-01T13:02:13.683589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = ['1.2.826.0.1.3680043.22327', '1.2.826.0.1.3680043.25399', '1.2.826.0.1.3680043.5876']","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:13.686826Z","iopub.execute_input":"2022-08-01T13:02:13.687571Z","iopub.status.idle":"2022-08-01T13:02:13.693219Z","shell.execute_reply.started":"2022-08-01T13:02:13.687524Z","shell.execute_reply":"2022-08-01T13:02:13.691903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir='../input/rsna-2022-cervical-spine-fracture-detection/test_images'\ntestset=[]\ntestidt=[]\nfor i in tqdm(range(len(test_df))):\n    idt=test_df[i]\n    path=os.path.join(test_dir,idt)   \n    \n    for im in os.listdir(path):\n        dc = dicom.read_file(os.path.join(path,im))\n        \n        if dc.file_meta.TransferSyntaxUID.name =='JPEG Lossless, Non-Hierarchical, First-Order Prediction (Process 14 [Selection Value 1])':\n            continue\n        img=load_dicom(os.path.join(path,im)) \n\n        img=cv.resize(img,(64,64)) \n        image=img_to_array(img)\n        image=image/255.0\n        testset+=[image]\n        testidt+=[idt]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:13.694531Z","iopub.execute_input":"2022-08-01T13:02:13.695451Z","iopub.status.idle":"2022-08-01T13:02:19.949839Z","shell.execute_reply.started":"2022-08-01T13:02:13.695415Z","shell.execute_reply":"2022-08-01T13:02:19.948638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.array(testset)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:19.951458Z","iopub.execute_input":"2022-08-01T13:02:19.95186Z","iopub.status.idle":"2022-08-01T13:02:19.961802Z","shell.execute_reply.started":"2022-08-01T13:02:19.951827Z","shell.execute_reply":"2022-08-01T13:02:19.960921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.layers.Conv2D(filters=64,kernel_size=(4,4),input_shape=(64,64,1),activation='relu',kernel_initializer=\"he_normal\"))\nmodel.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(filters=64,kernel_size=(4,4),activation='relu',kernel_initializer=\"he_normal\"))\nmodel.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\nmodel.add(keras.layers.Dropout(0.20))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Conv2D(filters=64,kernel_size=(4,4),activation='relu',kernel_initializer=\"he_normal\"))\nmodel.add(keras.layers.MaxPooling2D(pool_size=(2,2)))\nmodel.add(keras.layers.Dropout(0.25))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.Flatten())\nmodel.add(keras.layers.Dense(100,activation=\"relu\",kernel_initializer=\"he_normal\"))\nmodel.add(keras.layers.Dense(8,\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:19.963177Z","iopub.execute_input":"2022-08-01T13:02:19.963962Z","iopub.status.idle":"2022-08-01T13:02:20.087929Z","shell.execute_reply.started":"2022-08-01T13:02:19.963923Z","shell.execute_reply":"2022-08-01T13:02:20.086556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\",\n              optimizer = \"RMSprop\",metrics=[\"accuracy\"])\ncallback = keras.callbacks.EarlyStopping(monitor='loss', patience=8)","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:20.08949Z","iopub.execute_input":"2022-08-01T13:02:20.089817Z","iopub.status.idle":"2022-08-01T13:02:20.104139Z","shell.execute_reply.started":"2022-08-01T13:02:20.089789Z","shell.execute_reply":"2022-08-01T13:02:20.102682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" model_fit= model.fit(X_train, Y_train,epochs=30, batch_size=64, verbose=1,callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:02:20.105734Z","iopub.execute_input":"2022-08-01T13:02:20.106076Z","iopub.status.idle":"2022-08-01T13:08:04.907946Z","shell.execute_reply.started":"2022-08-01T13:02:20.106047Z","shell.execute_reply":"2022-08-01T13:08:04.90701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)\nresult = pd.DataFrame(columns = train.columns, index = range(len(testidt)))\ny_mean = np.mean(y_pred)\nfor i in tqdm(range(len(testidt))):\n    result.loc[i, 'StudyInstanceUID'] = testidt[i]\n    rows = np.int64(y_pred[i]>y_mean)\n    result.loc[i, 'patient_overall'] = rows[0]\n    result.loc[i, 'C1'] = rows[1]\n    result.loc[i, 'C2'] = rows[2]\n    result.loc[i, 'C3'] = rows[3]\n    result.loc[i, 'C4'] = rows[4]\n    result.loc[i, 'C5'] = rows[5]\n    result.loc[i, 'C6'] = rows[6]\n    result.loc[i, 'C7'] = rows[7]","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:08:25.810863Z","iopub.execute_input":"2022-08-01T13:08:25.811305Z","iopub.status.idle":"2022-08-01T13:08:27.246666Z","shell.execute_reply.started":"2022-08-01T13:08:25.811269Z","shell.execute_reply":"2022-08-01T13:08:27.245424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"means = result[['patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']].mean().to_dict()\ntest_df2 = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')\ntest_df2['fractured'] = test_df2['prediction_type'].map(means)\n\ntest_df2[['row_id','fractured']].to_csv('submission.csv', index=False, float_format='%.1g')","metadata":{"execution":{"iopub.status.busy":"2022-08-01T13:08:30.617279Z","iopub.execute_input":"2022-08-01T13:08:30.617837Z","iopub.status.idle":"2022-08-01T13:08:30.644218Z","shell.execute_reply.started":"2022-08-01T13:08:30.617791Z","shell.execute_reply":"2022-08-01T13:08:30.643429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}