{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport seaborn as sns\nsns.set()\n\nimport pydicom\n\nimport cv2\n\nfrom skimage import measure \nfrom mpl_toolkits.mplot3d.art3d import Poly3DCollection\n\nfrom os import listdir\n\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\nwarnings.filterwarnings(\"ignore\", category=UserWarning)\nwarnings.filterwarnings(\"ignore\", category=FutureWarning)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-09T07:00:15.561938Z","iopub.execute_input":"2022-08-09T07:00:15.562713Z","iopub.status.idle":"2022-08-09T07:00:15.575713Z","shell.execute_reply.started":"2022-08-09T07:00:15.562666Z","shell.execute_reply":"2022-08-09T07:00:15.574069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# #Importing the data and check total size of data","metadata":{"execution":{"iopub.status.busy":"2022-08-09T04:58:41.152792Z","iopub.execute_input":"2022-08-09T04:58:41.15321Z","iopub.status.idle":"2022-08-09T04:58:41.158731Z","shell.execute_reply.started":"2022-08-09T04:58:41.153173Z","shell.execute_reply":"2022-08-09T04:58:41.157372Z"}}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.616468Z","iopub.execute_input":"2022-08-09T07:00:15.617194Z","iopub.status.idle":"2022-08-09T07:00:15.643622Z","shell.execute_reply.started":"2022-08-09T07:00:15.617144Z","shell.execute_reply":"2022-08-09T07:00:15.642612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.676717Z","iopub.execute_input":"2022-08-09T07:00:15.677438Z","iopub.status.idle":"2022-08-09T07:00:15.691131Z","shell.execute_reply.started":"2022-08-09T07:00:15.6774Z","shell.execute_reply":"2022-08-09T07:00:15.689731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.StudyInstanceUID.nunique()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.73245Z","iopub.execute_input":"2022-08-09T07:00:15.734725Z","iopub.status.idle":"2022-08-09T07:00:15.742933Z","shell.execute_reply.started":"2022-08-09T07:00:15.734666Z","shell.execute_reply":"2022-08-09T07:00:15.741696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.786862Z","iopub.execute_input":"2022-08-09T07:00:15.787896Z","iopub.status.idle":"2022-08-09T07:00:15.800323Z","shell.execute_reply.started":"2022-08-09T07:00:15.787852Z","shell.execute_reply":"2022-08-09T07:00:15.799396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.842377Z","iopub.execute_input":"2022-08-09T07:00:15.843047Z","iopub.status.idle":"2022-08-09T07:00:15.853379Z","shell.execute_reply.started":"2022-08-09T07:00:15.843012Z","shell.execute_reply":"2022-08-09T07:00:15.852417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# How the targets distributed?","metadata":{}},{"cell_type":"code","source":"#Lets check about the distribution of data\nfig,ax=plt.subplots(2,4,figsize=(25,12))\n\nsns.countplot(train.patient_overall, ax=ax[0,0],palette=\"Reds_r\")\nax[0,0].set_title(\"Parient total count\")\nsns.countplot(train.C1, ax=ax[0,0],palette=\"Blues_r\")\nax[0,1].set_title(\" total count\")\nsns.countplot(train.C2, ax=ax[0,0],palette=\"Blues_r\")\nax[0,2].set_title(\"C2 total count\")\nsns.countplot(train.C3, ax=ax[0,0],palette=\"Blues_r\")\nax[0,3].set_title(\"C3 total count\")\n\nsns.countplot(train.C4, ax=ax[1,0], palette=\"Blues_r\")\nax[1,0].set_title(\"C4 target count\")\nsns.countplot(train.C5, ax=ax[1,1], palette=\"Blues_r\")\nax[1,1].set_title(\"C5 target count\")\nsns.countplot(train.C6, ax=ax[1,2], palette=\"Blues_r\")\nax[1,2].set_title(\"C6 target count\")\nsns.countplot(train.C7, ax=ax[1,3], palette=\"Blues_r\")\nax[1,3].set_title(\"C7 target count\")\n\nfor n in range(4):\n    ax[0,n].set_xlabel(\"\")\n    ax[1,n].set_xlabel(\"\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:15.906905Z","iopub.execute_input":"2022-08-09T07:00:15.907588Z","iopub.status.idle":"2022-08-09T07:00:17.093084Z","shell.execute_reply.started":"2022-08-09T07:00:15.907552Z","shell.execute_reply":"2022-08-09T07:00:17.091753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Conclusion :\n# The patient overall target is rouhly balanced compared to underlying subtarget structure.\n# The overall target tells us whether any of the vertebrae are fructured whereas the others show us in detail which of the verteberal col are affected.\n# By this conclusion we say that the patient has more than one fracture might be possible \n# Let's count total no of fractures in a patient:\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.095527Z","iopub.execute_input":"2022-08-09T07:00:17.09592Z","iopub.status.idle":"2022-08-09T07:00:17.101757Z","shell.execute_reply.started":"2022-08-09T07:00:17.095886Z","shell.execute_reply":"2022-08-09T07:00:17.100254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fracture_counts = train[train.patient_overall==1].drop([\"StudyInstanceUID\",\"patient_overall\"],axis=1).sum(axis=1)\nplt.figure(figsize=(10,5))\nsns.countplot(fracture_counts,palette=\"Blues_r\")\nplt.title(\"Total number of persons per fractures i.e. frequency\")\nplt.xlabel(\"Number of fractures\")\nplt.ylabel(\"Fractures per count\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.103424Z","iopub.execute_input":"2022-08-09T07:00:17.103792Z","iopub.status.idle":"2022-08-09T07:00:17.345442Z","shell.execute_reply.started":"2022-08-09T07:00:17.103759Z","shell.execute_reply":"2022-08-09T07:00:17.344151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# By this graph we can conclude that most of the people has 1 fracture in their body and which is good for us but here number of fractures more than 1 i.e. 2 and 3 \n#is also significant and we can not ignore these also \n# an one should have to take care of these things \n# For more than one fractures it can be interesting to explore whether the peoples having more than one combination is haveing any pattern in their fractures or not\n","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.349744Z","iopub.execute_input":"2022-08-09T07:00:17.350135Z","iopub.status.idle":"2022-08-09T07:00:17.355773Z","shell.execute_reply.started":"2022-08-09T07:00:17.3501Z","shell.execute_reply":"2022-08-09T07:00:17.354435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working with dicom files","metadata":{}},{"cell_type":"code","source":"example = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/104.dcm\"\nexample_file = pydicom.dcmread(example)\nexample_file","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.357913Z","iopub.execute_input":"2022-08-09T07:00:17.358409Z","iopub.status.idle":"2022-08-09T07:00:17.389203Z","shell.execute_reply.started":"2022-08-09T07:00:17.358355Z","shell.execute_reply":"2022-08-09T07:00:17.388338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusion:","metadata":{}},{"cell_type":"markdown","source":"* Our image data is stored in  the array Pixel Data \n* The rows and attribute columns yield us the image size\n* The pixel spacing and the slice thickness tell us how much physical distance is overed by one pixel in mm.\n* These can be used to calculate voxel size which holds the volume of one pixel.\n* The third value of the image position helps us to order the files right way from bottom to top ","metadata":{}},{"cell_type":"markdown","source":"# Transform the data to the household units","metadata":{}},{"cell_type":"code","source":"image=example_file.pixel_array.flatten()\nrescaled_image=image*example_file.RescaleSlope + example_file.RescaleIntercept","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.390198Z","iopub.execute_input":"2022-08-09T07:00:17.390891Z","iopub.status.idle":"2022-08-09T07:00:17.399108Z","shell.execute_reply.started":"2022-08-09T07:00:17.390856Z","shell.execute_reply":"2022-08-09T07:00:17.39779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,3,figsize=(20,5))\nax[0].imshow(example_file.pixel_array, cmap=\"bone\")\nax[0].axis(\"off\")\nsns.distplot(image.flatten(), ax=ax[1]);\nsns.distplot(rescaled_image.flatten(), ax=ax[2])\nax[1].set_title(\"Raw pixel array distributions\")\nax[2].set_title(\"HU unit distributions\")","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:17.400843Z","iopub.execute_input":"2022-08-09T07:00:17.401464Z","iopub.status.idle":"2022-08-09T07:00:19.929867Z","shell.execute_reply.started":"2022-08-09T07:00:17.401429Z","shell.execute_reply":"2022-08-09T07:00:19.928612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conclusions:\n* In the raw pixel array distribution most of the values exists between 0 and 1000. There is a sharp peak at -2000. These values belong to the outside scanner region which is coloured black in the raw ct-scan image . It's also been possible these values have been set to even lower values like -3000 or lower.\n* After transforming to HU Units there is a peak at -3000, at roughly -1000(air) and a peak at 0(water)","metadata":{}},{"cell_type":"code","source":"def set_outside_scanner_to_air(raw_pixelarrays):\n    raw_pixelarrays[raw_pixelarrays<=-1000]=0\n    return raw+pixelarrays\n\ndef transform_to_HU(slices):\n    images=np.stack([file.pixel_array for file in slices])\n    images = images.astype(np.int16)\n    for n in range(len(slices)):\n        \n        intercept = slices[n].RescaleIntercept\n        slope = slices[n].RescaleSlope\n        \n        if slope != 1:\n            images[n] = slope * images[n].astype(np.float64)\n            images[n] = images[n].astype(np.int16)\n            \n        images[n] += np.int16(intercept)\n    \n    return np.array(images, dtype=np.int16)\n\ndef load_scans(study_id_path):\n    slices = [pydicom.dcmread(study_id_path + \"/\" + file) for file in listdir(study_id_path)]\n    slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))\n    \n    hu_images = transform_to_hu(slices)\n    return hu_images","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:19.931809Z","iopub.execute_input":"2022-08-09T07:00:19.932189Z","iopub.status.idle":"2022-08-09T07:00:19.94204Z","shell.execute_reply.started":"2022-08-09T07:00:19.932156Z","shell.execute_reply":"2022-08-09T07:00:19.940881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* The dicom file contains much more information that is worth it to explore but to get started with a model, it might be sufficient to use the images that had been transformed to Hounsfield Units.","metadata":{}},{"cell_type":"code","source":"train[train.StudyInstanceUID==\"1.2.826.0.1.3680043.10001\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:19.943846Z","iopub.execute_input":"2022-08-09T07:00:19.944309Z","iopub.status.idle":"2022-08-09T07:00:19.969055Z","shell.execute_reply.started":"2022-08-09T07:00:19.944266Z","shell.execute_reply":"2022-08-09T07:00:19.967678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_path = \"../input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/\"","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:19.974645Z","iopub.execute_input":"2022-08-09T07:00:19.975634Z","iopub.status.idle":"2022-08-09T07:00:19.980631Z","shell.execute_reply.started":"2022-08-09T07:00:19.975591Z","shell.execute_reply":"2022-08-09T07:00:19.979288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_files = listdir(patient_path)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:19.981673Z","iopub.execute_input":"2022-08-09T07:00:19.98203Z","iopub.status.idle":"2022-08-09T07:00:20.029444Z","shell.execute_reply.started":"2022-08-09T07:00:19.981998Z","shell.execute_reply":"2022-08-09T07:00:20.028523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport os\nimport tensorflow_hub as hub\nimport pydicom as dicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom tqdm import tqdm\nimport cv2 as cv\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:00:20.03103Z","iopub.execute_input":"2022-08-09T07:00:20.032263Z","iopub.status.idle":"2022-08-09T07:00:20.039636Z","shell.execute_reply.started":"2022-08-09T07:00:20.032213Z","shell.execute_reply":"2022-08-09T07:00:20.038563Z"},"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-08-09T07:00:20.041105Z","iopub.execute_input":"2022-08-09T07:00:20.042522Z","iopub.status.idle":"2022-08-09T07:00:20.161914Z","shell.execute_reply.started":"2022-08-09T07:00:20.042432Z","shell.execute_reply":"2022-08-09T07:00:20.160603Z"},"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-09T07:00:20.163498Z","iopub.execute_input":"2022-08-09T07:00:20.163842Z","iopub.status.idle":"2022-08-09T07:00:20.171103Z","shell.execute_reply.started":"2022-08-09T07:00:20.163811Z","shell.execute_reply":"2022-08-09T07:00:20.16923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pydicom.data import get_testdata_files\ntrainset=[]\ntrainlabel=[]\ntrainidt=[]\nlimit = 50 \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        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, '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-09T07:00:20.173155Z","iopub.execute_input":"2022-08-09T07:00:20.174356Z","iopub.status.idle":"2022-08-09T07:03:17.271238Z","shell.execute_reply.started":"2022-08-09T07:00:20.174292Z","shell.execute_reply":"2022-08-09T07:03:17.269909Z"},"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-09T07:03:17.272939Z","iopub.execute_input":"2022-08-09T07:03:17.274302Z","iopub.status.idle":"2022-08-09T07:03:17.280726Z","shell.execute_reply.started":"2022-08-09T07:03:17.274249Z","shell.execute_reply":"2022-08-09T07:03:17.27922Z"},"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-09T07:03:17.282317Z","iopub.execute_input":"2022-08-09T07:03:17.282821Z","iopub.status.idle":"2022-08-09T07:03:25.230249Z","shell.execute_reply.started":"2022-08-09T07:03:17.282782Z","shell.execute_reply":"2022-08-09T07:03:25.228912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = np.array(testset)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:03:25.231782Z","iopub.execute_input":"2022-08-09T07:03:25.232477Z","iopub.status.idle":"2022-08-09T07:03:25.249112Z","shell.execute_reply.started":"2022-08-09T07:03:25.232437Z","shell.execute_reply":"2022-08-09T07:03:25.247737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import BatchNormalization, Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:03:25.250591Z","iopub.execute_input":"2022-08-09T07:03:25.251385Z","iopub.status.idle":"2022-08-09T07:03:25.256898Z","shell.execute_reply.started":"2022-08-09T07:03:25.251345Z","shell.execute_reply":"2022-08-09T07:03:25.255517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(32, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.05))\n\n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(64, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2)))\n    model.add(Dropout(0.05))\n\n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(128, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.05))\n    \n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(256, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.25)) \n    \n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(128, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.05))   \n\n    model = tf.keras.models.Sequential()\n    model.add(BatchNormalization(input_shape = X_train.shape[1:]))\n    model.add(Conv2D(64, (5, 5), padding='same', activation='elu'))\n    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\n    model.add(Dropout(0.05))\n    \n    model.add(Flatten())\n    model.add(Dense(32))\n    model.add(Activation('elu'))\n    model.add(Dropout(0.25))\n    model.add(Dense(7))\n    model.add(Activation('softmax'))\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:03:25.258393Z","iopub.execute_input":"2022-08-09T07:03:25.258848Z","iopub.status.idle":"2022-08-09T07:03:25.279035Z","shell.execute_reply.started":"2022-08-09T07:03:25.258804Z","shell.execute_reply":"2022-08-09T07:03:25.277718Z"},"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-09T07:03:25.280609Z","iopub.execute_input":"2022-08-09T07:03:25.281724Z","iopub.status.idle":"2022-08-09T07:03:25.430074Z","shell.execute_reply.started":"2022-08-09T07:03:25.281687Z","shell.execute_reply":"2022-08-09T07:03:25.42913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.compile(\n  optimizer=tf.keras.optimizers.Nadam(learning_rate=0.0005),\n  loss='binary_crossentropy',\n  metrics=['accuracy'])\n\ncallbacks = [\n    # TensorBoard will store logs for each epoch and graph performance for us.\n    # tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1),\n    # ModelCheckpoint will save models after each epoch for retrieval later.\n    # tf.keras.callbacks.ModelCheckpoint(checkpoint_path),\n    # EarlyStopping will terminate training when val_loss ceases to improve.\n    tf.keras.callbacks.EarlyStopping(monitor=\"loss\", min_delta=0.001, \n                                     patience=10, restore_best_weights=True)\n]\n\n\nmodel.fit(\n    X_train, Y_train,\n    epochs=100,\n    batch_size=64,  \n    callbacks=callbacks\n)\n\nmodel.save_weights('./fashion_mnist.h5', overwrite=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:03:25.431456Z","iopub.execute_input":"2022-08-09T07:03:25.43204Z","iopub.status.idle":"2022-08-09T07:32:13.76463Z","shell.execute_reply.started":"2022-08-09T07:03:25.432005Z","shell.execute_reply":"2022-08-09T07:32:13.763223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:32:13.767437Z","iopub.execute_input":"2022-08-09T07:32:13.767802Z","iopub.status.idle":"2022-08-09T07:32:14.26713Z","shell.execute_reply.started":"2022-08-09T07:32:13.76777Z","shell.execute_reply":"2022-08-09T07:32:14.266121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(columns = train.columns, index = range(len(testidt)))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:32:45.599453Z","iopub.execute_input":"2022-08-09T07:32:45.600188Z","iopub.status.idle":"2022-08-09T07:32:45.611866Z","shell.execute_reply.started":"2022-08-09T07:32:45.600149Z","shell.execute_reply":"2022-08-09T07:32:45.6107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(len(testidt))):\n    result.loc[i, 'StudyInstanceUID'] = testidt[i]\n    rows = np.int64(y_pred[i]>0.07)\n    result.loc[i, 'patient_overall'] = int(bool(np.sum(rows)))\n    result.loc[i, 'C1'] = rows[0]\n    result.loc[i, 'C2'] = rows[1]\n    result.loc[i, 'C3'] = rows[2]\n    result.loc[i, 'C4'] = rows[3]\n    result.loc[i, 'C5'] = rows[4]\n    result.loc[i, 'C6'] = rows[5]\n    result.loc[i, 'C7'] = rows[6]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:32:49.913915Z","iopub.execute_input":"2022-08-09T07:32:49.914625Z","iopub.status.idle":"2022-08-09T07:32:50.459881Z","shell.execute_reply.started":"2022-08-09T07:32:49.914581Z","shell.execute_reply":"2022-08-09T07:32:50.458433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"means = result[['patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']].mean().to_dict()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T07:32:52.487132Z","iopub.execute_input":"2022-08-09T07:32:52.487971Z","iopub.status.idle":"2022-08-09T07:32:52.509256Z","shell.execute_reply.started":"2022-08-09T07:32:52.487932Z","shell.execute_reply":"2022-08-09T07:32:52.507261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df2 = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/test.csv')\n# means = train_df.mean(numeric_only=True).to_dict()\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-09T07:32:59.35163Z","iopub.execute_input":"2022-08-09T07:32:59.352044Z","iopub.status.idle":"2022-08-09T07:32:59.393877Z","shell.execute_reply.started":"2022-08-09T07:32:59.35201Z","shell.execute_reply":"2022-08-09T07:32:59.392718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}