{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":11848,"databundleVersionId":862157,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport matplotlib.image as mpimg\nimport os\n\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.losses import categorical_crossentropy\nfrom tensorflow.keras.losses import sparse_categorical_crossentropy","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:33.502106Z","iopub.execute_input":"2024-12-10T15:59:33.502470Z","iopub.status.idle":"2024-12-10T15:59:45.288130Z","shell.execute_reply.started":"2024-12-10T15:59:33.502436Z","shell.execute_reply":"2024-12-10T15:59:45.287348Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv', dtype=str)\ntrain_img_dir = '/kaggle/input/histopathologic-cancer-detection/train'\ntest_img_dir = '/kaggle/input/histopathologic-cancer-detection/test'\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.289597Z","iopub.execute_input":"2024-12-10T15:59:45.290081Z","iopub.status.idle":"2024-12-10T15:59:45.635459Z","shell.execute_reply.started":"2024-12-10T15:59:45.290051Z","shell.execute_reply":"2024-12-10T15:59:45.634457Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head(15)\ntrain['fileName'] = train['id'] + '.tif'","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.636374Z","iopub.execute_input":"2024-12-10T15:59:45.636621Z","iopub.status.idle":"2024-12-10T15:59:45.672723Z","shell.execute_reply.started":"2024-12-10T15:59:45.636597Z","shell.execute_reply":"2024-12-10T15:59:45.671999Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head(15)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.674621Z","iopub.execute_input":"2024-12-10T15:59:45.674898Z","iopub.status.idle":"2024-12-10T15:59:45.688035Z","shell.execute_reply.started":"2024-12-10T15:59:45.674871Z","shell.execute_reply":"2024-12-10T15:59:45.687337Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"numberOfCatagories = train['label'].unique()\nprint(numberOfCatagories)\nnumberOfCatagories = len(numberOfCatagories)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.688996Z","iopub.execute_input":"2024-12-10T15:59:45.689351Z","iopub.status.idle":"2024-12-10T15:59:45.707459Z","shell.execute_reply.started":"2024-12-10T15:59:45.689321Z","shell.execute_reply":"2024-12-10T15:59:45.706542Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels, counts = np.unique(train['label'],return_counts=True)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.708522Z","iopub.execute_input":"2024-12-10T15:59:45.708771Z","iopub.status.idle":"2024-12-10T15:59:45.823298Z","shell.execute_reply.started":"2024-12-10T15:59:45.708747Z","shell.execute_reply":"2024-12-10T15:59:45.822455Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.bar(labels, counts)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:45.824284Z","iopub.execute_input":"2024-12-10T15:59:45.824533Z","iopub.status.idle":"2024-12-10T15:59:46.041425Z","shell.execute_reply.started":"2024-12-10T15:59:45.824509Z","shell.execute_reply":"2024-12-10T15:59:46.040573Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sampleSet = train.sample(5)\nfor idx, img_name in enumerate(sampleSet['fileName']):\n    img_path = os.path.join(train_img_dir, img_name)\n    img = Image.open(img_path)\n    print(img.size)\n    plt.subplot(1, len(sampleSet), idx+1)\n    plt.imshow(img)\n    plt.axis('off')\n    plt.title(f'lbl:{sampleSet.iloc[idx,1] }')\n    print()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:46.042691Z","iopub.execute_input":"2024-12-10T15:59:46.043042Z","iopub.status.idle":"2024-12-10T15:59:46.480981Z","shell.execute_reply.started":"2024-12-10T15:59:46.043003Z","shell.execute_reply":"2024-12-10T15:59:46.480023Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=1, stratify=train.label)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:46.482099Z","iopub.execute_input":"2024-12-10T15:59:46.482394Z","iopub.status.idle":"2024-12-10T15:59:46.690335Z","shell.execute_reply.started":"2024-12-10T15:59:46.482365Z","shell.execute_reply":"2024-12-10T15:59:46.689374Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:46.694139Z","iopub.execute_input":"2024-12-10T15:59:46.694453Z","iopub.status.idle":"2024-12-10T15:59:46.698689Z","shell.execute_reply.started":"2024-12-10T15:59:46.694409Z","shell.execute_reply":"2024-12-10T15:59:46.697784Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 64\nIMG_WIDTH = 96\nIMG_HEIGHT = 96\ntrain_path = train_img_dir\n\ntrain_loader = train_datagen.flow_from_dataframe(\n    dataframe = train_df,\n    directory = train_path,\n    x_col = 'fileName',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (IMG_WIDTH, IMG_HEIGHT)\n)\n\nvalid_loader = valid_datagen.flow_from_dataframe(\n    dataframe = valid_df,\n    directory = train_path,\n    x_col = 'fileName',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = 1,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (IMG_WIDTH, IMG_HEIGHT)\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T15:59:46.699702Z","iopub.execute_input":"2024-12-10T15:59:46.699955Z","iopub.status.idle":"2024-12-10T16:10:13.767203Z","shell.execute_reply.started":"2024-12-10T15:59:46.699930Z","shell.execute_reply":"2024-12-10T16:10:13.766529Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMG_SHAPE = (IMG_WIDTH, IMG_HEIGHT, 3)\n\nbaseModel = tf.keras.applications.ResNet50V2(\n    include_top=False,\n    weights='imagenet',\n    input_tensor=tf.keras.Input(shape=IMG_SHAPE)\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T16:10:13.768325Z","iopub.execute_input":"2024-12-10T16:10:13.769002Z","iopub.status.idle":"2024-12-10T16:10:16.474896Z","shell.execute_reply.started":"2024-12-10T16:10:13.768967Z","shell.execute_reply":"2024-12-10T16:10:16.474110Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"baseModel.trainable = False\n#baseModel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-12-10T16:10:16.476618Z","iopub.execute_input":"2024-12-10T16:10:16.477022Z","iopub.status.idle":"2024-12-10T16:10:16.484174Z","shell.execute_reply.started":"2024-12-10T16:10:16.476977Z","shell.execute_reply":"2024-12-10T16:10:16.483213Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cnn_model = Sequential([\n    baseModel,\n    Flatten(),\n    #Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=(IMG_WIDTH,IMG_HEIGHT,3)),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    Dense(numberOfCatagories, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-12-10T16:10:16.485456Z","iopub.execute_input":"2024-12-10T16:10:16.486115Z","iopub.status.idle":"2024-12-10T16:10:16.503112Z","shell.execute_reply.started":"2024-12-10T16:10:16.486070Z","shell.execute_reply":"2024-12-10T16:10:16.502249Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.001)\ncnn_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', 'AUC'])","metadata":{"execution":{"iopub.status.busy":"2024-12-10T16:10:16.504096Z","iopub.execute_input":"2024-12-10T16:10:16.504467Z","iopub.status.idle":"2024-12-10T16:10:16.519533Z","shell.execute_reply.started":"2024-12-10T16:10:16.504430Z","shell.execute_reply":"2024-12-10T16:10:16.518690Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#rescale=1/255,\n#rotation_range=20,  # Randomly rotate images by up to 20 degrees\n#width_shift_range=0.2,  # Randomly shift images horizontally by 20% of the width\n#height_shift_range=0.2, # Randomly shift images vertically by 20% of the height\n#shear_range=0.2,  # Apply random shearing transformations\n#zoom_range=0.2,  # Randomly zoom in or out by 20%\nh1 = False\ndef performImageAugmentation():\n    train_datagen = ImageDataGenerator(\n        rescale=1/255,\n        rotation_range=20, # Randomly rotate images by up to 20 degrees\n        horizontal_flip=True,  # Randomly flip images horizontally\n        vertical_flip=True,\n        fill_mode='nearest'  # Fill in any empty pixels resulting from transformations\n    )\n\n    train_loader = train_datagen.flow_from_dataframe(\n        dataframe = train_df,\n        directory = train_path,\n        x_col = 'fileName',\n        y_col = 'label',\n        batch_size = BATCH_SIZE,\n        seed = 1,\n        shuffle = True,\n        class_mode = 'categorical',\n        target_size = (IMG_WIDTH, IMG_HEIGHT)\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T16:10:16.520838Z","iopub.execute_input":"2024-12-10T16:10:16.521234Z","iopub.status.idle":"2024-12-10T16:10:16.527032Z","shell.execute_reply.started":"2024-12-10T16:10:16.521175Z","shell.execute_reply":"2024-12-10T16:10:16.526241Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_epochs = 10\nh1 = cnn_model.fit(\n    train_loader, \n    epochs=n_epochs,\n    validation_data=valid_loader,  \n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2024-12-10T16:10:16.528007Z","iopub.execute_input":"2024-12-10T16:10:16.528368Z","iopub.status.idle":"2024-12-10T17:20:03.573024Z","shell.execute_reply.started":"2024-12-10T16:10:16.528340Z","shell.execute_reply":"2024-12-10T17:20:03.572355Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"performImageAugmentation()\nn_epochs = 10\nh1 = cnn_model.fit(\n    train_loader, \n    epochs=n_epochs,\n    validation_data=valid_loader,  \n    verbose=1\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T17:20:03.576185Z","iopub.execute_input":"2024-12-10T17:20:03.576490Z","iopub.status.idle":"2024-12-10T18:07:18.525653Z","shell.execute_reply.started":"2024-12-10T17:20:03.576462Z","shell.execute_reply":"2024-12-10T18:07:18.524724Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plotHistory(history):\n    n_epochs = len(history['loss'])\n    \n    plt.figure(figsize=[10,4])\n    plt.subplot(1,3,1)\n    plt.plot(range(1, n_epochs+1), history['loss'], label='Training')\n    plt.plot(range(1, n_epochs+1), history['val_loss'], label='Validation')\n    plt.xlabel('Epoch'); plt.ylabel('Loss'); plt.title('Loss')\n    plt.legend()\n    \n    plt.subplot(1,3,2)\n    plt.plot(range(1, n_epochs+1), history['accuracy'], label='Training')\n    plt.plot(range(1, n_epochs+1), history['val_accuracy'], label='Validation')\n    plt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.title('Accuracy')\n    plt.legend()\n    \n    plt.subplot(1,3,3)\n    plt.plot(range(1, n_epochs+1), history['AUC'], label='Training')\n    plt.plot(range(1, n_epochs+1), history['val_AUC'], label='Validation')\n    plt.xlabel('Epoch'); plt.ylabel('AUC'); plt.title('AUC')\n    plt.legend()\n    \n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T18:07:18.527027Z","iopub.execute_input":"2024-12-10T18:07:18.527386Z","iopub.status.idle":"2024-12-10T18:07:18.534589Z","shell.execute_reply.started":"2024-12-10T18:07:18.527356Z","shell.execute_reply":"2024-12-10T18:07:18.533757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plotHistory(h1.history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T18:07:18.535687Z","iopub.execute_input":"2024-12-10T18:07:18.536020Z","iopub.status.idle":"2024-12-10T18:07:19.052836Z","shell.execute_reply.started":"2024-12-10T18:07:18.535982Z","shell.execute_reply":"2024-12-10T18:07:19.051939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"baseModel.trainable = True","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T18:07:19.054003Z","iopub.execute_input":"2024-12-10T18:07:19.054304Z","iopub.status.idle":"2024-12-10T18:07:19.061078Z","shell.execute_reply.started":"2024-12-10T18:07:19.054277Z","shell.execute_reply":"2024-12-10T18:07:19.060085Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.0000002)\ncnn_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', 'AUC'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T18:07:19.062061Z","iopub.execute_input":"2024-12-10T18:07:19.062466Z","iopub.status.idle":"2024-12-10T18:07:19.077416Z","shell.execute_reply.started":"2024-12-10T18:07:19.062419Z","shell.execute_reply":"2024-12-10T18:07:19.076609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"n_epochs = 70\nh1 = cnn_model.fit(\n    train_loader, \n    epochs=n_epochs,\n    validation_data=valid_loader,  \n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-10T18:07:19.078556Z","iopub.execute_input":"2024-12-10T18:07:19.078911Z","iopub.status.idle":"2024-12-11T01:32:40.947833Z","shell.execute_reply.started":"2024-12-10T18:07:19.078872Z","shell.execute_reply":"2024-12-11T01:32:40.947049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plotHistory(h1.history)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T01:32:40.962482Z","iopub.execute_input":"2024-12-11T01:32:40.962757Z","iopub.status.idle":"2024-12-11T01:32:41.516846Z","shell.execute_reply.started":"2024-12-11T01:32:40.962730Z","shell.execute_reply":"2024-12-11T01:32:41.516002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\ncnn_model.save('AM_histopathologic-cancer-detection_v01.h5')\npickle.dump(h1.history, open(f'histopathologic-cancer-detection_v01.pk1','wb'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-11T01:32:41.518160Z","iopub.execute_input":"2024-12-11T01:32:41.518858Z","iopub.status.idle":"2024-12-11T01:32:42.404135Z","shell.execute_reply.started":"2024-12-11T01:32:41.518816Z","shell.execute_reply":"2024-12-11T01:32:42.403344Z"}},"outputs":[],"execution_count":null}]}