{"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":"# Is the Cervical Spine fractured?","metadata":{}},{"cell_type":"markdown","source":"In this notebook I started with the notebook 'Is it a bird?' by the first lesson of the fastai course.\n\nI wanted to use it to make a first guess for the new kaggle competition [RSNA 2022 Cervical Spine Fracture Detection\n](https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/code).\n\nThe approach is a bit different because I am not searching for pictures on the web but using the dataset provided in the kaggle competition.","metadata":{}},{"cell_type":"code","source":"!pip install -Uqq fastai","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-08T14:05:39.984558Z","iopub.execute_input":"2022-08-08T14:05:39.985883Z","iopub.status.idle":"2022-08-08T14:05:49.646164Z","shell.execute_reply.started":"2022-08-08T14:05:39.985799Z","shell.execute_reply":"2022-08-08T14:05:49.644817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastcore.all import *\nfrom fastai.vision.all import *\nimport pandas as pd\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:49.653794Z","iopub.execute_input":"2022-08-08T14:05:49.654129Z","iopub.status.idle":"2022-08-08T14:05:50.699958Z","shell.execute_reply.started":"2022-08-08T14:05:49.654093Z","shell.execute_reply":"2022-08-08T14:05:50.698737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# defining the path to our training images\npath = Path('../input/rsna-csfd-256x256-jpg-dataset/train_images')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:50.701277Z","iopub.execute_input":"2022-08-08T14:05:50.701818Z","iopub.status.idle":"2022-08-08T14:05:50.706873Z","shell.execute_reply.started":"2022-08-08T14:05:50.701777Z","shell.execute_reply":"2022-08-08T14:05:50.70586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's have a look at the table with the target:","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:50.709738Z","iopub.execute_input":"2022-08-08T14:05:50.710761Z","iopub.status.idle":"2022-08-08T14:05:50.735927Z","shell.execute_reply.started":"2022-08-08T14:05:50.710723Z","shell.execute_reply":"2022-08-08T14:05:50.734916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The target here is the column <code>patient_overall</code>, which shows a 1 if the patient is fractured.","metadata":{}},{"cell_type":"markdown","source":"## Train our model","metadata":{}},{"cell_type":"markdown","source":"To train a model, we'll need `DataLoaders`, which is an object that contains a *training set* (the images used to create a model) and a *validation set* (the images used to check the accuracy of a model -- not used during training). In `fastai` we can create that easily using a `DataBlock`, and view sample images from it:","metadata":{}},{"cell_type":"code","source":"#taking first image in every folder\ndef get_x(r): return path/r['StudyInstanceUID']/os.listdir(path/r['StudyInstanceUID'])[0]\ndef get_y(r): return r['patient_overall']","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:50.737375Z","iopub.execute_input":"2022-08-08T14:05:50.738104Z","iopub.status.idle":"2022-08-08T14:05:50.743727Z","shell.execute_reply.started":"2022-08-08T14:05:50.73807Z","shell.execute_reply":"2022-08-08T14:05:50.742777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks=(ImageBlock, CategoryBlock),\n                   splitter=RandomSplitter(valid_pct=0.2, seed=42),\n                   get_x=get_x, \n                   get_y=get_y,\n                   item_tfms = RandomResizedCrop(128, min_scale=0.35))\ndls = dblock.dataloaders(df)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:50.745304Z","iopub.execute_input":"2022-08-08T14:05:50.746209Z","iopub.status.idle":"2022-08-08T14:05:52.833915Z","shell.execute_reply.started":"2022-08-08T14:05:50.746172Z","shell.execute_reply":"2022-08-08T14:05:52.832835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch(max_n=6)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:52.835579Z","iopub.execute_input":"2022-08-08T14:05:52.835995Z","iopub.status.idle":"2022-08-08T14:05:53.48253Z","shell.execute_reply.started":"2022-08-08T14:05:52.835955Z","shell.execute_reply":"2022-08-08T14:05:53.481534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(30)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:05:53.483581Z","iopub.execute_input":"2022-08-08T14:05:53.48388Z","iopub.status.idle":"2022-08-08T14:09:50.131079Z","shell.execute_reply.started":"2022-08-08T14:05:53.483852Z","shell.execute_reply":"2022-08-08T14:09:50.129819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Conclusions","metadata":{"execution":{"iopub.status.busy":"2022-08-08T14:01:16.824442Z","iopub.execute_input":"2022-08-08T14:01:16.825109Z","iopub.status.idle":"2022-08-08T14:01:16.832101Z","shell.execute_reply.started":"2022-08-08T14:01:16.825062Z","shell.execute_reply":"2022-08-08T14:01:16.829194Z"}}},{"cell_type":"markdown","source":"As we can see, the error rate is quite bad with 0.38. What can we do to improve?\n- Not randomly take the first image from every patient to compare but get a clue of which image is valuable for finding the target\n- preprocess images so they are better comparable \n- Do a general EDA to get a sense of the topic","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}