{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Packages","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport pickle\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:18:53.658040Z","iopub.execute_input":"2024-07-18T18:18:53.658899Z","iopub.status.idle":"2024-07-18T18:19:08.266315Z","shell.execute_reply.started":"2024-07-18T18:18:53.658865Z","shell.execute_reply":"2024-07-18T18:19:08.265304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Training DataFrames ","metadata":{}},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\ntrain_path = ('/kaggle/input/histopathologic-cancer-detection/train/')\nfilename = 'id'\ntrain_image_path = train_labels['id'].apply(lambda x: os.path.join(train_path, x + '.tif'))","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:19:08.268453Z","iopub.execute_input":"2024-07-18T18:19:08.269619Z","iopub.status.idle":"2024-07-18T18:19:09.136592Z","shell.execute_reply.started":"2024-07-18T18:19:08.269580Z","shell.execute_reply":"2024-07-18T18:19:09.135751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train_labels, test_size=0.2, stratify=train_labels['label'], random_state=1)\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:19:09.137911Z","iopub.execute_input":"2024-07-18T18:19:09.138287Z","iopub.status.idle":"2024-07-18T18:19:09.483374Z","shell.execute_reply.started":"2024-07-18T18:19:09.138258Z","shell.execute_reply":"2024-07-18T18:19:09.482385Z"},"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":"2024-07-18T18:19:09.486193Z","iopub.execute_input":"2024-07-18T18:19:09.486971Z","iopub.status.idle":"2024-07-18T18:19:09.491381Z","shell.execute_reply.started":"2024-07-18T18:19:09.486937Z","shell.execute_reply":"2024-07-18T18:19:09.490373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 256\n\ntrain_df['id'] = train_df['id'].apply(lambda x: x + '.tif')\nvalid_df['id'] = valid_df['id'].apply(lambda x: x + '.tif')\n\ntrain_loader = 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    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 = 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 = (32,32)\n)\n   ","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:19:09.492567Z","iopub.execute_input":"2024-07-18T18:19:09.492937Z","iopub.status.idle":"2024-07-18T18:31:16.389187Z","shell.execute_reply.started":"2024-07-18T18:19:09.492911Z","shell.execute_reply":"2024-07-18T18:31:16.388379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_loader)//BATCH_SIZE\nVA_STEPS = len(valid_loader)//BATCH_SIZE\n\nprint(TR_STEPS)\nprint(VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:31:16.390361Z","iopub.execute_input":"2024-07-18T18:31:16.390673Z","iopub.status.idle":"2024-07-18T18:31:16.395904Z","shell.execute_reply.started":"2024-07-18T18:31:16.390643Z","shell.execute_reply":"2024-07-18T18:31:16.395073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Network","metadata":{}},{"cell_type":"code","source":"np.random.seed(1)\ntf.random.set_seed(1)\n\ncnn = Sequential([\n    Conv2D(filters=32, kernel_size=(3,3), activation = 'relu', padding = 'same', input_shape=(32,32,3)),\n    Conv2D(filters=32, kernel_size=(3,3), activation = 'relu', padding = 'same'),\n    MaxPooling2D(2,2),\n    Dropout(0.5),\n    BatchNormalization(),\n\n    Conv2D(filters=64, kernel_size=(3,3), activation = 'relu', padding = 'same'),\n    Conv2D(filters=64, kernel_size=(3,3), activation = 'relu', padding = 'same'),\n    MaxPooling2D(2,2),\n    Dropout(0.5),\n    BatchNormalization(),\n    \n    Conv2D(filters=128, kernel_size=(3,3), activation = 'relu', padding = 'same'),\n    Conv2D(filters=128, kernel_size=(3,3), activation = 'relu', padding = 'same'),\n    MaxPooling2D(2,2),\n    Dropout(0.5),\n    BatchNormalization(),\n\n    Flatten(),\n    \n    Dense(256, activation='relu'),\n    Dropout(0.5),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    BatchNormalization(),\n    Dense(2, activation='softmax')\n])\n\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:31:16.396921Z","iopub.execute_input":"2024-07-18T18:31:16.397177Z","iopub.status.idle":"2024-07-18T18:31:17.269825Z","shell.execute_reply.started":"2024-07-18T18:31:16.397157Z","shell.execute_reply":"2024-07-18T18:31:17.268869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Network","metadata":{}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=0.001)\ncnn.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['AUC'])","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:31:17.270983Z","iopub.execute_input":"2024-07-18T18:31:17.271267Z","iopub.status.idle":"2024-07-18T18:31:17.286783Z","shell.execute_reply.started":"2024-07-18T18:31:17.271243Z","shell.execute_reply":"2024-07-18T18:31:17.285872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h1 = cnn.fit(\nx = train_loader, \nsteps_per_epoch = TR_STEPS, \nepochs = 20, \nvalidation_data = valid_loader, \nvalidation_steps = VA_STEPS, \nverbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:31:17.287930Z","iopub.execute_input":"2024-07-18T18:31:17.288204Z","iopub.status.idle":"2024-07-18T18:57:15.663946Z","shell.execute_reply.started":"2024-07-18T18:31:17.288181Z","shell.execute_reply":"2024-07-18T18:57:15.662954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h1.history\nprint(history.keys())","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:57:15.667991Z","iopub.execute_input":"2024-07-18T18:57:15.668307Z","iopub.status.idle":"2024-07-18T18:57:15.673755Z","shell.execute_reply.started":"2024-07-18T18:57:15.668280Z","shell.execute_reply":"2024-07-18T18:57:15.672564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch_range = range(1, len(history['loss'])+1)\n\nplt.figure(figsize=[14,4])\nplt.subplot(1,2,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,2,2)\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":"2024-07-18T18:57:15.675197Z","iopub.execute_input":"2024-07-18T18:57:15.675570Z","iopub.status.idle":"2024-07-18T18:57:16.417493Z","shell.execute_reply.started":"2024-07-18T18:57:15.675537Z","shell.execute_reply":"2024-07-18T18:57:16.416480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Run 2 ","metadata":{}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(learning_rate=0.0001)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:57:16.418722Z","iopub.execute_input":"2024-07-18T18:57:16.419018Z","iopub.status.idle":"2024-07-18T18:57:16.426891Z","shell.execute_reply.started":"2024-07-18T18:57:16.418993Z","shell.execute_reply":"2024-07-18T18:57:16.425772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h2 = cnn.fit(\nx = train_loader, \nsteps_per_epoch = TR_STEPS, \nepochs = 30, \nvalidation_data = valid_loader, \nvalidation_steps = VA_STEPS, \nverbose = 1\n)","metadata":{"execution":{"iopub.status.busy":"2024-07-18T18:57:16.428073Z","iopub.execute_input":"2024-07-18T18:57:16.428823Z","iopub.status.idle":"2024-07-18T19:28:09.406040Z","shell.execute_reply.started":"2024-07-18T18:57:16.428789Z","shell.execute_reply":"2024-07-18T19:28:09.405022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h2.history\nprint(history.keys())","metadata":{"execution":{"iopub.status.busy":"2024-07-18T19:28:09.408400Z","iopub.execute_input":"2024-07-18T19:28:09.408745Z","iopub.status.idle":"2024-07-18T19:28:09.413723Z","shell.execute_reply.started":"2024-07-18T19:28:09.408719Z","shell.execute_reply":"2024-07-18T19:28:09.412732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch_range = range(1, len(history['loss'])+1)\n\nplt.figure(figsize=[14,4])\nplt.subplot(1,2,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,2,2)\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":"2024-07-18T19:28:09.415131Z","iopub.execute_input":"2024-07-18T19:28:09.415809Z","iopub.status.idle":"2024-07-18T19:28:10.065439Z","shell.execute_reply.started":"2024-07-18T19:28:09.415774Z","shell.execute_reply":"2024-07-18T19:28:10.064408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save('Histopath_model_v01.h5')\npickle.dump(history, open(f'Histopath_history_v01.pk1', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2024-07-18T19:28:10.066810Z","iopub.execute_input":"2024-07-18T19:28:10.067128Z","iopub.status.idle":"2024-07-18T19:28:10.176260Z","shell.execute_reply.started":"2024-07-18T19:28:10.067100Z","shell.execute_reply":"2024-07-18T19:28:10.175163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}