{"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":"#  Histopathic Cancer Detection (HCD)\n### Taylor Kern","metadata":{}},{"cell_type":"code","source":"!pip install -U efficientnet -qq","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:29.501561Z","iopub.execute_input":"2022-04-12T19:48:29.50216Z","iopub.status.idle":"2022-04-12T19:48:39.108585Z","shell.execute_reply.started":"2022-04-12T19:48:29.502114Z","shell.execute_reply":"2022-04-12T19:48:39.10769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport pickle\nimport os\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import * \nimport efficientnet.tfkeras as efn\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:39.110554Z","iopub.execute_input":"2022-04-12T19:48:39.110816Z","iopub.status.idle":"2022-04-12T19:48:44.959318Z","shell.execute_reply.started":"2022-04-12T19:48:39.110781Z","shell.execute_reply":"2022-04-12T19:48:44.958581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Helper Functions","metadata":{}},{"cell_type":"code","source":"def merge_history(hlist):\n    history = {}\n    for k in hlist[0].history.keys():\n        history[k] = sum([h.history[k] for h in hlist], [])\n    return history\n\ndef vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:44.960563Z","iopub.execute_input":"2022-04-12T19:48:44.960809Z","iopub.status.idle":"2022-04-12T19:48:44.970308Z","shell.execute_reply.started":"2022-04-12T19:48:44.960776Z","shell.execute_reply":"2022-04-12T19:48:44.969751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load DataFrame","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:44.97229Z","iopub.execute_input":"2022-04-12T19:48:44.972832Z","iopub.status.idle":"2022-04-12T19:48:45.438942Z","shell.execute_reply.started":"2022-04-12T19:48:44.972781Z","shell.execute_reply":"2022-04-12T19:48:45.438164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:45.440172Z","iopub.execute_input":"2022-04-12T19:48:45.440445Z","iopub.status.idle":"2022-04-12T19:48:45.458084Z","shell.execute_reply.started":"2022-04-12T19:48:45.440406Z","shell.execute_reply":"2022-04-12T19:48:45.457291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.id = train.id + '.tif'\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:45.459446Z","iopub.execute_input":"2022-04-12T19:48:45.459713Z","iopub.status.idle":"2022-04-12T19:48:45.503591Z","shell.execute_reply.started":"2022-04-12T19:48:45.459678Z","shell.execute_reply":"2022-04-12T19:48:45.502763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:45.504831Z","iopub.execute_input":"2022-04-12T19:48:45.505187Z","iopub.status.idle":"2022-04-12T19:48:45.515392Z","shell.execute_reply.started":"2022-04-12T19:48:45.505149Z","shell.execute_reply":"2022-04-12T19:48:45.513333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Distribution","metadata":{}},{"cell_type":"code","source":"(train.label.value_counts() / len(train)).to_frame().sort_index().T\n","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:45.516809Z","iopub.execute_input":"2022-04-12T19:48:45.517415Z","iopub.status.idle":"2022-04-12T19:48:45.555293Z","shell.execute_reply.started":"2022-04-12T19:48:45.517379Z","shell.execute_reply":"2022-04-12T19:48:45.554562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extract Images","metadata":{}},{"cell_type":"code","source":"train_path = \"../input/histopathologic-cancer-detection/train\"\n\nsample = train.sample(n=16).reset_index()\n\nplt.figure(figsize=(6,6))\n\nfor i, row in sample.iterrows():\n\n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{row.id}')    \n    label = row.label\n\n    plt.subplot(4,4,i+1)\n    plt.imshow(img)\n    plt.text(0, -5, f'Class {label}', color='k')\n        \n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:45.556286Z","iopub.execute_input":"2022-04-12T19:48:45.556527Z","iopub.status.idle":"2022-04-12T19:48:46.378272Z","shell.execute_reply.started":"2022-04-12T19:48:45.556493Z","shell.execute_reply":"2022-04-12T19:48:46.376756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training and Validation Sets","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=1, stratify=train.label)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:46.38112Z","iopub.execute_input":"2022-04-12T19:48:46.381599Z","iopub.status.idle":"2022-04-12T19:48:46.677859Z","shell.execute_reply.started":"2022-04-12T19:48:46.381563Z","shell.execute_reply":"2022-04-12T19:48:46.677143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:46.679246Z","iopub.execute_input":"2022-04-12T19:48:46.679523Z","iopub.status.idle":"2022-04-12T19:48:46.68449Z","shell.execute_reply.started":"2022-04-12T19:48:46.679485Z","shell.execute_reply":"2022-04-12T19:48:46.683474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntrain_loader = train_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    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (96,96)\n)\n\nvalid_loader = train_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    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (96,96)\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:46.685967Z","iopub.execute_input":"2022-04-12T19:48:46.686227Z","iopub.status.idle":"2022-04-12T19:50:46.026499Z","shell.execute_reply.started":"2022-04-12T19:48:46.686192Z","shell.execute_reply":"2022-04-12T19:50:46.025745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_loader)\nVA_STEPS = len(valid_loader)\n\nprint(TR_STEPS)\nprint(VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:50:46.027809Z","iopub.execute_input":"2022-04-12T19:50:46.028224Z","iopub.status.idle":"2022-04-12T19:50:46.034147Z","shell.execute_reply.started":"2022-04-12T19:50:46.028185Z","shell.execute_reply":"2022-04-12T19:50:46.033427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Base Model","metadata":{}},{"cell_type":"code","source":"base_model = efn.EfficientNetB0(input_shape=(96,96,3), include_top=False, weights='imagenet')\n\ncnn = Sequential([\n    base_model,\n    \n    Flatten(),\n    \n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(32, activation='relu'),\n    Dropout(0.5),\n    BatchNormalization(),\n    Dense(2, activation='softmax')\n])\n\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:57:21.808039Z","iopub.execute_input":"2022-04-12T19:57:21.808774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Network","metadata":{}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.001)\ncnn.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:50:51.553551Z","iopub.execute_input":"2022-04-12T19:50:51.5538Z","iopub.status.idle":"2022-04-12T19:50:51.576576Z","shell.execute_reply.started":"2022-04-12T19:50:51.553764Z","shell.execute_reply":"2022-04-12T19:50:51.575885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 1","metadata":{}},{"cell_type":"code","source":"%%time \n\nh1 = cnn.fit(\n    x = train_loader, \n    steps_per_epoch = TR_STEPS, \n    epochs = 40,\n    validation_data = valid_loader, \n    validation_steps = VA_STEPS, \n    verbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:50:51.577821Z","iopub.execute_input":"2022-04-12T19:50:51.5781Z","iopub.status.idle":"2022-04-12T19:51:06.416581Z","shell.execute_reply.started":"2022-04-12T19:50:51.578067Z","shell.execute_reply":"2022-04-12T19:51:06.415879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1])\nvis_training(history)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.417945Z","iopub.execute_input":"2022-04-12T19:51:06.418351Z","iopub.status.idle":"2022-04-12T19:51:06.439882Z","shell.execute_reply.started":"2022-04-12T19:51:06.41831Z","shell.execute_reply":"2022-04-12T19:51:06.43876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 2","metadata":{}},{"cell_type":"code","source":"history = merge_history([h1])\nvis_training(history)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.440976Z","iopub.status.idle":"2022-04-12T19:51:06.441469Z","shell.execute_reply.started":"2022-04-12T19:51:06.44122Z","shell.execute_reply":"2022-04-12T19:51:06.441244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh2 = cnn.fit(\n    x = train_loader, \n    steps_per_epoch = TR_STEPS, \n    epochs = 30,\n    validation_data = valid_loader, \n    validation_steps = VA_STEPS, \n    verbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.443357Z","iopub.status.idle":"2022-04-12T19:51:06.443786Z","shell.execute_reply.started":"2022-04-12T19:51:06.443552Z","shell.execute_reply":"2022-04-12T19:51:06.443576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1, h2])\nvis_training(history, start=10)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.445301Z","iopub.status.idle":"2022-04-12T19:51:06.445713Z","shell.execute_reply.started":"2022-04-12T19:51:06.445496Z","shell.execute_reply":"2022-04-12T19:51:06.445517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train 3","metadata":{}},{"cell_type":"code","source":"%%time \n\nh3 = cnn.fit(\n    x = train_loader, \n    steps_per_epoch = TR_STEPS, \n    epochs = 20,\n    validation_data = valid_loader, \n    validation_steps = VA_STEPS, \n    verbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.447062Z","iopub.status.idle":"2022-04-12T19:51:06.448005Z","shell.execute_reply.started":"2022-04-12T19:51:06.447741Z","shell.execute_reply":"2022-04-12T19:51:06.447765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in history.keys():\n    history[k] += h3.history[k]\n\nepoch_range = range(1, len(history['loss'])+1)\n\nplt.figure(figsize=[14,4])\nplt.subplot(1,3,1)\nplt.plot(epoch_range, history['loss'], label='Training')\nplt.plot(epoch_range, history['val_loss'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('Loss'); plt.title('Loss')\nplt.legend()\nplt.subplot(1,3,2)\nplt.plot(epoch_range, history['accuracy'], label='Training')\nplt.plot(epoch_range, history['val_accuracy'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.title('Accuracy')\nplt.legend()\nplt.subplot(1,3,3)\nplt.plot(epoch_range, history['auc'], label='Training')\nplt.plot(epoch_range, history['val_auc'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('AUC'); plt.title('AUC')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:51:06.449158Z","iopub.status.idle":"2022-04-12T19:51:06.449563Z","shell.execute_reply.started":"2022-04-12T19:51:06.44934Z","shell.execute_reply":"2022-04-12T19:51:06.449362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save('HCDv01.h5')\npickle.dump(history, open(f'HCDv01.pkl', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2022-04-12T19:48:09.566971Z","iopub.status.idle":"2022-04-12T19:48:09.568108Z","shell.execute_reply.started":"2022-04-12T19:48:09.567814Z","shell.execute_reply":"2022-04-12T19:48:09.567841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","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":"2022-04-12T19:48:09.575209Z","iopub.execute_input":"2022-04-12T19:48:09.575701Z","iopub.status.idle":"2022-04-12T19:48:09.58882Z","shell.execute_reply.started":"2022-04-12T19:48:09.575671Z","shell.execute_reply":"2022-04-12T19:48:09.587141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['filename'] = test.id + '.tif'\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = \"../input/histopathologic-cancer-detection/test\"\nprint('Test Images:', len(os.listdir(test_path)))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntest_datagen = ImageDataGenerator(rescale=1/255)\n\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test,\n    directory = test_path,\n    x_col = 'filename',\n    batch_size = BATCH_SIZE,\n    shuffle = False,\n    class_mode = None,\n    target_size = (96,96)\n)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_probs = cnn.predict(test_loader)\nprint(test_probs.shape)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(test_loader))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_probs[:10,].round(2))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(test_probs, axis=1)\nprint(test_pred[:10])","metadata":{},"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_count":null,"outputs":[]},{"cell_type":"code","source":"submission.label = test_probs[:,1]\nsubmission.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', header=True, index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}