{"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":"# Load Packages","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport pandas as pd\nimport pickle\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import backend as K\nfrom sklearn.utils import shuffle\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport zipfile ","metadata":{"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":"2021-12-10T20:09:02.370381Z","iopub.execute_input":"2021-12-10T20:09:02.370731Z","iopub.status.idle":"2021-12-10T20:09:03.02373Z","shell.execute_reply.started":"2021-12-10T20:09:02.370694Z","shell.execute_reply":"2021-12-10T20:09:03.022495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:05.053641Z","iopub.execute_input":"2021-12-10T20:09:05.055044Z","iopub.status.idle":"2021-12-10T20:09:05.079545Z","shell.execute_reply.started":"2021-12-10T20:09:05.054972Z","shell.execute_reply":"2021-12-10T20:09:05.078352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Distribution","metadata":{}},{"cell_type":"code","source":"y_train = train.label\n\n(train.label.value_counts() / len(train)).to_frame().T","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:07.542905Z","iopub.execute_input":"2021-12-10T20:09:07.543507Z","iopub.status.idle":"2021-12-10T20:09:07.596508Z","shell.execute_reply.started":"2021-12-10T20:09:07.543461Z","shell.execute_reply":"2021-12-10T20:09:07.595288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# View Sample of Images","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10)) \n\nfor i in range(16):\n    plt.subplot(4,4,i+1)   \n    img = mpimg.imread(f'../input/histopathologic-cancer-detection/train/{train[\"id\"][i]}.tif')\n    plt.imshow(img)\n    plt.text(0, -5, f'Label {train[\"label\"][i]}')\n    plt.axis('off')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:09.575996Z","iopub.execute_input":"2021-12-10T20:09:09.577226Z","iopub.status.idle":"2021-12-10T20:09:11.101542Z","shell.execute_reply.started":"2021-12-10T20:09:09.577176Z","shell.execute_reply":"2021-12-10T20:09:11.100692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split and Sample Data","metadata":{}},{"cell_type":"code","source":"train_neg = train[train['label']=='0'].sample(10000,random_state=45)\ntrain_pos = train[train['label']=='1'].sample(10000,random_state=45)\n\ntrain_data = pd.concat([train_neg, train_pos], axis=0).reset_index(drop=True)\n\ntrain = shuffle(train_data)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:14.07414Z","iopub.execute_input":"2021-12-10T20:09:14.074434Z","iopub.status.idle":"2021-12-10T20:09:14.182069Z","shell.execute_reply.started":"2021-12-10T20:09:14.074402Z","shell.execute_reply":"2021-12-10T20:09:14.18105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:16.543061Z","iopub.execute_input":"2021-12-10T20:09:16.543467Z","iopub.status.idle":"2021-12-10T20:09:16.557028Z","shell.execute_reply.started":"2021-12-10T20:09:16.543429Z","shell.execute_reply":"2021-12-10T20:09:16.555919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".tif\"\n\n\ntrain['id'] = train['id'].apply(append_ext)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:18.441901Z","iopub.execute_input":"2021-12-10T20:09:18.442547Z","iopub.status.idle":"2021-12-10T20:09:18.46732Z","shell.execute_reply.started":"2021-12-10T20:09:18.442494Z","shell.execute_reply":"2021-12-10T20:09:18.466136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=45, stratify=train.label)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:21.333952Z","iopub.execute_input":"2021-12-10T20:09:21.334238Z","iopub.status.idle":"2021-12-10T20:09:21.378184Z","shell.execute_reply.started":"2021-12-10T20:09:21.334207Z","shell.execute_reply":"2021-12-10T20:09:21.37702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:23.675357Z","iopub.execute_input":"2021-12-10T20:09:23.675692Z","iopub.status.idle":"2021-12-10T20:09:23.681401Z","shell.execute_reply.started":"2021-12-10T20:09:23.675658Z","shell.execute_reply":"2021-12-10T20:09:23.68053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 64\n\ntrain_loader = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = '../input/histopathologic-cancer-detection/train/',\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 = (32,32)\n)\n\nvalid_loader = train_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = '../input/histopathologic-cancer-detection/train/',\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 = (32,32)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:09:25.824086Z","iopub.execute_input":"2021-12-10T20:09:25.824866Z","iopub.status.idle":"2021-12-10T20:09:55.932695Z","shell.execute_reply.started":"2021-12-10T20:09:25.824825Z","shell.execute_reply":"2021-12-10T20:09:55.931488Z"},"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":"2021-12-10T20:10:37.0272Z","iopub.execute_input":"2021-12-10T20:10:37.02756Z","iopub.status.idle":"2021-12-10T20:10:37.034156Z","shell.execute_reply.started":"2021-12-10T20:10:37.027493Z","shell.execute_reply":"2021-12-10T20:10:37.033277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Network","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.VGG16(input_shape=(32,32,3),\n                                         include_top=False,\n                                         weights='imagenet')\n\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:10:39.020063Z","iopub.execute_input":"2021-12-10T20:10:39.020689Z","iopub.status.idle":"2021-12-10T20:10:40.000376Z","shell.execute_reply.started":"2021-12-10T20:10:39.020639Z","shell.execute_reply":"2021-12-10T20:10:39.99941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:10:43.584384Z","iopub.execute_input":"2021-12-10T20:10:43.584734Z","iopub.status.idle":"2021-12-10T20:10:43.599805Z","shell.execute_reply.started":"2021-12-10T20:10:43.584699Z","shell.execute_reply":"2021-12-10T20:10:43.598833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn1 = Sequential([\n    base_model,\n    \n    Flatten(),\n    \n    Dense(32, activation='relu'),\n    Dropout(0.5),\n    Dense(16, activation='relu'),\n    Dropout(0.25),\n    BatchNormalization(),\n    Dense(2, activation='softmax')\n])\n\ncnn1.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:11:22.585604Z","iopub.execute_input":"2021-12-10T20:11:22.585965Z","iopub.status.idle":"2021-12-10T20:11:22.699221Z","shell.execute_reply.started":"2021-12-10T20:11:22.585928Z","shell.execute_reply":"2021-12-10T20:11:22.698225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Network","metadata":{}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.001)\ncnn1.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:11:32.063609Z","iopub.execute_input":"2021-12-10T20:11:32.063886Z","iopub.status.idle":"2021-12-10T20:11:32.093837Z","shell.execute_reply.started":"2021-12-10T20:11:32.063855Z","shell.execute_reply":"2021-12-10T20:11:32.09281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Run 1","metadata":{}},{"cell_type":"code","source":"%%time \n\nh1 = cnn1.fit(\n    x = train_loader, \n    steps_per_epoch = TR_STEPS, \n    epochs = 15,\n    validation_data = valid_loader, \n    validation_steps = VA_STEPS, \n    verbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-10T20:11:37.274548Z","iopub.execute_input":"2021-12-10T20:11:37.274841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h1.history\nprint(history.keys())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch_range = range(1, len(history['loss'])+1)\n\nplt.figure(figsize=[14,4])\n\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()\n\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()\n\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()\n\nplt.tight_layout()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Run 2","metadata":{}},{"cell_type":"code","source":"tf.keras.backend.set_value(cnn1.optimizer.learning_rate, 0.0001)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh2 = cnn1.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_count":null,"outputs":[]},{"cell_type":"code","source":"for k in history.keys():\n    history[k] += h2.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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Run 3","metadata":{}},{"cell_type":"code","source":"tf.keras.backend.set_value(cnn1.optimizer.learning_rate, 0.00001)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh3 = cnn1.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_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_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model and History","metadata":{}},{"cell_type":"code","source":"cnn1.save('cancer_model_v02.h5')\npickle.dump(history, open(f'cancer_history_v02.pkl', 'wb'))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}