{"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":"markdown","source":"# Team Kenya Modelling & Submission","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom keras.layers import Dense,Flatten\nfrom keras.models import Model\nfrom keras.applications.inception_v3 import InceptionV3,preprocess_input\nfrom keras.preprocessing.image import ImageDataGenerator\nimport keras\n","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:07:25.444056Z","iopub.execute_input":"2022-10-09T11:07:25.444502Z","iopub.status.idle":"2022-10-09T11:07:25.461954Z","shell.execute_reply.started":"2022-10-09T11:07:25.444465Z","shell.execute_reply":"2022-10-09T11:07:25.460782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom as dicom\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:56:40.824982Z","iopub.execute_input":"2022-10-09T11:56:40.82552Z","iopub.status.idle":"2022-10-09T11:56:41.061409Z","shell.execute_reply.started":"2022-10-09T11:56:40.825474Z","shell.execute_reply":"2022-10-09T11:56:41.059489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\ntrain_bbox = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv\")\ntest_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\nss = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:07:37.445251Z","iopub.execute_input":"2022-10-09T11:07:37.445704Z","iopub.status.idle":"2022-10-09T11:07:37.47637Z","shell.execute_reply.started":"2022-10-09T11:07:37.445668Z","shell.execute_reply":"2022-10-09T11:07:37.475127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport re\nDATA_DIR = \"../input/rsna-2022-cervical-spine-fracture-detection\"\nTRAIN_DIR = os.path.join(DATA_DIR, \"train_images\")\nTEST_DIR = os.path.join(DATA_DIR,\"test_images\")\nSEGM_DIR = os.path.join(DATA_DIR, \"segmentations\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:28:31.684039Z","iopub.execute_input":"2022-10-09T11:28:31.684466Z","iopub.status.idle":"2022-10-09T11:28:31.690704Z","shell.execute_reply.started":"2022-10-09T11:28:31.684424Z","shell.execute_reply":"2022-10-09T11:28:31.689482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:08:21.336812Z","iopub.execute_input":"2022-10-09T11:08:21.337262Z","iopub.status.idle":"2022-10-09T11:08:21.347263Z","shell.execute_reply.started":"2022-10-09T11:08:21.337226Z","shell.execute_reply":"2022-10-09T11:08:21.345879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:08:25.575174Z","iopub.execute_input":"2022-10-09T11:08:25.576172Z","iopub.status.idle":"2022-10-09T11:08:25.599214Z","shell.execute_reply.started":"2022-10-09T11:08:25.576114Z","shell.execute_reply":"2022-10-09T11:08:25.597973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T11:08:29.251703Z","iopub.execute_input":"2022-10-09T11:08:29.252927Z","iopub.status.idle":"2022-10-09T11:08:29.270385Z","shell.execute_reply.started":"2022-10-09T11:08:29.252867Z","shell.execute_reply":"2022-10-09T11:08:29.268706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n# TODO:\n    - For the folder containting the dicom files we need to see\n    if we can access the dicom files inside the files with unique UID \n    i.e /input/rsna-2022-cervical-spine-fracture-detection/train_images/1.2.826.0.1.3680043.10001/1.dcm\"\n    The unique id : 1.2.826.0.1.3680043.10001 brings a layer of complexity of accessing our target the .dcm file\n\n\n\"\"\"\n\n# create a list of every .dcm file in the folder test_dir we will do the same for TEST\ntrain_slices = glob.glob(f'{TRAIN_DIR}/*/*')\n# print(test_slices)\n\n# find all files ending with .dcm and the folder UID leading to it i.e folder name uid:(1.2.826.0.1.3680043.22327),file name id:(257)\ntrain_slices = [re.findall(f'{TRAIN_DIR}/(.*)/(.*).dcm', s)[0] for s in train_slices]\nprint(train_slices[:10])\n\n# From the result create a df with the folder name and file id ordered\ndf_train_slices = pd.DataFrame(data=train_slices, columns=['StudyInstanceUID', 'Slice']).astype({'Slice': int}).reset_index(drop=True)\n\n# With this we can be able to access the UID and id of the dicom folder and loop through the df\ndf_train_slices","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:10:57.734812Z","iopub.execute_input":"2022-10-09T12:10:57.735321Z","iopub.status.idle":"2022-10-09T12:11:03.274684Z","shell.execute_reply.started":"2022-10-09T12:10:57.735264Z","shell.execute_reply":"2022-10-09T12:11:03.273383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_slices = glob.glob(f'{TEST_DIR}/*/*')\n# print(test_slices)\n\n# find all files ending with .dcm and the folder UID leading to it i.e folder name uid:(1.2.826.0.1.3680043.22327),file name id:(257)\ntest_slices = [re.findall(f'{TEST_DIR}/(.*)/(.*).dcm', s)[0] for s in test_slices]\nprint(test_slices[:10])\n\n# From the result create a df with the folder name and file id ordered\ndf_test_slices = pd.DataFrame(data=test_slices, columns=['StudyInstanceUID', 'Slice']).astype({'Slice': int}).reset_index(drop=True)\n\n# With this we can be able to access the UID and id of the dicom folder and loop through the df\ndf_test_slices","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:07:03.33281Z","iopub.execute_input":"2022-10-09T12:07:03.333229Z","iopub.status.idle":"2022-10-09T12:07:03.364169Z","shell.execute_reply.started":"2022-10-09T12:07:03.333195Z","shell.execute_reply":"2022-10-09T12:07:03.362951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    \"\"\"\n    Credits: @VLADIMIR SLAYKOVSKIY [https://www.kaggle.com/code/vslaykovsky/infer-pytorch-effnetv2-single-model-lb-0-49]\n    This supports loading both regular and compressed JPEG images. \n    See the first sell with `pip install` commands for the necessary dependencies\n    \"\"\"\n    img=dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\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 cv2.cvtColor(data, cv2.COLOR_GRAY2RGB), img","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:11:17.282168Z","iopub.execute_input":"2022-10-09T12:11:17.282652Z","iopub.status.idle":"2022-10-09T12:11:17.290339Z","shell.execute_reply.started":"2022-10-09T12:11:17.282614Z","shell.execute_reply":"2022-10-09T12:11:17.289053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display train and test images\nim_train, meta_trian = load_dicom(f'{TRAIN_DIR}/1.2.826.0.1.3680043.17625/12.dcm')\nplt.figure()\nplt.imshow(im_train)\nplt.title(\"Test TRAIN Image Display\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:21:50.057084Z","iopub.execute_input":"2022-10-09T12:21:50.058167Z","iopub.status.idle":"2022-10-09T12:21:50.285517Z","shell.execute_reply.started":"2022-10-09T12:21:50.058118Z","shell.execute_reply":"2022-10-09T12:21:50.284663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    meta","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:21:20.320895Z","iopub.execute_input":"2022-10-09T12:21:20.321355Z","iopub.status.idle":"2022-10-09T12:21:20.330861Z","shell.execute_reply.started":"2022-10-09T12:21:20.321315Z","shell.execute_reply":"2022-10-09T12:21:20.329643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_test, meta_test = load_dicom(f'{TEST_DIR}/1.2.826.0.1.3680043.22327/38.dcm')\nplt.figure()\nplt.imshow(im_test)\nplt.title(\"Test TEST Image Display\")","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:22:15.271461Z","iopub.execute_input":"2022-10-09T12:22:15.271905Z","iopub.status.idle":"2022-10-09T12:22:15.505133Z","shell.execute_reply.started":"2022-10-09T12:22:15.27187Z","shell.execute_reply":"2022-10-09T12:22:15.503936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:22:37.929166Z","iopub.execute_input":"2022-10-09T12:22:37.930579Z","iopub.status.idle":"2022-10-09T12:22:37.938154Z","shell.execute_reply.started":"2022-10-09T12:22:37.930516Z","shell.execute_reply":"2022-10-09T12:22:37.937205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling & Submission","metadata":{}},{"cell_type":"code","source":"# Base model\n# Credits [https://www.youtube.com/watch?v=ejkRh9obVjk&ab_channel=edureka%21]\nb_mdl = InceptionV3(input_shape=(512, 512, 3),include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:24:13.787178Z","iopub.execute_input":"2022-10-09T12:24:13.787632Z","iopub.status.idle":"2022-10-09T12:24:17.667749Z","shell.execute_reply.started":"2022-10-09T12:24:13.787585Z","shell.execute_reply":"2022-10-09T12:24:17.666489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for single_layer in b_mdl.layers:\n#     print(single_layer)\n    single_layer.trainable = False\n    pritn('Done')","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:26:19.203046Z","iopub.execute_input":"2022-10-09T12:26:19.203542Z","iopub.status.idle":"2022-10-09T12:26:19.222167Z","shell.execute_reply.started":"2022-10-09T12:26:19.203497Z","shell.execute_reply":"2022-10-09T12:26:19.220486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = Flatten()(b_mdl.output)\nX = Dense(units=2,activation='sigmoid')(X)\n\n# Final model\nmodel = Model(b_mdl.input,X)\n\n# Compiling mdl\nmodel.compile(optimizer='adam',loss=keras.losses.binary_crossentropy,metrics=['accuracy'])\nprint('Done')","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:34:19.055556Z","iopub.execute_input":"2022-10-09T12:34:19.056449Z","iopub.status.idle":"2022-10-09T12:34:19.108982Z","shell.execute_reply.started":"2022-10-09T12:34:19.056401Z","shell.execute_reply":"2022-10-09T12:34:19.10772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summary\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-09T12:35:01.058106Z","iopub.execute_input":"2022-10-09T12:35:01.05866Z","iopub.status.idle":"2022-10-09T12:35:01.063984Z","shell.execute_reply.started":"2022-10-09T12:35:01.058612Z","shell.execute_reply":"2022-10-09T12:35:01.06261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(featurewise_center=True,\n                                   rotation_range=0.4,\n                                   width_shift_range=0.3,\n                                   horizontal_flip=True,\n                                   vertical_flip=True,\n                                   height_shift_range=0.2,\n                                   zoom_range=0.4,\n                                   preprocessing_function=preprocess_input,\n                                   shear_range=0.4, \n                                   fill_mode='nearest')\n\ntrain_generator = train_datagen.flow_from_directory(glob.glob(f'{TRAIN_DIR}/*/*'),batch_size=32,class_mode='categorical',target_size=(512, 512))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}