{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"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        print(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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":false,"_kg_hide-output":false},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.ensemble import RandomForestClassifier\ntrain = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train.csv')\ntest = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv')\nprint(train.shape)\nprint(test.shape)\ntrain.head(10)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import ast\n\nannot = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/train_annotations.csv')\nprint(annot.shape)\nannot.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tqdm\nimport os\nimport gc\nimport cv2\nimport glob\nimport numpy as np\nimport pandas as pd\nfrom numba import cuda\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\n\nRES = np.zeros( (512,512) )\nmask = RES.copy()\nmask[mask>0.5] = 1.\nmask[mask<1] = 0\nmask = mask.astype(np.uint8)\nmask = np.stack( (mask,mask,mask), 2 )\n\ndel RES\ngc.collect()\nplt.imshow(mask)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Chest X-Ray Visualisation"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -Uq fastai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"../input/ranzcr-clip-catheter-line-classification/train_annotations.csv\")[['StudyInstanceUID', 'label']]\ndf['StudyInstanceUID'] = [i + \".jpg\" for i in df['StudyInstanceUID']]\ndf.columns = ['name', 'label']\ndls = ImageDataLoaders.from_df(df, \"../input/ranzcr-clip-catheter-line-classification/\", folder='train', valid_pct=0.2, item_tfms=Resize(224))\ndls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Build the CNN model with the pretrained resnet34\n# Error rate = 1 - accuracy\nlearn = cnn_learner(dls, models.resnet34, metrics = [accuracy])\n# Train the model on 5 epochs of data at the default learning rate\nlearn.fit_one_cycle(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Rebuild interpreter and replot confusion matrix\ninterp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(figsize=(12,12), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit = False\n\nif submit:\n    test_preds = []\n    for i in range(len(enet_type)):\n        if enet_type[i] == 'resnet34':\n            print('resnet34 loaded')\n            model = models.resnet34(enet_type[i], out_dim=len(target_cols))\n            model = model.to(device)\n        model.load_state_dict(torch.load(model_path[i], map_location='cuda:0'))\n        if tta:\n            test_preds += [tta_inference_func(test_loader)]\n        else:\n            test_preds += [inference_func(test_loader)]\n\n    submission = pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv')\n    submission[target_cols] = np.mean(test_preds, axis=0)\n    submission.to_csv('submission.csv', index=False)\nelse:\n    pd.read_csv('../input/ranzcr-clip-catheter-line-classification/sample_submission.csv').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}