{"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"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":117682,"databundleVersionId":14443416,"sourceType":"competition"},{"sourceId":278531155,"sourceType":"kernelVersion"}],"dockerImageVersionId":30513,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Surface Segmentation Check Callback Images","metadata":{"papermill":{"duration":0.007345,"end_time":"2023-06-18T13:10:15.218791","exception":false,"start_time":"2023-06-18T13:10:15.211446","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"https://www.kaggle.com/code/stpeteishii/surface-segmentation-rgb-mask-unet-callback<br/>\nhttps://www.kaggle.com/code/stpeteishii/surface-segmentation-check-callback-images","metadata":{}},{"cell_type":"markdown","source":"An effective method to find the optimal number of epochs. Save the predicted mask images generated every 3 epoch and try arranging them later. You can see the best number of epochs at a glance.\nIf the epoch number is too large, the mask image will be lost.","metadata":{}},{"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":8.494547,"end_time":"2023-06-18T13:10:38.377093","exception":false,"start_time":"2023-06-18T13:10:29.882546","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-10-31T13:49:57.198146Z","iopub.execute_input":"2023-10-31T13:49:57.198663Z","iopub.status.idle":"2023-10-31T13:49:57.205712Z","shell.execute_reply.started":"2023-10-31T13:49:57.198619Z","shell.execute_reply":"2023-10-31T13:49:57.204558Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dir0 = '/kaggle/input/surface-segmentation-rgb-mask-unet-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.014753,"end_time":"2023-06-18T13:10:38.399814","exception":false,"start_time":"2023-06-18T13:10:38.385061","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-10-31T13:49:57.208065Z","iopub.execute_input":"2023-10-31T13:49:57.209265Z","iopub.status.idle":"2023-10-31T13:49:57.323468Z","shell.execute_reply.started":"2023-10-31T13:49:57.209221Z","shell.execute_reply":"2023-10-31T13:49:57.322299Z"},"trusted":true},"outputs":[],"execution_count":null},{"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.014613,"end_time":"2023-06-18T13:10:38.88257","exception":false,"start_time":"2023-06-18T13:10:38.867957","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-10-31T13:49:57.326778Z","iopub.execute_input":"2023-10-31T13:49:57.327171Z","iopub.status.idle":"2023-10-31T13:49:57.332938Z","shell.execute_reply.started":"2023-10-31T13:49:57.327139Z","shell.execute_reply":"2023-10-31T13:49:57.33152Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"    path0='/kaggle/input/roads-segmentation-dataset/images/0.png'\n    path1='/kaggle/input/roads-segmentation-dataset/masks/0.png'\n    img0=read_image(path0)\n    img1=read_image(path1)\n    result = np.hstack((img0,img1))\n    plt.imshow(result)\n    plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-31T13:49:57.335067Z","iopub.execute_input":"2023-10-31T13:49:57.335559Z","iopub.status.idle":"2023-10-31T13:49:57.595956Z","shell.execute_reply.started":"2023-10-31T13:49:57.335526Z","shell.execute_reply":"2023-10-31T13:49:57.594868Z"}}},{"cell_type":"markdown","source":"# Process of UNET mask image generation","metadata":{"papermill":{"duration":0.006797,"end_time":"2023-06-18T13:10:38.896166","exception":false,"start_time":"2023-06-18T13:10:38.889369","status":"completed"},"tags":[]}},{"cell_type":"code","source":"rows = 10\ncols = 3\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":1.626193,"end_time":"2023-06-18T13:10:40.529542","exception":false,"start_time":"2023-06-18T13:10:38.903349","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-10-31T13:49:57.597519Z","iopub.execute_input":"2023-10-31T13:49:57.597951Z","iopub.status.idle":"2023-10-31T13:50:00.405732Z","shell.execute_reply.started":"2023-10-31T13:49:57.597913Z","shell.execute_reply":"2023-10-31T13:50:00.404472Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null},{"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":{}},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}