{"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":30747,"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\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:05:22.541586Z","iopub.execute_input":"2024-07-30T15:05:22.541939Z","iopub.status.idle":"2024-07-30T15:05:35.292865Z","shell.execute_reply.started":"2024-07-30T15:05:22.541908Z","shell.execute_reply":"2024-07-30T15:05:35.292016Z"},"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-30T15:05:35.294354Z","iopub.execute_input":"2024-07-30T15:05:35.294895Z","iopub.status.idle":"2024-07-30T15:05:36.101794Z","shell.execute_reply.started":"2024-07-30T15:05:35.294868Z","shell.execute_reply":"2024-07-30T15:05:36.100982Z"},"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-30T15:05:36.102894Z","iopub.execute_input":"2024-07-30T15:05:36.103210Z","iopub.status.idle":"2024-07-30T15:05:36.421951Z","shell.execute_reply.started":"2024-07-30T15:05:36.103185Z","shell.execute_reply":"2024-07-30T15:05:36.421042Z"},"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-30T15:05:36.424703Z","iopub.execute_input":"2024-07-30T15:05:36.425164Z","iopub.status.idle":"2024-07-30T15:05:36.429986Z","shell.execute_reply.started":"2024-07-30T15:05:36.425128Z","shell.execute_reply":"2024-07-30T15:05:36.428979Z"},"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 = (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)\n   ","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:05:36.431116Z","iopub.execute_input":"2024-07-30T15:05:36.431378Z","iopub.status.idle":"2024-07-30T15:13:15.094168Z","shell.execute_reply.started":"2024-07-30T15:05:36.431355Z","shell.execute_reply":"2024-07-30T15:13:15.093383Z"},"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-30T15:13:15.095616Z","iopub.execute_input":"2024-07-30T15:13:15.095900Z","iopub.status.idle":"2024-07-30T15:13:15.100593Z","shell.execute_reply.started":"2024-07-30T15:13:15.095876Z","shell.execute_reply":"2024-07-30T15:13:15.099794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.MobileNetV2(\n    input_shape = (96,96,3),\n    include_top = False,\n    weights = 'imagenet'\n)\n\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:13:15.101774Z","iopub.execute_input":"2024-07-30T15:13:15.102139Z","iopub.status.idle":"2024-07-30T15:13:17.622274Z","shell.execute_reply.started":"2024-07-30T15:13:15.102115Z","shell.execute_reply":"2024-07-30T15:13:17.621304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Network","metadata":{}},{"cell_type":"code","source":"cnn = Sequential([\n    base_model,\n    Flatten(),\n    Dense(256, activation='relu'),\n    Dense(2, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:13:17.623739Z","iopub.execute_input":"2024-07-30T15:13:17.624248Z","iopub.status.idle":"2024-07-30T15:13:17.636159Z","shell.execute_reply.started":"2024-07-30T15:13:17.624208Z","shell.execute_reply":"2024-07-30T15:13:17.635108Z"},"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-30T15:13:17.637540Z","iopub.execute_input":"2024-07-30T15:13:17.637852Z","iopub.status.idle":"2024-07-30T15:13:17.659003Z","shell.execute_reply.started":"2024-07-30T15:13:17.637825Z","shell.execute_reply":"2024-07-30T15:13:17.658105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    EarlyStopping(\n    monitor=\"val_AUC\",\n    min_delta=0, \n    patience=3,\n    verbose=1,\n    mode=\"max\")\n]\n\nnp.random.seed(1)\ntf.random.set_seed(1)\n\n\nh1 = 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-30T15:13:17.662465Z","iopub.execute_input":"2024-07-30T15:13:17.662751Z","iopub.status.idle":"2024-07-30T15:45:58.762303Z","shell.execute_reply.started":"2024-07-30T15:13:17.662727Z","shell.execute_reply":"2024-07-30T15:45:58.761396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h1.history\nprint(history.keys())","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:45:58.763661Z","iopub.execute_input":"2024-07-30T15:45:58.763931Z","iopub.status.idle":"2024-07-30T15:45:58.768645Z","shell.execute_reply.started":"2024-07-30T15:45:58.763908Z","shell.execute_reply":"2024-07-30T15:45:58.767721Z"},"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-30T15:45:58.769775Z","iopub.execute_input":"2024-07-30T15:45:58.770101Z","iopub.status.idle":"2024-07-30T15:45:59.529840Z","shell.execute_reply.started":"2024-07-30T15:45:58.770078Z","shell.execute_reply":"2024-07-30T15:45:59.529012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Run 2 ","metadata":{}},{"cell_type":"code","source":"base_model.trainable = False\nopt = tf.keras.optimizers.Adam(learning_rate=0.0001)","metadata":{"execution":{"iopub.status.busy":"2024-07-30T15:45:59.530900Z","iopub.execute_input":"2024-07-30T15:45:59.531202Z","iopub.status.idle":"2024-07-30T15:45:59.541957Z","shell.execute_reply.started":"2024-07-30T15:45:59.531177Z","shell.execute_reply":"2024-07-30T15:45:59.541104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    EarlyStopping(\n    monitor=\"val_AUC\",\n    min_delta=0, \n    patience=3,\n    verbose=1,\n    mode=\"max\")\n]\n\nnp.random.seed(1)\ntf.random.set_seed(1)\n\nh2 = 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-30T15:45:59.543081Z","iopub.execute_input":"2024-07-30T15:45:59.543337Z","iopub.status.idle":"2024-07-30T16:05:54.550981Z","shell.execute_reply.started":"2024-07-30T15:45:59.543315Z","shell.execute_reply":"2024-07-30T16:05:54.550021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h2.history\nprint(history.keys())","metadata":{"execution":{"iopub.status.busy":"2024-07-30T16:05:54.552445Z","iopub.execute_input":"2024-07-30T16:05:54.552756Z","iopub.status.idle":"2024-07-30T16:05:54.557629Z","shell.execute_reply.started":"2024-07-30T16:05:54.552721Z","shell.execute_reply":"2024-07-30T16:05:54.556577Z"},"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-30T16:05:54.560934Z","iopub.execute_input":"2024-07-30T16:05:54.561578Z","iopub.status.idle":"2024-07-30T16:05:55.164908Z","shell.execute_reply.started":"2024-07-30T16:05:54.561544Z","shell.execute_reply":"2024-07-30T16:05:55.163992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save('Histopath_model_v10.h5')\npickle.dump(history, open(f'Histopath_history_v10.pk1', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2024-07-30T16:05:55.166158Z","iopub.execute_input":"2024-07-30T16:05:55.166453Z","iopub.status.idle":"2024-07-30T16:05:55.522933Z","shell.execute_reply.started":"2024-07-30T16:05:55.166427Z","shell.execute_reply":"2024-07-30T16:05:55.521983Z"},"trusted":true},"execution_count":null,"outputs":[]}]}