{"cells":[{"metadata":{},"cell_type":"markdown","source":"## What are we predicting?\n\nIn this competition, you’ll detect the presence and position of catheters and lines on chest x-rays. Use machine learning to train and test your model on 40,000 images to categorize a tube that is poorly placed.\n\n## Evaluation criteria?\n\nSubmissions are evaluated on area under the ROC curve between the predicted probability and the observed target.\nTo calculate the final score, AUC is calculated for each of the 11 labels, then averaged. The score is then the average of the individual AUCs of each predicted column.\n\n## Train vs Test?\n\nA code-only competition so there is a hidden test set (approximately 4x larger, with ~14k images) as well.\n\ntrain.csv contains image IDs, binary labels, and patient IDs.\n\nTFRecords are available for both train and test. (They are also available for the hidden test set.)\n\ntrain_annotations.csv includes segmentation annotations for training samples that have them as solely additional information.\n\n## Similar Dataset & Competitions?\n\n[RSNA Pneumonia Detection Challenge](\"https://www.kaggle.com/c/rsna-pneumonia-detection-challenge/notebooks\")\n\n[SIIM-ACR Pneumothorax Segmentation](\"https://www.kaggle.com/c/siim-acr-pneumothorax-segmentation\")\n\n[XRay Lung Segmentation](\"https://www.kaggle.com/c/xray-lung-segmentation/data\")\n\n[RSNA Pneumonia Detection Challenge](\"https://www.kaggle.com/c/rsna-pneumonia-detection-challenge/notebooks\")\n\nHave more?"},{"metadata":{},"cell_type":"markdown","source":"## Read"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\ntrain = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import plotly.express as px\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objs as go\nimport matplotlib.pyplot as plt\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cols = [\n    'ETT - Abnormal', 'ETT - Borderline', 'ETT - Normal', \n    'NGT - Abnormal', 'NGT - Borderline', 'NGT - Incompletely Imaged', \n    'NGT - Normal', 'CVC - Abnormal', 'CVC - Borderline', \n    'CVC - Normal', 'Swan Ganz Catheter Present'\n]\n\nfig = make_subplots(rows=4, cols=3)\n\ntraces = [\n    go.Bar(\n        x=[0, 1], \n        y=[\n            len(train[train[col]==0]),\n            len(train[train[col]==1])\n        ], \n        name=col,\n        text = [\n            str(round(100 * len(train[train[col]==0]) / len(train), 2)) + '%',\n            str(round(100 * len(train[train[col]==1]) / len(train), 2)) + '%'\n        ],\n        textposition='auto'\n    ) for col in cols\n]\n\nfor i in range(len(traces)):\n    fig.append_trace(traces[i], (i // 3) + 1, (i % 3)  +1)\n\nfig.update_layout(\n    title_text='Train columns',\n    height=1200,\n    width=1000\n)\n\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"[Creds](\"https://www.kaggle.com/isaienkov/ranzcr-clip-data-understanding\")"},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nf, plots = plt.subplots(1, 5, sharex='col', sharey='row', figsize=(17, 17))\nsamples = train.sample(n=5, random_state=666)['StudyInstanceUID'].values\n\nfor i in range(5):\n    image = cv2.imread(os.path.join(\"/kaggle/input/ranzcr-clip-catheter-line-classification/train/\", f\"{samples[i]}.jpg\"))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plots[i].imshow(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.iloc[:, 1:-1].sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(train.iloc[:, 1:-1].sum())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import random, os\nimport numpy as np\nimport torch\nfrom fastai.vision.all import *\nfrom fastai.callback import mixup\npath = Path('../input/ranzcr-clip-catheter-line-classification/')\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['path'] = train.StudyInstanceUID.map(lambda x:str(path / 'train' / x)+'.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = list(train.columns[1:12].values)\nlabels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_y(fname):\n    return fname[1:12].values.astype(np.float32)\n\ndef get_x(fname):\n    return fname[-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"item_tfms = RandomResizedCrop(256, min_scale=0.75, ratio=(1.,1.))\n\ndls = DataBlock(blocks=(ImageBlock, MultiCategoryBlock(encoded=True, vocab=labels)),\n                        get_x = get_x,get_y = get_y,  \n                        item_tfms = item_tfms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = dls.dataloaders(train)\ndls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet152, metrics = [accuracy_multi], cbs=[mixup.MixUp()], model_dir=\"/tmp/model/\").to_native_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, resnet50, metrics = [accuracy_multi], cbs=[mixup.MixUp()], model_dir=\"/tmp/model/\").to_native_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fine_tune(3, 2e-2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = learn.to_native_fp32()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/ranzcr-clip-catheter-line-classification')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv')\nsubmission_df.iloc[:,1:] = submission_df.iloc[:,1:].astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data_path = submission_df['StudyInstanceUID'].apply(lambda x: path/'test'/(x+'.jpg'))\ntst_dl = learn.dls.test_dl(test_data_path)\npreds,targs = learn.tta(dl = tst_dl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"columns = list(train.columns[1:12])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df[columns] = pd.DataFrame(preds,columns=columns)\n# submission_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.csv',index=False)  ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}