{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":36363,"databundleVersionId":4050810,"sourceType":"competition"}],"dockerImageVersionId":30260,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n         os.path.join(dirname, filename) \n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-17T18:09:26.586297Z","iopub.execute_input":"2022-09-17T18:09:26.586908Z","iopub.status.idle":"2022-09-17T18:10:44.113573Z","shell.execute_reply.started":"2022-09-17T18:09:26.586809Z","shell.execute_reply":"2022-09-17T18:10:44.112534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport matplotlib.pyplot as plt \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\n","metadata":{"execution":{"iopub.status.busy":"2022-09-24T15:49:56.238453Z","iopub.execute_input":"2022-09-24T15:49:56.23882Z","iopub.status.idle":"2022-09-24T15:50:02.808892Z","shell.execute_reply.started":"2022-09-24T15:49:56.238751Z","shell.execute_reply":"2022-09-24T15:50:02.807964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\n# test_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\") useless ,sample not exist","metadata":{"execution":{"iopub.status.busy":"2022-09-24T15:50:02.810492Z","iopub.execute_input":"2022-09-24T15:50:02.811078Z","iopub.status.idle":"2022-09-24T15:50:02.830052Z","shell.execute_reply.started":"2022-09-24T15:50:02.811048Z","shell.execute_reply":"2022-09-24T15:50:02.828702Z"},"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-09-24T15:50:02.831364Z","iopub.execute_input":"2022-09-24T15:50:02.831749Z","iopub.status.idle":"2022-09-24T15:50:02.839499Z","shell.execute_reply.started":"2022-09-24T15:50:02.83172Z","shell.execute_reply":"2022-09-24T15:50:02.837863Z"},"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-09-24T15:50:05.992885Z","iopub.execute_input":"2022-09-24T15:50:05.993233Z","iopub.status.idle":"2022-09-24T15:50:05.997523Z","shell.execute_reply.started":"2022-09-24T15:50:05.993207Z","shell.execute_reply":"2022-09-24T15:50:05.996534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/rsna-2022-cervical-spine-fracture-detection/train_images'\npatients = sorted(os.listdir(train_dir))\npatients[:5]","metadata":{"execution":{"iopub.status.busy":"2022-09-24T15:50:13.835599Z","iopub.execute_input":"2022-09-24T15:50:13.836038Z","iopub.status.idle":"2022-09-24T15:50:13.910295Z","shell.execute_reply.started":"2022-09-24T15:50:13.835999Z","shell.execute_reply":"2022-09-24T15:50:13.909378Z"},"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-09-17T18:10:51.390217Z","iopub.execute_input":"2022-09-17T18:10:51.391161Z","iopub.status.idle":"2022-09-17T18:10:54.380559Z","shell.execute_reply.started":"2022-09-17T18:10:51.391117Z","shell.execute_reply":"2022-09-17T18:10:54.379123Z"},"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-09-17T18:10:54.382079Z","iopub.execute_input":"2022-09-17T18:10:54.382446Z","iopub.status.idle":"2022-09-17T18:11:32.359056Z","shell.execute_reply.started":"2022-09-17T18:10:54.382415Z","shell.execute_reply":"2022-09-17T18:11:32.357435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pydicom.data import get_testdata_files\ntrainset=[]\ntrainlabel=[]\ntrainidt=[] \nfor i in tqdm(range(len(train_df))[:20]): #there are 2019 rows, need much times, so just process 100 rows to test\n    idt=train_df.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            i+=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_df.loc[i,'C1'])\n        cur_label.append(train_df.loc[i,'C2'])\n        cur_label.append(train_df.loc[i,'C3'])\n        cur_label.append(train_df.loc[i,'C4'])\n        cur_label.append(train_df.loc[i,'C5'])\n        cur_label.append(train_df.loc[i,'C6'])\n        cur_label.append(train_df.loc[i,'C7'])\n        trainlabel+=[cur_label]\n        trainidt+=[idt]\n   ","metadata":{"execution":{"iopub.status.busy":"2022-09-24T15:50:17.559059Z","iopub.execute_input":"2022-09-24T15:50:17.559829Z","iopub.status.idle":"2022-09-24T15:51:48.636213Z","shell.execute_reply.started":"2022-09-24T15:50:17.5598Z","shell.execute_reply":"2022-09-24T15:51:48.634571Z"},"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-09-24T15:51:54.149107Z","iopub.execute_input":"2022-09-24T15:51:54.149464Z","iopub.status.idle":"2022-09-24T15:51:54.196296Z","shell.execute_reply.started":"2022-09-24T15:51:54.149436Z","shell.execute_reply":"2022-09-24T15:51:54.195344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert(list):\n    return (*list, )\nconvert(trainlabel)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:10:21.301091Z","iopub.execute_input":"2022-09-24T16:10:21.301453Z","iopub.status.idle":"2022-09-24T16:10:21.446562Z","shell.execute_reply.started":"2022-09-24T16:10:21.301425Z","shell.execute_reply":"2022-09-24T16:10:21.445606Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-09-24T15:55:14.237034Z","iopub.execute_input":"2022-09-24T15:55:14.237494Z","iopub.status.idle":"2022-09-24T15:55:14.242163Z","shell.execute_reply.started":"2022-09-24T15:55:14.237453Z","shell.execute_reply":"2022-09-24T15:55:14.2413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n  X_train, labels=convert(trainlabel) ,label_mode='categorical',\n  seed=123\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:10:37.997254Z","iopub.execute_input":"2022-09-24T16:10:37.99859Z","iopub.status.idle":"2022-09-24T16:10:38.09693Z","shell.execute_reply.started":"2022-09-24T16:10:37.998529Z","shell.execute_reply":"2022-09-24T16:10:38.095448Z"},"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-09-17T18:32:10.088628Z","iopub.execute_input":"2022-09-17T18:32:10.089722Z","iopub.status.idle":"2022-09-17T18:32:10.095127Z","shell.execute_reply.started":"2022-09-17T18:32:10.089651Z","shell.execute_reply":"2022-09-17T18:32:10.094041Z"},"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-09-17T18:32:21.4928Z","iopub.execute_input":"2022-09-17T18:32:21.493231Z","iopub.status.idle":"2022-09-17T18:32:39.189629Z","shell.execute_reply.started":"2022-09-17T18:32:21.493195Z","shell.execute_reply":"2022-09-17T18:32:39.188553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.array(testset)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T18:34:08.72525Z","iopub.execute_input":"2022-09-17T18:34:08.725689Z","iopub.status.idle":"2022-09-17T18:34:08.742785Z","shell.execute_reply.started":"2022-09-17T18:34:08.725648Z","shell.execute_reply":"2022-09-17T18:34:08.741797Z"},"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(7,\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:11:19.05584Z","iopub.execute_input":"2022-09-24T16:11:19.057276Z","iopub.status.idle":"2022-09-24T16:11:19.349098Z","shell.execute_reply.started":"2022-09-24T16:11:19.057229Z","shell.execute_reply":"2022-09-24T16:11:19.348208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, show_shapes=True, rankdir='TB')","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:11:21.820563Z","iopub.execute_input":"2022-09-24T16:11:21.820887Z","iopub.status.idle":"2022-09-24T16:11:22.937159Z","shell.execute_reply.started":"2022-09-24T16:11:21.820861Z","shell.execute_reply":"2022-09-24T16:11:22.936272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",\n              optimizer = \"RMSprop\",metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:11:24.204497Z","iopub.execute_input":"2022-09-24T16:11:24.204875Z","iopub.status.idle":"2022-09-24T16:11:24.223265Z","shell.execute_reply.started":"2022-09-24T16:11:24.204844Z","shell.execute_reply":"2022-09-24T16:11:24.222302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = keras.callbacks.EarlyStopping(monitor='loss', patience=8)","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:11:25.379557Z","iopub.execute_input":"2022-09-24T16:11:25.380366Z","iopub.status.idle":"2022-09-24T16:11:25.385834Z","shell.execute_reply.started":"2022-09-24T16:11:25.380336Z","shell.execute_reply":"2022-09-24T16:11:25.384554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(X_train, Y_train,epochs=100, batch_size=64, verbose=1,callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2022-09-24T16:11:27.042771Z","iopub.execute_input":"2022-09-24T16:11:27.043117Z","iopub.status.idle":"2022-09-24T16:11:35.922499Z","shell.execute_reply.started":"2022-09-24T16:11:27.043091Z","shell.execute_reply":"2022-09-24T16:11:35.91924Z"},"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":[]},{"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":[]},{"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":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}