{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Blood Vessel Check Callback Images","metadata":{"papermill":{"duration":0.005342,"end_time":"2023-10-31T14:51:40.547123","exception":false,"start_time":"2023-10-31T14:51:40.541781","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"https://www.kaggle.com/stpeteishii/blood-vessel-mask-unet-w-callback<br/>\nhttps://www.kaggle.com/stpeteishii/blood-vessel-check-callback-images<br/>\n","metadata":{"papermill":{"duration":0.004008,"end_time":"2023-10-31T14:51:40.555603","exception":false,"start_time":"2023-10-31T14:51:40.551595","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"An effective method to find the optimal number of epochs. Save the predicted mask images generated each epoch and try arranging them later. You can see the best number of epochs for detecting blood vessel. Surprisingly, predicted mask image changes epoch by epoch. \n","metadata":{"papermill":{"duration":0.003875,"end_time":"2023-10-31T14:51:40.563721","exception":false,"start_time":"2023-10-31T14:51:40.559846","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom PIL import Image\nimport random\n%matplotlib inline","metadata":{"papermill":{"duration":0.315716,"end_time":"2023-10-31T14:51:40.883713","exception":false,"start_time":"2023-10-31T14:51:40.567997","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T02:24:03.096031Z","iopub.execute_input":"2023-11-11T02:24:03.096786Z","iopub.status.idle":"2023-11-11T02:24:03.744217Z","shell.execute_reply.started":"2023-11-11T02:24:03.096751Z","shell.execute_reply":"2023-11-11T02:24:03.742884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir0 = '/kaggle/input/blood-vessel-mask-unet-w-callback/output_masks'\npaths=[]\nfor dirname, _, filenames in os.walk(dir0):\n    for filename in filenames:\n        if filename[5:7]=='00':\n            paths+=[(os.path.join(dirname, filename))]\npaths.sort()","metadata":{"papermill":{"duration":0.114179,"end_time":"2023-10-31T14:51:41.003052","exception":false,"start_time":"2023-10-31T14:51:40.888873","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T02:24:03.746552Z","iopub.execute_input":"2023-11-11T02:24:03.747111Z","iopub.status.idle":"2023-11-11T02:24:03.771606Z","shell.execute_reply.started":"2023-11-11T02:24:03.747066Z","shell.execute_reply":"2023-11-11T02:24:03.770760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size=512\ndef read_image(path):\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = cv2.resize(img, (image_size, image_size))\n    return img","metadata":{"papermill":{"duration":0.014533,"end_time":"2023-10-31T14:51:41.021937","exception":false,"start_time":"2023-10-31T14:51:41.007404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T02:24:03.773079Z","iopub.execute_input":"2023-11-11T02:24:03.773661Z","iopub.status.idle":"2023-11-11T02:24:03.779088Z","shell.execute_reply.started":"2023-11-11T02:24:03.773613Z","shell.execute_reply":"2023-11-11T02:24:03.778084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process of UNET mask image generation","metadata":{"papermill":{"duration":0.003863,"end_time":"2023-10-31T14:51:41.039026","exception":false,"start_time":"2023-10-31T14:51:41.035163","status":"completed"},"tags":[]}},{"cell_type":"code","source":"rows = 10\ncols = 4\nfig, ax = plt.subplots(rows, cols, figsize = (12,30))\nfor i, ax in enumerate(ax.flat):\n    if i < len(paths):\n        img = read_image(paths[i])\n        file=paths[i].split('/')[-1]\n        ax.set_title(file)\n        ax.imshow(img)\n        ax.axis('off')\nplt.show()","metadata":{"papermill":{"duration":6.324137,"end_time":"2023-10-31T14:51:47.367251","exception":false,"start_time":"2023-10-31T14:51:41.043114","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-11-11T02:24:03.781184Z","iopub.execute_input":"2023-11-11T02:24:03.781573Z","iopub.status.idle":"2023-11-11T02:24:09.522973Z","shell.execute_reply.started":"2023-11-11T02:24:03.781526Z","shell.execute_reply":"2023-11-11T02:24:09.521914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.022143,"end_time":"2023-10-31T14:51:47.410503","exception":false,"start_time":"2023-10-31T14:51:47.388360","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Can you decide on the best number of epochs, or do you consider other means to reliably generate a mask image?","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"    # Callback\n\n    from tensorflow import keras\n\n    class SaveMaskImagesCallback(keras.callbacks.Callback):\n        def __init__(self, output_dir, images, masks, save_every_n_epochs=5):\n            super(SaveMaskImagesCallback, self).__init__()\n            self.output_dir = output_dir\n            self.images = images\n            self.masks = masks\n            self.save_every_n_epochs = save_every_n_epochs\n\n        def on_epoch_end(self, epoch, logs=None):\n            if (epoch + 1) % self.save_every_n_epochs == 0:\n                predictions = self.model.predict(self.images)\n                for i, pred_mask in enumerate(predictions):\n\n                    output_path = os.path.join(self.output_dir, f\"epoch_{epoch + 1}_mask_{i}.png\")\n                    pred_mask = (pred_mask * 255).astype(np.uint8)\n                    keras.preprocessing.image.save_img(output_path, pred_mask)\n\n    output_directory = \"output_masks\"  \n    os.makedirs(output_directory, exist_ok=True)\n    mask_callback = SaveMaskImagesCallback(output_directory, images_train, masks_train)\n\n    unet_result = unet_model.fit(\n        images_train, masks_train, \n        validation_split=0.2, batch_size=4, epochs=75,\n        callbacks=[mask_callback]\n    )\n\n    # Train\n\n    unet_result = unet_model.fit(\n        images_train, masks_train, \n        validation_split=0.2, batch_size=4, epochs=75,\n        callbacks=[mask_callback]\n    )","metadata":{"papermill":{"duration":0.023094,"end_time":"2023-10-31T14:51:47.458625","exception":false,"start_time":"2023-10-31T14:51:47.435531","status":"completed"},"tags":[]}},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.021393,"end_time":"2023-10-31T14:51:47.503365","exception":false,"start_time":"2023-10-31T14:51:47.481972","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}