{"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-30T11:58:32.179900Z","iopub.execute_input":"2023-04-30T11:58:32.180622Z","iopub.status.idle":"2023-04-30T11:58:33.106847Z","shell.execute_reply.started":"2023-04-30T11:58:32.180573Z","shell.execute_reply":"2023-04-30T11:58:33.105255Z"},"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-30T11:58:33.112611Z","iopub.execute_input":"2023-04-30T11:58:33.113130Z","iopub.status.idle":"2023-04-30T11:58:40.711304Z","shell.execute_reply.started":"2023-04-30T11:58:33.113082Z","shell.execute_reply":"2023-04-30T11:58:40.710124Z"},"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-30T11:58:40.712778Z","iopub.execute_input":"2023-04-30T11:58:40.713663Z","iopub.status.idle":"2023-04-30T11:58:41.211937Z","shell.execute_reply.started":"2023-04-30T11:58:40.713608Z","shell.execute_reply":"2023-04-30T11:58:41.210885Z"},"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-30T11:58:41.215081Z","iopub.execute_input":"2023-04-30T11:58:41.215490Z","iopub.status.idle":"2023-04-30T11:58:55.041513Z","shell.execute_reply.started":"2023-04-30T11:58:41.215451Z","shell.execute_reply":"2023-04-30T11:58:55.040463Z"},"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-30T11:58:55.042999Z","iopub.execute_input":"2023-04-30T11:58:55.043391Z","iopub.status.idle":"2023-04-30T11:58:57.447608Z","shell.execute_reply.started":"2023-04-30T11:58:55.043329Z","shell.execute_reply":"2023-04-30T11:58:57.446563Z"},"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-30T11:58:57.449071Z","iopub.execute_input":"2023-04-30T11:58:57.449484Z","iopub.status.idle":"2023-04-30T11:59:32.448486Z","shell.execute_reply.started":"2023-04-30T11:58:57.449443Z","shell.execute_reply":"2023-04-30T11:59:32.447388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T11:59:32.450342Z","iopub.execute_input":"2023-04-30T11:59:32.450812Z","iopub.status.idle":"2023-04-30T11:59:32.641788Z","shell.execute_reply.started":"2023-04-30T11:59:32.450769Z","shell.execute_reply":"2023-04-30T11:59:32.640368Z"},"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-30T11:59:32.643791Z","iopub.execute_input":"2023-04-30T11:59:32.644507Z","iopub.status.idle":"2023-04-30T11:59:32.653311Z","shell.execute_reply.started":"2023-04-30T11:59:32.644441Z","shell.execute_reply":"2023-04-30T11:59:32.652219Z"},"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-30T11:59:32.654874Z","iopub.execute_input":"2023-04-30T11:59:32.655369Z","iopub.status.idle":"2023-04-30T11:59:32.663718Z","shell.execute_reply.started":"2023-04-30T11:59:32.655314Z","shell.execute_reply":"2023-04-30T11:59:32.662548Z"},"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 = \"relu\", padding = \"same\", input_shape = X_shape))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\n\nFS = 3\nNF = NF*2\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.MaxPooling2D((2,2)))\n\nFS = 3\nNF = NF\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", padding = \"same\"))\nmodel.add(layers.Conv2D(NF, (FS,FS), activation = \"relu\", 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-30T11:59:32.669452Z","iopub.execute_input":"2023-04-30T11:59:32.669732Z","iopub.status.idle":"2023-04-30T11:59:35.541434Z","shell.execute_reply.started":"2023-04-30T11:59:32.669707Z","shell.execute_reply":"2023-04-30T11:59:35.540647Z"},"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-30T11:59:35.542468Z","iopub.execute_input":"2023-04-30T11:59:35.542809Z","iopub.status.idle":"2023-04-30T11:59:35.548131Z","shell.execute_reply.started":"2023-04-30T11:59:35.542772Z","shell.execute_reply":"2023-04-30T11:59:35.547381Z"},"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-30T11:59:35.549506Z","iopub.execute_input":"2023-04-30T11:59:35.549886Z","iopub.status.idle":"2023-04-30T11:59:35.586224Z","shell.execute_reply.started":"2023-04-30T11:59:35.549851Z","shell.execute_reply":"2023-04-30T11:59:35.585206Z"},"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-30T11:59:35.587678Z","iopub.execute_input":"2023-04-30T11:59:35.588275Z","iopub.status.idle":"2023-04-30T12:06:42.563022Z","shell.execute_reply.started":"2023-04-30T11:59:35.588239Z","shell.execute_reply":"2023-04-30T12:06:42.561291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()\ntf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.564183Z","iopub.status.idle":"2023-04-30T12:06:42.565304Z","shell.execute_reply.started":"2023-04-30T12:06:42.565049Z","shell.execute_reply":"2023-04-30T12:06:42.565076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train\n#del X_val","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.566573Z","iopub.status.idle":"2023-04-30T12:06:42.567449Z","shell.execute_reply.started":"2023-04-30T12:06:42.567169Z","shell.execute_reply":"2023-04-30T12:06:42.567196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.568981Z","iopub.status.idle":"2023-04-30T12:06:42.569929Z","shell.execute_reply.started":"2023-04-30T12:06:42.569663Z","shell.execute_reply":"2023-04-30T12:06:42.569688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"CNN_simple\")","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.571369Z","iopub.status.idle":"2023-04-30T12:06:42.572211Z","shell.execute_reply.started":"2023-04-30T12:06:42.571954Z","shell.execute_reply":"2023-04-30T12:06:42.571980Z"},"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-30T12:06:42.573678Z","iopub.status.idle":"2023-04-30T12:06:42.574536Z","shell.execute_reply.started":"2023-04-30T12:06:42.574263Z","shell.execute_reply":"2023-04-30T12:06:42.574289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_scores(history)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.575954Z","iopub.status.idle":"2023-04-30T12:06:42.576817Z","shell.execute_reply.started":"2023-04-30T12:06:42.576551Z","shell.execute_reply":"2023-04-30T12:06:42.576577Z"},"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-30T12:06:42.578236Z","iopub.status.idle":"2023-04-30T12:06:42.579185Z","shell.execute_reply.started":"2023-04-30T12:06:42.578897Z","shell.execute_reply":"2023-04-30T12:06:42.578927Z"},"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-30T12:06:42.580654Z","iopub.status.idle":"2023-04-30T12:06:42.581519Z","shell.execute_reply.started":"2023-04-30T12:06:42.581242Z","shell.execute_reply":"2023-04-30T12:06:42.581268Z"},"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-30T12:06:42.582945Z","iopub.status.idle":"2023-04-30T12:06:42.584726Z","shell.execute_reply.started":"2023-04-30T12:06:42.584460Z","shell.execute_reply":"2023-04-30T12:06:42.584486Z"},"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-30T12:06:42.586179Z","iopub.status.idle":"2023-04-30T12:06:42.587022Z","shell.execute_reply.started":"2023-04-30T12:06:42.586770Z","shell.execute_reply":"2023-04-30T12:06:42.586796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_submit[\"label\"].hist()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.588937Z","iopub.status.idle":"2023-04-30T12:06:42.589834Z","shell.execute_reply.started":"2023-04-30T12:06:42.589570Z","shell.execute_reply":"2023-04-30T12:06:42.589596Z"},"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-30T12:06:42.591323Z","iopub.status.idle":"2023-04-30T12:06:42.592172Z","shell.execute_reply.started":"2023-04-30T12:06:42.591906Z","shell.execute_reply":"2023-04-30T12:06:42.591932Z"},"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-30T12:06:42.593660Z","iopub.status.idle":"2023-04-30T12:06:42.594513Z","shell.execute_reply.started":"2023-04-30T12:06:42.594229Z","shell.execute_reply":"2023-04-30T12:06:42.594257Z"},"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-30T12:06:42.595951Z","iopub.status.idle":"2023-04-30T12:06:42.596786Z","shell.execute_reply.started":"2023-04-30T12:06:42.596518Z","shell.execute_reply":"2023-04-30T12:06:42.596544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.598285Z","iopub.status.idle":"2023-04-30T12:06:42.599246Z","shell.execute_reply.started":"2023-04-30T12:06:42.598939Z","shell.execute_reply":"2023-04-30T12:06:42.598968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_val, yhat_val_n)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T12:06:42.600933Z","iopub.status.idle":"2023-04-30T12:06:42.601917Z","shell.execute_reply.started":"2023-04-30T12:06:42.601616Z","shell.execute_reply":"2023-04-30T12:06:42.601642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}