{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":61446,"databundleVersionId":6962461,"sourceType":"competition"},{"sourceId":7497215,"sourceType":"datasetVersion","datasetId":4365499}],"dockerImageVersionId":30636,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install keras-unet-collection","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:07.179684Z","iopub.execute_input":"2024-01-28T10:32:07.180508Z","iopub.status.idle":"2024-01-28T10:32:20.011953Z","shell.execute_reply.started":"2024-01-28T10:32:07.180466Z","shell.execute_reply":"2024-01-28T10:32:20.010984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.optimizers import Adam\nfrom keras_unet_collection import models, losses\nfrom datetime import datetime\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:20.013900Z","iopub.execute_input":"2024-01-28T10:32:20.014199Z","iopub.status.idle":"2024-01-28T10:32:31.976488Z","shell.execute_reply.started":"2024-01-28T10:32:20.014173Z","shell.execute_reply":"2024-01-28T10:32:31.975688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# LOAD IMAGES AND MASK\n### Create a dataframe using pandas which is contain image path and mask path\n\n### Preprocessing images and mask dataset in this below link\n\n[https://www.kaggle.com/code/greedyfornothing/sennet-hoa-preprocessing-images-and-mask/notebook](http://)","metadata":{}},{"cell_type":"code","source":"train_base_path = '/kaggle/input/sennet-hoa-512x512-images/img_512x512/'\ntrain_new_images_path = os.path.join(train_base_path, 'images')\ntrain_new_masks_path = os.path.join(train_base_path, 'masks')\n\ntrain_image512x512_files = sorted([os.path.join(train_new_images_path, file) for file in os.listdir(train_new_images_path) if os.path.isfile(os.path.join(train_new_images_path, file))])\ntrain_mask512x512_files = sorted([os.path.join(train_new_masks_path, file) for file in os.listdir(train_new_masks_path) if os.path.isfile(os.path.join(train_new_masks_path, file))])\n\n\nprint(\"Image files:\")\nprint(len(train_image512x512_files), type(train_image512x512_files))\n\nprint(\"\\nMask files:\")\nprint(len(train_mask512x512_files), type(train_mask512x512_files))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-28T10:32:31.977567Z","iopub.execute_input":"2024-01-28T10:32:31.978102Z","iopub.status.idle":"2024-01-28T10:32:41.576165Z","shell.execute_reply.started":"2024-01-28T10:32:31.978075Z","shell.execute_reply":"2024-01-28T10:32:41.575255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_base_path = '/kaggle/input/sennet-hoa-512x512-images/val_img_512x512/'\nval_new_images_path = os.path.join(val_base_path, 'images')\nval_new_masks_path = os.path.join(val_base_path, 'masks')\n\nval_image512x512_files = sorted([os.path.join(val_new_images_path, file) for file in os.listdir(val_new_images_path) if os.path.isfile(os.path.join(val_new_images_path, file))])\nval_mask512x512_files = sorted([os.path.join(val_new_masks_path, file) for file in os.listdir(val_new_masks_path) if os.path.isfile(os.path.join(val_new_masks_path, file))])\n\n\nprint(\"Image files:\")\nprint(len(val_image512x512_files), type(val_image512x512_files))\n\nprint(\"\\nMask files:\")\nprint(len(val_mask512x512_files), type(val_mask512x512_files))","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:41.577347Z","iopub.execute_input":"2024-01-28T10:32:41.577634Z","iopub.status.idle":"2024-01-28T10:32:42.920042Z","shell.execute_reply.started":"2024-01-28T10:32:41.577610Z","shell.execute_reply":"2024-01-28T10:32:42.919076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df = pd.DataFrame(list(zip(train_image512x512_files, train_mask512x512_files)), columns = [\"image path\", \"mask path\"])\nnew_df.to_csv(\"new_df_train_path.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:42.923444Z","iopub.execute_input":"2024-01-28T10:32:42.923825Z","iopub.status.idle":"2024-01-28T10:32:42.985365Z","shell.execute_reply.started":"2024-01-28T10:32:42.923799Z","shell.execute_reply":"2024-01-28T10:32:42.984608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:42.986340Z","iopub.execute_input":"2024-01-28T10:32:42.986583Z","iopub.status.idle":"2024-01-28T10:32:43.003937Z","shell.execute_reply.started":"2024-01-28T10:32:42.986561Z","shell.execute_reply":"2024-01-28T10:32:43.002739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_new_df = pd.DataFrame(list(zip(val_image512x512_files, val_mask512x512_files)), columns = [\"image path\", \"mask path\"])\nval_new_df.to_csv(\"val_new_df_train_path.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:43.005270Z","iopub.execute_input":"2024-01-28T10:32:43.005954Z","iopub.status.idle":"2024-01-28T10:32:43.025433Z","shell.execute_reply.started":"2024-01-28T10:32:43.005914Z","shell.execute_reply":"2024-01-28T10:32:43.024484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_new_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:43.026540Z","iopub.execute_input":"2024-01-28T10:32:43.026865Z","iopub.status.idle":"2024-01-28T10:32:43.042069Z","shell.execute_reply.started":"2024-01-28T10:32:43.026829Z","shell.execute_reply":"2024-01-28T10:32:43.041234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# I'll try using ImageDataGenerator\nlet's try using ImageDataGenerator from Tensorflow","metadata":{}},{"cell_type":"code","source":"input_size = (512,512)\nbatch_size = 8\n\n#no data augmentation\nstd_img_data_gen = ImageDataGenerator(rescale=1./255)\nstd_mask_data_gen = ImageDataGenerator()\n\n#with data augmentation\nimg_data_gen_args = dict(rotation_range=20,\n                    width_shift_range=0.2,\n                    height_shift_range=0.2,\n                    shear_range=0.2,\n                    zoom_range=0.2,\n                    horizontal_flip=True,\n                    vertical_flip=True,\n                    rescale=1./255)\n\nmask_data_gen_args = dict(rotation_range=20,\n                    width_shift_range=0.2,\n                    height_shift_range=0.2,\n                    shear_range=0.2,\n                    zoom_range=0.2,\n                    horizontal_flip=True,\n                    vertical_flip=True)\n\n\ndef image_mask_dataset(df, img_path_name, mask_path_name, target_size, batch):\n#     img_gen = ImageDataGenerator(**img_data_gen_args)\n#     mask_gen = ImageDataGenerator(**mask_data_gen_args)\n    \n    img_data = std_img_data_gen.flow_from_dataframe(dataframe = df,\n                                          x_col = img_path_name,\n                                          target_size = target_size, \n                                          class_mode = None,\n                                          color_mode = 'rgb', \n                                          batch_size = batch,\n                                          seed = 42)\n    mask_data = std_mask_data_gen.flow_from_dataframe(dataframe = df, x_col = mask_path_name, target_size = target_size,\n                                           class_mode = None, batch_size = batch, color_mode = 'grayscale',\n                                           seed = 42)\n    \n    datagen = zip(img_data, mask_data)\n    return datagen\n\n# def image_mask_val_dataset(df, img_path_name, mask_path_name, target_size, batch):    \n#     img_data = std_img_data_gen.flow_from_dataframe(dataframe = df,\n#                                           x_col = img_path_name,\n#                                           target_size = target_size,\n#                                           class_mode = None,\n#                                           color_mode = 'rgb',\n#                                           batch_size = batch,\n#                                           seed = 42)\n#     mask_data = std_mask_data_gen.flow_from_dataframe(dataframe = df,\n#                                           x_col = mask_path_name,\n#                                           target_size = target_size,\n#                                           class_mode = None,\n#                                           color_mode = 'grayscale',\n#                                           batch_size = batch,\n#                                           seed = 42)\n#     datagen = zip(img_data, mask_data)\n#     return datagen","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:43.043323Z","iopub.execute_input":"2024-01-28T10:32:43.043639Z","iopub.status.idle":"2024-01-28T10:32:43.053195Z","shell.execute_reply.started":"2024-01-28T10:32:43.043614Z","shell.execute_reply":"2024-01-28T10:32:43.052373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_training = image_mask_dataset(df = new_df, img_path_name = \"image path\", mask_path_name = \"mask path\",\n                             target_size = input_size, batch = batch_size)\n\ndata_validation = image_mask_dataset(df = val_new_df, img_path_name = \"image path\", mask_path_name = \"mask path\",\n                                target_size = input_size, batch = batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:43.054332Z","iopub.execute_input":"2024-01-28T10:32:43.054606Z","iopub.status.idle":"2024-01-28T10:32:48.603845Z","shell.execute_reply.started":"2024-01-28T10:32:43.054575Z","shell.execute_reply":"2024-01-28T10:32:48.602902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## the batch information","metadata":{}},{"cell_type":"code","source":"sample_batch = next(data_training)\nimg_batch, mask_batch = sample_batch\n\nprint(\"Image Batch Shape:\", img_batch.shape, \"\\nImage Batch max value:\", img_batch.max())\nprint(\"\\nMask Batch Shape:\", mask_batch.shape, \"\\nMask Batch max value:\", mask_batch.max())","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:48.605134Z","iopub.execute_input":"2024-01-28T10:32:48.605431Z","iopub.status.idle":"2024-01-28T10:32:48.863690Z","shell.execute_reply.started":"2024-01-28T10:32:48.605406Z","shell.execute_reply":"2024-01-28T10:32:48.862797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_sample_batch = next(data_validation)\nval_img_batch, val_mask_batch = val_sample_batch\n\nprint(\"Image Batch Shape:\", val_img_batch.shape, \"\\nImage Batch max value:\", val_img_batch.max())\nprint(\"\\nMask Batch Shape:\", val_mask_batch.shape, \"\\nMask Batch max value:\", val_mask_batch.max())","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:48.864823Z","iopub.execute_input":"2024-01-28T10:32:48.865175Z","iopub.status.idle":"2024-01-28T10:32:49.080193Z","shell.execute_reply.started":"2024-01-28T10:32:48.865142Z","shell.execute_reply":"2024-01-28T10:32:49.079278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PLOT SAMPLE BATCH IMAGES","metadata":{}},{"cell_type":"code","source":"def display_images_with_masks(img_batch, mask_batch, target_size, num_rows=2, num_cols=5):\n    plt.figure(figsize=(15, 7))\n\n    for i in range(num_rows):\n        for j in range(num_cols):\n            idx = i * num_cols + j\n            if idx < len(img_batch):\n                plt.subplot(num_rows, 2 * num_cols, 2 * idx + 1)\n                plt.imshow(img_batch[idx])\n                plt.axis('off')\n                plt.title('Image')\n\n                plt.subplot(num_rows, 2 * num_cols, 2 * idx + 2)\n                plt.imshow(mask_batch[idx].reshape(target_size[0], target_size[1]), cmap='gray')\n                plt.axis('off')\n                plt.title('Mask')\n    \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:49.081305Z","iopub.execute_input":"2024-01-28T10:32:49.081568Z","iopub.status.idle":"2024-01-28T10:32:49.089630Z","shell.execute_reply.started":"2024-01-28T10:32:49.081546Z","shell.execute_reply":"2024-01-28T10:32:49.088645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images_to_display = 3\n\nfor i, (img_batch, mask_batch) in enumerate(data_training):\n    if i == num_images_to_display:\n        break\n        \n    display_images_with_masks(img_batch, mask_batch, target_size=input_size)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:49.093113Z","iopub.execute_input":"2024-01-28T10:32:49.093394Z","iopub.status.idle":"2024-01-28T10:32:54.461583Z","shell.execute_reply.started":"2024-01-28T10:32:49.093370Z","shell.execute_reply":"2024-01-28T10:32:54.460717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL by Keras UNet Collection\nmodel UNet2D + VGG16 + weight \"imagenet\"\n\n[https://github.com/yingkaisha/keras-unet-collection](http://)","metadata":{}},{"cell_type":"code","source":"help(models.unet_2d)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:54.462745Z","iopub.execute_input":"2024-01-28T10:32:54.463008Z","iopub.status.idle":"2024-01-28T10:32:54.468528Z","shell.execute_reply.started":"2024-01-28T10:32:54.462986Z","shell.execute_reply":"2024-01-28T10:32:54.467567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_Unet = models.unet_2d((512, 512, 3), filter_num=[64, 128, 256, 512, 1024], \n                           n_labels=1, \n                           stack_num_down=2, stack_num_up=2, \n                           activation='ReLU', \n                           output_activation='Sigmoid', \n                           batch_norm=True, pool=False, unpool=False, \n                           backbone='VGG16', weights='imagenet', \n                           freeze_backbone=True, freeze_batch_norm=True, \n                           name='unet')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:54.469748Z","iopub.execute_input":"2024-01-28T10:32:54.470424Z","iopub.status.idle":"2024-01-28T10:32:58.461800Z","shell.execute_reply.started":"2024-01-28T10:32:54.470389Z","shell.execute_reply":"2024-01-28T10:32:58.460959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_Unet.compile(loss='binary_crossentropy', optimizer=Adam(lr = 1e-3), \n              metrics=['accuracy', losses.dice_coef])","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:58.462923Z","iopub.execute_input":"2024-01-28T10:32:58.463229Z","iopub.status.idle":"2024-01-28T10:32:58.481885Z","shell.execute_reply.started":"2024-01-28T10:32:58.463203Z","shell.execute_reply":"2024-01-28T10:32:58.481193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model_Unet.summary())","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:58.482820Z","iopub.execute_input":"2024-01-28T10:32:58.483086Z","iopub.status.idle":"2024-01-28T10:32:58.636701Z","shell.execute_reply.started":"2024-01-28T10:32:58.483064Z","shell.execute_reply":"2024-01-28T10:32:58.635856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start1 = datetime.now() \n\nUnet_history = model_Unet.fit(data_training, \n                    verbose=1,\n                    steps_per_epoch = 693,\n                    validation_data=data_validation,\n                    validation_steps = 173,\n                    shuffle=False,\n                    epochs=5)\n\nmodel_Unet.save('SenNet_HOA_5_epochs.hdf5')\nstop1 = datetime.now()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:32:58.637764Z","iopub.execute_input":"2024-01-28T10:32:58.638012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"execution_time_Unet = stop1-start1\nprint(\"UNet execution time is: \", execution_time_Unet)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}