{"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":"code","source":"import numpy as np \nimport pandas as pd \n\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport math\nimport time\nimport cv2\nfrom sklearn import metrics\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-29T15:35:06.107800Z","iopub.execute_input":"2023-04-29T15:35:06.108632Z","iopub.status.idle":"2023-04-29T15:35:07.554299Z","shell.execute_reply.started":"2023-04-29T15:35:06.108590Z","shell.execute_reply":"2023-04-29T15:35:07.553088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n#from tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import models","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:07.556788Z","iopub.execute_input":"2023-04-29T15:35:07.557529Z","iopub.status.idle":"2023-04-29T15:35:15.839024Z","shell.execute_reply.started":"2023-04-29T15:35:07.557488Z","shell.execute_reply":"2023-04-29T15:35:15.837875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/sample_submission.csv\")\nlabel_df = pd.read_csv(\"/kaggle/input/histopathologic-cancer-detection/train_labels.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:15.841100Z","iopub.execute_input":"2023-04-29T15:35:15.841907Z","iopub.status.idle":"2023-04-29T15:35:16.527448Z","shell.execute_reply.started":"2023-04-29T15:35:15.841867Z","shell.execute_reply":"2023-04-29T15:35:16.526400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fpath = \"/kaggle/input/cancerdetection-npy/X_test.npy\"\nX_test = np.load(fpath)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:16.530156Z","iopub.execute_input":"2023-04-29T15:35:16.530553Z","iopub.status.idle":"2023-04-29T15:35:30.141686Z","shell.execute_reply.started":"2023-04-29T15:35:16.530512Z","shell.execute_reply":"2023-04-29T15:35:30.140568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfpath = \"/kaggle/input/cancerdetection-npy/X_val.npy\"\nX_val = np.load(fpath)\n\nfpath = \"/kaggle/input/cancerdetection-npy/y_val.npy\"\ny_val = np.load(fpath)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:30.143143Z","iopub.execute_input":"2023-04-29T15:35:30.144149Z","iopub.status.idle":"2023-04-29T15:35:31.766779Z","shell.execute_reply.started":"2023-04-29T15:35:30.144116Z","shell.execute_reply":"2023-04-29T15:35:31.765753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fpath = \"/kaggle/input/cancerdetection-npy/X_train.npy\"\nX_train = np.load(fpath)[0:100000]\n\nfpath = \"/kaggle/input/cancerdetection-npy/y_train.npy\"\ny_train = np.load(fpath)[0:100000]","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:31.768575Z","iopub.execute_input":"2023-04-29T15:35:31.768970Z","iopub.status.idle":"2023-04-29T15:35:59.765852Z","shell.execute_reply.started":"2023-04-29T15:35:31.768911Z","shell.execute_reply":"2023-04-29T15:35:59.764842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:59.767827Z","iopub.execute_input":"2023-04-29T15:35:59.768572Z","iopub.status.idle":"2023-04-29T15:35:59.952571Z","shell.execute_reply.started":"2023-04-29T15:35:59.768528Z","shell.execute_reply":"2023-04-29T15:35:59.951220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_val.shape, y_train.shape, y_val.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:35:59.954401Z","iopub.execute_input":"2023-04-29T15:35:59.955309Z","iopub.status.idle":"2023-04-29T15:35:59.969046Z","shell.execute_reply.started":"2023-04-29T15:35:59.955263Z","shell.execute_reply":"2023-04-29T15:35:59.967844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_shape = X_train[0].shape\nX_shape","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:36:20.507277Z","iopub.execute_input":"2023-04-29T15:36:20.507645Z","iopub.status.idle":"2023-04-29T15:36:20.517789Z","shell.execute_reply.started":"2023-04-29T15:36:20.507611Z","shell.execute_reply":"2023-04-29T15:36:20.513760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.set_random_seed(\n    seed = 2\n)\n\nmodel = models.Sequential()\n\nNF =64\nFS = 3\n\nmodel.add(layers.Rescaling(scale=1./127.5, offset=-1., input_shape = X_shape))\nmodel.add(layers.RandomFlip(mode=\"horizontal_and_vertical\", seed=1, input_shape = X_shape))\n\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\", input_shape = X_shape))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\nFS = 3\nNF = NF\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"LeakyReLU\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\nmodel.add(layers.Flatten())\n#model.add(layers.BatchNormalization())\nmodel.add(layers.Dropout(0.5, seed = 1))\nmodel.add(layers.Dense(512*9, activation = \"relu\"))\n\nmodel.add(layers.Dense(2))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:36:23.103283Z","iopub.execute_input":"2023-04-29T15:36:23.104084Z","iopub.status.idle":"2023-04-29T15:36:26.194600Z","shell.execute_reply.started":"2023-04-29T15:36:23.104046Z","shell.execute_reply":"2023-04-29T15:36:26.193792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:36:35.861850Z","iopub.execute_input":"2023-04-29T15:36:35.862569Z","iopub.status.idle":"2023-04-29T15:36:35.867428Z","shell.execute_reply.started":"2023-04-29T15:36:35.862531Z","shell.execute_reply":"2023-04-29T15:36:35.866251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    #optimizer=Optimizer, \n    optimizer = \"adam\",\n    loss=loss_fn, \n    metrics=['accuracy'] \n    #metrics=[tf.keras.metrics.AUC()]\n    )","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:36:36.683973Z","iopub.execute_input":"2023-04-29T15:36:36.685107Z","iopub.status.idle":"2023-04-29T15:36:36.705945Z","shell.execute_reply.started":"2023-04-29T15:36:36.685058Z","shell.execute_reply":"2023-04-29T15:36:36.704959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size = 128, epochs = 7, validation_data = (X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:37:08.363262Z","iopub.execute_input":"2023-04-29T15:37:08.363673Z","iopub.status.idle":"2023-04-29T15:53:42.812179Z","shell.execute_reply.started":"2023-04-29T15:37:08.363637Z","shell.execute_reply":"2023-04-29T15:53:42.811198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\ntf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:42.814276Z","iopub.execute_input":"2023-04-29T15:53:42.814910Z","iopub.status.idle":"2023-04-29T15:53:43.203786Z","shell.execute_reply.started":"2023-04-29T15:53:42.814869Z","shell.execute_reply":"2023-04-29T15:53:43.202756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train\n#del X_val","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.206107Z","iopub.execute_input":"2023-04-29T15:53:43.206788Z","iopub.status.idle":"2023-04-29T15:53:43.232584Z","shell.execute_reply.started":"2023-04-29T15:53:43.206743Z","shell.execute_reply":"2023-04-29T15:53:43.231381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.235928Z","iopub.execute_input":"2023-04-29T15:53:43.236963Z","iopub.status.idle":"2023-04-29T15:53:43.428241Z","shell.execute_reply.started":"2023-04-29T15:53:43.236904Z","shell.execute_reply":"2023-04-29T15:53:43.426909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"CNN_simple\")","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:13:47.641274Z","iopub.execute_input":"2023-04-29T15:13:47.642177Z","iopub.status.idle":"2023-04-29T15:13:50.655367Z","shell.execute_reply.started":"2023-04-29T15:13:47.642116Z","shell.execute_reply":"2023-04-29T15:13:50.654360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_scores(history):\n    \n    fig, ax = plt.subplots(1,2, figsize = (12, 5))\n    ax[0].plot(history.history[\"accuracy\"], label = \"train\")\n    ax[0].plot(history.history[\"val_accuracy\"], label = \"val\")\n    ax[0].set_xlabel(\"epochs\")\n    ax[0].set_ylabel(\"accuracy\")\n    ax[0].set_ylim([0.7,1])\n    ax[0].set_title(\"Accuracy\")\n    ax[0].legend()\n\n    ax[1].plot(history.history[\"loss\"], label = \"train\")\n    ax[1].plot(history.history[\"val_loss\"], label = \"val\")\n    ax[1].set_xlabel(\"epochs\")\n    ax[1].set_ylabel(\"loss\")\n    ax[1].set_ylim([0,1])\n    ax[1].set_title(\"Loss\")\n    ax[1].legend()\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.430310Z","iopub.execute_input":"2023-04-29T15:53:43.430748Z","iopub.status.idle":"2023-04-29T15:53:43.441683Z","shell.execute_reply.started":"2023-04-29T15:53:43.430702Z","shell.execute_reply":"2023-04-29T15:53:43.440628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_scores(history)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.442871Z","iopub.execute_input":"2023-04-29T15:53:43.444368Z","iopub.status.idle":"2023-04-29T15:53:43.818672Z","shell.execute_reply.started":"2023-04-29T15:53:43.444337Z","shell.execute_reply":"2023-04-29T15:53:43.817734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred_val():\n    \n    yhat_test1 = model.predict(X_val)\n    yhat_test1 = tf.nn.softmax(yhat_test1).numpy()[:,1]\n    \n    gc.collect()\n\n    yhat_test2 = model.predict(X_val[:,::-1,::-1,:])\n    yhat_test2 = tf.nn.softmax(yhat_test2).numpy()[:,1]\n    \n    gc.collect()\n\n    yhat_test3 = model.predict(X_val[:,::-1,:,:])\n    yhat_test3 = tf.nn.softmax(yhat_test3).numpy()[:,1]\n    \n    gc.collect()\n\n    yhat_test4 = model.predict(X_val[:,:,::-1,:])\n    yhat_test4 = tf.nn.softmax(yhat_test4).numpy()[:,1]\n    \n    gc.collect()\n    \n    yhat_test =  (yhat_test1 + yhat_test2 + yhat_test3 + yhat_test4)/4\n    \n    return yhat_test","metadata":{"execution":{"iopub.status.busy":"2023-04-29T16:02:55.295445Z","iopub.execute_input":"2023-04-29T16:02:55.295841Z","iopub.status.idle":"2023-04-29T16:02:55.305279Z","shell.execute_reply.started":"2023-04-29T16:02:55.295804Z","shell.execute_reply":"2023-04-29T16:02:55.304055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred_test():\n    \n    yhat_test1 = model.predict(X_test)\n    yhat_test1 = tf.nn.softmax(yhat_test1).numpy()[:,1]\n    \n    gc.collect()\n    tf.keras.backend.clear_session()\n    \n    yhat_test2 = model.predict(X_test[:,::-1,::-1,:])\n    yhat_test2 = tf.nn.softmax(yhat_test2).numpy()[:,1]\n    \n    gc.collect()\n    tf.keras.backend.clear_session()\n\n    yhat_test3 = model.predict(X_test[:,::-1,:,:])\n    yhat_test3 = tf.nn.softmax(yhat_test3).numpy()[:,1]\n    \n    gc.collect()\n    tf.keras.backend.clear_session()\n\n    yhat_test4 = model.predict(X_test[:,:,::-1,:])\n    yhat_test4 = tf.nn.softmax(yhat_test4).numpy()[:,1]\n    \n    gc.collect()\n    tf.keras.backend.clear_session()\n    \n    yhat_test =  (yhat_test1 + yhat_test2 + yhat_test3 + yhat_test4)/4\n    \n    return yhat_test","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.820039Z","iopub.execute_input":"2023-04-29T15:53:43.820877Z","iopub.status.idle":"2023-04-29T15:53:43.830315Z","shell.execute_reply.started":"2023-04-29T15:53:43.820836Z","shell.execute_reply":"2023-04-29T15:53:43.829033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_test = pred_test()\nnp.min(yhat_test), np.max(yhat_test), np.mean(yhat_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:53:43.831980Z","iopub.execute_input":"2023-04-29T15:53:43.832451Z","iopub.status.idle":"2023-04-29T15:56:23.067232Z","shell.execute_reply.started":"2023-04-29T15:53:43.832411Z","shell.execute_reply":"2023-04-29T15:56:23.065953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_submit = sample_df.copy()\ntest_submit[\"label\"] = yhat_test\ntest_submit.to_csv('submission.csv',index=False)\ntest_submit[\"label\"].mean()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:56:23.069145Z","iopub.execute_input":"2023-04-29T15:56:23.069593Z","iopub.status.idle":"2023-04-29T15:56:23.201955Z","shell.execute_reply.started":"2023-04-29T15:56:23.069549Z","shell.execute_reply":"2023-04-29T15:56:23.200901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_submit[\"label\"].hist()","metadata":{"execution":{"iopub.status.busy":"2023-04-29T16:06:54.326203Z","iopub.execute_input":"2023-04-29T16:06:54.326653Z","iopub.status.idle":"2023-04-29T16:06:54.573391Z","shell.execute_reply.started":"2023-04-29T16:06:54.326610Z","shell.execute_reply":"2023-04-29T16:06:54.571315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val = model.predict(X_val)\nyhat_val = tf.nn.softmax(yhat_val).numpy()[:,1]\n\nfpr, tpr, thresholds = metrics.roc_curve(y_val, yhat_val, pos_label=1)\nval_auc = metrics.auc(fpr, tpr)\nprint(\"val auc\", val_auc)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T16:02:44.302499Z","iopub.execute_input":"2023-04-29T16:02:44.303601Z","iopub.status.idle":"2023-04-29T16:02:49.320329Z","shell.execute_reply.started":"2023-04-29T16:02:44.303549Z","shell.execute_reply":"2023-04-29T16:02:49.318873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val = pred_val()\nfpr, tpr, thresholds = metrics.roc_curve(y_val, yhat_val, pos_label=1)\nval_auc = metrics.auc(fpr, tpr)\nprint(\"val auc\", val_auc)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T16:03:05.394318Z","iopub.execute_input":"2023-04-29T16:03:05.395245Z","iopub.status.idle":"2023-04-29T16:03:29.705324Z","shell.execute_reply.started":"2023-04-29T16:03:05.395192Z","shell.execute_reply":"2023-04-29T16:03:29.704105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat_val_n = (yhat_val > 0.5).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:20:25.996393Z","iopub.execute_input":"2023-04-29T15:20:25.996849Z","iopub.status.idle":"2023-04-29T15:20:26.010997Z","shell.execute_reply.started":"2023-04-29T15:20:25.996804Z","shell.execute_reply":"2023-04-29T15:20:26.005542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:21:04.221835Z","iopub.execute_input":"2023-04-29T15:21:04.222585Z","iopub.status.idle":"2023-04-29T15:21:04.227639Z","shell.execute_reply.started":"2023-04-29T15:21:04.222544Z","shell.execute_reply":"2023-04-29T15:21:04.226445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_val, yhat_val_n)","metadata":{"execution":{"iopub.status.busy":"2023-04-29T15:21:08.613932Z","iopub.execute_input":"2023-04-29T15:21:08.614660Z","iopub.status.idle":"2023-04-29T15:21:08.625575Z","shell.execute_reply.started":"2023-04-29T15:21:08.614618Z","shell.execute_reply":"2023-04-29T15:21:08.624322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}