{"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":"# **Pneumonia Detection**","metadata":{"id":"MySRJxnbmVr-"}},{"cell_type":"code","source":"#!pip install tensorflow==2.0.0\n#!pip install keras==2.3.1","metadata":{"id":"z0iGewD2FDoO","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport tensorflow as tf\nimport numpy as np\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport tqdm\nfrom tqdm import tqdm_notebook\nfrom matplotlib.patches import Rectangle\nimport seaborn as sns\n!pip install pydicom\nimport pydicom as dcm\n%matplotlib inline \nIS_LOCAL = False\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\n# for 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 5GB 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","metadata":{"id":"m-n_fZEMyddM","outputId":"5397e8c1-57b4-4125-b43d-a0d9263562f9","execution":{"iopub.status.busy":"2021-07-02T00:22:11.81946Z","iopub.execute_input":"2021-07-02T00:22:11.819866Z","iopub.status.idle":"2021-07-02T00:22:18.473122Z","shell.execute_reply.started":"2021-07-02T00:22:11.819833Z","shell.execute_reply":"2021-07-02T00:22:18.472339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Load Datasets**","metadata":{"id":"Zmkh_kre12-c"}},{"cell_type":"code","source":"detailclassinfo_df = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_detailed_class_info.csv')\ntrainlabels_df = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')","metadata":{"id":"aZMo4BIBz6BZ","execution":{"iopub.status.busy":"2021-07-02T00:22:31.4144Z","iopub.execute_input":"2021-07-02T00:22:31.41478Z","iopub.status.idle":"2021-07-02T00:22:31.493442Z","shell.execute_reply.started":"2021-07-02T00:22:31.414748Z","shell.execute_reply":"2021-07-02T00:22:31.492405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"detailclassinfo_df.head()","metadata":{"id":"qqifXEBw4_ov","outputId":"20b617f7-fa9f-4cc7-8238-da75f7906165","execution":{"iopub.status.busy":"2021-07-02T00:22:34.763043Z","iopub.execute_input":"2021-07-02T00:22:34.763411Z","iopub.status.idle":"2021-07-02T00:22:34.775334Z","shell.execute_reply.started":"2021-07-02T00:22:34.763379Z","shell.execute_reply":"2021-07-02T00:22:34.774549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainlabels_df.head()","metadata":{"id":"Jx5gfMzZ5Gsq","outputId":"49c1eaed-496f-4cb6-c6c2-3ca30b627b1c","execution":{"iopub.status.busy":"2021-07-02T00:22:38.293566Z","iopub.execute_input":"2021-07-02T00:22:38.29392Z","iopub.status.idle":"2021-07-02T00:22:38.308244Z","shell.execute_reply.started":"2021-07-02T00:22:38.293889Z","shell.execute_reply":"2021-07-02T00:22:38.307086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of Detailed Class Information: {}'.format(detailclassinfo_df.shape))\nprint('Shape of Train Labels: {}'.format(trainlabels_df.shape))","metadata":{"id":"zq6WCLu4JpX-","outputId":"d44c1350-c1dd-462b-aab1-338ce34ac6d2","execution":{"iopub.status.busy":"2021-07-02T00:22:40.939436Z","iopub.execute_input":"2021-07-02T00:22:40.939856Z","iopub.status.idle":"2021-07-02T00:22:40.94654Z","shell.execute_reply.started":"2021-07-02T00:22:40.939816Z","shell.execute_reply":"2021-07-02T00:22:40.945176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# **Exploratory Data Analysis**","metadata":{"id":"iGu3Hn5l6QjJ"}},{"cell_type":"markdown","source":"**Data Preprocessing**","metadata":{"id":"QTF-3qZIFLld"}},{"cell_type":"markdown","source":"Merging Datasets","metadata":{"id":"pmyKRbXYLxU2"}},{"cell_type":"code","source":"merge_train_df = trainlabels_df.merge(detailclassinfo_df, left_on='patientId', right_on='patientId', how='inner')\nmerge_train_df.sample(5)","metadata":{"id":"2pXgsLcNL0U9","outputId":"c99e30d2-1aab-42c2-82c8-3fcda93719d1","execution":{"iopub.status.busy":"2021-07-02T00:22:44.236096Z","iopub.execute_input":"2021-07-02T00:22:44.236512Z","iopub.status.idle":"2021-07-02T00:22:44.286308Z","shell.execute_reply.started":"2021-07-02T00:22:44.23646Z","shell.execute_reply":"2021-07-02T00:22:44.285373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_train_df = merge_train_df. drop_duplicates()\nmerge_train_df.shape","metadata":{"id":"fAJE_cAuBwT2","outputId":"66a572c3-a0ea-4bd5-f595-58a98987a62b","execution":{"iopub.status.busy":"2021-07-02T00:22:47.174702Z","iopub.execute_input":"2021-07-02T00:22:47.175108Z","iopub.status.idle":"2021-07-02T00:22:47.201328Z","shell.execute_reply.started":"2021-07-02T00:22:47.17507Z","shell.execute_reply":"2021-07-02T00:22:47.20029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Summary on the values, types and null values**","metadata":{"id":"1bB4NZ4nAhMd"}},{"cell_type":"code","source":"merge_train_df.isnull().sum()","metadata":{"id":"P1-U8E9OCPJT","outputId":"a44922f5-e8d2-4103-9283-53fe6444deb7","execution":{"iopub.status.busy":"2021-07-02T00:22:50.008951Z","iopub.execute_input":"2021-07-02T00:22:50.009352Z","iopub.status.idle":"2021-07-02T00:22:50.025786Z","shell.execute_reply.started":"2021-07-02T00:22:50.009314Z","shell.execute_reply":"2021-07-02T00:22:50.02452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inference**: Bounding box parameters x, y , width and height are having Null/NaN values","metadata":{"id":"AY53gArSotTw"}},{"cell_type":"markdown","source":"**Distribution of classes**","metadata":{"id":"Wisr102TEpdc"}},{"cell_type":"code","source":"detailclassinfo_df.groupby([\"class\"]).count().transpose().style.background_gradient(cmap='Wistia',axis=1)","metadata":{"id":"jkvUTynOCxon","outputId":"4a00e096-89c5-444d-e5ea-2da44aa39ef6","execution":{"iopub.status.busy":"2021-07-02T00:22:53.622257Z","iopub.execute_input":"2021-07-02T00:22:53.622696Z","iopub.status.idle":"2021-07-02T00:22:53.755415Z","shell.execute_reply.started":"2021-07-02T00:22:53.622654Z","shell.execute_reply":"2021-07-02T00:22:53.754416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_train_df.groupby([\"class\"]).count().transpose().style.background_gradient(cmap='Wistia',axis=1)","metadata":{"id":"f9CIAyHSCkFf","outputId":"95f26aab-1689-4de1-9565-898cd4bf2eed","execution":{"iopub.status.busy":"2021-07-02T00:22:57.960849Z","iopub.execute_input":"2021-07-02T00:22:57.961222Z","iopub.status.idle":"2021-07-02T00:22:57.999979Z","shell.execute_reply.started":"2021-07-02T00:22:57.961188Z","shell.execute_reply":"2021-07-02T00:22:57.998784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inference**: 9555 patients are in class of 'Lung Opacity', 11821 are in 'No Lung Opacity / Not Normal' and 8851 are in Normal category.","metadata":{"id":"gx0IgwHhEt2a"}},{"cell_type":"code","source":"f, ax = plt.subplots(1,1, figsize=(6,4))\ntotal = float(len(merge_train_df))\nsns.countplot(merge_train_df['class'],order = merge_train_df['class'].value_counts().index, palette='Set1')\nfor p in ax.patches:\n    height = p.get_height()\n    ax.text(p.get_x()+p.get_width()/2.,\n            height + 3,\n            '{:1.2f}%'.format(100*height/total),\n            ha=\"center\") \nplt.show()","metadata":{"id":"UNseE0ZDDS5t","outputId":"40a517f0-264a-461a-f158-2f7edf7204b2","execution":{"iopub.status.busy":"2021-07-02T00:23:29.943603Z","iopub.execute_input":"2021-07-02T00:23:29.94408Z","iopub.status.idle":"2021-07-02T00:23:30.236584Z","shell.execute_reply.started":"2021-07-02T00:23:29.94404Z","shell.execute_reply":"2021-07-02T00:23:30.235537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insight**: The classes \"No Lung Opacity / Not Normal\", \"Lung Opacity\" and \"Normal\" are in the proportions of 39%, 32% and 29% respectively","metadata":{"id":"y35jKMkzDmtu"}},{"cell_type":"markdown","source":"**Distribution of Labels (Positive and Negative)**","metadata":{"id":"_9vGuyyuMRoe"}},{"cell_type":"code","source":"f, ax = plt.subplots(1,1, figsize=(6,4))\nsns.countplot(x = \"Target\", data= merge_train_df, hue=\"class\", palette='Set1')","metadata":{"id":"OgZJkn78pyEl","outputId":"e3c7967b-9c9f-4d28-f301-baf29209452c","execution":{"iopub.status.busy":"2021-07-02T00:23:39.395413Z","iopub.execute_input":"2021-07-02T00:23:39.395769Z","iopub.status.idle":"2021-07-02T00:23:39.577227Z","shell.execute_reply.started":"2021-07-02T00:23:39.395738Z","shell.execute_reply":"2021-07-02T00:23:39.576203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.style.use('ggplot')\nf, ax = plt.subplots(1,1, figsize=(6,4))\ntotale = float(len(merge_train_df))\npd.value_counts(merge_train_df[\"Target\"]).plot(kind='bar', position=0.5, rot=0)\nfor p in ax.patches:\n    height = p.get_height()\n    ax.text(p.get_x()+p.get_width()/2.,\n            height + 3,\n            '{:1.2f}%'.format(100*height/total),\n            ha=\"center\") \nplt.show()","metadata":{"id":"DABJrBo1HuoY","outputId":"8d119f36-af78-46dc-a441-92db7892bfbc","execution":{"iopub.status.busy":"2021-07-02T00:23:42.246944Z","iopub.execute_input":"2021-07-02T00:23:42.247346Z","iopub.status.idle":"2021-07-02T00:23:42.407021Z","shell.execute_reply.started":"2021-07-02T00:23:42.247308Z","shell.execute_reply":"2021-07-02T00:23:42.40607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of positive targets\nprint(round((9555 / (9555 + 20672)) * 100, 2), '% of the patients are positive')","metadata":{"id":"6gK_kdnFMCcI","outputId":"4f2a26f2-e340-402c-bfeb-9aa2c278aae2","execution":{"iopub.status.busy":"2021-07-02T00:23:47.291122Z","iopub.execute_input":"2021-07-02T00:23:47.291578Z","iopub.status.idle":"2021-07-02T00:23:47.297478Z","shell.execute_reply.started":"2021-07-02T00:23:47.291542Z","shell.execute_reply":"2021-07-02T00:23:47.29622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge_train_df.groupby([\"Target\"]).count()","metadata":{"id":"GgSOvQsY_uUk","outputId":"fc1065d0-ed86-4b10-9da3-cdbbfab36ecf","execution":{"iopub.status.busy":"2021-07-02T00:23:50.377384Z","iopub.execute_input":"2021-07-02T00:23:50.377768Z","iopub.status.idle":"2021-07-02T00:23:50.403923Z","shell.execute_reply.started":"2021-07-02T00:23:50.377733Z","shell.execute_reply":"2021-07-02T00:23:50.402786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insight**: \n\n1. Patients with Target = 0 (no pathology detected) are either of class: Normal or class: No Lung Opacity / Not Normal don't have bounding box coordinates (x, y, w, h) parameters.\n2. **31.61%** of the patients belongs to **Lung Opacity** class.","metadata":{"id":"5LY1cWbW0JQP"}},{"cell_type":"markdown","source":"**Handling Null using KNN Imputation method**","metadata":{"id":"EWY_3--iGwtI"}},{"cell_type":"code","source":"merge_bb_df = merge_train_df.drop(columns = [\"patientId\", \"class\"])\n\n## Impute NaN with KNN mean\nfrom sklearn.impute import KNNImputer\nimputer = KNNImputer(n_neighbors=2)\nmerge_bb_imputed_df = imputer.fit_transform(merge_bb_df)\n\n## Converted array to dataframe\nmerge_bb_final = pd.DataFrame(data=merge_bb_imputed_df, columns=[\"x\", \"y\", \"width\", \"height\", \"Target\"])\nmerge_bb_final[\"Target\"] = merge_bb_final[\"Target\"].astype('int64')\nmerge_bb_final.head(5)","metadata":{"id":"hoqlpHv9G2OT","outputId":"ca1f9008-0c5c-4fbc-e3af-cffd310df4ea","execution":{"iopub.status.busy":"2021-07-02T00:24:10.76347Z","iopub.execute_input":"2021-07-02T00:24:10.763843Z","iopub.status.idle":"2021-07-02T00:24:47.451933Z","shell.execute_reply.started":"2021-07-02T00:24:10.763812Z","shell.execute_reply":"2021-07-02T00:24:47.450473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seggregating Opacity data from imputed data\nopacity_bb_final = merge_bb_final[(merge_bb_final['Target']== 1)]\nopacity_bb_final.head()","metadata":{"id":"HoWieGCbsJ73","outputId":"f7f89646-eea6-49d9-ff58-f9addc961dd0","execution":{"iopub.status.busy":"2021-07-02T00:24:47.454363Z","iopub.execute_input":"2021-07-02T00:24:47.454816Z","iopub.status.idle":"2021-07-02T00:24:47.478204Z","shell.execute_reply.started":"2021-07-02T00:24:47.454772Z","shell.execute_reply":"2021-07-02T00:24:47.476809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking Data Distribution**","metadata":{"id":"8yA4SYwOsks5"}},{"cell_type":"code","source":"fig, ax = plt.subplots(2,2,figsize=(12,12))\nsns.distplot(opacity_bb_final['x'],kde=True,bins=50, color=\"grey\", ax=ax[0,0])\nsns.distplot(opacity_bb_final['y'],kde=True,bins=50, color=\"orange\", ax=ax[0,1])\nsns.distplot(opacity_bb_final['width'],kde=True,bins=50, color=\"blue\", ax=ax[1,0])\nsns.distplot(opacity_bb_final['height'],kde=True,bins=50, color=\"brown\", ax=ax[1,1])\nlocs, labels = plt.xticks()\nplt.tick_params(axis='both', which='major', labelsize=12)\nplt.show()","metadata":{"id":"6ctzIDfQM2vx","outputId":"90b2278c-f566-4e05-cc4a-c65529f2e125","execution":{"iopub.status.busy":"2021-07-02T00:26:03.921873Z","iopub.execute_input":"2021-07-02T00:26:03.922246Z","iopub.status.idle":"2021-07-02T00:26:04.935806Z","shell.execute_reply.started":"2021-07-02T00:26:03.922215Z","shell.execute_reply":"2021-07-02T00:26:04.934827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Finding Correlation**","metadata":{"id":"H5sNSDHMstca"}},{"cell_type":"code","source":"# Correlation\nplt.figure(figsize=(7,5))\nsns.heatmap(opacity_bb_final.corr(),linewidths=0.1,vmax=1.0, \n            square=True,  linecolor='white', annot=True)\nplt.show()","metadata":{"id":"56wYNO3XsxMh","outputId":"5b3f067d-3829-4da1-99b6-e2169bd85712","execution":{"iopub.status.busy":"2021-07-02T00:26:08.697041Z","iopub.execute_input":"2021-07-02T00:26:08.697567Z","iopub.status.idle":"2021-07-02T00:26:08.977068Z","shell.execute_reply.started":"2021-07-02T00:26:08.697531Z","shell.execute_reply":"2021-07-02T00:26:08.976062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.jointplot(x = 'width', y = 'height', data = opacity_bb_final, kind= 'reg')","metadata":{"id":"bG4ejZfuTVj1","outputId":"7279e455-5195-469c-f705-bf544b0b0c34","execution":{"iopub.status.busy":"2021-07-02T00:26:11.948137Z","iopub.execute_input":"2021-07-02T00:26:11.948528Z","iopub.status.idle":"2021-07-02T00:26:13.478431Z","shell.execute_reply.started":"2021-07-02T00:26:11.948494Z","shell.execute_reply":"2021-07-02T00:26:13.477227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Inference**: There is a fine correlation between width and height variables.","metadata":{"id":"zwyDMKchSnYs"}},{"cell_type":"markdown","source":"**Lung Opacity Representation** (2000 samples)","metadata":{"id":"PsAolXBEt1k8"}},{"cell_type":"code","source":"fig, ax = plt.subplots(1,1,figsize=(7,7))\ntarget_sample = opacity_bb_final.sample(2000)\ntarget_sample['xc'] = target_sample['x'] + target_sample['width'] / 2\ntarget_sample['yc'] = target_sample['y'] + target_sample['height'] / 2\nplt.title(\"Lung Opacity representation for 2000 data samples\")\ntarget_sample.plot.scatter(x='xc', y='yc', xlim=(0,1024), ylim=(0,1024), ax=ax, alpha=0.8, marker=\".\", color=\"black\")\nfor i, crt_sample in target_sample.iterrows():\n    ax.add_patch(Rectangle(xy=(crt_sample['x'], crt_sample['y']),\n                width=crt_sample['width'],height=crt_sample['height'],alpha=3.5e-3, color=\"green\"))\nplt.show()","metadata":{"id":"qvNhmk3vOx3n","outputId":"f452624d-bcc1-4757-e153-e65401f5eb8c","execution":{"iopub.status.busy":"2021-07-02T00:26:16.346217Z","iopub.execute_input":"2021-07-02T00:26:16.346795Z","iopub.status.idle":"2021-07-02T00:26:20.901537Z","shell.execute_reply.started":"2021-07-02T00:26:16.346743Z","shell.execute_reply":"2021-07-02T00:26:20.900464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Split Data sets** (Train= 80%, Test= 20%)","metadata":{"id":"_cNIgE4NuHa7"}},{"cell_type":"code","source":"tmp = merge_train_df[[\"patientId\", \"class\"]]\ntrain_labels_data = pd.merge(tmp, merge_bb_final, how= 'inner', left_index=True, right_index=True)\ntrain_labels_data.head()","metadata":{"id":"PU9b-0z9t-WS","outputId":"1d65ce21-c5ac-4e3d-9ee9-5842d53eb61e","execution":{"iopub.status.busy":"2021-07-02T00:26:20.903552Z","iopub.execute_input":"2021-07-02T00:26:20.903906Z","iopub.status.idle":"2021-07-02T00:26:20.930363Z","shell.execute_reply.started":"2021-07-02T00:26:20.903871Z","shell.execute_reply":"2021-07-02T00:26:20.929037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_labels_data.drop(columns= [\"patientId\", \"Target\", \"class\"])\ny = train_labels_data[[\"Target\"]]\nX.shape, y.shape","metadata":{"id":"ZBeLNWu8ucbK","outputId":"bd9f8412-991a-48df-883f-71156eb309f7","execution":{"iopub.status.busy":"2021-07-02T00:26:23.974935Z","iopub.execute_input":"2021-07-02T00:26:23.975317Z","iopub.status.idle":"2021-07-02T00:26:23.985611Z","shell.execute_reply.started":"2021-07-02T00:26:23.975261Z","shell.execute_reply":"2021-07-02T00:26:23.984679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report, accuracy_score, average_precision_score\nfrom sklearn.metrics import roc_auc_score, auc, plot_confusion_matrix, plot_roc_curve, roc_curve\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state =7)\nprint('Training data shape:', (X_train.shape, y_train.shape))\nprint('Test data shape:', (X_test.shape, y_test.shape))","metadata":{"id":"jQMp7_JIuilA","outputId":"77a7d407-6df7-465a-d1e3-c941eccd9f3d","execution":{"iopub.status.busy":"2021-07-02T00:26:26.68808Z","iopub.execute_input":"2021-07-02T00:26:26.688496Z","iopub.status.idle":"2021-07-02T00:26:26.704295Z","shell.execute_reply.started":"2021-07-02T00:26:26.688459Z","shell.execute_reply":"2021-07-02T00:26:26.702963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Classification using SVC**","metadata":{"id":"ciQG85dyvmol"}},{"cell_type":"code","source":"from sklearn.svm import SVC\nsvm_model = SVC(kernel = 'rbf', C = 1.0).fit(X_train, y_train)\ny_preds = svm_model.predict(X_test)\nprint('Accuracy score:', accuracy_score(y_test, y_preds))\nprint('Average Precision Score:', average_precision_score(y_test, y_preds))","metadata":{"id":"5Z4Gg1EUvu8U","outputId":"9f9a52fb-405b-4f77-a170-d206bd0c4d4c","execution":{"iopub.status.busy":"2021-07-02T00:26:32.616429Z","iopub.execute_input":"2021-07-02T00:26:32.616794Z","iopub.status.idle":"2021-07-02T00:26:32.703804Z","shell.execute_reply.started":"2021-07-02T00:26:32.616763Z","shell.execute_reply":"2021-07-02T00:26:32.702589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Score: \"{:.2%}\"'.format(svm_model.score(X, y)))","metadata":{"id":"v-czYpm6xljs","outputId":"9a2d9e46-17f9-4c98-9b19-820be279aec3","execution":{"iopub.status.busy":"2021-07-02T00:26:50.653986Z","iopub.execute_input":"2021-07-02T00:26:50.654407Z","iopub.status.idle":"2021-07-02T00:26:50.711806Z","shell.execute_reply.started":"2021-07-02T00:26:50.65437Z","shell.execute_reply":"2021-07-02T00:26:50.710834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Classification report: \\n')\nprint(classification_report(y_test, y_preds))","metadata":{"id":"VyJD5BL9wPFK","outputId":"e452f8dc-d668-46e1-b2d8-3a4b89cdbd95","execution":{"iopub.status.busy":"2021-07-02T00:26:53.602396Z","iopub.execute_input":"2021-07-02T00:26:53.602811Z","iopub.status.idle":"2021-07-02T00:26:53.620114Z","shell.execute_reply.started":"2021-07-02T00:26:53.602772Z","shell.execute_reply":"2021-07-02T00:26:53.618464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(svm_model, X_test, y_test, values_format='d', cmap ='plasma')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"id":"fFq15f6CwWxZ","outputId":"94098f0a-0df8-446a-ef2b-65ed1f4ef11f","execution":{"iopub.status.busy":"2021-07-02T00:26:57.84364Z","iopub.execute_input":"2021-07-02T00:26:57.844084Z","iopub.status.idle":"2021-07-02T00:26:58.05283Z","shell.execute_reply.started":"2021-07-02T00:26:57.844048Z","shell.execute_reply":"2021-07-02T00:26:58.051521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_roc_curve(svm_model, X_test, y_test, name= 'ROC-TP/FP')","metadata":{"id":"257rbTmdw5RK","outputId":"60df56bb-0457-47f1-9019-e37ff0a323ac","execution":{"iopub.status.busy":"2021-07-02T00:27:01.573418Z","iopub.execute_input":"2021-07-02T00:27:01.573814Z","iopub.status.idle":"2021-07-02T00:27:01.755554Z","shell.execute_reply.started":"2021-07-02T00:27:01.573775Z","shell.execute_reply":"2021-07-02T00:27:01.753667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fpr, tpr, thresholds = roc_curve(y_test, y_preds)\n\n# calculate AUC\nauc = roc_auc_score(y_test, y_preds)\nprint('AUC: %.3f' % auc)","metadata":{"id":"u5-9Ldjo1mQU","outputId":"b6f353ba-0a83-45da-a968-ed9412dc2ba1","execution":{"iopub.status.busy":"2021-07-02T00:27:04.34387Z","iopub.execute_input":"2021-07-02T00:27:04.344358Z","iopub.status.idle":"2021-07-02T00:27:04.356692Z","shell.execute_reply.started":"2021-07-02T00:27:04.344237Z","shell.execute_reply":"2021-07-02T00:27:04.35548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Using Pydicom to analyze .dcm images**\n\nMedical images are stored in DICOM file format (.DCM). \nIn python pydicom library is used to manipulate these kind of files.","metadata":{"id":"VlmU_ImmOQ7b"}},{"cell_type":"markdown","source":"**Load DICOM data**","metadata":{"id":"NaUbxSk8PHGz"}},{"cell_type":"code","source":"patientId = trainlabels_df['patientId'][0]\ndcm_file = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images/%s.dcm' % patientId\ndcm_data = dcm.read_file(dcm_file)\nprint(dcm_data)","metadata":{"id":"UFbJKw4jTE9Q","outputId":"e448da3f-c363-4ed1-f617-181319bbf4c6","execution":{"iopub.status.busy":"2021-07-02T00:27:07.960319Z","iopub.execute_input":"2021-07-02T00:27:07.960723Z","iopub.status.idle":"2021-07-02T00:27:07.986137Z","shell.execute_reply.started":"2021-07-02T00:27:07.96069Z","shell.execute_reply":"2021-07-02T00:27:07.985208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = dcm_data.pixel_array\nprint(type(im))\nprint(im.dtype)\nprint(im.shape)","metadata":{"id":"GN7TDz3XQDV7","outputId":"1f0516d7-1ae0-44e6-e1c1-0a5b72a5228a","execution":{"iopub.status.busy":"2021-07-02T00:27:18.416543Z","iopub.execute_input":"2021-07-02T00:27:18.417077Z","iopub.status.idle":"2021-07-02T00:27:18.465716Z","shell.execute_reply.started":"2021-07-02T00:27:18.41704Z","shell.execute_reply":"2021-07-02T00:27:18.464609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize sample image**","metadata":{"id":"YKERdag5TmVs"}},{"cell_type":"code","source":"import pylab\npylab.imshow(im, cmap=pylab.cm.gist_gray)\npylab.axis('off')\n","metadata":{"id":"j2kVhW2ATLRf","outputId":"774fc608-cdae-4798-a7b4-bee68711ec68","execution":{"iopub.status.busy":"2021-07-02T00:27:21.133365Z","iopub.execute_input":"2021-07-02T00:27:21.133838Z","iopub.status.idle":"2021-07-02T00:27:21.264902Z","shell.execute_reply.started":"2021-07-02T00:27:21.133796Z","shell.execute_reply":"2021-07-02T00:27:21.263782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Plot bounding boxes over Images**","metadata":{"id":"6_UbiiE5YrSh"}},{"cell_type":"code","source":"def parse_data(df):\n    \"\"\"\n    Method to read a CSV file (Pandas dataframe) and parse the \n    data into the following nested dictionary:\n\n      parsed = {\n        \n        'patientId-00': {\n            'dicom': path/to/dicom/file,\n            'label': either 0 or 1 for normal or pnuemonia, \n            'boxes': list of box(es)\n        },\n        'patientId-01': {\n            'dicom': path/to/dicom/file,\n            'label': either 0 or 1 for normal or pnuemonia, \n            'boxes': list of box(es)\n        }, ...\n\n      }\n\n    \"\"\"\n    # --- Define lambda to extract coords in list [y, x, height, width]\n    extract_box = lambda row: [row['y'], row['x'], row['height'], row['width']]\n\n    parsed = {}\n    for n, row in df.iterrows():\n        # --- Initialize patient entry into parsed \n        pid = row['patientId']\n        if pid not in parsed:\n            parsed[pid] = {\n                'dicom': '../input/rsna-pneumonia-detection-challenge/stage_2_train_images/%s.dcm' % pid,\n                'label': row['Target'],\n                'boxes': []}\n\n        # --- Add box if opacity is present\n        if parsed[pid]['label'] == 1:\n            parsed[pid]['boxes'].append(extract_box(row))\n\n    return parsed\n\nparsed = parse_data(trainlabels_df)\n\ndef draw(data):\n    \"\"\"\n    Method to draw single patient with bounding box(es) if present \n\n    \"\"\"\n    # --- Open DICOM file\n    d = dcm.read_file(data['dicom'])\n    im = d.pixel_array\n\n    # --- Convert from single-channel grayscale to 3-channel RGB\n    im = np.stack([im] * 3, axis=2)\n\n    # --- Add boxes with random color if present\n    for box in data['boxes']:\n        #rgb = np.floor(np.random.rand(3) * 256).astype('int')\n        rgb = [0, 0, 255] # Just use blue\n        im = overlay_box(im=im, box=box, rgb=rgb, stroke=15)\n\n    plt.imshow(im, cmap=plt.cm.gist_gray)\n    plt.axis('off')\n\ndef overlay_box(im, box, rgb, stroke=2):\n    \"\"\"\n    Method to overlay single box on image\n\n    \"\"\"\n    # --- Convert coordinates to integers\n    box = [int(b) for b in box]\n    \n    # --- Extract coordinates\n    y1, x1, height, width = box\n    y2 = y1 + height\n    x2 = x1 + width\n\n    im[y1:y1 + stroke, x1:x2] = rgb\n    im[y2:y2 + stroke, x1:x2] = rgb\n    im[y1:y2, x1:x1 + stroke] = rgb\n    im[y1:y2, x2:x2 + stroke] = rgb\n\n    return im","metadata":{"id":"fnlDlQpbUPW8","execution":{"iopub.status.busy":"2021-07-02T00:27:35.86795Z","iopub.execute_input":"2021-07-02T00:27:35.868366Z","iopub.status.idle":"2021-07-02T00:27:39.956254Z","shell.execute_reply.started":"2021-07-02T00:27:35.868331Z","shell.execute_reply":"2021-07-02T00:27:39.955115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize Train Labels dataset images**","metadata":{"id":"LcTj9yksjEOY"}},{"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(8, 8))\ncolumns = 5; rows = 4\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[trainlabels_df['patientId'].unique()[i]])\n    fig.add_subplot","metadata":{"id":"LHMERVpXYKTo","outputId":"fb459e41-8724-4cfa-bda4-1eccee7c3c3c","execution":{"iopub.status.busy":"2021-07-02T00:27:45.538559Z","iopub.execute_input":"2021-07-02T00:27:45.538922Z","iopub.status.idle":"2021-07-02T00:27:48.696129Z","shell.execute_reply.started":"2021-07-02T00:27:45.538891Z","shell.execute_reply":"2021-07-02T00:27:48.695105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize Detail Class dataset images**","metadata":{"id":"57Wyv_e-epC_"}},{"cell_type":"code","source":"opacity = detailclassinfo_df \\\n    .loc[detailclassinfo_df['class'] == 'Lung Opacity'] \\\n    .reset_index()","metadata":{"id":"tvJ1Vi7pdVqc","execution":{"iopub.status.busy":"2021-07-02T00:27:57.869486Z","iopub.execute_input":"2021-07-02T00:27:57.870021Z","iopub.status.idle":"2021-07-02T00:27:57.883348Z","shell.execute_reply.started":"2021-07-02T00:27:57.869975Z","shell.execute_reply":"2021-07-02T00:27:57.882418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"not_normal = detailclassinfo_df \\\n    .loc[detailclassinfo_df['class'] == 'No Lung Opacity / Not Normal'] \\\n    .reset_index()","metadata":{"id":"wL39cc-Ce_x4","execution":{"iopub.status.busy":"2021-07-02T00:28:00.72919Z","iopub.execute_input":"2021-07-02T00:28:00.729732Z","iopub.status.idle":"2021-07-02T00:28:00.741388Z","shell.execute_reply.started":"2021-07-02T00:28:00.729697Z","shell.execute_reply":"2021-07-02T00:28:00.740379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal = detailclassinfo_df \\\n    .loc[detailclassinfo_df['class'] == 'Normal'] \\\n    .reset_index()","metadata":{"id":"jLrNpFO8fEeA","execution":{"iopub.status.busy":"2021-07-02T00:28:03.903171Z","iopub.execute_input":"2021-07-02T00:28:03.903888Z","iopub.status.idle":"2021-07-02T00:28:03.915355Z","shell.execute_reply.started":"2021-07-02T00:28:03.903847Z","shell.execute_reply":"2021-07-02T00:28:03.914286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**1. Lung Opacity Samples**","metadata":{"id":"RLmsGM0Qfg-x"}},{"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(6, 6))\ncolumns = 4; rows = 4\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[opacity['patientId'].unique()[i]])","metadata":{"id":"YaK2kgxxfXvE","outputId":"55c055d0-c966-4e88-e72e-03d387d9cec0","execution":{"iopub.status.busy":"2021-07-02T00:28:08.015611Z","iopub.execute_input":"2021-07-02T00:28:08.016036Z","iopub.status.idle":"2021-07-02T00:28:10.435219Z","shell.execute_reply.started":"2021-07-02T00:28:08.015956Z","shell.execute_reply":"2021-07-02T00:28:10.43415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**2. Normal Samples**","metadata":{"id":"WkLil9hNgFCK"}},{"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(6, 6))\ncolumns = 4; rows = 4\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[normal['patientId'].unique()[i]])","metadata":{"id":"ftlOUfAnf2g6","outputId":"0f7392d6-cbc9-45b2-e62f-0f91c5f13ec0","execution":{"iopub.status.busy":"2021-07-02T00:28:14.333248Z","iopub.execute_input":"2021-07-02T00:28:14.333655Z","iopub.status.idle":"2021-07-02T00:28:17.06556Z","shell.execute_reply.started":"2021-07-02T00:28:14.33362Z","shell.execute_reply":"2021-07-02T00:28:17.064468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3. No Lung Opacity/Not Normal Samples**","metadata":{"id":"RINmpxavghwN"}},{"cell_type":"code","source":"plt.style.use('default')\nfig=plt.figure(figsize=(6, 6))\ncolumns = 4; rows = 4\nfor i in range(1, columns*rows +1):\n    fig.add_subplot(rows, columns, i)\n    draw(parsed[not_normal['patientId'].unique()[i]])","metadata":{"id":"a6V8rh-0gLyX","outputId":"bf0cd94f-b1f3-4237-8a18-79faa1037fbf","execution":{"iopub.status.busy":"2021-07-02T00:28:22.390196Z","iopub.execute_input":"2021-07-02T00:28:22.390606Z","iopub.status.idle":"2021-07-02T00:28:24.779107Z","shell.execute_reply.started":"2021-07-02T00:28:22.39057Z","shell.execute_reply":"2021-07-02T00:28:24.778062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(20, 10))\ncolumns = 3; rows = 1\nfig.add_subplot(rows, columns, 1).set_title(\"Normal\", fontsize=30)\ndraw(parsed[normal['patientId'].unique()[0]])\nfig.add_subplot(rows, columns, 2).set_title(\"Not Normal\", fontsize=30)\n# ax2.set_title(\"Not Normal\", fontsize=30)\ndraw(parsed[not_normal['patientId'].unique()[0]])\nfig.add_subplot(rows, columns, 3).set_title(\"Opacity\", fontsize=30)\n# ax3.set_title(\"Opacity\", fontsize=30)\ndraw(parsed[opacity['patientId'].unique()[0]])","metadata":{"id":"0N_KZN1Fg0ms","outputId":"ad1b3f65-e8ba-4521-e69e-db624901b344","execution":{"iopub.status.busy":"2021-07-02T00:28:29.630539Z","iopub.execute_input":"2021-07-02T00:28:29.630903Z","iopub.status.idle":"2021-07-02T00:28:30.370396Z","shell.execute_reply.started":"2021-07-02T00:28:29.630873Z","shell.execute_reply":"2021-07-02T00:28:30.369499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Image Segmentation**","metadata":{"id":"IlvxnhaWgNvK"}},{"cell_type":"markdown","source":"**Image segmentation** creates a pixel-wise mask for each object in the image. This technique gives us a far more granular understanding of the object(s) in the image.","metadata":{"id":"2ZN7zvHMjetg"}},{"cell_type":"markdown","source":"Class generatortransfer:\n\nThe dataset is too large to fit into memory, so we need to create a generator that loads data on the fly.\n\n**I/p**: filenames, batch_size and other parameters.\n\n**O/p**: A random batch of numpy images and numpy masks.","metadata":{"id":"bJHu_ttPjBht"}},{"cell_type":"markdown","source":"# **Model Selection**","metadata":{"id":"I96jfqOSgtik"}},{"cell_type":"code","source":"import os\nimport csv\nimport random\nimport pydicom\nimport numpy as np\nimport pandas as pd\nfrom skimage import measure\nfrom skimage.transform import resize\n\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"id":"iFe9LmuWWg5G","execution":{"iopub.status.busy":"2021-07-02T00:28:40.410963Z","iopub.execute_input":"2021-07-02T00:28:40.411369Z","iopub.status.idle":"2021-07-02T00:28:40.6207Z","shell.execute_reply.started":"2021-07-02T00:28:40.411336Z","shell.execute_reply":"2021-07-02T00:28:40.619711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load opacity locations**","metadata":{"id":"7yzNN7s9igEd"}},{"cell_type":"code","source":"opacity_locations = {}\n# load table\nwith open(os.path.join('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv'), mode='r') as infile:\n    # open reader\n    reader = csv.reader(infile)\n    # skip header\n    next(reader, None)\n    # loop through rows\n    for rows in reader:\n        # retrieve information\n        filename = rows[0]\n        location = rows[1:5]\n        lungopacity = rows[5]\n        # if row contains lungopacity add label to dictionary\n        # which contains a list of lungopacity locations per filename\n        if lungopacity == '1':\n            # convert string to float to int\n            location = [int(float(i)) for i in location]\n            # save lungopacity location in dictionary\n            if filename in opacity_locations:\n                opacity_locations[filename].append(location)\n            else:\n                opacity_locations[filename] = [location]","metadata":{"id":"gpR4EzqKWgwo","execution":{"iopub.status.busy":"2021-07-02T00:28:43.690434Z","iopub.execute_input":"2021-07-02T00:28:43.690827Z","iopub.status.idle":"2021-07-02T00:28:43.794985Z","shell.execute_reply.started":"2021-07-02T00:28:43.690791Z","shell.execute_reply":"2021-07-02T00:28:43.793914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load File names**","metadata":{"id":"JnAnLzFmippI"}},{"cell_type":"code","source":"folder = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\nfilenames = os.listdir(folder)\nprint('Number of Train images:', len(filenames))\nprint('First 5 samples: \\n')\nfilenames[0:5]","metadata":{"id":"XZUY3Uv1WgsT","outputId":"a482953f-3904-4b12-ad0b-73cf53ff578b","execution":{"iopub.status.busy":"2021-07-02T00:28:47.225083Z","iopub.execute_input":"2021-07-02T00:28:47.22546Z","iopub.status.idle":"2021-07-02T00:28:47.246438Z","shell.execute_reply.started":"2021-07-02T00:28:47.225429Z","shell.execute_reply":"2021-07-02T00:28:47.245464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#folder = '/content/drive/My Drive/GL_Capstone/stage_2_train_images'\n#filenames = os.listdir(folder)\nrandom.shuffle(filenames)\n# split into train and validation filenames\nn_valid_samples = 5336\nn_train_samples = len(filenames) - n_valid_samples\ntrain_filenames = filenames[n_valid_samples:]\nvalid_filenames = filenames[:n_valid_samples]\nprint('Total file samples:', len(filenames))\nprint('Train samples (80%):', len(train_filenames))\nprint('Valid samples (20%):', len(valid_filenames))","metadata":{"id":"Vtqp-CoO6RpB","execution":{"iopub.status.busy":"2021-07-02T00:28:52.346115Z","iopub.execute_input":"2021-07-02T00:28:52.346481Z","iopub.status.idle":"2021-07-02T00:28:52.385426Z","shell.execute_reply.started":"2021-07-02T00:28:52.34645Z","shell.execute_reply":"2021-07-02T00:28:52.384609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Downsampling Train samples**","metadata":{"id":"KokTOccS6gww"}},{"cell_type":"code","source":"n_downsamples = 5336\ntrain_dsamps = train_filenames[:n_downsamples]\nprint('Training Downsamples:', len(train_dsamps))","metadata":{"id":"OPLqayP_6fMk","execution":{"iopub.status.busy":"2021-07-02T00:28:55.693178Z","iopub.execute_input":"2021-07-02T00:28:55.693564Z","iopub.status.idle":"2021-07-02T00:28:55.699927Z","shell.execute_reply.started":"2021-07-02T00:28:55.693532Z","shell.execute_reply":"2021-07-02T00:28:55.698723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Transfer Learning**","metadata":{"id":"AxINpeSWEmjO"}},{"cell_type":"code","source":"!pip install opencv-python\nimport cv2\nkeras = tf.compat.v1.keras\n#Sequence = keras.utils.Sequence","metadata":{"id":"RaVv8UDBIcEK","execution":{"iopub.status.busy":"2021-07-02T00:29:00.178511Z","iopub.execute_input":"2021-07-02T00:29:00.17904Z","iopub.status.idle":"2021-07-02T00:29:07.222195Z","shell.execute_reply.started":"2021-07-02T00:29:00.178984Z","shell.execute_reply":"2021-07-02T00:29:07.221209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Define class generatortransfer**","metadata":{"id":"H2gkwbdQQy6S"}},{"cell_type":"code","source":"class generatortransfer(keras.utils.Sequence):\n    \n    def __init__(self, folder, filenames, opacity_locations=None, batch_size=32, image_size=320, shuffle=True, augment=False, predict=False):\n        self.folder = folder\n        self.filenames = filenames\n        self.opacity_locations = opacity_locations\n        self.batch_size = batch_size\n        self.image_size = image_size\n        self.shuffle = shuffle\n        self.augment = augment\n        self.predict = predict\n        self.on_epoch_end()\n        \n    def __load__(self, filename):\n        # load dicom file as numpy array\n        img = pydicom.dcmread(os.path.join(self.folder, filename)).pixel_array\n        # create empty mask\n        msk = np.zeros(img.shape)\n        # get filename without extension\n        filename = filename.split('.')[0]\n        # if image contains lung opacity\n        if filename in opacity_locations:\n            # loop through opacity\n            for location in opacity_locations[filename]:\n                # add 1's at the location of the lung opacity\n                x, y, w, h = location\n                msk[y:y+h, x:x+w] = 1\n        # if augment then horizontal flip half the time\n        if self.augment and random.random() > 0.5:\n            img = np.fliplr(img)\n            msk = np.fliplr(msk)\n        # resize both image and mask\n        #img = resize(img, (self.image_size, self.image_size), mode='reflect')\n        msk = resize(msk, (self.image_size, self.image_size), mode='reflect') > 0.5\n        # add trailing channel dimension\n        msk = np.expand_dims(msk, -1)\n         #Converting Image from GrayScale to RGB \n        if len(img.shape) != 3 or img.shape[2] != 3:\n            img = np.stack((img,) * 3, -1)\n            img = cv2.resize(img, dsize=(self.image_size, self.image_size), interpolation=cv2.INTER_CUBIC)\n        return img, msk\n    \n    def __loadpredict__(self, filename):\n        # load dicom file as numpy array\n        img = pydicom.dcmread(os.path.join(self.folder, filename)).pixel_array\n        # resize image\n        #img = resize(img, (self.image_size, self.image_size), mode='reflect')\n        #Converting Image from GrayScale to RGB \n        if len(img.shape) != 3 or img.shape[2] != 3:\n          img = np.stack((img,) * 3, -1)\n          img = cv2.resize(img, dsize=(self.image_size, self.image_size), interpolation=cv2.INTER_CUBIC)\n        return img\n        \n    def __getitem__(self, index):\n        # select batch\n        filenames = self.filenames[index*self.batch_size:(index+1)*self.batch_size]\n        # predict mode: return images and filenames\n        if self.predict:\n            # load files\n            imgs = [self.__loadpredict__(filename) for filename in filenames]\n            # create numpy batch\n            imgs = np.array(imgs)\n            return imgs, filenames\n        # train mode: return images and masks\n        else:\n            # load files\n            items = [self.__load__(filename) for filename in filenames]\n            # unzip images and masks\n            imgs, msks = zip(*items)\n            # create numpy batch\n            imgs = np.array(imgs)\n            msks = np.array(msks)\n            return imgs, msks\n        \n    def on_epoch_end(self):\n        if self.shuffle:\n            random.shuffle(self.filenames)\n        \n    def __len__(self):\n        if self.predict:\n            # return everything\n            return int(np.ceil(len(self.filenames) / self.batch_size))\n        else:\n            # return full batches only\n            return int(len(self.filenames) / self.batch_size)","metadata":{"id":"SHcAaaH0Eq2u","execution":{"iopub.status.busy":"2021-07-02T00:29:13.033421Z","iopub.execute_input":"2021-07-02T00:29:13.034117Z","iopub.status.idle":"2021-07-02T00:29:13.065965Z","shell.execute_reply.started":"2021-07-02T00:29:13.034049Z","shell.execute_reply":"2021-07-02T00:29:13.064997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width = 128\nimg_height = 128\nIMAGE_SIZE=128\nkernel =3\nnum_of_classes =2\nBATCH_SIZE = 16\nSHUFFLE_BUFFER_SIZE=1000\n#input_shape = (img_width, img_height, 3)","metadata":{"id":"Ab1g5Mpy-GWn","execution":{"iopub.status.busy":"2021-07-02T00:29:17.249831Z","iopub.execute_input":"2021-07-02T00:29:17.250479Z","iopub.status.idle":"2021-07-02T00:29:17.25539Z","shell.execute_reply.started":"2021-07-02T00:29:17.250436Z","shell.execute_reply":"2021-07-02T00:29:17.25427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create training and validation data\nfolder = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images'\ntrain_trans = generatortransfer(folder, train_filenames, opacity_locations, batch_size=BATCH_SIZE, image_size=IMAGE_SIZE, shuffle=True, augment=False, predict=False)\nvalid_trans = generatortransfer(folder, valid_filenames, opacity_locations, batch_size=BATCH_SIZE, image_size=IMAGE_SIZE, shuffle=False, predict=False)\n#train_downsamples = generatortransfer(folder, tune_train_samples, opacity_locations, batch_size=BATCH_SIZE, image_size=IMAGE_SIZE, shuffle=False, predict=False)","metadata":{"id":"Lwvc3w2W94uJ","execution":{"iopub.status.busy":"2021-07-02T00:29:20.53704Z","iopub.execute_input":"2021-07-02T00:29:20.537706Z","iopub.status.idle":"2021-07-02T00:29:20.572967Z","shell.execute_reply.started":"2021-07-02T00:29:20.537652Z","shell.execute_reply":"2021-07-02T00:29:20.572069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom tensorflow.keras import Sequential, backend as K\n#from tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.layers import Concatenate, UpSampling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense\n\nmodel = Sequential()\nmodel.add(ResNet50(input_shape= (img_width, img_height, 3), include_top=False, weights='imagenet'))\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(UpSampling2D())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(UpSampling2D())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(UpSampling2D())\nmodel.add(Dense(64, activation='relu'))\nmodel.add(UpSampling2D())\nmodel.add(Dense(8, activation='relu'))\nmodel.add(UpSampling2D())\nmodel.add(Dense(1, activation='sigmoid'))\n# Say not to train first layer (ResNet) model. It is already trained\nmodel.layers[0].trainable = False\nprint(model.summary())","metadata":{"id":"ARLLB1LG-Tza","execution":{"iopub.status.busy":"2021-07-02T00:29:24.149844Z","iopub.execute_input":"2021-07-02T00:29:24.150553Z","iopub.status.idle":"2021-07-02T00:29:30.878653Z","shell.execute_reply.started":"2021-07-02T00:29:24.150508Z","shell.execute_reply":"2021-07-02T00:29:30.877554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam',\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"id":"8BXa-lufJhyd","execution":{"iopub.status.busy":"2021-07-02T00:29:35.342469Z","iopub.execute_input":"2021-07-02T00:29:35.342824Z","iopub.status.idle":"2021-07-02T00:29:35.366415Z","shell.execute_reply.started":"2021-07-02T00:29:35.342794Z","shell.execute_reply":"2021-07-02T00:29:35.365528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_trans, epochs=5, steps_per_epoch =10, shuffle=True)","metadata":{"id":"80YGx70LKMhI","outputId":"7c1e5ee1-f46d-4815-d022-e9373f3603b2","execution":{"iopub.status.busy":"2021-07-02T00:29:37.971939Z","iopub.execute_input":"2021-07-02T00:29:37.972385Z","iopub.status.idle":"2021-07-02T00:31:04.995121Z","shell.execute_reply.started":"2021-07-02T00:29:37.972346Z","shell.execute_reply":"2021-07-02T00:31:04.994288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_trans)","metadata":{"id":"4XDvtGqEDjFo","outputId":"16e850d0-529d-4ef1-a52a-759502b50b45","execution":{"iopub.status.busy":"2021-07-02T00:31:04.997083Z","iopub.execute_input":"2021-07-02T00:31:04.997405Z","iopub.status.idle":"2021-07-02T00:39:12.896513Z","shell.execute_reply.started":"2021-07-02T00:31:04.997374Z","shell.execute_reply":"2021-07-02T00:39:12.895365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Hyper Parameter Tuning**","metadata":{"id":"q6KN8BpGPLbw"}},{"cell_type":"markdown","source":"I have tried couple of times to use GridSearchCV in order to find out the optimal parameters, but the session ran for longer time and the runtime was disconnected and finally lead to lost of changes.","metadata":{"id":"O53E6QcNVkOf"}},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","metadata":{"id":"_bZTngROSWFa","outputId":"87de2f3e-574c-4e4e-ea10-cbc04bd303af","execution":{"iopub.status.busy":"2021-07-02T00:39:12.898005Z","iopub.execute_input":"2021-07-02T00:39:12.898342Z","iopub.status.idle":"2021-07-02T00:39:12.904135Z","shell.execute_reply.started":"2021-07-02T00:39:12.898304Z","shell.execute_reply":"2021-07-02T00:39:12.903128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install tensorflow==1.14.0\n#!pip install keras==2.0","metadata":{"id":"SAu3ZTjUkMfd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAC5CAYAAAABICL/AAAgAElEQVR4Ae19B5yVxdV+Ej//SUy+L1FgG13QFLsiilGxJKZoLLGmfSZfEpNorLHEjr2DUam7e7cAdoMVpS8gUhdYiiDSWZZdlqbA7t76/H/Pmfe8d/ayy97t3GXe387O+06fc+eZc+ZM+wrc4yjQThSIxMIQgzBCkbCUIhYBEAIQiQLhGoQQEYNoCIjEAAaLANEYEEUEMUZoTxOFKUyEhTIFi7FsXlFNpUJAtBqI7AFiISAWRiwWQzgSE5vvrf18pbUzcOk7CtRHAYK8JlqFEGoQRhQ1kahgBQRPkAAOswsQIwAhmNgR0CsWQxSh9gV61ADV9Dq1gc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XIPemhtZ/x41Uj03uMlJFeEwYJtIVLayRxlH0okIKAgJgiOlbJY2Zk7mCUXC4Ck9Ujlr7QEp5YB+0EMmtQlAPmYMYa/GUzizarXW47AHMD2CxvNrTwc+KWWzcuTgYTmqQ6fPPYIoYcT+/wQPFh6cierDAAAAAElFTkSuQmCC)","metadata":{"id":"jTd1B__tIlIp"}},{"cell_type":"markdown","source":"**IoU Mean**","metadata":{"id":"6dzmZ00jKXd9"}},{"cell_type":"code","source":"# mean iou as a metric\ndef mean_iou(y_true, y_pred):\n    y_pred = tf.round(y_pred)\n    intersect = tf.reduce_sum(y_true * y_pred, axis=[1, 2, 3])\n    union = tf.reduce_sum(y_true, axis=[1, 2, 3]) + tf.reduce_sum(y_pred, axis=[1, 2, 3])\n    smooth = tf.ones(tf.shape(intersect))\n    return tf.reduce_mean((intersect + smooth) / (union - intersect + smooth))","metadata":{"id":"Ej2xagypj3VI","execution":{"iopub.status.busy":"2021-07-02T00:39:12.905193Z","iopub.execute_input":"2021-07-02T00:39:12.905503Z","iopub.status.idle":"2021-07-02T00:39:12.921956Z","shell.execute_reply.started":"2021-07-02T00:39:12.905473Z","shell.execute_reply":"2021-07-02T00:39:12.920837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Call Backs (Earlystop, ModelCheckpoint)**","metadata":{"id":"5qju2OcSQGMY"}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n## Earlystopping\nearlystop = EarlyStopping(monitor='val_loss', patience=3)\n\n## Model Check point\nfilepath=\"/kaggle/working/LOHPT-{epoch:02d}-{val_accuracy:.2f}.hdf5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min', save_weights_only = True)\n\n## Reduce learning rate when metric has stopped improving\nreduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.8, \n                                   patience=2, verbose=1, mode='auto', \n                                   epsilon=0.0001, cooldown=5, min_lr=0.0001)","metadata":{"id":"prVqHWcXQC7V","outputId":"b0d3f157-c8d9-4fbd-d9ea-6c914a98859c","execution":{"iopub.status.busy":"2021-07-02T00:39:12.925048Z","iopub.execute_input":"2021-07-02T00:39:12.925573Z","iopub.status.idle":"2021-07-02T00:39:12.945736Z","shell.execute_reply.started":"2021-07-02T00:39:12.925538Z","shell.execute_reply":"2021-07-02T00:39:12.944359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" **Define Parameters**","metadata":{"id":"WT1pKxjrmPz0"}},{"cell_type":"code","source":"BATCH_SIZE = 16\nIMAGE_SIZE = 128","metadata":{"id":"BTAiPKYlQct7","execution":{"iopub.status.busy":"2021-07-02T00:39:12.948149Z","iopub.execute_input":"2021-07-02T00:39:12.948995Z","iopub.status.idle":"2021-07-02T00:39:12.961664Z","shell.execute_reply.started":"2021-07-02T00:39:12.948939Z","shell.execute_reply":"2021-07-02T00:39:12.960381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom tensorflow.keras import Sequential, backend as K\n#from tensorflow.keras.applications.mobilenet import MobileNet\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.layers import Concatenate, UpSampling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense\n\nhpt_model = Sequential()\nhpt_model.add(ResNet50(input_shape= (img_width, img_height, 3), include_top=False, weights='imagenet'))\nhpt_model.add(Dense(1024, activation='relu'))\nhpt_model.add(UpSampling2D())\nhpt_model.add(Dense(512, activation='relu'))\nhpt_model.add(UpSampling2D())\nhpt_model.add(Dense(256, activation='relu'))\nhpt_model.add(UpSampling2D())\nhpt_model.add(Dense(64, activation='relu'))\nhpt_model.add(UpSampling2D())\nhpt_model.add(Dense(8, activation='relu'))\nhpt_model.add(UpSampling2D())\nhpt_model.add(Dense(1, activation='sigmoid'))\n# Say not to train first layer (ResNet) model. It is already trained\nhpt_model.layers[0].trainable = False\nprint(hpt_model.summary())","metadata":{"id":"iVqsVuBJnmJ0","outputId":"6a19bca8-a693-434a-902e-b2d61b5b1e63","execution":{"iopub.status.busy":"2021-07-02T00:39:12.963697Z","iopub.execute_input":"2021-07-02T00:39:12.964653Z","iopub.status.idle":"2021-07-02T00:39:18.850737Z","shell.execute_reply.started":"2021-07-02T00:39:12.964599Z","shell.execute_reply":"2021-07-02T00:39:18.848654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Compilation**","metadata":{"id":"dd7l4nPymUDv"}},{"cell_type":"code","source":"hpt_model.compile(optimizer= 'Adam',\n              loss='binary_crossentropy',\n              metrics=['accuracy', mean_iou])","metadata":{"id":"N8Hd2r98kW3y","execution":{"iopub.status.busy":"2021-07-02T00:39:18.852833Z","iopub.execute_input":"2021-07-02T00:39:18.853349Z","iopub.status.idle":"2021-07-02T00:39:18.880196Z","shell.execute_reply.started":"2021-07-02T00:39:18.853297Z","shell.execute_reply":"2021-07-02T00:39:18.87841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fitting Model**","metadata":{"id":"1NEIDyvpnDLq"}},{"cell_type":"code","source":"history = hpt_model.fit(train_trans, validation_data= valid_trans, \n                        ep\n                        ochs=10, callbacks=[earlystop, checkpoint, reduceLROnPlat],\n                        steps_per_epoch=10, shuffle=True)","metadata":{"id":"QEFymVb30Gc6","outputId":"900d47b2-dd55-4f2c-d37e-96c0da869a63","execution":{"iopub.status.busy":"2021-07-02T00:41:37.87004Z","iopub.execute_input":"2021-07-02T00:41:37.870446Z","iopub.status.idle":"2021-07-02T01:58:51.634815Z","shell.execute_reply.started":"2021-07-02T00:41:37.870411Z","shell.execute_reply":"2021-07-02T01:58:51.633711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Plots for Accuracy, Loss, IoU Mean**","metadata":{"id":"4jc0vKYmmIW0"}},{"cell_type":"markdown","source":"**Accuracy**","metadata":{"id":"yI7AID_kQ0an"}},{"cell_type":"code","source":"# history.history","metadata":{"execution":{"iopub.status.busy":"2021-07-02T02:05:10.620938Z","iopub.execute_input":"2021-07-02T02:05:10.621356Z","iopub.status.idle":"2021-07-02T02:05:10.627515Z","shell.execute_reply.started":"2021-07-02T02:05:10.621324Z","shell.execute_reply":"2021-07-02T02:05:10.624823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nepochs = range(1, len(acc) + 1)\nplt.plot(epochs, acc, 'y', label='Training Accuracy')\nplt.plot(epochs, val_acc, 'r', label='Validation Accuracy')\nplt.title('Training and Validation Accuracy Graph')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"id":"BPZ_9mGsPxIa","execution":{"iopub.status.busy":"2021-07-02T01:58:51.649722Z","iopub.execute_input":"2021-07-02T01:58:51.650259Z","iopub.status.idle":"2021-07-02T01:58:51.878628Z","shell.execute_reply.started":"2021-07-02T01:58:51.650211Z","shell.execute_reply":"2021-07-02T01:58:51.877355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Loss**","metadata":{"id":"_1DqXoxdQ4ea"}},{"cell_type":"code","source":"Train_Loss = history.history['loss']\nVal_Loss = history.history['val_loss']\nEpochs = range(1, len(Train_Loss) + 1)\nplt.plot(Epochs, Train_Loss, 'r', label='Training Loss')\nplt.plot(Epochs, Val_Loss, 'g', label='Validation Loss')\nplt.title('Training and Validation Loss Graph')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"id":"pKA4LR6wQGlC","execution":{"iopub.status.busy":"2021-07-02T01:58:51.880314Z","iopub.execute_input":"2021-07-02T01:58:51.880967Z","iopub.status.idle":"2021-07-02T01:58:52.084166Z","shell.execute_reply.started":"2021-07-02T01:58:51.880911Z","shell.execute_reply":"2021-07-02T01:58:52.083073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**iou_mean**","metadata":{"id":"KC48sMylQ8N0"}},{"cell_type":"code","source":"train_iou = history.history['mean_iou']\nval_iou = history.history['val_mean_iou']\nepochs = range(1, len(train_iou) + 1)\nplt.plot(epochs, train_iou, 'r', label='Training iou')\nplt.plot(epochs, val_iou, 'g', label='Validation iou')\nplt.title('Training and Validation IOU Graph')\nplt.xlabel('Epochs')\nplt.ylabel('IOU')\nplt.legend()\nplt.show()","metadata":{"id":"8f1-DcyFQ_H9","execution":{"iopub.status.busy":"2021-07-02T01:58:52.086401Z","iopub.execute_input":"2021-07-02T01:58:52.086725Z","iopub.status.idle":"2021-07-02T01:58:52.283366Z","shell.execute_reply.started":"2021-07-02T01:58:52.086693Z","shell.execute_reply":"2021-07-02T01:58:52.28146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load the model weights**","metadata":{"id":"4Fl24SPQN_0A"}},{"cell_type":"code","source":"# model.save_weights('/kaggle/working/RESNET50-05-0.97.hdf5')","metadata":{"id":"y89Li2GOqa28","outputId":"0ed8d3b0-02a6-474b-83d3-2ef42a922257","execution":{"iopub.status.busy":"2021-07-02T00:39:18.94191Z","iopub.status.idle":"2021-07-02T00:39:18.942469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Predict Test images**","metadata":{"id":"a0bD0DPmML7e"}},{"cell_type":"code","source":"# load and shuffle filenames\n#folder = '../input/stage_2_test_images'\nfolder = '../input/rsna-pneumonia-detection-challenge/stage_2_test_images'\ntest_filenames = os.listdir(folder)\nprint('n test samples:', len(test_filenames))","metadata":{"id":"HlVdFOnyMQqW","outputId":"db7f4546-cc2f-42ab-db79-372b5a97bac5","execution":{"iopub.status.busy":"2021-07-02T02:35:30.191571Z","iopub.execute_input":"2021-07-02T02:35:30.192035Z","iopub.status.idle":"2021-07-02T02:35:30.203396Z","shell.execute_reply.started":"2021-07-02T02:35:30.191996Z","shell.execute_reply":"2021-07-02T02:35:30.201833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create test generator with predict flag set to True\ntest_trans = generatortransfer(folder, test_filenames, None, batch_size=16, image_size=IMAGE_SIZE, shuffle=False, predict=True)","metadata":{"id":"c09jJHTAoM9_","execution":{"iopub.status.busy":"2021-07-02T02:35:33.326748Z","iopub.execute_input":"2021-07-02T02:35:33.327132Z","iopub.status.idle":"2021-07-02T02:35:33.333037Z","shell.execute_reply.started":"2021-07-02T02:35:33.327101Z","shell.execute_reply":"2021-07-02T02:35:33.331813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dicom_dir = '../input/rsna-pneumonia-detection-challenge/stage_2_test_images'","metadata":{"id":"RJUaXMxgBtIg","execution":{"iopub.status.busy":"2021-07-02T02:35:40.753051Z","iopub.execute_input":"2021-07-02T02:35:40.753415Z","iopub.status.idle":"2021-07-02T02:35:40.760794Z","shell.execute_reply.started":"2021-07-02T02:35:40.753384Z","shell.execute_reply":"2021-07-02T02:35:40.759503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ndef get_dicom_fps(dicom_dir):\n    dicom_fps = glob.glob(dicom_dir+'/'+'*.dcm')\n    return list(set(dicom_fps))","metadata":{"id":"E1nFZS8pBMn5","execution":{"iopub.status.busy":"2021-07-02T02:35:43.421193Z","iopub.execute_input":"2021-07-02T02:35:43.421594Z","iopub.status.idle":"2021-07-02T02:35:43.426883Z","shell.execute_reply.started":"2021-07-02T02:35:43.421559Z","shell.execute_reply":"2021-07-02T02:35:43.425877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get filenames of test dataset DICOM images\ntest_image_fps = get_dicom_fps(test_dicom_dir)","metadata":{"id":"IPllzQaSBhzW","execution":{"iopub.status.busy":"2021-07-02T02:35:46.918595Z","iopub.execute_input":"2021-07-02T02:35:46.918923Z","iopub.status.idle":"2021-07-02T02:35:46.939818Z","shell.execute_reply.started":"2021-07-02T02:35:46.918894Z","shell.execute_reply":"2021-07-02T02:35:46.938641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_fps[:5]","metadata":{"execution":{"iopub.status.busy":"2021-07-02T02:35:50.009075Z","iopub.execute_input":"2021-07-02T02:35:50.009455Z","iopub.status.idle":"2021-07-02T02:35:50.015823Z","shell.execute_reply.started":"2021-07-02T02:35:50.009421Z","shell.execute_reply":"2021-07-02T02:35:50.014766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on test images, write out sample submission \nimport cv2\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\ndef predict(image_fps, filepath='/content/sample_submission.csv', min_conf=0.98): \n    \n    # assume square image\n    \n    with open(filepath, 'w') as file:\n      for image_id in tqdm_notebook(image_fps): \n        print(image_id)\n        ds = dcm.read_file(image_id)\n        image = ds.pixel_array\n          \n        # If grayscale. Convert to RGB for consistency.\n        #if len(image.shape) != 3 or image.shape[2] != 3:\n\n        image = np.stack((image,) * 3, -1)\n        #img = cv2.resize(image, dsize=(128, 128), interpolation=cv2.INTER_CUBIC)\n\n\n        #patient_id = os.path.splitext(os.path.basename(image_fps))[0]\n        patient_id = image_id\n        print(image.shape)\n        image = np.resize(image, (1, image.shape[0], image.shape[1], image.shape[2]))\n        print(image.shape)\n        results = hpt_model.predict(image)\n        print(results.shape)\n        r = results[0]\n        print(r.shape)\n\n        out_str = \"\"\n        out_str += patient_id \n        print(type(r))\n        print(r)\n\n\n)\n#         assert( len(r['rois']) == len(r['class_ids']) == len(r['scores']) )\n#         if len(r['rois']) == 0: \n#             pass\n#         else: \n#             num_instances = len(r['rois'])\n#             out_str += \",\"\n#             for i in range(num_instances): \n#                 if r['scores'][i] > min_conf: \n#                     out_str += ' '\n#                     out_str += str(round(r['scores'][i], 2))\n#                     out_str += ' '\n\n#                     # x1, y1, width, height \n#                     x1 = r['rois'][i][1]\n#                     y1 = r['rois'][i][0]\n#                     width = r['rois'][i][3] - x1 \n#                     height = r['rois'][i][2] - y1 \n#                     bboxes_str = \"{} {} {} {}\".format(x1, y1, \\\n#                                                       width, height)    \n#                     out_str += bboxes_str\n\n#         filepath.write(out_str+\"\\n\")","metadata":{"id":"YrLJWdi0WrOX","execution":{"iopub.status.busy":"2021-07-02T04:03:50.60188Z","iopub.execute_input":"2021-07-02T04:03:50.602386Z","iopub.status.idle":"2021-07-02T04:03:50.614756Z","shell.execute_reply.started":"2021-07-02T04:03:50.602352Z","shell.execute_reply":"2021-07-02T04:03:50.613729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict only the first 50 entries\nsample_submission_fp = '/kaggle/working/sample_submission.csv'\npredict(test_image_fps[:1], filepath=sample_submission_fp)","metadata":{"id":"B7GA2GjlJQxU","execution":{"iopub.status.busy":"2021-07-02T04:03:53.83245Z","iopub.execute_input":"2021-07-02T04:03:53.832999Z","iopub.status.idle":"2021-07-02T04:03:55.812605Z","shell.execute_reply.started":"2021-07-02T04:03:53.832955Z","shell.execute_reply":"2021-07-02T04:03:55.810755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}