{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#import packages\nimport os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport pickle\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import *\nfrom kerastuner.tuners import RandomSearch\nfrom kerastuner.engine.hyperparameters import HyperParameters","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-10T14:20:45.982384Z","iopub.execute_input":"2023-12-10T14:20:45.982723Z","iopub.status.idle":"2023-12-10T14:20:58.433872Z","shell.execute_reply.started":"2023-12-10T14:20:45.982697Z","shell.execute_reply":"2023-12-10T14:20:58.432927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train and test folder\nprint('Number of images in train set',len(os.listdir('../input/histopathologic-cancer-detection/train')))\nprint('Number of images in test set',len(os.listdir('../input/histopathologic-cancer-detection/test')))","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:20:58.436148Z","iopub.execute_input":"2023-12-10T14:20:58.437085Z","iopub.status.idle":"2023-12-10T14:21:02.254291Z","shell.execute_reply.started":"2023-12-10T14:20:58.437048Z","shell.execute_reply":"2023-12-10T14:21:02.253277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the training data into a DataFrame. \n# Print the shape of the resulting DataFrame.\n\nhcd = pd.read_csv('/kaggle/input/histopathologic-cancer-detection/train_labels.csv')\nprint(hcd.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:02.255641Z","iopub.execute_input":"2023-12-10T14:21:02.256044Z","iopub.status.idle":"2023-12-10T14:21:02.591056Z","shell.execute_reply.started":"2023-12-10T14:21:02.256010Z","shell.execute_reply":"2023-12-10T14:21:02.590091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first few rows of the dataframe.\nhcd.head() ","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:02.592272Z","iopub.execute_input":"2023-12-10T14:21:02.592558Z","iopub.status.idle":"2023-12-10T14:21:02.608488Z","shell.execute_reply.started":"2023-12-10T14:21:02.592534Z","shell.execute_reply":"2023-12-10T14:21:02.607500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label distrobution\n(hcd.label.value_counts() / len(hcd)).to_frame()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:02.611047Z","iopub.execute_input":"2023-12-10T14:21:02.611326Z","iopub.status.idle":"2023-12-10T14:21:02.628873Z","shell.execute_reply.started":"2023-12-10T14:21:02.611303Z","shell.execute_reply":"2023-12-10T14:21:02.627954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding a variable for the image directory\nimg_dir = '/kaggle/input/histopathologic-cancer-detection/train'\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:02.630130Z","iopub.execute_input":"2023-12-10T14:21:02.630703Z","iopub.status.idle":"2023-12-10T14:21:02.634719Z","shell.execute_reply.started":"2023-12-10T14:21:02.630677Z","shell.execute_reply":"2023-12-10T14:21:02.633825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = hcd.sample(n=9).reset_index()\n\nplt.figure(figsize=(3,3))\n\nfor i, row in sample.iterrows():\n\n    img = mpimg.imread(f'{img_dir}/{row.id}.tif')    \n    label = row.label\n\n    plt.subplot(3,3,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":"2023-12-10T14:21:02.635766Z","iopub.execute_input":"2023-12-10T14:21:02.636049Z","iopub.status.idle":"2023-12-10T14:21:03.223391Z","shell.execute_reply.started":"2023-12-10T14:21:02.636027Z","shell.execute_reply":"2023-12-10T14:21:03.221992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using data generators \ntrain_df, valid_df = train_test_split(hcd, test_size=0.2, random_state=39, stratify=hcd.label)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:03.224875Z","iopub.execute_input":"2023-12-10T14:21:03.226442Z","iopub.status.idle":"2023-12-10T14:21:03.342098Z","shell.execute_reply.started":"2023-12-10T14:21:03.226390Z","shell.execute_reply":"2023-12-10T14:21:03.341192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['id'] = train_df['id'] + '.tif'\nvalid_df['id'] = valid_df['id'] + '.tif'\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:03.343326Z","iopub.execute_input":"2023-12-10T14:21:03.343621Z","iopub.status.idle":"2023-12-10T14:21:03.402421Z","shell.execute_reply.started":"2023-12-10T14:21:03.343596Z","shell.execute_reply":"2023-12-10T14:21:03.401605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating Data Generators for CNN\ntrain_datagen = ImageDataGenerator(\n    rescale=1/255,\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\nvalid_datagen = ImageDataGenerator(rescale=1/255)\n\ntrain_df['label'] = train_df['label'].astype(str)\nvalid_df['label'] = valid_df['label'].astype(str)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=img_dir,\n    x_col='id',\n    y_col='label',\n    target_size=(96, 96),\n    batch_size=64,\n    class_mode='binary'\n)\n\nvalidation_generator = valid_datagen.flow_from_dataframe(\n    dataframe=valid_df,\n    directory=img_dir,\n    x_col='id',\n    y_col='label',\n    target_size=(96, 96),\n    batch_size=64,\n    class_mode='binary'\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:21:03.403660Z","iopub.execute_input":"2023-12-10T14:21:03.403978Z","iopub.status.idle":"2023-12-10T14:28:29.612635Z","shell.execute_reply.started":"2023-12-10T14:21:03.403949Z","shell.execute_reply":"2023-12-10T14:28:29.611634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_generator)\nVA_STEPS = len(validation_generator)\n\nprint('Number of batches in the training set:',TR_STEPS)\nprint('Number of batches in the validation set:',VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:28:29.614092Z","iopub.execute_input":"2023-12-10T14:28:29.614856Z","iopub.status.idle":"2023-12-10T14:28:29.620074Z","shell.execute_reply.started":"2023-12-10T14:28:29.614818Z","shell.execute_reply":"2023-12-10T14:28:29.619152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(hp):\n    model = Sequential()\n\n    # Convolutional layers\n    model.add(Conv2D(hp.Int('conv1_units', min_value=32, max_value=128, step=32), (3, 3), activation='relu', input_shape=(96, 96, 3)))\n    model.add(MaxPooling2D((2, 2)))\n\n    model.add(Conv2D(hp.Int('conv2_units', min_value=32, max_value=128, step=32), (3, 3), activation='relu'))\n    model.add(MaxPooling2D((2, 2)))\n\n    model.add(Conv2D(hp.Int('conv3_units', min_value=32, max_value=128, step=32), (3, 3), activation='relu'))\n    model.add(MaxPooling2D((2, 2)))\n\n    model.add(Flatten())\n\n    # Dense layers\n    model.add(Dense(hp.Int('dense_units', min_value=32, max_value=64, step=64), activation='relu'))\n\n    # Dropout layer\n    model.add(Dropout(hp.Float('dropout_rate', min_value=0.2, max_value=0.5, step=0.1)))\n\n    # Output layer\n    model.add(Dense(1, activation='sigmoid'))\n\n    # Compile the model\n    opt = tf.keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-1]))\n    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:28:29.621276Z","iopub.execute_input":"2023-12-10T14:28:29.621557Z","iopub.status.idle":"2023-12-10T14:28:29.644425Z","shell.execute_reply.started":"2023-12-10T14:28:29.621533Z","shell.execute_reply":"2023-12-10T14:28:29.643469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuner = RandomSearch(\n    build_model,\n    objective='val_accuracy',\n    max_trials=3,  \n    directory='HCD_tuner_dir',  \n    project_name='HCD_SB_6'\n)\n\ntuner.search(train_generator, epochs=5, validation_data=validation_generator, validation_steps=VA_STEPS)\n\n# Get the best hyperparameters\nbest_hps = tuner.get_best_hyperparameters(num_trials=1)[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-10T14:28:29.645531Z","iopub.execute_input":"2023-12-10T14:28:29.645822Z","iopub.status.idle":"2023-12-10T18:05:15.235791Z","shell.execute_reply.started":"2023-12-10T14:28:29.645798Z","shell.execute_reply":"2023-12-10T18:05:15.234963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model = tuner.hypermodel.build(best_hps)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T18:05:15.239644Z","iopub.execute_input":"2023-12-10T18:05:15.239961Z","iopub.status.idle":"2023-12-10T18:05:15.337713Z","shell.execute_reply.started":"2023-12-10T18:05:15.239934Z","shell.execute_reply":"2023-12-10T18:05:15.336818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"h1 = final_model.fit(\n    train_generator,\n    steps_per_epoch=TR_STEPS,\n    epochs=3,\n    validation_data=validation_generator,\n    validation_steps=VA_STEPS,\n    verbose=1\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-10T18:05:15.338676Z","iopub.execute_input":"2023-12-10T18:05:15.338964Z","iopub.status.idle":"2023-12-10T18:46:27.848859Z","shell.execute_reply.started":"2023-12-10T18:05:15.338938Z","shell.execute_reply":"2023-12-10T18:46:27.847895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = h1.history\nepoch_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['accuracy'], label='Training')\nplt.plot(epoch_range, history['val_accuracy'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.title('Accuracy')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-10T18:46:27.850432Z","iopub.execute_input":"2023-12-10T18:46:27.851368Z","iopub.status.idle":"2023-12-10T18:46:28.493764Z","shell.execute_reply.started":"2023-12-10T18:46:27.851331Z","shell.execute_reply":"2023-12-10T18:46:28.492831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\nfinal_model.save('HCD_SB_6_3_Model.h5')\n\n# Save the history\nwith open('HCD_SB_6_3_Model_history.pkl', 'wb') as file:\n    pickle.dump(h1.history, file)","metadata":{"execution":{"iopub.status.busy":"2023-12-10T18:46:28.495052Z","iopub.execute_input":"2023-12-10T18:46:28.495360Z","iopub.status.idle":"2023-12-10T18:46:28.545698Z","shell.execute_reply.started":"2023-12-10T18:46:28.495335Z","shell.execute_reply":"2023-12-10T18:46:28.544795Z"},"trusted":true},"execution_count":null,"outputs":[]}]}