{"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":"none","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-27T14:44:42.061357Z","iopub.execute_input":"2024-06-27T14:44:42.061768Z","iopub.status.idle":"2024-06-27T14:44:42.556462Z","shell.execute_reply.started":"2024-06-27T14:44:42.061735Z","shell.execute_reply":"2024-06-27T14:44:42.555154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport random\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:44:42.558724Z","iopub.execute_input":"2024-06-27T14:44:42.559291Z","iopub.status.idle":"2024-06-27T14:44:59.898464Z","shell.execute_reply.started":"2024-06-27T14:44:42.559255Z","shell.execute_reply":"2024-06-27T14:44:59.896901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\nwith ZipFile(\"../input/data-science-bowl-2018/stage1_train.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"./stage1_train\")\n    \nwith ZipFile(\"../input/data-science-bowl-2018/stage1_test.zip\",\"r\") as zip_ref:\n    zip_ref.extractall(\"./stage1_test\")","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:44:59.900106Z","iopub.execute_input":"2024-06-27T14:44:59.900878Z","iopub.status.idle":"2024-06-27T14:45:06.598117Z","shell.execute_reply.started":"2024-06-27T14:44:59.900815Z","shell.execute_reply":"2024-06-27T14:45:06.596763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 42\nnp.random.seed = seed\n\nIMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3\n\nTRAIN_PATH = 'stage1_train/'\nTEST_PATH = 'stage1_test/'\n\ntrain_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]\n\nX = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\ny = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool_)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:45:06.600152Z","iopub.execute_input":"2024-06-27T14:45:06.600543Z","iopub.status.idle":"2024-06-27T14:45:06.610605Z","shell.execute_reply.started":"2024-06-27T14:45:06.600512Z","shell.execute_reply":"2024-06-27T14:45:06.609287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm \n\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:45:06.614118Z","iopub.execute_input":"2024-06-27T14:45:06.614555Z","iopub.status.idle":"2024-06-27T14:45:07.613967Z","shell.execute_reply.started":"2024-06-27T14:45:06.614523Z","shell.execute_reply":"2024-06-27T14:45:07.612553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Resizing training images and masks')\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):   \n    path = TRAIN_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]  \n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    X[n] = img  #Fill empty X_train with values from img\n    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n    for mask_file in next(os.walk(path + '/masks/'))[2]:\n        mask_ = imread(path + '/masks/' + mask_file)\n        mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant',  \n                                      preserve_range=True), axis=-1)\n        mask = np.maximum(mask, mask_)  \n            \n    y[n] = mask ","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:45:07.615368Z","iopub.execute_input":"2024-06-27T14:45:07.616092Z","iopub.status.idle":"2024-06-27T14:50:47.934682Z","shell.execute_reply.started":"2024-06-27T14:45:07.616053Z","shell.execute_reply":"2024-06-27T14:50:47.933507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test images\ntest_images = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nsizes_test = []\nprint('Resizing test images') \nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TEST_PATH + id_\n    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]\n    sizes_test.append([img.shape[0], img.shape[1]])\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    test_images[n] = img\n\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:50:47.936190Z","iopub.execute_input":"2024-06-27T14:50:47.936581Z","iopub.status.idle":"2024-06-27T14:50:49.681468Z","shell.execute_reply.started":"2024-06-27T14:50:47.936549Z","shell.execute_reply":"2024-06-27T14:50:49.680084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:51:56.733388Z","iopub.execute_input":"2024-06-27T14:51:56.734116Z","iopub.status.idle":"2024-06-27T14:51:56.753226Z","shell.execute_reply.started":"2024-06-27T14:51:56.734082Z","shell.execute_reply":"2024-06-27T14:51:56.751809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_x = random.randint(0, len(X_train))\nplt.axis(\"off\")\nimshow(X_train[image_x])\nplt.show()\nplt.axis(\"off\")\nimshow(np.squeeze(y_train[image_x]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:52:09.645394Z","iopub.execute_input":"2024-06-27T14:52:09.645817Z","iopub.status.idle":"2024-06-27T14:52:09.930144Z","shell.execute_reply.started":"2024-06-27T14:52:09.645786Z","shell.execute_reply":"2024-06-27T14:52:09.928086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 1","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:50:50.168906Z","iopub.status.idle":"2024-06-27T14:50:50.169500Z","shell.execute_reply.started":"2024-06-27T14:50:50.169209Z","shell.execute_reply":"2024-06-27T14:50:50.169233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MODEL ##","metadata":{}},{"cell_type":"code","source":"inputs=tf.keras.layers.Input((IMG_HEIGHT,IMG_WIDTH,IMG_CHANNELS))\ns=tf.keras.layers.Lambda(lambda x:x/255)(inputs)\n\nc1= tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(s)\nc1=tf.keras.layers.Dropout(0.1)(c1)\nc1= tf.keras.layers.Conv2D(16,(3,3),activation='relu',kernel_initializer='he_normal',padding='same')(c1)\np1= tf.keras.layers.MaxPooling2D(2,2)(c1)\n\nc2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)\nc2 = tf.keras.layers.Dropout(0.1)(c2)\nc2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)\np2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)\n \nc3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)\nc3 = tf.keras.layers.Dropout(0.2)(c3)\nc3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)\np3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)\n \nc4 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)\nc4 = tf.keras.layers.Dropout(0.2)(c4)\nc4 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c4)\np4 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c4)\n \nc5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p4)\nc5 = tf.keras.layers.Dropout(0.3)(c5)\nc5 = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c5)\n\n#Expansive path \nu6 = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(c5)\nu6 = tf.keras.layers.concatenate([u6, c4])\nc6 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u6)\nc6 = tf.keras.layers.Dropout(0.2)(c6)\nc6 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c6)\n \nu7 = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(c6)\nu7 = tf.keras.layers.concatenate([u7, c3])\nc7 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u7)\nc7 = tf.keras.layers.Dropout(0.2)(c7)\nc7 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c7)\n\nu8 = tf.keras.layers.Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(c7)\nu8 = tf.keras.layers.concatenate([u8, c2])\nc8 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u8)\nc8 = tf.keras.layers.Dropout(0.1)(c8)\nc8 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c8)\n \nu9 = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(c8)\nu9 = tf.keras.layers.concatenate([u9, c1], axis=3)\nc9 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(u9)\nc9 = tf.keras.layers.Dropout(0.1)(c9)\nc9 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c9)\n \noutputs = tf.keras.layers.Conv2D(1, (1, 1), activation='sigmoid')(c9)\n \nmodel = tf.keras.Model(inputs=[inputs], outputs=[outputs])\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:21:38.084237Z","iopub.execute_input":"2024-06-27T15:21:38.084732Z","iopub.status.idle":"2024-06-27T15:21:38.595841Z","shell.execute_reply.started":"2024-06-27T15:21:38.084699Z","shell.execute_reply":"2024-06-27T15:21:38.594672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpointer= tf.keras.callbacks.ModelCheckpoint('Model_for_Nuclei.keras',verbose=1,save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:32:54.069802Z","iopub.execute_input":"2024-06-27T15:32:54.070668Z","iopub.status.idle":"2024-06-27T15:32:54.076350Z","shell.execute_reply.started":"2024-06-27T15:32:54.070632Z","shell.execute_reply":"2024-06-27T15:32:54.074979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n        tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),\n        tf.keras.callbacks.TensorBoard(log_dir='logs')]\n\nresults=model.fit(X_train, y_train, validation_data=(X_test,y_test), batch_size=16, epochs=25, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:38:28.271955Z","iopub.execute_input":"2024-06-27T15:38:28.272486Z","iopub.status.idle":"2024-06-27T15:53:15.702109Z","shell.execute_reply.started":"2024-06-27T15:38:28.272446Z","shell.execute_reply":"2024-06-27T15:53:15.700889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = model.history.history['loss']\nval_loss = model.history.history['val_loss']\n\nplt.figure()\nplt.plot( loss, 'r', label='Training loss')\nplt.plot( val_loss, 'bo', label='Validation loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss Value')\nplt.ylim([0, 1])\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:57:29.185973Z","iopub.execute_input":"2024-06-27T15:57:29.186924Z","iopub.status.idle":"2024-06-27T15:57:29.524889Z","shell.execute_reply.started":"2024-06-27T15:57:29.186883Z","shell.execute_reply":"2024-06-27T15:57:29.523603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = model.history.history['accuracy']\nval_accuracy = model.history.history['val_accuracy']\n\nplt.figure()\nplt.plot( accuracy, 'r', label='Training accuracy')\nplt.plot( val_accuracy, 'bo', label='Validation accuracy')\nplt.title('Training and Validation accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Loss Value')\nplt.ylim([0, 1])\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:57:34.315635Z","iopub.execute_input":"2024-06-27T15:57:34.316065Z","iopub.status.idle":"2024-06-27T15:57:34.659990Z","shell.execute_reply.started":"2024-06-27T15:57:34.316032Z","shell.execute_reply":"2024-06-27T15:57:34.658681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\n\ndef display(display_list):\n    plt.figure(figsize=(15, 15))\n    title = ['Input image', 'True mask', 'Predicted mask']\n    for i in range(len(display_list)):\n        plt.subplot(1, len(display_list), i+1)\n        plt.title(title[i])\n        plt.imshow(tf.keras.utils.array_to_img(display_list[i]))\n        plt.axis('off')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:58:01.462653Z","iopub.execute_input":"2024-06-27T15:58:01.463088Z","iopub.status.idle":"2024-06-27T15:58:01.471161Z","shell.execute_reply.started":"2024-06-27T15:58:01.463055Z","shell.execute_reply":"2024-06-27T15:58:01.470037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = random.randint(0, len(X_test))\nsample_image = X_test[i]\nsample_mask = y_test[i]","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:59:09.599240Z","iopub.execute_input":"2024-06-27T15:59:09.599680Z","iopub.status.idle":"2024-06-27T15:59:09.605602Z","shell.execute_reply.started":"2024-06-27T15:59:09.599647Z","shell.execute_reply":"2024-06-27T15:59:09.604356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(sample_image[tf.newaxis, ...])[0]\npredicted_mask = (prediction > 0.5).astype(np.uint8)\ndisplay([sample_image, sample_mask,predicted_mask])","metadata":{"execution":{"iopub.status.busy":"2024-06-27T15:59:16.235376Z","iopub.execute_input":"2024-06-27T15:59:16.235801Z","iopub.status.idle":"2024-06-27T15:59:16.794911Z","shell.execute_reply.started":"2024-06-27T15:59:16.235769Z","shell.execute_reply":"2024-06-27T15:59:16.793708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}