{"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":"# SIIM: Step-by-Step Image Detection for Beginners\n## Part 4(mini). Multi-Output Regression\n\n👉 Part 1. [EDA to Preprocessing](https://www.kaggle.com/songseungwon/siim-covid-19-detection-10-step-tutorial-1)\n\n👉 Part 2. [Basic Modeling - Simplest Image Classification Models using Keras](https://www.kaggle.com/songseungwon/siim-covid-19-detection-10-step-tutorial-2)\n\n👉 Part 3(mini). [Preprocessing for Multi-Output Regression that Detect Opacities](https://www.kaggle.com/songseungwon/siim-covid-19-detection-mini-part-preprocess)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T08:17:37.258709Z","iopub.execute_input":"2021-05-29T08:17:37.259041Z","iopub.status.idle":"2021-05-29T08:17:37.264999Z","shell.execute_reply.started":"2021-05-29T08:17:37.259012Z","shell.execute_reply":"2021-05-29T08:17:37.263621Z"}}},{"cell_type":"markdown","source":"> index\n```\nStep 1. Load Train Data Table\n     1-a. extract data with only one opacity\n     1-b. extract image paths\nStep 2. Load Image Dataset\n     2-a. Data Preprocessing\nStep 3. Modeling\n     3-a. Train-valid split\n     3-b. Modeling\n     3-c. Training\n     3-d. Evaluation\n ```","metadata":{}},{"cell_type":"markdown","source":"\nThis model trains on data with only one opacity.\n\nThe X matrix consists of the image and the Y matrix consists of 4 borders, i.e. 4 coordinate vectors, that make up the box that detects opacity.\n\nWe will deal with multi output regression through this simple learning.","metadata":{}},{"cell_type":"markdown","source":"## Step 1. Load Train Data Table","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-29T06:57:48.706501Z","iopub.execute_input":"2021-05-29T06:57:48.70692Z","iopub.status.idle":"2021-05-29T06:57:48.716101Z","shell.execute_reply.started":"2021-05-29T06:57:48.706838Z","shell.execute_reply":"2021-05-29T06:57:48.714967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/siim-covid19-preprocessed-datasettrain/train_full_info.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:48.934127Z","iopub.execute_input":"2021-05-29T06:57:48.93444Z","iopub.status.idle":"2021-05-29T06:57:49.041579Z","shell.execute_reply.started":"2021-05-29T06:57:48.934408Z","shell.execute_reply":"2021-05-29T06:57:49.040757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(columns=['Unnamed: 0'], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.080203Z","iopub.execute_input":"2021-05-29T06:57:49.080505Z","iopub.status.idle":"2021-05-29T06:57:49.086835Z","shell.execute_reply.started":"2021-05-29T06:57:49.080474Z","shell.execute_reply":"2021-05-29T06:57:49.085775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1-a. extract data with only one opacity","metadata":{}},{"cell_type":"code","source":"train_df[train_df.OpacityCount == 1]","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.247127Z","iopub.execute_input":"2021-05-29T06:57:49.247465Z","iopub.status.idle":"2021-05-29T06:57:49.294525Z","shell.execute_reply.started":"2021-05-29T06:57:49.247434Z","shell.execute_reply":"2021-05-29T06:57:49.293636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1-b. extract image paths","metadata":{}},{"cell_type":"code","source":"train_df[train_df.OpacityCount == 1]['path']","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.385872Z","iopub.execute_input":"2021-05-29T06:57:49.386231Z","iopub.status.idle":"2021-05-29T06:57:49.399083Z","shell.execute_reply.started":"2021-05-29T06:57:49.386189Z","shell.execute_reply":"2021-05-29T06:57:49.397776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path_array = train_df[train_df.OpacityCount == 1]['path'].values\nimg_path_array[:5]","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.515137Z","iopub.execute_input":"2021-05-29T06:57:49.51546Z","iopub.status.idle":"2021-05-29T06:57:49.523707Z","shell.execute_reply.started":"2021-05-29T06:57:49.515428Z","shell.execute_reply":"2021-05-29T06:57:49.522682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 2. Load Image Dataset","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.673191Z","iopub.execute_input":"2021-05-29T06:57:49.673511Z","iopub.status.idle":"2021-05-29T06:57:49.678893Z","shell.execute_reply.started":"2021-05-29T06:57:49.673481Z","shell.execute_reply":"2021-05-29T06:57:49.677761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imread(img_path_array[1]).shape","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:49.836051Z","iopub.execute_input":"2021-05-29T06:57:49.836405Z","iopub.status.idle":"2021-05-29T06:57:49.865512Z","shell.execute_reply.started":"2021-05-29T06:57:49.836373Z","shell.execute_reply":"2021-05-29T06:57:49.864558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.empty((256,256),dtype=int)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:50.285161Z","iopub.execute_input":"2021-05-29T06:57:50.285481Z","iopub.status.idle":"2021-05-29T06:57:50.291819Z","shell.execute_reply.started":"2021-05-29T06:57:50.28545Z","shell.execute_reply":"2021-05-29T06:57:50.290796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(img_path_array)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:50.454369Z","iopub.execute_input":"2021-05-29T06:57:50.454717Z","iopub.status.idle":"2021-05-29T06:57:50.460601Z","shell.execute_reply.started":"2021-05-29T06:57:50.454681Z","shell.execute_reply":"2021-05-29T06:57:50.459494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs = []\ni = 0\nfor path in img_path_array:\n    imgs.append(plt.imread(path))\n    i+=1\n    if i % 100 == 0:\n        print('{}/{}'.format(i,len(img_path_array)))\n    elif i == len(img_path_array):\n        print('{}/{} - done!'.format(i,len(img_path_array)))","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:51.265218Z","iopub.execute_input":"2021-05-29T06:57:51.265527Z","iopub.status.idle":"2021-05-29T06:57:57.539413Z","shell.execute_reply.started":"2021-05-29T06:57:51.265496Z","shell.execute_reply":"2021-05-29T06:57:57.53856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(imgs)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-29T06:57:57.540946Z","iopub.execute_input":"2021-05-29T06:57:57.541328Z","iopub.status.idle":"2021-05-29T06:57:57.569528Z","shell.execute_reply.started":"2021-05-29T06:57:57.541287Z","shell.execute_reply":"2021-05-29T06:57:57.568425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train[:,:,:,np.newaxis]\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:05:31.106517Z","iopub.execute_input":"2021-05-29T07:05:31.106925Z","iopub.status.idle":"2021-05-29T07:05:31.114251Z","shell.execute_reply.started":"2021-05-29T07:05:31.106893Z","shell.execute_reply":"2021-05-29T07:05:31.113033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2-a. Data Preprocessing","metadata":{"execution":{"iopub.status.busy":"2021-05-29T08:45:46.576695Z","iopub.execute_input":"2021-05-29T08:45:46.577042Z","iopub.status.idle":"2021-05-29T08:45:46.580837Z","shell.execute_reply.started":"2021-05-29T08:45:46.577011Z","shell.execute_reply":"2021-05-29T08:45:46.57996Z"}}},{"cell_type":"code","source":"train_df[train_df.OpacityCount==1].iloc[:,-4:].apply(lambda x : x.str.strip('[]'))","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:29:24.624878Z","iopub.execute_input":"2021-05-29T07:29:24.625215Z","iopub.status.idle":"2021-05-29T07:29:24.649105Z","shell.execute_reply.started":"2021-05-29T07:29:24.625183Z","shell.execute_reply":"2021-05-29T07:29:24.647847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"remove_brk_y = train_df[train_df.OpacityCount==1].iloc[:,-4:].apply(lambda x : x.str.strip('[]'))\nfor col in remove_brk_y.columns:\n    remove_brk_y[col] = remove_brk_y[col].astype('float')","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:33:44.02331Z","iopub.execute_input":"2021-05-29T07:33:44.023754Z","iopub.status.idle":"2021-05-29T07:33:44.042421Z","shell.execute_reply.started":"2021-05-29T07:33:44.023717Z","shell.execute_reply":"2021-05-29T07:33:44.041457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"remove_brk_y.info()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:34:04.01356Z","iopub.execute_input":"2021-05-29T07:34:04.014023Z","iopub.status.idle":"2021-05-29T07:34:04.035541Z","shell.execute_reply.started":"2021-05-29T07:34:04.013982Z","shell.execute_reply":"2021-05-29T07:34:04.033945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = np.array(remove_brk_y)\nY_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:34:17.210419Z","iopub.execute_input":"2021-05-29T07:34:17.210767Z","iopub.status.idle":"2021-05-29T07:34:17.217998Z","shell.execute_reply.started":"2021-05-29T07:34:17.210735Z","shell.execute_reply":"2021-05-29T07:34:17.217051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 3. Modeling","metadata":{}},{"cell_type":"markdown","source":"### 3-a. Train-valid split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_valid, Y_train, Y_valid = train_test_split(\n    X_train, Y_train, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:50:07.430044Z","iopub.execute_input":"2021-05-29T07:50:07.430387Z","iopub.status.idle":"2021-05-29T07:50:08.028887Z","shell.execute_reply.started":"2021-05-29T07:50:07.430353Z","shell.execute_reply":"2021-05-29T07:50:08.027981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Shape of X_train : ', X_train.shape)\nprint('Shape of Y_train : ', Y_train.shape)\nprint('Shape of X_valid : ', X_valid.shape)\nprint('Shape of Y_valid : ', Y_valid.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:51:12.10882Z","iopub.execute_input":"2021-05-29T07:51:12.109152Z","iopub.status.idle":"2021-05-29T07:51:12.116278Z","shell.execute_reply.started":"2021-05-29T07:51:12.10912Z","shell.execute_reply":"2021-05-29T07:51:12.114961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-b. Modeling","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense\n\nmodel = Sequential([\n    Conv2D(16,(3,3),activation='relu',input_shape=(256,256,1)),\n    MaxPooling2D(2,2),\n    Conv2D(32,(3,3),activation='relu'),\n    MaxPooling2D(2,2),\n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D(2,2),\n    Conv2D(128,(3,3),activation='relu'),\n    MaxPooling2D(2,2),\n    Flatten(),\n    Dropout(0.5),\n    Dense(128,activation='relu'),\n    Dense(32,activation='relu'),\n    Dense(4,activation='linear')\n])","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:51:23.310173Z","iopub.execute_input":"2021-05-29T07:51:23.310533Z","iopub.status.idle":"2021-05-29T07:51:23.396932Z","shell.execute_reply.started":"2021-05-29T07:51:23.310501Z","shell.execute_reply":"2021-05-29T07:51:23.396019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:51:24.001562Z","iopub.execute_input":"2021-05-29T07:51:24.001933Z","iopub.status.idle":"2021-05-29T07:51:24.016909Z","shell.execute_reply.started":"2021-05-29T07:51:24.0019Z","shell.execute_reply":"2021-05-29T07:51:24.015984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='mse',metrics=['mae'])","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:51:26.969864Z","iopub.execute_input":"2021-05-29T07:51:26.970199Z","iopub.status.idle":"2021-05-29T07:51:26.983434Z","shell.execute_reply.started":"2021-05-29T07:51:26.970165Z","shell.execute_reply":"2021-05-29T07:51:26.982595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:52:11.92302Z","iopub.execute_input":"2021-05-29T07:52:11.923375Z","iopub.status.idle":"2021-05-29T07:52:11.927889Z","shell.execute_reply.started":"2021-05-29T07:52:11.923343Z","shell.execute_reply":"2021-05-29T07:52:11.926667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = 'my_checkpoint.ckpt'\ncp = ModelCheckpoint(\n    filepath=filepath,\n    save_weights_only=True,\n    save_best_only=True,\n    monitor='val_loss',\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:53:05.030663Z","iopub.execute_input":"2021-05-29T07:53:05.031002Z","iopub.status.idle":"2021-05-29T07:53:05.037059Z","shell.execute_reply.started":"2021-05-29T07:53:05.030973Z","shell.execute_reply":"2021-05-29T07:53:05.035921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-c. Training","metadata":{}},{"cell_type":"code","source":"model.fit(\n    X_train, Y_train,\n    validation_data=(X_valid,Y_valid),\n    epochs=12,\n    callbacks=[cp]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T07:53:12.142437Z","iopub.execute_input":"2021-05-29T07:53:12.14281Z","iopub.status.idle":"2021-05-29T07:53:36.548532Z","shell.execute_reply.started":"2021-05-29T07:53:12.142777Z","shell.execute_reply":"2021-05-29T07:53:36.547645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-d. Evaluation","metadata":{}},{"cell_type":"code","source":"model.load_weights(filepath)\nmodel.evaluate(X_valid, Y_valid)","metadata":{"execution":{"iopub.status.busy":"2021-05-29T08:13:09.716695Z","iopub.execute_input":"2021-05-29T08:13:09.717023Z","iopub.status.idle":"2021-05-29T08:13:10.347314Z","shell.execute_reply.started":"2021-05-29T08:13:09.716993Z","shell.execute_reply":"2021-05-29T08:13:10.34653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\nIf there are any mistakes, please feel free to give feedback! Thank you!","metadata":{}}]}