{"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 2. Basic Modeling - Simplest Image Classification Models using Keras\n\n👉 Part 1. [EDA to Preprocessing](https://www.kaggle.com/songseungwon/siim-covid-19-detection-10-step-tutorial-1)\n\n👉 Mini Part. [Preprocessing for Multi-Output Regression that Detect Opacities](https://www.kaggle.com/songseungwon/siim-covid-19-detection-mini-part-preprocess)","metadata":{}},{"cell_type":"markdown","source":"### Thanks for nice reference :\n\n`load dataset(original image size info-)`\n- [Resized to 256px JPG](https://www.kaggle.com/xhlulu/siim-covid19-resized-to-256px-jpg)","metadata":{}},{"cell_type":"markdown","source":"> Index\n```\nStep 1. Load Data and Trim for use\n     1-a. load train-dataframe\n     1-b. load meta-dataframe\n     1-c. load image data array\n     1-d. calculate image resize ratio information\nStep 2. Image Pre-Classification with Data generator\n     2-a. classify image id by opacity types\n     2-b. sort image files into each type's folder\n     2-c. data generation, split train/valid set\nStep 3. Modeling I - Basic Multiclass classifier\n     3-a. import libraries\n     3-b. basic modeling with keras api\n     3-c. model compile\n     3-d. save model checkpoint\n     3-e. model fit\n     3-f. model evaluate & save\n     3-g. reload model & model summary\nStep 4. Modeling II - Multiclass classifier using EfficientNet(Transfer Learning)\n     4-a. Load the EfficientNet and try it out\n     4-b. Improving performance with an appropriate form\n```","metadata":{}},{"cell_type":"markdown","source":"## Step 1. Load Data and Trim for use","metadata":{}},{"cell_type":"markdown","source":"### 1-a. load train-dataframe","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.568648Z","iopub.execute_input":"2021-05-25T09:45:48.569121Z","iopub.status.idle":"2021-05-25T09:45:48.573258Z","shell.execute_reply.started":"2021-05-25T09:45:48.569077Z","shell.execute_reply":"2021-05-25T09:45:48.572087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = pd.read_csv('/kaggle/input/siimcovid19-train-data-that-opacitycount-added/train_df.csv')\n# local\ntrain_df = pd.read_csv('/kaggle/input/siimcovid19-train-data-that-opacitycount-added/train_df.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.575007Z","iopub.execute_input":"2021-05-25T09:45:48.575441Z","iopub.status.idle":"2021-05-25T09:45:48.656999Z","shell.execute_reply.started":"2021-05-25T09:45:48.575348Z","shell.execute_reply":"2021-05-25T09:45:48.656241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.658718Z","iopub.execute_input":"2021-05-25T09:45:48.659044Z","iopub.status.idle":"2021-05-25T09:45:48.68454Z","shell.execute_reply.started":"2021-05-25T09:45:48.659011Z","shell.execute_reply":"2021-05-25T09:45:48.683541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We don't use dcm file. drop 'path' column","metadata":{}},{"cell_type":"code","source":"train_df.drop(columns='Path', axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.686518Z","iopub.execute_input":"2021-05-25T09:45:48.686904Z","iopub.status.idle":"2021-05-25T09:45:48.693675Z","shell.execute_reply.started":"2021-05-25T09:45:48.686865Z","shell.execute_reply":"2021-05-25T09:45:48.692742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.695319Z","iopub.execute_input":"2021-05-25T09:45:48.696052Z","iopub.status.idle":"2021-05-25T09:45:48.720958Z","shell.execute_reply.started":"2021-05-25T09:45:48.696012Z","shell.execute_reply":"2021-05-25T09:45:48.71979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And add 'Opacity' Column. The Value is 1 If Opacity detected, else 0","metadata":{}},{"cell_type":"code","source":"train_df['Opacity'] = train_df.apply(lambda row : 1 if row.label.split(' ')[0]=='opacity' else 0, axis=1)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.722366Z","iopub.execute_input":"2021-05-25T09:45:48.722796Z","iopub.status.idle":"2021-05-25T09:45:48.843761Z","shell.execute_reply.started":"2021-05-25T09:45:48.722759Z","shell.execute_reply":"2021-05-25T09:45:48.842864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.drop(columns=['Unnamed: 0'], inplace=True)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.845002Z","iopub.execute_input":"2021-05-25T09:45:48.845335Z","iopub.status.idle":"2021-05-25T09:45:48.866958Z","shell.execute_reply.started":"2021-05-25T09:45:48.8453Z","shell.execute_reply":"2021-05-25T09:45:48.866218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1-b. load meta-dataframe","metadata":{}},{"cell_type":"markdown","source":"We need the size of the individual images. This is necessary later to calculate the ratio and find the coordinates of the box border to detect the opacity.","metadata":{}},{"cell_type":"code","source":"meta_df = pd.read_csv('/kaggle/input/siim-covid19-resized-to-256px-jpg/meta.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.86956Z","iopub.execute_input":"2021-05-25T09:45:48.86991Z","iopub.status.idle":"2021-05-25T09:45:48.892118Z","shell.execute_reply.started":"2021-05-25T09:45:48.869882Z","shell.execute_reply":"2021-05-25T09:45:48.891384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.894772Z","iopub.execute_input":"2021-05-25T09:45:48.895029Z","iopub.status.idle":"2021-05-25T09:45:48.905874Z","shell.execute_reply.started":"2021-05-25T09:45:48.895007Z","shell.execute_reply":"2021-05-25T09:45:48.904946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Y(height) : `dim0` \n- X(width) : `dim1`\n","metadata":{}},{"cell_type":"code","source":"meta_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.907443Z","iopub.execute_input":"2021-05-25T09:45:48.90787Z","iopub.status.idle":"2021-05-25T09:45:48.924254Z","shell.execute_reply.started":"2021-05-25T09:45:48.907833Z","shell.execute_reply":"2021-05-25T09:45:48.922938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df.split.unique()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.925448Z","iopub.execute_input":"2021-05-25T09:45:48.925854Z","iopub.status.idle":"2021-05-25T09:45:48.932647Z","shell.execute_reply.started":"2021-05-25T09:45:48.925823Z","shell.execute_reply":"2021-05-25T09:45:48.931541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.934327Z","iopub.execute_input":"2021-05-25T09:45:48.935172Z","iopub.status.idle":"2021-05-25T09:45:48.940381Z","shell.execute_reply.started":"2021-05-25T09:45:48.935119Z","shell.execute_reply":"2021-05-25T09:45:48.939268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta_df = meta_df.loc[meta_df.split=='train']\ntrain_meta_df.drop('split',axis=1,inplace=True)\ntrain_meta_df.columns = ['id', 'origin_img_height','origin_img_width']\ntrain_meta_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.942135Z","iopub.execute_input":"2021-05-25T09:45:48.942618Z","iopub.status.idle":"2021-05-25T09:45:48.959873Z","shell.execute_reply.started":"2021-05-25T09:45:48.94258Z","shell.execute_reply":"2021-05-25T09:45:48.958901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta_df","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.961102Z","iopub.execute_input":"2021-05-25T09:45:48.961617Z","iopub.status.idle":"2021-05-25T09:45:48.975137Z","shell.execute_reply.started":"2021-05-25T09:45:48.961579Z","shell.execute_reply":"2021-05-25T09:45:48.973986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.976704Z","iopub.execute_input":"2021-05-25T09:45:48.977172Z","iopub.status.idle":"2021-05-25T09:45:48.992798Z","shell.execute_reply.started":"2021-05-25T09:45:48.977125Z","shell.execute_reply":"2021-05-25T09:45:48.991736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test lambda\ntrain_df['id'].apply(lambda x : x.split('_')[0])\n","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:48.994492Z","iopub.execute_input":"2021-05-25T09:45:48.994897Z","iopub.status.idle":"2021-05-25T09:45:49.008144Z","shell.execute_reply.started":"2021-05-25T09:45:48.994861Z","shell.execute_reply":"2021-05-25T09:45:49.007173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'] = train_df['id'].apply(lambda x : x.split('_')[0])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.009574Z","iopub.execute_input":"2021-05-25T09:45:49.010215Z","iopub.status.idle":"2021-05-25T09:45:49.021142Z","shell.execute_reply.started":"2021-05-25T09:45:49.010175Z","shell.execute_reply":"2021-05-25T09:45:49.020206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.022702Z","iopub.execute_input":"2021-05-25T09:45:49.023171Z","iopub.status.idle":"2021-05-25T09:45:49.041702Z","shell.execute_reply.started":"2021-05-25T09:45:49.023125Z","shell.execute_reply":"2021-05-25T09:45:49.040705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.merge(train_df, train_meta_df, on='id')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.043113Z","iopub.execute_input":"2021-05-25T09:45:49.043584Z","iopub.status.idle":"2021-05-25T09:45:49.061022Z","shell.execute_reply.started":"2021-05-25T09:45:49.043544Z","shell.execute_reply":"2021-05-25T09:45:49.060152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.062299Z","iopub.execute_input":"2021-05-25T09:45:49.062697Z","iopub.status.idle":"2021-05-25T09:45:49.078384Z","shell.execute_reply.started":"2021-05-25T09:45:49.06265Z","shell.execute_reply":"2021-05-25T09:45:49.077487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1-c. load image data array","metadata":{}},{"cell_type":"code","source":"path = '/kaggle/input/siim-covid19-resized-to-256px-jpg/train/'\ntrain_imgs_path = list(train_df['id'].apply(lambda x : path + x + '.jpg').values)\ntrain_imgs_path[:10]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.079625Z","iopub.execute_input":"2021-05-25T09:45:49.079957Z","iopub.status.idle":"2021-05-25T09:45:49.089094Z","shell.execute_reply.started":"2021-05-25T09:45:49.079925Z","shell.execute_reply":"2021-05-25T09:45:49.088209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Test sample image","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.090593Z","iopub.execute_input":"2021-05-25T09:45:49.091073Z","iopub.status.idle":"2021-05-25T09:45:49.09793Z","shell.execute_reply.started":"2021-05-25T09:45:49.091017Z","shell.execute_reply":"2021-05-25T09:45:49.096994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = plt.imread(train_imgs_path[0])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.103777Z","iopub.execute_input":"2021-05-25T09:45:49.104051Z","iopub.status.idle":"2021-05-25T09:45:49.126384Z","shell.execute_reply.started":"2021-05-25T09:45:49.104027Z","shell.execute_reply":"2021-05-25T09:45:49.12566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.128586Z","iopub.execute_input":"2021-05-25T09:45:49.128909Z","iopub.status.idle":"2021-05-25T09:45:49.13381Z","shell.execute_reply.started":"2021-05-25T09:45:49.128878Z","shell.execute_reply":"2021-05-25T09:45:49.132852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img, cmap='gray');","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.135199Z","iopub.execute_input":"2021-05-25T09:45:49.135577Z","iopub.status.idle":"2021-05-25T09:45:49.280618Z","shell.execute_reply.started":"2021-05-25T09:45:49.135544Z","shell.execute_reply":"2021-05-25T09:45:49.279725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.281914Z","iopub.execute_input":"2021-05-25T09:45:49.282243Z","iopub.status.idle":"2021-05-25T09:45:49.285919Z","shell.execute_reply.started":"2021-05-25T09:45:49.282209Z","shell.execute_reply":"2021-05-25T09:45:49.285048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 0\ntrain_imgs = []\nfor img_path in train_imgs_path:\n    img = plt.imread(img_path)\n    train_imgs.append(img)\n    i += 1\n    if i % 1000 == 0:\n        print('{} / {}'.format(i, len(train_imgs_path)))\n    elif i == 6334:\n        print('6334 / 6334 (End)')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:45:49.287268Z","iopub.execute_input":"2021-05-25T09:45:49.287622Z","iopub.status.idle":"2021-05-25T09:46:15.665576Z","shell.execute_reply.started":"2021-05-25T09:45:49.287589Z","shell.execute_reply":"2021-05-25T09:46:15.663919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.666867Z","iopub.execute_input":"2021-05-25T09:46:15.66725Z","iopub.status.idle":"2021-05-25T09:46:15.675937Z","shell.execute_reply.started":"2021-05-25T09:46:15.667213Z","shell.execute_reply":"2021-05-25T09:46:15.675161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = np.array(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.677067Z","iopub.execute_input":"2021-05-25T09:46:15.677769Z","iopub.status.idle":"2021-05-25T09:46:15.811797Z","shell.execute_reply.started":"2021-05-25T09:46:15.677721Z","shell.execute_reply":"2021-05-25T09:46:15.810891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.813082Z","iopub.execute_input":"2021-05-25T09:46:15.813434Z","iopub.status.idle":"2021-05-25T09:46:15.819214Z","shell.execute_reply.started":"2021-05-25T09:46:15.813398Z","shell.execute_reply":"2021-05-25T09:46:15.818329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"add Channel (3dim to 4dim, gray)","metadata":{}},{"cell_type":"code","source":"train_imgs_path[0]","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.820559Z","iopub.execute_input":"2021-05-25T09:46:15.82109Z","iopub.status.idle":"2021-05-25T09:46:15.829045Z","shell.execute_reply.started":"2021-05-25T09:46:15.821054Z","shell.execute_reply":"2021-05-25T09:46:15.828202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs[:,:,:,np.newaxis].shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.830189Z","iopub.execute_input":"2021-05-25T09:46:15.830649Z","iopub.status.idle":"2021-05-25T09:46:15.839946Z","shell.execute_reply.started":"2021-05-25T09:46:15.830617Z","shell.execute_reply":"2021-05-25T09:46:15.83896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs_4dim = train_imgs[:,:,:,np.newaxis]\ntrain_imgs_4dim.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.841359Z","iopub.execute_input":"2021-05-25T09:46:15.84176Z","iopub.status.idle":"2021-05-25T09:46:15.854402Z","shell.execute_reply.started":"2021-05-25T09:46:15.841725Z","shell.execute_reply":"2021-05-25T09:46:15.853385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"And simply EDA","metadata":{}},{"cell_type":"code","source":"len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.85581Z","iopub.execute_input":"2021-05-25T09:46:15.856226Z","iopub.status.idle":"2021-05-25T09:46:15.86394Z","shell.execute_reply.started":"2021-05-25T09:46:15.856193Z","shell.execute_reply":"2021-05-25T09:46:15.863109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(train_imgs[0].reshape(-1)), max(train_imgs[0].reshape(-1))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.865362Z","iopub.execute_input":"2021-05-25T09:46:15.865761Z","iopub.status.idle":"2021-05-25T09:46:15.899839Z","shell.execute_reply.started":"2021-05-25T09:46:15.865705Z","shell.execute_reply":"2021-05-25T09:46:15.899033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(train_imgs[13].reshape(-1)), max(train_imgs[13].reshape(-1))","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.902671Z","iopub.execute_input":"2021-05-25T09:46:15.903011Z","iopub.status.idle":"2021-05-25T09:46:15.935885Z","shell.execute_reply.started":"2021-05-25T09:46:15.902989Z","shell.execute_reply":"2021-05-25T09:46:15.935138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 1-d. calculate image resize ratio information","metadata":{}},{"cell_type":"code","source":"train_df['origin_img_height']","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.936912Z","iopub.execute_input":"2021-05-25T09:46:15.937226Z","iopub.status.idle":"2021-05-25T09:46:15.95024Z","shell.execute_reply.started":"2021-05-25T09:46:15.937197Z","shell.execute_reply":"2021-05-25T09:46:15.949418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['height_ratio'] = train_df['origin_img_height'].apply(lambda x : 255/x)\ntrain_df['height_ratio']","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.951396Z","iopub.execute_input":"2021-05-25T09:46:15.951753Z","iopub.status.idle":"2021-05-25T09:46:15.96506Z","shell.execute_reply.started":"2021-05-25T09:46:15.95172Z","shell.execute_reply":"2021-05-25T09:46:15.963855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['origin_img_width']","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.966258Z","iopub.execute_input":"2021-05-25T09:46:15.966595Z","iopub.status.idle":"2021-05-25T09:46:15.974419Z","shell.execute_reply.started":"2021-05-25T09:46:15.966562Z","shell.execute_reply":"2021-05-25T09:46:15.973375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['width_ratio'] = train_df['origin_img_width'].apply(lambda x : 255/x)\ntrain_df['width_ratio']","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.97573Z","iopub.execute_input":"2021-05-25T09:46:15.97607Z","iopub.status.idle":"2021-05-25T09:46:15.989536Z","shell.execute_reply.started":"2021-05-25T09:46:15.976035Z","shell.execute_reply":"2021-05-25T09:46:15.988816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:15.990521Z","iopub.execute_input":"2021-05-25T09:46:15.990763Z","iopub.status.idle":"2021-05-25T09:46:16.014119Z","shell.execute_reply.started":"2021-05-25T09:46:15.990738Z","shell.execute_reply":"2021-05-25T09:46:16.013197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 2. Image Pre-Classification with Data generator","metadata":{}},{"cell_type":"markdown","source":"### 2-a. classify image id by Opacity types","metadata":{}},{"cell_type":"code","source":"types = list(train_df.columns[5:9])\ntypes","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:16.015441Z","iopub.execute_input":"2021-05-25T09:46:16.015824Z","iopub.status.idle":"2021-05-25T09:46:16.021901Z","shell.execute_reply.started":"2021-05-25T09:46:16.015788Z","shell.execute_reply":"2021-05-25T09:46:16.020909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:16.023452Z","iopub.execute_input":"2021-05-25T09:46:16.023887Z","iopub.status.idle":"2021-05-25T09:46:16.03053Z","shell.execute_reply.started":"2021-05-25T09:46:16.023849Z","shell.execute_reply":"2021-05-25T09:46:16.029416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:16.032112Z","iopub.execute_input":"2021-05-25T09:46:16.032636Z","iopub.status.idle":"2021-05-25T09:46:16.038771Z","shell.execute_reply.started":"2021-05-25T09:46:16.032601Z","shell.execute_reply":"2021-05-25T09:46:16.03766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2-b. sort image files into each type's folder","metadata":{}},{"cell_type":"markdown","source":"Create folders for each class **in advance**, and save images in each folder.","metadata":{}},{"cell_type":"code","source":"!mkdir ./genData\n!mkdir ./genData/Negative\n!mkdir ./genData/Typical\n!mkdir ./genData/Indeterminate\n!mkdir ./genData/Atypical","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:16.040306Z","iopub.execute_input":"2021-05-25T09:46:16.040825Z","iopub.status.idle":"2021-05-25T09:46:19.214033Z","shell.execute_reply.started":"2021-05-25T09:46:16.04079Z","shell.execute_reply":"2021-05-25T09:46:19.213013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Negative for Pneumonia\nimgs_Negative = list(train_df[train_df[types[0]]==1].index)\nfor idx in imgs_Negative:\n    plt.imsave('./genData/Negative/{}.jpg'.format(train_df.loc[idx,'id']), train_imgs[idx], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:19.217324Z","iopub.execute_input":"2021-05-25T09:46:19.217609Z","iopub.status.idle":"2021-05-25T09:46:25.08464Z","shell.execute_reply.started":"2021-05-25T09:46:19.217579Z","shell.execute_reply":"2021-05-25T09:46:25.083824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Typical Apperance\nimgs_Typical = list(train_df[train_df[types[1]]==1].index)\nfor idx in imgs_Typical:\n    plt.imsave('./genData/Typical/{}.jpg'.format(train_df.loc[idx,'id']), train_imgs[idx], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:25.08591Z","iopub.execute_input":"2021-05-25T09:46:25.086281Z","iopub.status.idle":"2021-05-25T09:46:34.9193Z","shell.execute_reply.started":"2021-05-25T09:46:25.086246Z","shell.execute_reply":"2021-05-25T09:46:34.918409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Indeterminate Apearance\nimgs_Indeterminate = list(train_df[train_df[types[2]]==1].index)\nfor idx in imgs_Indeterminate:\n    plt.imsave('./genData/Indeterminate/{}.jpg'.format(train_df.loc[idx,'id']), train_imgs[idx], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:34.920574Z","iopub.execute_input":"2021-05-25T09:46:34.920942Z","iopub.status.idle":"2021-05-25T09:46:38.458566Z","shell.execute_reply.started":"2021-05-25T09:46:34.920904Z","shell.execute_reply":"2021-05-25T09:46:38.45773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Atypical Apearance\nimgs_Atypical = list(train_df[train_df[types[3]]==1].index)\nfor idx in imgs_Atypical:\n    plt.imsave('./genData/Atypical/{}.jpg'.format(train_df.loc[idx,'id']), train_imgs[idx], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:38.459797Z","iopub.execute_input":"2021-05-25T09:46:38.460153Z","iopub.status.idle":"2021-05-25T09:46:40.030103Z","shell.execute_reply.started":"2021-05-25T09:46:38.460118Z","shell.execute_reply":"2021-05-25T09:46:40.029281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2-c. data generation, split train/valid set","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.031292Z","iopub.execute_input":"2021-05-25T09:46:40.031657Z","iopub.status.idle":"2021-05-25T09:46:40.036056Z","shell.execute_reply.started":"2021-05-25T09:46:40.031608Z","shell.execute_reply":"2021-05-25T09:46:40.035166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idg = ImageDataGenerator(\n    rescale=1. / 255,\n    rotation_range=3,\n    width_shift_range=0.05,\n    height_shift_range=0.05,\n    zoom_range=0.05,\n    horizontal_flip=False,\n    fill_mode='reflect',\n    validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.037555Z","iopub.execute_input":"2021-05-25T09:46:40.038183Z","iopub.status.idle":"2021-05-25T09:46:40.045155Z","shell.execute_reply.started":"2021-05-25T09:46:40.038146Z","shell.execute_reply":"2021-05-25T09:46:40.044428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = './genData'\nbatch_size = 64\ntarget_size = (256, 256)\nclass_mode = 'categorical'\ncolor_mode = 'grayscale'","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.046445Z","iopub.execute_input":"2021-05-25T09:46:40.04682Z","iopub.status.idle":"2021-05-25T09:46:40.053741Z","shell.execute_reply.started":"2021-05-25T09:46:40.046785Z","shell.execute_reply":"2021-05-25T09:46:40.05294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = idg.flow_from_directory(\n    data_path,\n    batch_size=batch_size,\n    target_size=target_size,\n    class_mode=class_mode,\n    color_mode=color_mode,\n    subset = 'training'\n)\n\nvalid_gen = idg.flow_from_directory(\n    data_path,\n    batch_size = batch_size,\n    target_size = target_size,\n    class_mode = class_mode,\n    color_mode=color_mode,\n    subset = 'validation'\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.054924Z","iopub.execute_input":"2021-05-25T09:46:40.055307Z","iopub.status.idle":"2021-05-25T09:46:40.483176Z","shell.execute_reply.started":"2021-05-25T09:46:40.055272Z","shell.execute_reply":"2021-05-25T09:46:40.482416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 3. Modeling I - Basic Multiclass classifier","metadata":{}},{"cell_type":"markdown","source":"### 3-a. import libraries","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.484238Z","iopub.execute_input":"2021-05-25T09:46:40.484576Z","iopub.status.idle":"2021-05-25T09:46:40.491914Z","shell.execute_reply.started":"2021-05-25T09:46:40.484539Z","shell.execute_reply":"2021-05-25T09:46:40.491156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-b. basic modeling with keras api","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(64, (3,3), activation='relu', input_shape=(256, 256,1)),\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    Conv2D(128, (3,3), activation='relu'),\n    MaxPooling2D(2,2),\n    Flatten(),\n    Dropout(0.5),\n    Dense(128, activation='relu'),\n    Dense(4, activation='softmax')\n])\nmodel.summary() ","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:40.493125Z","iopub.execute_input":"2021-05-25T09:46:40.493605Z","iopub.status.idle":"2021-05-25T09:46:42.743806Z","shell.execute_reply.started":"2021-05-25T09:46:40.493566Z","shell.execute_reply":"2021-05-25T09:46:42.742992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-c. model compile","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:42.747746Z","iopub.execute_input":"2021-05-25T09:46:42.749795Z","iopub.status.idle":"2021-05-25T09:46:42.771962Z","shell.execute_reply.started":"2021-05-25T09:46:42.749754Z","shell.execute_reply":"2021-05-25T09:46:42.77121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-d. save model checkpoint","metadata":{}},{"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-25T09:46:42.775684Z","iopub.execute_input":"2021-05-25T09:46:42.777744Z","iopub.status.idle":"2021-05-25T09:46:42.783852Z","shell.execute_reply.started":"2021-05-25T09:46:42.777707Z","shell.execute_reply":"2021-05-25T09:46:42.782854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-e. model fit","metadata":{}},{"cell_type":"code","source":"epochs = 1 # just for test\nmodel.fit(\n    train_gen,\n    validation_data = (valid_gen),\n    epochs = epochs,\n    callbacks=[cp]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:46:42.787815Z","iopub.execute_input":"2021-05-25T09:46:42.790139Z","iopub.status.idle":"2021-05-25T09:47:46.051623Z","shell.execute_reply.started":"2021-05-25T09:46:42.790102Z","shell.execute_reply":"2021-05-25T09:47:46.050786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-f. model evaluate & save","metadata":{}},{"cell_type":"code","source":"model.load_weights(filepath)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:46.053067Z","iopub.execute_input":"2021-05-25T09:47:46.053431Z","iopub.status.idle":"2021-05-25T09:47:46.124374Z","shell.execute_reply.started":"2021-05-25T09:47:46.053391Z","shell.execute_reply":"2021-05-25T09:47:46.123576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_gen)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:46.125629Z","iopub.execute_input":"2021-05-25T09:47:46.125955Z","iopub.status.idle":"2021-05-25T09:47:55.940432Z","shell.execute_reply.started":"2021-05-25T09:47:46.125919Z","shell.execute_reply":"2021-05-25T09:47:55.939676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./model/basic_cnn.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:55.942935Z","iopub.execute_input":"2021-05-25T09:47:55.943194Z","iopub.status.idle":"2021-05-25T09:47:56.027444Z","shell.execute_reply.started":"2021-05-25T09:47:55.943168Z","shell.execute_reply":"2021-05-25T09:47:56.026607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 3-g. reload model & model summary","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:56.028605Z","iopub.execute_input":"2021-05-25T09:47:56.028929Z","iopub.status.idle":"2021-05-25T09:47:56.032297Z","shell.execute_reply.started":"2021-05-25T09:47:56.028896Z","shell.execute_reply":"2021-05-25T09:47:56.031375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel = tf.keras.models.load_model('./model/basic_cnn.h5')","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:56.033445Z","iopub.execute_input":"2021-05-25T09:47:56.034287Z","iopub.status.idle":"2021-05-25T09:47:56.195974Z","shell.execute_reply.started":"2021-05-25T09:47:56.03425Z","shell.execute_reply":"2021-05-25T09:47:56.195153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mymodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T09:47:56.197124Z","iopub.execute_input":"2021-05-25T09:47:56.197446Z","iopub.status.idle":"2021-05-25T09:47:56.209426Z","shell.execute_reply.started":"2021-05-25T09:47:56.197413Z","shell.execute_reply":"2021-05-25T09:47:56.20856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Step 4. Modeling II - Multiclass classifier using EfficientNet(Transfer Learning)","metadata":{}},{"cell_type":"markdown","source":"### 4-a. Load the EfficientNet and try it out","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB0","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:38.340567Z","iopub.execute_input":"2021-05-25T10:18:38.340911Z","iopub.status.idle":"2021-05-25T10:18:38.34488Z","shell.execute_reply.started":"2021-05-25T10:18:38.340884Z","shell.execute_reply":"2021-05-25T10:18:38.343569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"efc = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(256,256,3))\nefc.trainable=False","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:38.511792Z","iopub.execute_input":"2021-05-25T10:18:38.512101Z","iopub.status.idle":"2021-05-25T10:18:39.935743Z","shell.execute_reply.started":"2021-05-25T10:18:38.512074Z","shell.execute_reply":"2021-05-25T10:18:39.93487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    efc,\n    Flatten(),\n    Dropout(0.5),\n    Dense(256, activation='relu'),\n    Dense(4, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:39.939078Z","iopub.execute_input":"2021-05-25T10:18:39.939339Z","iopub.status.idle":"2021-05-25T10:18:40.509991Z","shell.execute_reply.started":"2021-05-25T10:18:39.939314Z","shell.execute_reply":"2021-05-25T10:18:40.509162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:40.511892Z","iopub.execute_input":"2021-05-25T10:18:40.512226Z","iopub.status.idle":"2021-05-25T10:18:40.532262Z","shell.execute_reply.started":"2021-05-25T10:18:40.512191Z","shell.execute_reply":"2021-05-25T10:18:40.531328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:40.533699Z","iopub.execute_input":"2021-05-25T10:18:40.534038Z","iopub.status.idle":"2021-05-25T10:18:40.548001Z","shell.execute_reply.started":"2021-05-25T10:18:40.534004Z","shell.execute_reply":"2021-05-25T10:18:40.547108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = 'my_checkpoint_efc.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-25T10:18:40.550584Z","iopub.execute_input":"2021-05-25T10:18:40.550827Z","iopub.status.idle":"2021-05-25T10:18:40.556654Z","shell.execute_reply.started":"2021-05-25T10:18:40.550805Z","shell.execute_reply":"2021-05-25T10:18:40.555821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=1\nmodel.fit(\n    train_gen,\n    validation_data=(valid_gen),\n    epochs=epochs,\n    callbacks=[cp]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:18:40.557925Z","iopub.execute_input":"2021-05-25T10:18:40.558205Z","iopub.status.idle":"2021-05-25T10:19:20.942092Z","shell.execute_reply.started":"2021-05-25T10:18:40.558182Z","shell.execute_reply":"2021-05-25T10:19:20.941279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(filepath)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:19:23.223916Z","iopub.execute_input":"2021-05-25T10:19:23.224231Z","iopub.status.idle":"2021-05-25T10:19:24.308388Z","shell.execute_reply.started":"2021-05-25T10:19:23.224203Z","shell.execute_reply":"2021-05-25T10:19:24.306751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_gen)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:19:24.309806Z","iopub.execute_input":"2021-05-25T10:19:24.310132Z","iopub.status.idle":"2021-05-25T10:19:30.928556Z","shell.execute_reply.started":"2021-05-25T10:19:24.310097Z","shell.execute_reply":"2021-05-25T10:19:30.927773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The performance is not very different from the basic cnn model.\n\n\nIn fact, efficientnet (which is precisely efficientnetB0) is designed according to the image size (224,224), and the input data range should be 0~255. That is, pure data that has not been normalized must pass through the model. normalize is included in the model itself\n\ndocument : [Image classification via fine-tuning with EfficientNet](https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/)\n\nLet's use the model as recommended in the official documentation.","metadata":{}},{"cell_type":"markdown","source":"### 4-b.  Improving performance with an appropriate form","metadata":{}},{"cell_type":"code","source":"idg = ImageDataGenerator(\n    # rescale False\n    rotation_range=3,\n    width_shift_range=0.05,\n    height_shift_range=0.05,\n    zoom_range=0.05,\n    horizontal_flip=False,\n    fill_mode='reflect',\n    validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:27:55.715992Z","iopub.execute_input":"2021-05-25T10:27:55.716326Z","iopub.status.idle":"2021-05-25T10:27:55.721756Z","shell.execute_reply.started":"2021-05-25T10:27:55.716293Z","shell.execute_reply":"2021-05-25T10:27:55.720875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = './genData'\nbatch_size = 64\ntarget_size = (224, 224)\nclass_mode = 'categorical'\ncolor_mode = 'grayscale'","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:27:56.915082Z","iopub.execute_input":"2021-05-25T10:27:56.915393Z","iopub.status.idle":"2021-05-25T10:27:56.920952Z","shell.execute_reply.started":"2021-05-25T10:27:56.915364Z","shell.execute_reply":"2021-05-25T10:27:56.91997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = idg.flow_from_directory(\n    data_path,\n    batch_size=batch_size,\n    target_size=target_size,\n    class_mode=class_mode,\n    color_mode=color_mode,\n    subset = 'training'\n)\n\nvalid_gen = idg.flow_from_directory(\n    data_path,\n    batch_size = batch_size,\n    target_size = target_size,\n    class_mode = class_mode,\n    color_mode=color_mode,\n    subset = 'validation'\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:27:58.117324Z","iopub.execute_input":"2021-05-25T10:27:58.117702Z","iopub.status.idle":"2021-05-25T10:27:58.556384Z","shell.execute_reply.started":"2021-05-25T10:27:58.117672Z","shell.execute_reply":"2021-05-25T10:27:58.555566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"efc = EfficientNetB0(weights='imagenet',\n                     include_top=False, \n                     input_shape=(224,224,3),\n                     drop_connect_rate=0.4)\nefc.trainable=False","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:29:35.171176Z","iopub.execute_input":"2021-05-25T10:29:35.171508Z","iopub.status.idle":"2021-05-25T10:29:36.582948Z","shell.execute_reply.started":"2021-05-25T10:29:35.171473Z","shell.execute_reply":"2021-05-25T10:29:36.582083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    efc,\n    Flatten(),\n    Dropout(0.5),\n    Dense(256, activation='relu'),\n    Dense(4, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:28:00.417379Z","iopub.execute_input":"2021-05-25T10:28:00.417739Z","iopub.status.idle":"2021-05-25T10:28:00.990289Z","shell.execute_reply.started":"2021-05-25T10:28:00.417703Z","shell.execute_reply":"2021-05-25T10:28:00.989472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:28:00.991865Z","iopub.execute_input":"2021-05-25T10:28:00.992206Z","iopub.status.idle":"2021-05-25T10:28:01.006646Z","shell.execute_reply.started":"2021-05-25T10:28:00.99217Z","shell.execute_reply":"2021-05-25T10:28:01.005865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = 'my_checkpoint_efc_224.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-25T10:28:01.390773Z","iopub.execute_input":"2021-05-25T10:28:01.391053Z","iopub.status.idle":"2021-05-25T10:28:01.394954Z","shell.execute_reply.started":"2021-05-25T10:28:01.391028Z","shell.execute_reply":"2021-05-25T10:28:01.3941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=1\nmodel.fit(\n    train_gen,\n    validation_data=(valid_gen),\n    epochs=epochs,\n    callbacks=[cp]\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:28:01.890237Z","iopub.execute_input":"2021-05-25T10:28:01.890535Z","iopub.status.idle":"2021-05-25T10:28:37.12948Z","shell.execute_reply.started":"2021-05-25T10:28:01.890507Z","shell.execute_reply":"2021-05-25T10:28:37.128712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights(filepath)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:28:43.921884Z","iopub.execute_input":"2021-05-25T10:28:43.922201Z","iopub.status.idle":"2021-05-25T10:28:44.934273Z","shell.execute_reply.started":"2021-05-25T10:28:43.922174Z","shell.execute_reply":"2021-05-25T10:28:44.933397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_gen)","metadata":{"execution":{"iopub.status.busy":"2021-05-25T10:28:45.859282Z","iopub.execute_input":"2021-05-25T10:28:45.859605Z","iopub.status.idle":"2021-05-25T10:28:51.318044Z","shell.execute_reply.started":"2021-05-25T10:28:45.859577Z","shell.execute_reply":"2021-05-25T10:28:51.317274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nIn the first epoch, the accuracy increased noticeably (approximately 13%). If model learn iteratively, we can expect the difference in performance to become larger.\n\nIn this kernel, I made the simplest model with minimal coding. And now, Try to create model with better performance than this! with more complex models and more effective data!","metadata":{}}]}