{"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":"## Import Statements","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport zipfile\nfrom tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.utils import shuffle\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\n\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.linear_model import LogisticRegression","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:06.691213Z","iopub.execute_input":"2021-12-15T19:20:06.691818Z","iopub.status.idle":"2021-12-15T19:20:12.265010Z","shell.execute_reply.started":"2021-12-15T19:20:06.691728Z","shell.execute_reply":"2021-12-15T19:20:12.264250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load DataFrame\n","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\n\nprint('Test Set Size:', test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:12.266601Z","iopub.execute_input":"2021-12-15T19:20:12.266830Z","iopub.status.idle":"2021-12-15T19:20:12.552461Z","shell.execute_reply.started":"2021-12-15T19:20:12.266797Z","shell.execute_reply":"2021-12-15T19:20:12.550920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:12.553618Z","iopub.execute_input":"2021-12-15T19:20:12.554350Z","iopub.status.idle":"2021-12-15T19:20:12.571060Z","shell.execute_reply.started":"2021-12-15T19:20:12.554309Z","shell.execute_reply":"2021-12-15T19:20:12.570265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:12.573070Z","iopub.execute_input":"2021-12-15T19:20:12.573375Z","iopub.status.idle":"2021-12-15T19:20:13.087028Z","shell.execute_reply.started":"2021-12-15T19:20:12.573338Z","shell.execute_reply":"2021-12-15T19:20:13.086297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_1 = train['label']\ntrain_df_1, valid_df_1 = train_test_split(train, test_size=0.75, random_state=45, stratify=y_1)\n\ny = train_df_1['label']\n\ntrain_df, valid_df = train_test_split(train_df_1, test_size=0.2, random_state=45, stratify=y)\n\nprint(train_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:13.088299Z","iopub.execute_input":"2021-12-15T19:20:13.088572Z","iopub.status.idle":"2021-12-15T19:20:13.285211Z","shell.execute_reply.started":"2021-12-15T19:20:13.088536Z","shell.execute_reply":"2021-12-15T19:20:13.284481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Test Images","metadata":{}},{"cell_type":"code","source":"test_path = \"../input/histopathologic-cancer-detection/test/\"\nprint('Test Images:', len(os.listdir(test_path)))","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:13.286445Z","iopub.execute_input":"2021-12-15T19:20:13.286886Z","iopub.status.idle":"2021-12-15T19:20:14.255109Z","shell.execute_reply.started":"2021-12-15T19:20:13.286843Z","shell.execute_reply":"2021-12-15T19:20:14.254379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:14.257187Z","iopub.execute_input":"2021-12-15T19:20:14.257660Z","iopub.status.idle":"2021-12-15T19:20:14.263727Z","shell.execute_reply.started":"2021-12-15T19:20:14.257620Z","shell.execute_reply":"2021-12-15T19:20:14.262899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['id'] = test['id'].apply(append_ext)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:14.265325Z","iopub.execute_input":"2021-12-15T19:20:14.265855Z","iopub.status.idle":"2021-12-15T19:20:14.299082Z","shell.execute_reply.started":"2021-12-15T19:20:14.265792Z","shell.execute_reply":"2021-12-15T19:20:14.298444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'] = train_df['id'].apply(append_ext)\nvalid_df['id'] = valid_df['id'].apply(append_ext)\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:14.301289Z","iopub.execute_input":"2021-12-15T19:20:14.301746Z","iopub.status.idle":"2021-12-15T19:20:14.339356Z","shell.execute_reply.started":"2021-12-15T19:20:14.301707Z","shell.execute_reply":"2021-12-15T19:20:14.338688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Generator","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntest_datagen = ImageDataGenerator(rescale=1/255)\n\ntest_loader_1 = test_datagen.flow_from_dataframe(\n    dataframe = test,\n    directory = test_path,\n    x_col = 'id',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = None,\n    target_size = (32,32)\n)\n\ntest_loader_2 = test_datagen.flow_from_dataframe(\n    dataframe = test,\n    directory = test_path,\n    x_col = 'id',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = None,\n    target_size = (64,64)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:20:14.343091Z","iopub.execute_input":"2021-12-15T19:20:14.343278Z","iopub.status.idle":"2021-12-15T19:22:15.710646Z","shell.execute_reply.started":"2021-12-15T19:20:14.343255Z","shell.execute_reply":"2021-12-15T19:22:15.709865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/histopathologic-cancer-detection/train'\ntrain_df['label'] = train_df['label'].astype(str)\nvalid_df['label'] = valid_df['label'].astype(str)\n\ntrain_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)\n\ntrain_loader_1 = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_path,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = 'categorical',\n    target_size = (32,32)\n)\ntrain_loader_2 = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_path,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = 'categorical',\n    target_size = (64,64)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:22:15.711991Z","iopub.execute_input":"2021-12-15T19:22:15.712446Z","iopub.status.idle":"2021-12-15T19:24:11.020156Z","shell.execute_reply.started":"2021-12-15T19:22:15.712390Z","shell.execute_reply":"2021-12-15T19:24:11.018544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:24:11.021655Z","iopub.execute_input":"2021-12-15T19:24:11.021903Z","iopub.status.idle":"2021-12-15T19:24:11.030876Z","shell.execute_reply.started":"2021-12-15T19:24:11.021868Z","shell.execute_reply":"2021-12-15T19:24:11.030206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_loader_1 = valid_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = train_path,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = 'categorical',\n    target_size = (32,32)\n)\n\nvalid_loader_2 = valid_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = train_path,\n    x_col = 'id',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = 'categorical',\n    target_size = (64,64)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:24:11.032095Z","iopub.execute_input":"2021-12-15T19:24:11.032493Z","iopub.status.idle":"2021-12-15T19:24:39.650786Z","shell.execute_reply.started":"2021-12-15T19:24:11.032456Z","shell.execute_reply":"2021-12-15T19:24:39.648952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load Model","metadata":{}},{"cell_type":"code","source":"model_1 = keras.models.load_model('../input/cancer-detection-models/cancer_detection_model_v16.h5')\n\nmodel_1.summary()\n\nmodel_2 = keras.models.load_model('../input/cancer-detection-models/cancer_detection_model_v04.h5')\n\nmodel_3 = keras.models.load_model('../input/cancer-detection-models/cancer_detection_model_v01.h5')\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:24:39.652165Z","iopub.execute_input":"2021-12-15T19:24:39.652451Z","iopub.status.idle":"2021-12-15T19:24:45.337248Z","shell.execute_reply.started":"2021-12-15T19:24:39.652412Z","shell.execute_reply":"2021-12-15T19:24:45.336524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Predictions","metadata":{}},{"cell_type":"code","source":"train_pred_1 = model_1.predict(train_loader_2)[:,1]\ntrain_pred_2 = model_2.predict(train_loader_1)[:,1]\ntrain_pred_3 = model_3.predict(train_loader_1)[:,1]\n\n\nvalid_pred_1 = model_1.predict(valid_loader_2)[:,1]\nvalid_pred_2 = model_2.predict(valid_loader_1)[:,1]\nvalid_pred_3 = model_3.predict(valid_loader_1)[:,1]\n\n\ntest_pred_1 = model_1.predict(test_loader_2)[:,1]\ntest_pred_2 = model_2.predict(test_loader_1)[:,1]\ntest_pred_3 = model_3.predict(test_loader_1)[:,1]","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:24:45.340745Z","iopub.execute_input":"2021-12-15T19:24:45.340948Z","iopub.status.idle":"2021-12-15T19:39:49.878857Z","shell.execute_reply.started":"2021-12-15T19:24:45.340923Z","shell.execute_reply":"2021-12-15T19:39:49.878087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = pd.DataFrame({\n    'model_1': train_pred_1,\n    'model_2': train_pred_2,\n    'model_3': train_pred_3\n})\n\nX_valid = pd.DataFrame({\n    'model_1': valid_pred_1,\n    'model_2': valid_pred_2,\n    'model_3': valid_pred_3\n})\ny_train = train_df['label']\ny_valid = valid_df['label']\n\nX_test = pd.DataFrame({\n    'model_1': test_pred_1,\n    'model_2': test_pred_2,\n    'model_3': test_pred_3\n})","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:39:49.880340Z","iopub.execute_input":"2021-12-15T19:39:49.880615Z","iopub.status.idle":"2021-12-15T19:39:49.889687Z","shell.execute_reply.started":"2021-12-15T19:39:49.880581Z","shell.execute_reply":"2021-12-15T19:39:49.889028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:39:49.890669Z","iopub.execute_input":"2021-12-15T19:39:49.891200Z","iopub.status.idle":"2021-12-15T19:39:49.899960Z","shell.execute_reply.started":"2021-12-15T19:39:49.891162Z","shell.execute_reply":"2021-12-15T19:39:49.899276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:39:49.901249Z","iopub.execute_input":"2021-12-15T19:39:49.901749Z","iopub.status.idle":"2021-12-15T19:39:49.907435Z","shell.execute_reply.started":"2021-12-15T19:39:49.901713Z","shell.execute_reply":"2021-12-15T19:39:49.906729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_clf = LogisticRegression(max_iter=1000, solver='saga', penalty='elasticnet')\n\nlr_parameters = {\n    'l1_ratio': [0, 0.25, 0.5, 0.75, 1],\n    'C': [0.01, 0.1, 1, 10]\n}\n\nlr_grid = GridSearchCV(lr_clf, lr_parameters, cv=10, refit='True', n_jobs=-1, verbose=10, scoring='accuracy')\nlr_grid.fit(X_train, y_train)\n\nlr_model = lr_grid.best_estimator_\n\nprint('Best Parameters:', lr_grid.best_params_)\nprint('Best CV Score:  ', lr_grid.best_score_)\nprint('Training Acc:   ', lr_model.score(X_train, y_train))","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:39:49.908876Z","iopub.execute_input":"2021-12-15T19:39:49.909207Z","iopub.status.idle":"2021-12-15T19:40:25.173702Z","shell.execute_reply.started":"2021-12-15T19:39:49.909171Z","shell.execute_reply":"2021-12-15T19:40:25.172716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = lr_model.predict_proba(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.175336Z","iopub.execute_input":"2021-12-15T19:40:25.175854Z","iopub.status.idle":"2021-12-15T19:40:25.184081Z","shell.execute_reply.started":"2021-12-15T19:40:25.175809Z","shell.execute_reply":"2021-12-15T19:40:25.183366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_pred.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.185148Z","iopub.execute_input":"2021-12-15T19:40:25.185540Z","iopub.status.idle":"2021-12-15T19:40:25.192849Z","shell.execute_reply.started":"2021-12-15T19:40:25.185503Z","shell.execute_reply":"2021-12-15T19:40:25.192169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_pred[:10,].round(2))","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.194193Z","iopub.execute_input":"2021-12-15T19:40:25.194769Z","iopub.status.idle":"2021-12-15T19:40:25.201952Z","shell.execute_reply.started":"2021-12-15T19:40:25.194731Z","shell.execute_reply":"2021-12-15T19:40:25.201262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluation","metadata":{}},{"cell_type":"code","source":"valid_probs = lr_model.predict_proba(X_valid)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.203329Z","iopub.execute_input":"2021-12-15T19:40:25.203963Z","iopub.status.idle":"2021-12-15T19:40:25.213442Z","shell.execute_reply.started":"2021-12-15T19:40:25.203925Z","shell.execute_reply":"2021-12-15T19:40:25.212450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(valid_probs.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.216842Z","iopub.execute_input":"2021-12-15T19:40:25.217376Z","iopub.status.idle":"2021-12-15T19:40:25.221879Z","shell.execute_reply.started":"2021-12-15T19:40:25.217340Z","shell.execute_reply":"2021-12-15T19:40:25.221179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_pred = np.argmax(valid_probs, axis=1)\nprint(valid_pred[:10])","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.223043Z","iopub.execute_input":"2021-12-15T19:40:25.223621Z","iopub.status.idle":"2021-12-15T19:40:25.231386Z","shell.execute_reply.started":"2021-12-15T19:40:25.223587Z","shell.execute_reply":"2021-12-15T19:40:25.230722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Confusion matrix ","metadata":{}},{"cell_type":"code","source":"valid_pred = valid_pred.astype(str)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:52:57.949904Z","iopub.execute_input":"2021-12-15T19:52:57.950150Z","iopub.status.idle":"2021-12-15T19:52:57.962606Z","shell.execute_reply.started":"2021-12-15T19:52:57.950123Z","shell.execute_reply":"2021-12-15T19:52:57.961631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_valid, valid_pred)\ncm_df = pd.DataFrame(cm)\ncm_df","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:53:06.737187Z","iopub.execute_input":"2021-12-15T19:53:06.737459Z","iopub.status.idle":"2021-12-15T19:53:06.787831Z","shell.execute_reply.started":"2021-12-15T19:53:06.737427Z","shell.execute_reply":"2021-12-15T19:53:06.787036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(cm, index=['True No', 'True Has'], \n            columns=['Pred No', 'Pred Has'] )","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:53:11.500438Z","iopub.execute_input":"2021-12-15T19:53:11.501088Z","iopub.status.idle":"2021-12-15T19:53:11.510501Z","shell.execute_reply.started":"2021-12-15T19:53:11.501053Z","shell.execute_reply":"2021-12-15T19:53:11.509724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Classification Report","metadata":{}},{"cell_type":"code","source":"print(classification_report(y_valid, valid_pred))","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:53:20.332357Z","iopub.execute_input":"2021-12-15T19:53:20.333041Z","iopub.status.idle":"2021-12-15T19:53:20.483442Z","shell.execute_reply.started":"2021-12-15T19:53:20.332997Z","shell.execute_reply":"2021-12-15T19:53:20.482607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prepare Submission","metadata":{}},{"cell_type":"code","source":"submission = pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.615875Z","iopub.status.idle":"2021-12-15T19:40:25.616288Z","shell.execute_reply.started":"2021-12-15T19:40:25.616068Z","shell.execute_reply":"2021-12-15T19:40:25.616091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsubmission.label = test_pred[:,1]\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.617727Z","iopub.status.idle":"2021-12-15T19:40:25.618368Z","shell.execute_reply.started":"2021-12-15T19:40:25.618134Z","shell.execute_reply":"2021-12-15T19:40:25.618158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', header=True, index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T19:40:25.619415Z","iopub.status.idle":"2021-12-15T19:40:25.620326Z","shell.execute_reply.started":"2021-12-15T19:40:25.620096Z","shell.execute_reply":"2021-12-15T19:40:25.620119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}