{"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":"# Imports","metadata":{}},{"cell_type":"code","source":"# import pandas as pd\n# import warnings\n# warnings.filterwarnings(\"ignore\")\n# from glob import glob\n# from tqdm.notebook import tqdm\n# import matplotlib\n# matplotlib.rcParams.update({'font.size': 22})\n# from sklearn.metrics import accuracy_score\n# from tensorflow.keras import layers\n# from tensorflow.keras.applications import ResNet50, DenseNet121, Xception\n# from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, GlobalAveragePooling2D\n# from tensorflow.keras.optimizers import Adam\n# from tensorflow.keras import models\n# from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping\n# import tensorflow.keras.backend as K\n# from tensorflow.math import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:50.260052Z","iopub.execute_input":"2021-07-10T20:09:50.260439Z","iopub.status.idle":"2021-07-10T20:09:50.266513Z","shell.execute_reply.started":"2021-07-10T20:09:50.260355Z","shell.execute_reply":"2021-07-10T20:09:50.265573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### this work is inspired by:\n\nhttps://www.kaggle.com/rbhambri/covid-detection-studies-eda-viz\n\nhttps://www.kaggle.com/rbhambri/chexnet-transfer-learning-binary-image-clf\n    \nhttps://www.kaggle.com/sinamhd9/classification-model\n\nhttps://www.kaggle.com/rbhambri/chest-x-ray-abnormalities-bams-keras-pipeline\n\nhttps://www.kaggle.com/rbhambri/densenet-weights-nih-coursera-ai4m\n\nhttps://www.coursera.org/specializations/ai-for-medicine\n\nhttps://www.kaggle.com/rbhambri/chest-x-ray-abnormalities-densenet-pipeline\n\nhttps://www.kaggle.com/c/siim-covid19-detection/discussion/242606\n\nhttps://www.kaggle.com/saeedniksaz/transferlearning-with-vgg16\n\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data manipulations","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:50.514871Z","iopub.execute_input":"2021-07-10T20:09:50.515141Z","iopub.status.idle":"2021-07-10T20:09:50.523627Z","shell.execute_reply.started":"2021-07-10T20:09:50.515114Z","shell.execute_reply":"2021-07-10T20:09:50.522791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')\ndisplay(image_df.head(3))\nprint(image_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:50.721396Z","iopub.execute_input":"2021-07-10T20:09:50.721686Z","iopub.status.idle":"2021-07-10T20:09:50.793893Z","shell.execute_reply.started":"2021-07-10T20:09:50.721659Z","shell.execute_reply":"2021-07-10T20:09:50.792997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df = pd.read_csv('../input/siim-covid19-detection/train_study_level.csv')\ndisplay(study_df.head(3))\nprint(study_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:50.957643Z","iopub.execute_input":"2021-07-10T20:09:50.957983Z","iopub.status.idle":"2021-07-10T20:09:50.982892Z","shell.execute_reply.started":"2021-07-10T20:09:50.957953Z","shell.execute_reply":"2021-07-10T20:09:50.982102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_sampleSub = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\n# display(df_sampleSub.head(3))\n# print(df_sampleSub.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:51.130362Z","iopub.execute_input":"2021-07-10T20:09:51.130679Z","iopub.status.idle":"2021-07-10T20:09:51.134246Z","shell.execute_reply.started":"2021-07-10T20:09:51.130647Z","shell.execute_reply":"2021-07-10T20:09:51.133217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df['id'] = study_df['id'].str.replace('_study',\"\")\nstudy_df['StudyInstanceUID'] = study_df['id']\nstudy_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:51.634708Z","iopub.execute_input":"2021-07-10T20:09:51.635070Z","iopub.status.idle":"2021-07-10T20:09:51.654175Z","shell.execute_reply.started":"2021-07-10T20:09:51.635039Z","shell.execute_reply":"2021-07-10T20:09:51.653042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### for simplicity keeping studies with only 1 img per study - right now","metadata":{}},{"cell_type":"code","source":"def get_absolute_file_paths(directory):\n    all_abs_file_paths = []\n    for dirpath,_,filenames in os.walk(directory):\n        for f in filenames:\n            all_abs_file_paths.append(os.path.abspath(os.path.join(dirpath, f)))\n    return all_abs_file_paths","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:51.735100Z","iopub.execute_input":"2021-07-10T20:09:51.735367Z","iopub.status.idle":"2021-07-10T20:09:51.740542Z","shell.execute_reply.started":"2021-07-10T20:09:51.735341Z","shell.execute_reply":"2021-07-10T20:09:51.739626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm; tqdm.pandas();\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:51.996224Z","iopub.execute_input":"2021-07-10T20:09:51.996524Z","iopub.status.idle":"2021-07-10T20:09:52.003043Z","shell.execute_reply.started":"2021-07-10T20:09:51.996494Z","shell.execute_reply":"2021-07-10T20:09:52.002109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:52.211567Z","iopub.execute_input":"2021-07-10T20:09:52.211910Z","iopub.status.idle":"2021-07-10T20:09:52.216161Z","shell.execute_reply.started":"2021-07-10T20:09:52.211881Z","shell.execute_reply":"2021-07-10T20:09:52.214902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df[\"study_dir\"] = \"/kaggle/input/siim-covid19-detection/train/\"+study_df[\"id\"]\nstudy_df[\"images_per_study\"] = study_df.study_dir.progress_apply(lambda x: len(get_absolute_file_paths(x)))","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:09:52.460136Z","iopub.execute_input":"2021-07-10T20:09:52.460434Z","iopub.status.idle":"2021-07-10T20:10:14.267808Z","shell.execute_reply.started":"2021-07-10T20:09:52.460405Z","shell.execute_reply":"2021-07-10T20:10:14.267026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.269192Z","iopub.execute_input":"2021-07-10T20:10:14.269546Z","iopub.status.idle":"2021-07-10T20:10:14.281674Z","shell.execute_reply.started":"2021-07-10T20:10:14.269509Z","shell.execute_reply":"2021-07-10T20:10:14.280574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df.images_per_study.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.283540Z","iopub.execute_input":"2021-07-10T20:10:14.284201Z","iopub.status.idle":"2021-07-10T20:10:14.294493Z","shell.execute_reply.started":"2021-07-10T20:10:14.284163Z","shell.execute_reply":"2021-07-10T20:10:14.293440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_studies = study_df[study_df.images_per_study == 1]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.296527Z","iopub.execute_input":"2021-07-10T20:10:14.297104Z","iopub.status.idle":"2021-07-10T20:10:14.303384Z","shell.execute_reply.started":"2021-07-10T20:10:14.297068Z","shell.execute_reply":"2021-07-10T20:10:14.302441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_studies.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.304735Z","iopub.execute_input":"2021-07-10T20:10:14.305230Z","iopub.status.idle":"2021-07-10T20:10:14.319525Z","shell.execute_reply.started":"2021-07-10T20:10:14.305192Z","shell.execute_reply":"2021-07-10T20:10:14.318678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_studies.images_per_study.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.320577Z","iopub.execute_input":"2021-07-10T20:10:14.320952Z","iopub.status.idle":"2021-07-10T20:10:14.328029Z","shell.execute_reply.started":"2021-07-10T20:10:14.320918Z","shell.execute_reply":"2021-07-10T20:10:14.327230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_studies_ids = list(unique_studies.id)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.329222Z","iopub.execute_input":"2021-07-10T20:10:14.329804Z","iopub.status.idle":"2021-07-10T20:10:14.336727Z","shell.execute_reply.started":"2021-07-10T20:10:14.329750Z","shell.execute_reply":"2021-07-10T20:10:14.335891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_studies_ids[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.339627Z","iopub.execute_input":"2021-07-10T20:10:14.340163Z","iopub.status.idle":"2021-07-10T20:10:14.350321Z","shell.execute_reply.started":"2021-07-10T20:10:14.340029Z","shell.execute_reply":"2021-07-10T20:10:14.349369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(unique_studies_ids)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.352464Z","iopub.execute_input":"2021-07-10T20:10:14.352990Z","iopub.status.idle":"2021-07-10T20:10:14.360221Z","shell.execute_reply.started":"2021-07-10T20:10:14.352955Z","shell.execute_reply":"2021-07-10T20:10:14.359230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### only keep images for these studies","metadata":{}},{"cell_type":"code","source":"image_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.361941Z","iopub.execute_input":"2021-07-10T20:10:14.362340Z","iopub.status.idle":"2021-07-10T20:10:14.375815Z","shell.execute_reply.started":"2021-07-10T20:10:14.362305Z","shell.execute_reply":"2021-07-10T20:10:14.375138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df_unique = image_df[image_df.StudyInstanceUID.isin(unique_studies_ids)]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.376635Z","iopub.execute_input":"2021-07-10T20:10:14.376891Z","iopub.status.idle":"2021-07-10T20:10:14.385705Z","shell.execute_reply.started":"2021-07-10T20:10:14.376869Z","shell.execute_reply":"2021-07-10T20:10:14.384900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df_unique.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.387076Z","iopub.execute_input":"2021-07-10T20:10:14.387561Z","iopub.status.idle":"2021-07-10T20:10:14.392599Z","shell.execute_reply.started":"2021-07-10T20:10:14.387526Z","shell.execute_reply":"2021-07-10T20:10:14.391653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.393946Z","iopub.execute_input":"2021-07-10T20:10:14.394614Z","iopub.status.idle":"2021-07-10T20:10:14.401416Z","shell.execute_reply.started":"2021-07-10T20:10:14.394533Z","shell.execute_reply":"2021-07-10T20:10:14.400496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### now merge","metadata":{}},{"cell_type":"code","source":"df_train = image_df_unique.merge(study_df, \n                                 on='StudyInstanceUID',\n                                 suffixes=('_image', '_study'))\ndf_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.402690Z","iopub.execute_input":"2021-07-10T20:10:14.403650Z","iopub.status.idle":"2021-07-10T20:10:14.428463Z","shell.execute_reply.started":"2021-07-10T20:10:14.403587Z","shell.execute_reply":"2021-07-10T20:10:14.427567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.429709Z","iopub.execute_input":"2021-07-10T20:10:14.430170Z","iopub.status.idle":"2021-07-10T20:10:14.435436Z","shell.execute_reply.started":"2021-07-10T20:10:14.430134Z","shell.execute_reply":"2021-07-10T20:10:14.434609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dir_jpg = '../input/covid-jpg-512/train'\n# train_dir_origin ='../input/siim-covid19-detection/train'\n# paths_original = []\n# paths_jpg = []\n# for _, row in tqdm(df_train.iterrows()):\n#     image_id = row['id'].split('_')[0]\n#     study_id = row['StudyInstanceUID']\n#     image_path_jpg = glob(f'{train_dir_jpg}/{image_id}.jpg')\n#     image_path_original = glob(f'{train_dir_origin}/{study_id}/*/{image_id}.dcm')\n#     paths_jpg.append(image_path_jpg)\n#     paths_original.append(image_path_original)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.436687Z","iopub.execute_input":"2021-07-10T20:10:14.437350Z","iopub.status.idle":"2021-07-10T20:10:14.443252Z","shell.execute_reply.started":"2021-07-10T20:10:14.437217Z","shell.execute_reply":"2021-07-10T20:10:14.442401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train['path'] = paths_jpg\n# df_train['origin'] = paths_original\n# df_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.444653Z","iopub.execute_input":"2021-07-10T20:10:14.445106Z","iopub.status.idle":"2021-07-10T20:10:14.451270Z","shell.execute_reply.started":"2021-07-10T20:10:14.445067Z","shell.execute_reply":"2021-07-10T20:10:14.450477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[df_train['Negative for Pneumonia']==1, 'study_label'] = 'negative'\ndf_train.loc[df_train['Typical Appearance']==1, 'study_label'] = 'typical'\ndf_train.loc[df_train['Indeterminate Appearance']==1, 'study_label'] = 'indeterminate'\ndf_train.loc[df_train['Atypical Appearance']==1, 'study_label'] = 'atypical'\ndf_train.drop(['Negative for Pneumonia','Typical Appearance', 'Indeterminate Appearance', 'Atypical Appearance'], axis=1, inplace=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.452567Z","iopub.execute_input":"2021-07-10T20:10:14.453223Z","iopub.status.idle":"2021-07-10T20:10:14.468361Z","shell.execute_reply.started":"2021-07-10T20:10:14.453186Z","shell.execute_reply":"2021-07-10T20:10:14.467633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['id_image'] = df_train['id_image'].str.replace('_image', '.jpg')\ndf_train['image_label'] = df_train['label'].str.split().apply(lambda x : x[0])\ndf_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.470868Z","iopub.execute_input":"2021-07-10T20:10:14.471096Z","iopub.status.idle":"2021-07-10T20:10:14.503530Z","shell.execute_reply.started":"2021-07-10T20:10:14.471074Z","shell.execute_reply":"2021-07-10T20:10:14.502835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_size = pd.read_csv('../input/covid-jpg-512/size.csv')\ndf_size['id_image'] = df_size['id']\ndf_size.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.504654Z","iopub.execute_input":"2021-07-10T20:10:14.504986Z","iopub.status.idle":"2021-07-10T20:10:14.544337Z","shell.execute_reply.started":"2021-07-10T20:10:14.504954Z","shell.execute_reply":"2021-07-10T20:10:14.543599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.merge(df_size, on='id_image')\ndf_train.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.545392Z","iopub.execute_input":"2021-07-10T20:10:14.545693Z","iopub.status.idle":"2021-07-10T20:10:14.573516Z","shell.execute_reply.started":"2021-07-10T20:10:14.545659Z","shell.execute_reply":"2021-07-10T20:10:14.572694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.574630Z","iopub.execute_input":"2021-07-10T20:10:14.574969Z","iopub.status.idle":"2021-07-10T20:10:14.582565Z","shell.execute_reply.started":"2021-07-10T20:10:14.574935Z","shell.execute_reply":"2021-07-10T20:10:14.581498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.study_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.587681Z","iopub.execute_input":"2021-07-10T20:10:14.587948Z","iopub.status.idle":"2021-07-10T20:10:14.596270Z","shell.execute_reply.started":"2021-07-10T20:10:14.587924Z","shell.execute_reply":"2021-07-10T20:10:14.595196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.image_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.599519Z","iopub.execute_input":"2021-07-10T20:10:14.599893Z","iopub.status.idle":"2021-07-10T20:10:14.609309Z","shell.execute_reply.started":"2021-07-10T20:10:14.599857Z","shell.execute_reply":"2021-07-10T20:10:14.608397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.id_image.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.610455Z","iopub.execute_input":"2021-07-10T20:10:14.610851Z","iopub.status.idle":"2021-07-10T20:10:14.616621Z","shell.execute_reply.started":"2021-07-10T20:10:14.610811Z","shell.execute_reply":"2021-07-10T20:10:14.615860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.id_study.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.617977Z","iopub.execute_input":"2021-07-10T20:10:14.618381Z","iopub.status.idle":"2021-07-10T20:10:14.624648Z","shell.execute_reply.started":"2021-07-10T20:10:14.618346Z","shell.execute_reply":"2021-07-10T20:10:14.623916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.625946Z","iopub.execute_input":"2021-07-10T20:10:14.626392Z","iopub.status.idle":"2021-07-10T20:10:14.633648Z","shell.execute_reply.started":"2021-07-10T20:10:14.626355Z","shell.execute_reply":"2021-07-10T20:10:14.632849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### data is imbalanced.\n\noversample the minority class.","metadata":{}},{"cell_type":"code","source":"df_train_atypical = df_train[df_train.study_label == 'atypical']","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.634932Z","iopub.execute_input":"2021-07-10T20:10:14.635307Z","iopub.status.idle":"2021-07-10T20:10:14.645631Z","shell.execute_reply.started":"2021-07-10T20:10:14.635258Z","shell.execute_reply":"2021-07-10T20:10:14.644874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_atypical.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.647374Z","iopub.execute_input":"2021-07-10T20:10:14.647930Z","iopub.status.idle":"2021-07-10T20:10:14.654092Z","shell.execute_reply.started":"2021-07-10T20:10:14.647765Z","shell.execute_reply":"2021-07-10T20:10:14.653069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_augmented_list = [df_train, df_train_atypical]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.655611Z","iopub.execute_input":"2021-07-10T20:10:14.656133Z","iopub.status.idle":"2021-07-10T20:10:14.661017Z","shell.execute_reply.started":"2021-07-10T20:10:14.656094Z","shell.execute_reply":"2021-07-10T20:10:14.660006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_augmented = pd.concat(df_train_augmented_list, ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.662445Z","iopub.execute_input":"2021-07-10T20:10:14.662972Z","iopub.status.idle":"2021-07-10T20:10:14.673838Z","shell.execute_reply.started":"2021-07-10T20:10:14.662934Z","shell.execute_reply":"2021-07-10T20:10:14.673039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_augmented.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.675222Z","iopub.execute_input":"2021-07-10T20:10:14.675543Z","iopub.status.idle":"2021-07-10T20:10:14.682498Z","shell.execute_reply.started":"2021-07-10T20:10:14.675510Z","shell.execute_reply":"2021-07-10T20:10:14.681606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_augmented.study_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.683976Z","iopub.execute_input":"2021-07-10T20:10:14.684390Z","iopub.status.idle":"2021-07-10T20:10:14.693389Z","shell.execute_reply.started":"2021-07-10T20:10:14.684345Z","shell.execute_reply":"2021-07-10T20:10:14.692448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_augmented.image_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.694719Z","iopub.execute_input":"2021-07-10T20:10:14.695093Z","iopub.status.idle":"2021-07-10T20:10:14.705147Z","shell.execute_reply.started":"2021-07-10T20:10:14.695058Z","shell.execute_reply":"2021-07-10T20:10:14.704087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### now use this df for further processing","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom ast import literal_eval\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.706685Z","iopub.execute_input":"2021-07-10T20:10:14.707129Z","iopub.status.idle":"2021-07-10T20:10:14.873297Z","shell.execute_reply.started":"2021-07-10T20:10:14.707096Z","shell.execute_reply":"2021-07-10T20:10:14.872492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib.patches import Rectangle\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.875200Z","iopub.execute_input":"2021-07-10T20:10:14.875439Z","iopub.status.idle":"2021-07-10T20:10:14.882252Z","shell.execute_reply.started":"2021-07-10T20:10:14.875414Z","shell.execute_reply":"2021-07-10T20:10:14.881474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['id'][0]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.885552Z","iopub.execute_input":"2021-07-10T20:10:14.885806Z","iopub.status.idle":"2021-07-10T20:10:14.894251Z","shell.execute_reply.started":"2021-07-10T20:10:14.885783Z","shell.execute_reply":"2021-07-10T20:10:14.893485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! ls ../input/siimcovid19-1024-jpg-image-dataset/train","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.895653Z","iopub.execute_input":"2021-07-10T20:10:14.896012Z","iopub.status.idle":"2021-07-10T20:10:14.900098Z","shell.execute_reply.started":"2021-07-10T20:10:14.895978Z","shell.execute_reply":"2021-07-10T20:10:14.899114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 20\ntrain_dir = '../input/covid-jpg-512/train'\n# train_dir = '../input/siimcovid19-1024-jpg-image-dataset/train'\n\n\nfig, axs = plt.subplots(4, 5, figsize=(20,20))\nfig.subplots_adjust(hspace=.2, wspace=.2)\naxs = axs.ravel()\nfor i in range(n):\n#     print('----------')\n    image_id = df_train['id'][i]\n#     print('ix, image_id=', i, image_id)\n\n    img = cv2.imread(os.path.join(train_dir, image_id))\n    axs[i].imshow(img)\n    \n    study_label = df_train['study_label'][i]\n    image_label = df_train['image_label'][i]\n#     print('study_label=', study_label)\n#     print('image_label=', image_label)\n    \n    if type(df_train['boxes'][i])==str:\n#         print('box seen for i=', i)\n        boxes = literal_eval(df_train['boxes'][i])\n        \n        for box in boxes:\n#             print('box=', box)\n            axs[i].add_patch(Rectangle((box['x']*(512/df_train['dim1'][i]), box['y']*(512/df_train['dim0'][i])), box['width']*(512/df_train['dim1'][i]), box['height']*(512/df_train['dim0'][i]), fill=0, color='y', linewidth=2))\n        axs[i].set_title(str(i) + ',' + study_label + ',' + image_label)\n            \n    else:\n#         print('box NOT seen for i=', i)\n        axs[i].set_title(str(i) + ',' + study_label + ',' + image_label)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:14.901738Z","iopub.execute_input":"2021-07-10T20:10:14.902475Z","iopub.status.idle":"2021-07-10T20:10:17.982755Z","shell.execute_reply.started":"2021-07-10T20:10:14.902436Z","shell.execute_reply":"2021-07-10T20:10:17.982013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PreProcessing","metadata":{}},{"cell_type":"code","source":"from skimage import exposure\nimport numpy as np\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:17.983747Z","iopub.execute_input":"2021-07-10T20:10:17.984103Z","iopub.status.idle":"2021-07-10T20:10:18.379227Z","shell.execute_reply.started":"2021-07-10T20:10:17.984068Z","shell.execute_reply":"2021-07-10T20:10:18.378255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def preprocess_image(img):\n#     equ_img = exposure.equalize_hist(img)\n#     return equ_img\n\n# im= cv2.imread('../input/covid-jpg-512/train/007cf31356c6.jpg')\n# im2 = preprocess_image(im)\n# res = np.concatenate((im/255, im2), axis=1)\n# plt.imshow(res)\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:18.383493Z","iopub.execute_input":"2021-07-10T20:10:18.383741Z","iopub.status.idle":"2021-07-10T20:10:18.389360Z","shell.execute_reply.started":"2021-07-10T20:10:18.383716Z","shell.execute_reply":"2021-07-10T20:10:18.388454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:18.392599Z","iopub.execute_input":"2021-07-10T20:10:18.392930Z","iopub.status.idle":"2021-07-10T20:10:18.404847Z","shell.execute_reply.started":"2021-07-10T20:10:18.392901Z","shell.execute_reply":"2021-07-10T20:10:18.403938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ImageGenerators and Augmentations","metadata":{}},{"cell_type":"code","source":"# img_size = 1024\nimg_size = 512\n# img_size = 299\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:18.406248Z","iopub.execute_input":"2021-07-10T20:10:18.406984Z","iopub.status.idle":"2021-07-10T20:10:18.411572Z","shell.execute_reply.started":"2021-07-10T20:10:18.406913Z","shell.execute_reply":"2021-07-10T20:10:18.410702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# batch_size = 32\nbatch_size = 16\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:18.413052Z","iopub.execute_input":"2021-07-10T20:10:18.413443Z","iopub.status.idle":"2021-07-10T20:10:18.419581Z","shell.execute_reply.started":"2021-07-10T20:10:18.413408Z","shell.execute_reply":"2021-07-10T20:10:18.418804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:18.420722Z","iopub.execute_input":"2021-07-10T20:10:18.421145Z","iopub.status.idle":"2021-07-10T20:10:23.222749Z","shell.execute_reply.started":"2021-07-10T20:10:18.421106Z","shell.execute_reply":"2021-07-10T20:10:23.221895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_generator = ImageDataGenerator(\n#         rescale = 1./255,\n        validation_split=0.25,\n        rotation_range=5,\n#         width_shift_range=0.1,\n#         height_shift_range=0.1,\n#         shear_range=0.1,\n        zoom_range=0.1,\n        horizontal_flip=True,\n        fill_mode='nearest',\n        brightness_range = [0.8, 1.1],\n)\n\nimage_generator_valid = ImageDataGenerator(validation_split=0.25,\n#                                            rescale = 1./255,  \n                                          )\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:23.224176Z","iopub.execute_input":"2021-07-10T20:10:23.224515Z","iopub.status.idle":"2021-07-10T20:10:23.231643Z","shell.execute_reply.started":"2021-07-10T20:10:23.224478Z","shell.execute_reply":"2021-07-10T20:10:23.230417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train_augmented","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:23.233181Z","iopub.execute_input":"2021-07-10T20:10:23.233558Z","iopub.status.idle":"2021-07-10T20:10:23.241574Z","shell.execute_reply.started":"2021-07-10T20:10:23.233521Z","shell.execute_reply":"2021-07-10T20:10:23.240680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:23.242925Z","iopub.execute_input":"2021-07-10T20:10:23.243292Z","iopub.status.idle":"2021-07-10T20:10:23.265204Z","shell.execute_reply.started":"2021-07-10T20:10:23.243253Z","shell.execute_reply":"2021-07-10T20:10:23.264135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:23.266647Z","iopub.execute_input":"2021-07-10T20:10:23.267092Z","iopub.status.idle":"2021-07-10T20:10:23.274572Z","shell.execute_reply.started":"2021-07-10T20:10:23.267053Z","shell.execute_reply":"2021-07-10T20:10:23.273421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = image_generator.flow_from_dataframe(\n        dataframe = df_train,\n        directory = train_dir,\n        x_col = 'id',\n        y_col =  'study_label',  \n        target_size=(img_size, img_size),\n        batch_size=batch_size,\n#         class_mode='binary',\n        subset='training', \n        seed = 23) \n\nvalid_generator = image_generator_valid.flow_from_dataframe(\n    dataframe = df_train,\n    directory = train_dir,\n    x_col = 'id',\n    y_col = 'study_label',\n    target_size=(img_size, img_size),\n    batch_size=batch_size,\n#     class_mode='binary',\n    subset='validation', \n    shuffle=False, \n    seed=23) \n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:23.276401Z","iopub.execute_input":"2021-07-10T20:10:23.276843Z","iopub.status.idle":"2021-07-10T20:10:38.643279Z","shell.execute_reply.started":"2021-07-10T20:10:23.276806Z","shell.execute_reply":"2021-07-10T20:10:38.641658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for j in range(2):\n#     aug_images = [train_generator[0][0][j] for i in range(5)]\n#     fig, axes = plt.subplots(1, 5, figsize=(24,24))\n#     axes = axes.flatten()\n#     for img, ax in zip(aug_images, axes):\n#         ax.imshow(img)\n#         ax.axis('off')\n# plt.tight_layout()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.644593Z","iopub.execute_input":"2021-07-10T20:10:38.644985Z","iopub.status.idle":"2021-07-10T20:10:38.649277Z","shell.execute_reply.started":"2021-07-10T20:10:38.644947Z","shell.execute_reply":"2021-07-10T20:10:38.648145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for j in range(2):\n#     aug_images = [valid_generator[0][0][j] for i in range(5)]\n#     fig, axes = plt.subplots(1, 5, figsize=(24,24))\n#     axes = axes.flatten()\n#     for img, ax in zip(aug_images, axes):\n#         ax.imshow(img)\n#         ax.axis('off')\n# plt.tight_layout()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.650715Z","iopub.execute_input":"2021-07-10T20:10:38.651075Z","iopub.status.idle":"2021-07-10T20:10:38.659705Z","shell.execute_reply.started":"2021-07-10T20:10:38.651041Z","shell.execute_reply":"2021-07-10T20:10:38.658847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Architecture","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras.optimizers import Adam, RMSprop\nfrom tensorflow.keras.utils import plot_model\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.660874Z","iopub.execute_input":"2021-07-10T20:10:38.661378Z","iopub.status.idle":"2021-07-10T20:10:38.672566Z","shell.execute_reply.started":"2021-07-10T20:10:38.661343Z","shell.execute_reply":"2021-07-10T20:10:38.671866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model, Sequential\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.673946Z","iopub.execute_input":"2021-07-10T20:10:38.674322Z","iopub.status.idle":"2021-07-10T20:10:38.681716Z","shell.execute_reply.started":"2021-07-10T20:10:38.674288Z","shell.execute_reply":"2021-07-10T20:10:38.680933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout\nfrom tensorflow.keras.layers import GlobalAveragePooling2D,  BatchNormalization, Activation\n# from tensorflow.keras import models\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.683140Z","iopub.execute_input":"2021-07-10T20:10:38.683818Z","iopub.status.idle":"2021-07-10T20:10:38.690722Z","shell.execute_reply.started":"2021-07-10T20:10:38.683707Z","shell.execute_reply":"2021-07-10T20:10:38.689842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.691848Z","iopub.execute_input":"2021-07-10T20:10:38.692195Z","iopub.status.idle":"2021-07-10T20:10:38.699423Z","shell.execute_reply.started":"2021-07-10T20:10:38.692159Z","shell.execute_reply":"2021-07-10T20:10:38.698616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.700625Z","iopub.execute_input":"2021-07-10T20:10:38.701066Z","iopub.status.idle":"2021-07-10T20:10:38.711108Z","shell.execute_reply.started":"2021-07-10T20:10:38.701023Z","shell.execute_reply":"2021-07-10T20:10:38.710318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import  VGG16\nfrom tensorflow.keras.applications import DenseNet121\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.712654Z","iopub.execute_input":"2021-07-10T20:10:38.713026Z","iopub.status.idle":"2021-07-10T20:10:38.718671Z","shell.execute_reply.started":"2021-07-10T20:10:38.712992Z","shell.execute_reply":"2021-07-10T20:10:38.717476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_chextnet_model_v1():\n    \"\"\"\n    v1 - uses densenet + chextnet weights\n    \"\"\"\n    # load model design\n    pre_model = DenseNet121(weights=None,\n                        include_top=False,\n                        input_shape=(img_size,img_size,3)\n                        )\n    out = Dense(14, activation='sigmoid')(pre_model.output)\n    pre_model = Model(inputs=pre_model.input, outputs=out) \n    \n    # load model wieghts\n    chex_weights_path = '../input/chexnet-weights/brucechou1983_CheXNet_Keras_0.3.0_weights.h5'\n    pre_model.load_weights(chex_weights_path)\n\n    # make layers trainable?\n    # pre_model.trainable = False\n    pre_model.trainable = True\n\n    # get summary/print\n    pre_model.summary()\n    \n    \n    # add future layers.\n    # last_layer = pre_model.get_layer('conv5_block16_concat')\n    last_layer = pre_model.layers[-2]\n\n    print('last layer output shape: ', last_layer.output_shape)\n    last_output = last_layer.output\n#     last_layer\n\n    # Flatten the output layer to 1 dimension\n    # x = Flatten()(last_output)\n    x = GlobalAveragePooling2D()(last_output)\n\n    # Add a fully connected layer with 512 hidden units and ReLU activation\n    # x = Dense(512, activation='relu')(x)\n    # Add a dropout rate of 0.2\n    # x = Dropout(0.2)(x)                  \n\n\n    # # Add a fully connected layer with 128 hidden units and ReLU activation\n    # x = Dense(128, activation='relu')(x)\n\n\n    # Add final classification layer\n    x = Dense(4, activation='softmax')(x)\n\n    # final model\n    model = Model( pre_model.input, x) \n\n    # model.summary()\n    # plot_model(model)\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.720084Z","iopub.execute_input":"2021-07-10T20:10:38.720482Z","iopub.status.idle":"2021-07-10T20:10:38.728993Z","shell.execute_reply.started":"2021-07-10T20:10:38.720426Z","shell.execute_reply":"2021-07-10T20:10:38.727863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_vanilla_cnn_model_v1():\n    in1 = tf.keras.layers.Input(shape=(img_size, img_size, 3))\n    \n#     out1 = tf.keras.layers.Conv2D(4,(3,3),activation=\"relu\")(in1)\n    out1 = tf.keras.layers.Conv2D(32,(3,3),\n                                  activation=\"relu\",\n                                  padding='same')(in1)\n    out1 = tf.keras.layers.MaxPooling2D((2,2))(out1)\n    \n    out1 = tf.keras.layers.Conv2D(32,(3,3),\n                                  activation=\"relu\",\n                                  padding='same')(out1)\n    out1 = tf.keras.layers.MaxPooling2D((2,2))(out1)\n\n    out1 = tf.keras.layers.Flatten()(out1)\n    \n    out2 = tf.keras.layers.Dense(30,activation=\"relu\")(out1)\n    out2 = tf.keras.layers.Dense(30,activation=\"relu\")(out2)\n    \n    \n    out2 = Dense(4, \n                 activation='softmax',\n                 name='class_out',\n                 kernel_regularizer=regularizers.l2(0.01))(out2)\n\n\n    model = tf.keras.Model(inputs=in1,\n                           outputs=out2)\n\n    model.summary()\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.732455Z","iopub.execute_input":"2021-07-10T20:10:38.732693Z","iopub.status.idle":"2021-07-10T20:10:38.741888Z","shell.execute_reply.started":"2021-07-10T20:10:38.732670Z","shell.execute_reply":"2021-07-10T20:10:38.741019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_densenet_coursera_model_v1():\n    \"\"\"\n    v1 - uses densenet + coursera model\n    https://www.kaggle.com/rbhambri/densenet-weights-nih-coursera-ai4m\n    \"\"\"\n    # load model design\n    # also load model wieghts\n    nih_weights_path = '../input/densenet-weights-nih-coursera-ai4m/densenet.hdf5'\n    base_model = DenseNet121(weights=nih_weights_path, \n                             input_shape=(img_size,img_size,3),\n                             include_top=False)\n\n    # get summary/print\n#     base_model.summary()\n\n    last_output = base_model.output\n#     last_layer\n\n    # Flatten the output layer to 1 dimension\n    # x = Flatten()(last_output)\n    x = GlobalAveragePooling2D()(last_output)\n\n    # Add final classification layer\n    x = Dense(4, activation='softmax')(x)\n\n    # final model\n    model = Model(base_model.input, x) \n\n    model.summary()\n    plot_model(model)\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.743353Z","iopub.execute_input":"2021-07-10T20:10:38.743831Z","iopub.status.idle":"2021-07-10T20:10:38.753044Z","shell.execute_reply.started":"2021-07-10T20:10:38.743764Z","shell.execute_reply":"2021-07-10T20:10:38.752201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_vgg16_model_v1():\n    \"\"\"\n    v1 - https://www.kaggle.com/saeedniksaz/transferlearning-with-vgg16\n    \"\"\"\n    base_model = VGG16(input_shape=(img_size,img_size,3), \n                         include_top=False,\n                         weights=\"imagenet\")\n    # get summary/print\n#     base_model.summary()\n\n    for layer in base_model.layers:\n        layer.trainable = False\n        \n    model = Sequential()\n    model.add(base_model)\n    model.add(Dropout(0.5))\n    model.add(Flatten())\n    model.add(BatchNormalization())\n    \n    model.add(Dense(256,kernel_initializer='he_uniform'))\n    model.add(BatchNormalization())\n    model.add(Activation('relu'))\n    model.add(Dropout(0.5))\n    \n    model.add(Dense(32,kernel_initializer='he_uniform'))\n    model.add(BatchNormalization())\n    model.add(Activation('relu'))\n    model.add(Dropout(0.5))\n\n    model.add(Dense(4,activation='softmax'))\n        \n    model.summary()\n    plot_model(model)\n\n    return model\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.754036Z","iopub.execute_input":"2021-07-10T20:10:38.754549Z","iopub.status.idle":"2021-07-10T20:10:38.763944Z","shell.execute_reply.started":"2021-07-10T20:10:38.754511Z","shell.execute_reply":"2021-07-10T20:10:38.763047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = build_vanilla_cnn_model_v1()\nmodel = build_chextnet_model_v1()\n# model = build_densenet_coursera_model_v1()\n# model = build_vgg16_model_v1()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:38.765152Z","iopub.execute_input":"2021-07-10T20:10:38.765800Z","iopub.status.idle":"2021-07-10T20:10:44.549673Z","shell.execute_reply.started":"2021-07-10T20:10:38.765737Z","shell.execute_reply":"2021-07-10T20:10:44.548869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# metrics = ['categorical_accuracy', 'accuracy']\n# metrics = [tf.keras.metrics.AUC(), 'accuracy']\nmetrics = [ 'accuracy', tf.keras.metrics.AUC()]\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.551034Z","iopub.execute_input":"2021-07-10T20:10:44.551552Z","iopub.status.idle":"2021-07-10T20:10:44.563520Z","shell.execute_reply.started":"2021-07-10T20:10:44.551511Z","shell.execute_reply":"2021-07-10T20:10:44.562652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(Adam(lr=1e-3),\n              loss='categorical_crossentropy',\n              metrics=metrics)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.564997Z","iopub.execute_input":"2021-07-10T20:10:44.565393Z","iopub.status.idle":"2021-07-10T20:10:44.588141Z","shell.execute_reply.started":"2021-07-10T20:10:44.565338Z","shell.execute_reply":"2021-07-10T20:10:44.587217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.589329Z","iopub.execute_input":"2021-07-10T20:10:44.589653Z","iopub.status.idle":"2021-07-10T20:10:44.597227Z","shell.execute_reply.started":"2021-07-10T20:10:44.589620Z","shell.execute_reply":"2021-07-10T20:10:44.596092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rlr = ReduceLROnPlateau(monitor = 'val_accuracy', \n#                         factor = 0.2, \n#                         patience = 2, \n#                         verbose = 1, \n#                         min_delta = 1e-4, \n#                         min_lr = 1e-4, \n#                         mode = 'max')\n\nrlr = ReduceLROnPlateau(monitor = 'val_loss', \n                        factor = 0.1, \n                        patience = 2, \n                        verbose = 1, \n                        min_delta = 1e-4, \n                        min_lr = 1e-6, \n                        mode = 'min')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.598400Z","iopub.execute_input":"2021-07-10T20:10:44.598790Z","iopub.status.idle":"2021-07-10T20:10:44.603851Z","shell.execute_reply.started":"2021-07-10T20:10:44.598742Z","shell.execute_reply":"2021-07-10T20:10:44.602904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# es = EarlyStopping(monitor = 'val_accuracy', \n#                    min_delta = 1e-4, \n#                    patience = 3, \n#                    mode = 'max', \n#                    restore_best_weights = True, \n#                    verbose = 1)\n\nes = EarlyStopping(monitor = 'val_loss', \n                   min_delta = 1e-4, \n                   patience = 3, \n                   mode = 'min', \n                   restore_best_weights = True, \n                   verbose = 1)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.610554Z","iopub.execute_input":"2021-07-10T20:10:44.611101Z","iopub.status.idle":"2021-07-10T20:10:44.615512Z","shell.execute_reply.started":"2021-07-10T20:10:44.611065Z","shell.execute_reply":"2021-07-10T20:10:44.614713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_name = 'vgg16_model_512_july11.h5'\nmodel_name = 'chextnet_model_512_july11.h5'\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.617630Z","iopub.execute_input":"2021-07-10T20:10:44.618027Z","iopub.status.idle":"2021-07-10T20:10:44.627804Z","shell.execute_reply.started":"2021-07-10T20:10:44.617990Z","shell.execute_reply":"2021-07-10T20:10:44.626953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ckp = ModelCheckpoint('model.h5',\n#                       monitor = 'val_accuracy',\n#                       verbose = 0, \n#                       save_best_only = True, \n#                       mode = 'max')\n\n\nckp = ModelCheckpoint(model_name,\n                      monitor = 'val_loss',\n                      verbose = 0, \n                      save_best_only = True, \n                      mode = 'min')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.630751Z","iopub.execute_input":"2021-07-10T20:10:44.631105Z","iopub.status.idle":"2021-07-10T20:10:44.637212Z","shell.execute_reply.started":"2021-07-10T20:10:44.631079Z","shell.execute_reply":"2021-07-10T20:10:44.636412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class_weight = {0: 2,1: 1, 2: 1,3: 1}","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.638542Z","iopub.execute_input":"2021-07-10T20:10:44.638968Z","iopub.status.idle":"2021-07-10T20:10:44.646161Z","shell.execute_reply.started":"2021-07-10T20:10:44.638932Z","shell.execute_reply":"2021-07-10T20:10:44.645356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\n# epochs = 5\n# epochs = 2\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.648519Z","iopub.execute_input":"2021-07-10T20:10:44.649058Z","iopub.status.idle":"2021-07-10T20:10:44.654761Z","shell.execute_reply.started":"2021-07-10T20:10:44.649022Z","shell.execute_reply":"2021-07-10T20:10:44.654009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n      train_generator,\n      validation_data=valid_generator,\n      epochs=epochs,\n      callbacks=[es, ckp, rlr],\n#     class_weight=class_weight\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:10:44.656844Z","iopub.execute_input":"2021-07-10T20:10:44.657079Z","iopub.status.idle":"2021-07-10T20:55:49.260227Z","shell.execute_reply.started":"2021-07-10T20:10:44.657057Z","shell.execute_reply":"2021-07-10T20:55:49.258122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# abc","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:56:04.756756Z","iopub.execute_input":"2021-07-10T20:56:04.757104Z","iopub.status.idle":"2021-07-10T20:56:04.761985Z","shell.execute_reply.started":"2021-07-10T20:56:04.757071Z","shell.execute_reply":"2021-07-10T20:56:04.760865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! ls ../input/image-clf-chexnet-vanilla-cnn\n! ls","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:57:02.347704Z","iopub.execute_input":"2021-07-10T20:57:02.348040Z","iopub.status.idle":"2021-07-10T20:57:03.030272Z","shell.execute_reply.started":"2021-07-10T20:57:02.348008Z","shell.execute_reply":"2021-07-10T20:57:03.029174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('../input/image-clf-chexnet-vanilla-cnn/model.h5')\n# model.load_weights('./chextnet_model_512_july11.h5')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:02:05.453918Z","iopub.execute_input":"2021-07-10T21:02:05.454259Z","iopub.status.idle":"2021-07-10T21:02:05.458512Z","shell.execute_reply.started":"2021-07-10T21:02:05.454229Z","shell.execute_reply":"2021-07-10T21:02:05.457217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### model perf","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend(loc=0)\nplt.figure()\n\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:57:41.274296Z","iopub.execute_input":"2021-07-10T20:57:41.274738Z","iopub.status.idle":"2021-07-10T20:57:41.332517Z","shell.execute_reply.started":"2021-07-10T20:57:41.274687Z","shell.execute_reply":"2021-07-10T20:57:41.329486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.math import confusion_matrix\nfrom sklearn.metrics import accuracy_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:57:45.466628Z","iopub.execute_input":"2021-07-10T20:57:45.466983Z","iopub.status.idle":"2021-07-10T20:57:45.992073Z","shell.execute_reply.started":"2021-07-10T20:57:45.466953Z","shell.execute_reply":"2021-07-10T20:57:45.991218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual =  valid_generator.labels\npreds = np.argmax(model.predict(valid_generator), axis=1)\ncfmx = confusion_matrix(actual, preds)\nacc = accuracy_score(actual, preds)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:57:45.993455Z","iopub.execute_input":"2021-07-10T20:57:45.993806Z","iopub.status.idle":"2021-07-10T20:58:01.131863Z","shell.execute_reply.started":"2021-07-10T20:57:45.993750Z","shell.execute_reply":"2021-07-10T20:58:01.130892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(valid_generator)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:01.136668Z","iopub.execute_input":"2021-07-10T20:58:01.138745Z","iopub.status.idle":"2021-07-10T20:58:17.201143Z","shell.execute_reply.started":"2021-07-10T20:58:01.138694Z","shell.execute_reply":"2021-07-10T20:58:17.200334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual[:25]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.202873Z","iopub.execute_input":"2021-07-10T20:58:17.203233Z","iopub.status.idle":"2021-07-10T20:58:17.212354Z","shell.execute_reply.started":"2021-07-10T20:58:17.203194Z","shell.execute_reply":"2021-07-10T20:58:17.211399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[:25]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.214443Z","iopub.execute_input":"2021-07-10T20:58:17.214713Z","iopub.status.idle":"2021-07-10T20:58:17.222535Z","shell.execute_reply.started":"2021-07-10T20:58:17.214688Z","shell.execute_reply":"2021-07-10T20:58:17.221641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfmx","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.223807Z","iopub.execute_input":"2021-07-10T20:58:17.224137Z","iopub.status.idle":"2021-07-10T20:58:17.232423Z","shell.execute_reply.started":"2021-07-10T20:58:17.224102Z","shell.execute_reply":"2021-07-10T20:58:17.231307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.234516Z","iopub.execute_input":"2021-07-10T20:58:17.235041Z","iopub.status.idle":"2021-07-10T20:58:17.241103Z","shell.execute_reply.started":"2021-07-10T20:58:17.235004Z","shell.execute_reply":"2021-07-10T20:58:17.240085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(actual, preds))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.242793Z","iopub.execute_input":"2021-07-10T20:58:17.243305Z","iopub.status.idle":"2021-07-10T20:58:17.257961Z","shell.execute_reply.started":"2021-07-10T20:58:17.243270Z","shell.execute_reply":"2021-07-10T20:58:17.257128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.study_label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:58:17.259101Z","iopub.execute_input":"2021-07-10T20:58:17.259489Z","iopub.status.idle":"2021-07-10T20:58:17.269173Z","shell.execute_reply.started":"2021-07-10T20:58:17.259452Z","shell.execute_reply":"2021-07-10T20:58:17.268151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:59:51.106952Z","iopub.execute_input":"2021-07-10T20:59:51.107394Z","iopub.status.idle":"2021-07-10T20:59:51.119525Z","shell.execute_reply.started":"2021-07-10T20:59:51.107355Z","shell.execute_reply":"2021-07-10T20:59:51.118191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = model.predict(valid_generator)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T20:59:51.725866Z","iopub.execute_input":"2021-07-10T20:59:51.726204Z","iopub.status.idle":"2021-07-10T21:00:05.522047Z","shell.execute_reply.started":"2021-07-10T20:59:51.726173Z","shell.execute_reply":"2021-07-10T21:00:05.521142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:00:06.776418Z","iopub.execute_input":"2021-07-10T21:00:06.776760Z","iopub.status.idle":"2021-07-10T21:00:06.782937Z","shell.execute_reply.started":"2021-07-10T21:00:06.776727Z","shell.execute_reply":"2021-07-10T21:00:06.782066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# c[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:00:56.835713Z","iopub.execute_input":"2021-07-10T21:00:56.836088Z","iopub.status.idle":"2021-07-10T21:00:56.839941Z","shell.execute_reply.started":"2021-07-10T21:00:56.836056Z","shell.execute_reply":"2021-07-10T21:00:56.838824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ix = 3","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:01:05.603791Z","iopub.execute_input":"2021-07-10T21:01:05.604133Z","iopub.status.idle":"2021-07-10T21:01:05.608011Z","shell.execute_reply.started":"2021-07-10T21:01:05.604101Z","shell.execute_reply":"2021-07-10T21:01:05.607015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual[ix]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:01:09.825731Z","iopub.execute_input":"2021-07-10T21:01:09.826070Z","iopub.status.idle":"2021-07-10T21:01:09.833879Z","shell.execute_reply.started":"2021-07-10T21:01:09.826039Z","shell.execute_reply":"2021-07-10T21:01:09.832818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op = c[ix]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:01:14.820268Z","iopub.execute_input":"2021-07-10T21:01:14.820593Z","iopub.status.idle":"2021-07-10T21:01:14.824430Z","shell.execute_reply.started":"2021-07-10T21:01:14.820562Z","shell.execute_reply":"2021-07-10T21:01:14.823563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"op","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:01:17.161426Z","iopub.execute_input":"2021-07-10T21:01:17.161816Z","iopub.status.idle":"2021-07-10T21:01:17.170851Z","shell.execute_reply.started":"2021-07-10T21:01:17.161755Z","shell.execute_reply":"2021-07-10T21:01:17.168514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ix, y_pred in enumerate(list(op)):\n    if y_pred > 0.5:\n#         print(ix, y_pred, classes[ix])        \n        print(ix, y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T21:01:19.194293Z","iopub.execute_input":"2021-07-10T21:01:19.194609Z","iopub.status.idle":"2021-07-10T21:01:19.200062Z","shell.execute_reply.started":"2021-07-10T21:01:19.194576Z","shell.execute_reply":"2021-07-10T21:01:19.198950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### address class imbalance\n\nnow try focal loss? https://towardsdatascience.com/a-loss-function-suitable-for-class-imbalanced-data-focal-loss-af1702d75d75\n\nOR specify in model.fit\n\nhttps://stackoverflow.com/questions/44716150/how-can-i-assign-a-class-weight-in-keras-in-a-simple-way/44721883\n\nhttps://github.com/keras-team/keras/issues/1875\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### TODO\n\ncheck old kaggle competition on chest xray - done - doing better than this\n\nadd metrics - https://keras.io/api/metrics/classification_metrics/ - done.\n\nadd weighted loss function / class weights in model/ balance train df - done with oversampling\n\ncheck coursera assignments\n\ntry better models.\n\ngradcam?\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}