{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":8089,"databundleVersionId":44321,"sourceType":"competition"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-08-14T08:32:21.691191Z","iopub.execute_input":"2024-08-14T08:32:21.692198Z","iopub.status.idle":"2024-08-14T08:32:22.73047Z","shell.execute_reply.started":"2024-08-14T08:32:21.692159Z","shell.execute_reply":"2024-08-14T08:32:22.729116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os \nimport glob \nimport cv2\nimport pandas as pd\nimport numpy as np\n\nfrom tqdm import tqdm \n\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\n\nimport matplotlib.pyplot as plt\n\nseed = 42\nnp.random.seed = seed","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:22.732696Z","iopub.execute_input":"2024-08-14T08:32:22.733198Z","iopub.status.idle":"2024-08-14T08:32:23.747233Z","shell.execute_reply.started":"2024-08-14T08:32:22.733168Z","shell.execute_reply":"2024-08-14T08:32:23.746162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\ntf.__version__","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:23.748696Z","iopub.execute_input":"2024-08-14T08:32:23.749148Z","iopub.status.idle":"2024-08-14T08:32:36.130888Z","shell.execute_reply.started":"2024-08-14T08:32:23.749119Z","shell.execute_reply":"2024-08-14T08:32:36.129754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\n# Define the path to the zip file and the extraction directory\nzip_path = '/kaggle/input/data-science-bowl-2018/stage1_test.zip'\nextract_to = '/kaggle/working/stage1_test'\n\n# Create extraction directory if it doesn't exist\nif not os.path.exists(extract_to):\n    os.makedirs(extract_to)\n\n# Extract the zip file\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_to)\n\nprint(\"stage1_test extraction completed.\")\n\n#####################################################################################\n#####################################################################################\n\n# Define the path to the zip file and the extraction directory\nzip_path = '/kaggle/input/data-science-bowl-2018/stage1_train.zip'\nextract_to = '/kaggle/working/stage1_train'\n\n# Create extraction directory if it doesn't exist\nif not os.path.exists(extract_to):\n    os.makedirs(extract_to)\n\n# Extract the zip file\nwith zipfile.ZipFile(zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_to)\n\nprint(\"stage1_train extraction completed.\")","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:36.133379Z","iopub.execute_input":"2024-08-14T08:32:36.133961Z","iopub.status.idle":"2024-08-14T08:32:41.703641Z","shell.execute_reply.started":"2024-08-14T08:32:36.133934Z","shell.execute_reply":"2024-08-14T08:32:41.702539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH = 128\nIMG_HEIGHT = 128\nIMG_CHANNELS = 3","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:41.705081Z","iopub.execute_input":"2024-08-14T08:32:41.705422Z","iopub.status.idle":"2024-08-14T08:32:41.710338Z","shell.execute_reply.started":"2024-08-14T08:32:41.705395Z","shell.execute_reply":"2024-08-14T08:32:41.709283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"/kaggle/working/stage1_train/\"\nTEST_PATH =   \"/kaggle/working/stage1_test/\"","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:41.712188Z","iopub.execute_input":"2024-08-14T08:32:41.713687Z","iopub.status.idle":"2024-08-14T08:32:41.793882Z","shell.execute_reply.started":"2024-08-14T08:32:41.713629Z","shell.execute_reply":"2024-08-14T08:32:41.792721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ids = next(os.walk(TRAIN_PATH))[1]\ntest_ids = next(os.walk(TEST_PATH))[1]","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:41.795103Z","iopub.execute_input":"2024-08-14T08:32:41.795427Z","iopub.status.idle":"2024-08-14T08:32:41.804246Z","shell.execute_reply.started":"2024-08-14T08:32:41.795402Z","shell.execute_reply":"2024-08-14T08:32:41.803299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nY_train = np.zeros((len(train_ids), IMG_HEIGHT, IMG_WIDTH, 1), dtype=bool)\n\nX_train.shape, Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:41.805759Z","iopub.execute_input":"2024-08-14T08:32:41.806089Z","iopub.status.idle":"2024-08-14T08:32:41.814898Z","shell.execute_reply.started":"2024-08-14T08:32:41.806063Z","shell.execute_reply":"2024-08-14T08:32:41.813779Z"},"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_train[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_train[n] = mask   \n\n# test images\nX_test = 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    X_test[n] = img\n\nprint('Done processing train images and masks and test images!')","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:32:41.816582Z","iopub.execute_input":"2024-08-14T08:32:41.817372Z","iopub.status.idle":"2024-08-14T08:37:43.14592Z","shell.execute_reply.started":"2024-08-14T08:32:41.817334Z","shell.execute_reply":"2024-08-14T08:37:43.14487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# randomly plot some images and processed masks:\nimport random\n\nimage_x = random.randint(0, len(train_ids))\nimshow(X_train[image_x])\nplt.show()\nimshow(np.squeeze(Y_train[image_x]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:37:43.148886Z","iopub.execute_input":"2024-08-14T08:37:43.149241Z","iopub.status.idle":"2024-08-14T08:37:43.648751Z","shell.execute_reply.started":"2024-08-14T08:37:43.149213Z","shell.execute_reply":"2024-08-14T08:37:43.647757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Build the model\ninputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\n\n# convert the images to float values as thats what the conv layers expect and also scale values between 0 and 1\ns = tf.keras.layers.Lambda(lambda x: x / 255)(inputs) # Lambda here same as lambda function in regular python\n\n#Contraction path\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-08-14T08:37:43.651667Z","iopub.execute_input":"2024-08-14T08:37:43.6521Z","iopub.status.idle":"2024-08-14T08:37:44.819785Z","shell.execute_reply.started":"2024-08-14T08:37:43.652069Z","shell.execute_reply":"2024-08-14T08:37:44.818775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Modelcheckpoint\ncheckpointer = tf.keras.callbacks.ModelCheckpoint('/kaggle/working/unet_v1.keras', verbose=1, save_best_only=True)\n    \ncallbacks = [\n        tf.keras.callbacks.EarlyStopping(patience=2, monitor='val_loss'),\n        tf.keras.callbacks.TensorBoard(log_dir='logs'),\n        checkpointer]\n\nresults = model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=25, callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:37:44.821074Z","iopub.execute_input":"2024-08-14T08:37:44.821427Z","iopub.status.idle":"2024-08-14T08:39:17.032348Z","shell.execute_reply.started":"2024-08-14T08:37:44.821399Z","shell.execute_reply":"2024-08-14T08:39:17.031375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = random.randint(0, len(X_train))\n\n\npreds_train = model.predict(X_train[:int(X_train.shape[0]*0.9)], verbose=1)\npreds_val = model.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\npreds_test = model.predict(X_test, verbose=1)\n\n \npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n\n\n# Perform a sanity check on some random training samples\nix = random.randint(0, len(preds_train_t))\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(Y_train[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()\n\n# Perform a sanity check on some random validation samples\nix = random.randint(0, len(preds_val_t))\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\nimshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.show()\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:39:17.033967Z","iopub.execute_input":"2024-08-14T08:39:17.034319Z","iopub.status.idle":"2024-08-14T08:39:34.253032Z","shell.execute_reply.started":"2024-08-14T08:39:17.034269Z","shell.execute_reply":"2024-08-14T08:39:34.251933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tensorboard --logdir=logs/ --host localhost --port 8080","metadata":{"execution":{"iopub.status.busy":"2024-08-14T08:44:44.556489Z","iopub.execute_input":"2024-08-14T08:44:44.55693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}