{"metadata":{"colab":{"provenance":[{"file_id":"1MKqvgS-2TO3WjinW4eHuYI8kcnl3D73b","timestamp":1716772190300}],"gpuType":"T4"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU","kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":39272,"databundleVersionId":4629629,"sourceType":"competition"},{"sourceId":4619805,"sourceType":"datasetVersion","datasetId":2688675}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/drive', force_remount = True)","metadata":{"id":"nudUJ-mO0meG","executionInfo":{"status":"ok","timestamp":1716775346246,"user_tz":-330,"elapsed":39036,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"9f37f511-3931-45e5-c605-d129aace8f1b"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /content/drive/MyDrive/Colab Notebooks/Project\n%pwd","metadata":{"id":"frFNjt590oUe","executionInfo":{"status":"ok","timestamp":1716775347128,"user_tz":-330,"elapsed":886,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"7a6880b4-daed-45b5-af19-39eaac3df8d9"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydicom --quiet","metadata":{"id":"WsZOXjiMmFHn","executionInfo":{"status":"ok","timestamp":1716775353700,"user_tz":-330,"elapsed":6578,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"ad59d93c-6b87-43ca-f549-b193c1267a97"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import (Conv2D,\n                                     MaxPool2D,\n                                     BatchNormalization,\n                                     Dense,\n                                     Dropout,\n                                     GlobalMaxPool2D)\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport cv2\nimport os\nimport glob\nfrom tqdm import tqdm\nimport pydicom\nimport pandas as pd","metadata":{"id":"gBM1Zo2e66bY"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(img_height, img_width, folder_path):\n  normal_paths = glob.glob(folder_path + '/' + 'normal' + '/' + '*.png')\n  cancer_paths = glob.glob(folder_path + '/' + 'cancer' + '/' + '*.png')\n  total_images = len(normal_paths)+len(cancer_paths)\n  X_train = np.zeros((total_images, img_height, img_width), dtype=np.float32)\n  Y_train = np.zeros((total_images, 1))\n\n  for n, path in tqdm(enumerate(normal_paths)):\n    image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    image = cv2.resize(image, (img_height, img_width))\n    X_train[n] = image / 255\n    Y_train[n] = 0\n  for n, path in tqdm(enumerate(cancer_paths)):\n    image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    image = cv2.resize(image, (img_height, img_width))\n    X_train[n] = image / 255\n    Y_train[n] = 1\n\n  Y_train = np.expand_dims(Y_train, axis=-1)\n  return X_train, Y_train","metadata":{"id":"00EkG-tfwub7"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/content/drive/MyDrive/Colab Notebooks/Project/val/normal/10439_1825831835.png', cv2.IMREAD_GRAYSCALE)\nprint(img.shape)\nplt.imshow(img, 'Greys_r')","metadata":{"id":"SIwqyaLE0W9H","executionInfo":{"status":"ok","timestamp":1716775362766,"user_tz":-330,"elapsed":4193,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"0511a965-3426-481f-ec22-09e3e57494e6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, Y_train = load_data(img_height=512, img_width=512, folder_path=\"train\")","metadata":{"id":"6znafFDw2fsN","executionInfo":{"status":"ok","timestamp":1716775673688,"user_tz":-330,"elapsed":310928,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"9565e278-2665-45c1-9847-3567c5048f79"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, 5, padding = 'same', activation ='relu', input_shape = (512, 512, 1)))\nmodel.add(Conv2D(64, 5, padding = 'same',  activation = 'relu'))\nmodel.add(Conv2D(64, 5, padding = 'same', activation = 'relu'))\nmodel.add(MaxPool2D())\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(64, 5, padding = 'same', activation = 'relu'))\nmodel.add(Conv2D(128, 5, padding = 'same', activation = 'relu'))\nmodel.add(Conv2D(128, 5, padding = 'same', activation = 'relu'))\nmodel.add(MaxPool2D())\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(128, 5, padding = 'same', activation = 'relu'))\nmodel.add(Conv2D(256, 5, padding = 'same', activation = 'relu'))\nmodel.add(Conv2D(256, 5, padding = 'same', activation = 'relu'))\nmodel.add(GlobalMaxPool2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(1, activation='sigmoid'))\nmodel.compile(optimizer = Adam(1e-4),\n              loss='binary_crossentropy',\n              metrics = [tf.keras.metrics.BinaryAccuracy(),\n                         tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])\n\nmodel.summary()","metadata":{"id":"mUxme41K9uXW","executionInfo":{"status":"ok","timestamp":1716777943824,"user_tz":-330,"elapsed":503,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"1e5fed45-70db-4666-d731-ddeefdd15e58"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val, Y_val = load_data(512, 512, folder_path='val')","metadata":{"id":"xEXgJ1GpFPlH","executionInfo":{"status":"ok","timestamp":1716775777929,"user_tz":-330,"elapsed":102928,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"b4ebbc19-fd33-4ed6-9ac7-302cb761fe84"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stop = tf.keras.callbacks.EarlyStopping(patience = 5, restore_best_weights = True, verbose = 1)\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(factor = 0.5, patience = 5, verbose = 1)","metadata":{"id":"dNTmGAKdFkKF"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train,\n                    batch_size = 16,\n                    epochs = 10,\n                    validation_data = (X_val, Y_val),\n                    callbacks = [reduce_lr, early_stop])","metadata":{"id":"TEHpwaC4F89R","executionInfo":{"status":"ok","timestamp":1716779477154,"user_tz":-330,"elapsed":1527208,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"92e85a19-f3cf-4592-c44a-7dd68797e1ef"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to save entire model\nmodel.save('final_model.keras')","metadata":{"id":"jTysKwSiNT-y"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# to load whole model\nmodel = keras.saving.load_model('final_model.keras')","metadata":{"id":"PIi4W_IiN46l"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('test.csv')\ntest_df.head(4)","metadata":{"id":"GCiLMy5KneCJ","executionInfo":{"status":"ok","timestamp":1716779477154,"user_tz":-330,"elapsed":17,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"c5fa06f2-741a-4aee-e49e-593294de32bb"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(8, 8))\nrows = 4\ncolumns = 4\n\nfor i in range(1, len(test_df)+1):\n  path = str(test_df.loc[i-1, 'image_id'])+\".dcm\"\n  img = pydicom.dcmread(path)\n  image = img.pixel_array\n  image = cv2.resize(image, (512, 512))\n  image = (image/255).astype(np.float32)\n  input = np.expand_dims(image, axis=-1)\n  input = np.expand_dims(input, axis=0)\n  print(input.shape)\n  fig.add_subplot(rows, columns, i)\n  plt.imshow(image, cmap=\"Greys_r\")\n  print(path, 'prediction:', model.predict(input))\nplt.show()","metadata":{"id":"U4EOk7rpolHe","executionInfo":{"status":"ok","timestamp":1716779670972,"user_tz":-330,"elapsed":10782,"user":{"displayName":"Komal Kesav","userId":"09853558731117127269"}},"outputId":"0fa59e96-79cc-4d04-8cdc-4f839c16e880"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from google.colab import runtime\nruntime.unassign()","metadata":{"id":"7Jw2-bXtHVdw"},"execution_count":null,"outputs":[]}]}