{"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":"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\n# import os\n# for 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom pathlib import Path\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:08:42.444967Z","iopub.execute_input":"2023-08-06T14:08:42.445734Z","iopub.status.idle":"2023-08-06T14:08:42.452156Z","shell.execute_reply.started":"2023-08-06T14:08:42.445698Z","shell.execute_reply":"2023-08-06T14:08:42.450951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Prepration","metadata":{}},{"cell_type":"markdown","source":"### Functions for data collection and prepration","metadata":{}},{"cell_type":"code","source":"# Get rgb band combination\ndef normalize_range(data, bounds):\n    \"\"\"Maps data to the range [0, 1].\"\"\"\n    return (data - bounds[0]) / (bounds[1] - bounds[0])\n\ndef get_rgb(img):\n    _T11_BOUNDS = (243, 303)\n    _CLOUD_TOP_TDIFF_BOUNDS = (-4, 5)\n    _TDIFF_BOUNDS = (-4, 2)\n    #band_img = image_train[..., 4]\n    band15 = img[15-8] # band \n    band14 = img[14-8]\n    band11 = img[11-8]\n    r = normalize_range(band15 - band14, _TDIFF_BOUNDS)\n    g = normalize_range(band14 - band11, _CLOUD_TOP_TDIFF_BOUNDS)\n    b = normalize_range(band14, _T11_BOUNDS)\n    false_color = np.clip(np.stack([r, g, b], axis=2), 0, 1)\n    return false_color\n\n\n# Prepare data for model \ndef load_model_image(img_path):\n    image = []\n    # Load and preprocess the image and mask\n    bands = range(8, 17)  # Range from band_08 to band_16\n    for band in bands:\n        band_path = img_path / f\"band_{band:02d}.npy\"\n        img = np.load(band_path)\n        image.append(img)\n    image = np.array(image)\n    fc_image = get_rgb(image[...,4])\n    #image = np.transpose(image[...,4], (1,2,0))\n    mask_path = img_path / \"human_pixel_masks.npy\"\n    mask = np.load(mask_path)\n    \n    # Preprocessing steps on the image and mask\n    # Example:\n    #fc_image = fc_image / 255.0  # Normalize the pixel values to [0, 1]\n    \n    return fc_image, mask\n\n# Function to load and preprocess images and masks\ndef load_and_preprocess_image(img_path):\n    fc_image, mask = load_model_image(img_path)\n    mask = mask.astype(np.float32)  # Convert the mask to float32\n    return fc_image, mask\n\n# Create TensorFlow data generator\ndef data_generator(data_paths, batch_size=32):\n    while True:\n        # Shuffle the data paths in each epoch\n        np.random.shuffle(data_paths)\n        \n        for i in range(0, len(data_paths), batch_size):\n            batch_data = data_paths[i:i+batch_size]\n            images = []\n            masks = []\n            for img_path, mask_path in batch_data:\n                image, mask = load_and_preprocess_image(img_path)\n                images.append(image)\n                masks.append(mask)\n                \n            yield np.array(images), np.array(masks)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:08:43.982988Z","iopub.execute_input":"2023-08-06T14:08:43.98338Z","iopub.status.idle":"2023-08-06T14:08:43.998643Z","shell.execute_reply.started":"2023-08-06T14:08:43.983351Z","shell.execute_reply":"2023-08-06T14:08:43.99744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load data path and split into train/val","metadata":{}},{"cell_type":"code","source":"# Load all image paths\ntarin_path = \"/kaggle/input/google-research-identify-contrails-reduce-global-warming/train\"\ntrain_folder = Path(tarin_path)\ntrain_img_paths = [x for x in train_folder.iterdir()] # store the list of image folder\n\n# Create a list of (image_path, mask_path) tuples\ndata_paths = [(img_path, img_path / \"human_pixel_masks.npy\") for img_path in train_img_paths]\nsample_paths = random.sample(data_paths,2000)\n# Split the data into training and validation sets\ntrain_paths, val_paths = train_test_split(sample_paths, test_size=0.25, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:08:45.557725Z","iopub.execute_input":"2023-08-06T14:08:45.558153Z","iopub.status.idle":"2023-08-06T14:08:45.903073Z","shell.execute_reply.started":"2023-08-06T14:08:45.558118Z","shell.execute_reply":"2023-08-06T14:08:45.902063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_paths)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:08:46.399531Z","iopub.execute_input":"2023-08-06T14:08:46.399946Z","iopub.status.idle":"2023-08-06T14:08:46.407129Z","shell.execute_reply.started":"2023-08-06T14:08:46.399887Z","shell.execute_reply":"2023-08-06T14:08:46.405985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Intiate data generator for model","metadata":{}},{"cell_type":"code","source":"%%time \n# get train_generator\nbatch_size = 8\ntrain_generator = data_generator(train_paths, batch_size=batch_size)\nval_generator = data_generator(val_paths, batch_size=batch_size)\n\n# Get one batch of images and masks from the generator\nimages_batch, masks_batch = next(train_generator)\nimages_batch_val, masks_batch_val = next(val_generator)\n\n# Print the shape of the first image in the batch\nprint(\"Image shape:\", images_batch[0].shape)\nprint(\"Mask shape:\", masks_batch[0].shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:08:51.144768Z","iopub.execute_input":"2023-08-06T14:08:51.14585Z","iopub.status.idle":"2023-08-06T14:08:54.310163Z","shell.execute_reply.started":"2023-08-06T14:08:51.145811Z","shell.execute_reply":"2023-08-06T14:08:54.309186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Visualize genrated batch image ","metadata":{}},{"cell_type":"code","source":"plt.hist(images_batch[1].ravel(), bins=256)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:07.184946Z","iopub.execute_input":"2023-08-06T14:09:07.185301Z","iopub.status.idle":"2023-08-06T14:09:08.101217Z","shell.execute_reply.started":"2023-08-06T14:09:07.185272Z","shell.execute_reply":"2023-08-06T14:09:08.099848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image = images_batch[3]\n# # Plot the pixel values of each channel\n# for channel in range(3):\n#     plt.figure(figsize=(8, 6))\n#     plt.imshow(image[:, :, channel])\n#     plt.colorbar()\n#     plt.title(f\"Pixel values of Channel {channel}\")\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:10.323709Z","iopub.execute_input":"2023-08-06T14:09:10.324091Z","iopub.status.idle":"2023-08-06T14:09:10.328871Z","shell.execute_reply.started":"2023-08-06T14:09:10.324058Z","shell.execute_reply":"2023-08-06T14:09:10.327702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(masks_batch[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:15.671702Z","iopub.execute_input":"2023-08-06T14:09:15.672076Z","iopub.status.idle":"2023-08-06T14:09:15.950784Z","shell.execute_reply.started":"2023-08-06T14:09:15.672044Z","shell.execute_reply":"2023-08-06T14:09:15.949806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(images_batch[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:21.196667Z","iopub.execute_input":"2023-08-06T14:09:21.197042Z","iopub.status.idle":"2023-08-06T14:09:21.616129Z","shell.execute_reply.started":"2023-08-06T14:09:21.197012Z","shell.execute_reply":"2023-08-06T14:09:21.615011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Build simple FCN model","metadata":{}},{"cell_type":"code","source":"# # network variables \n# input_shape = images_batch[0].shape\n# output_shape = masks_batch[0].shape\n\n# def create_fcn_model(input_shape):\n#     # Input layer\n#     inputs = tf.keras.layers.Input(shape=input_shape)\n\n#     # Encoder part (You can replace this with any desired architecture)\n#     x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)\n#     x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n#     x = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)\n#     x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n#     x = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)\n#     x = tf.keras.layers.MaxPooling2D((2, 2))(x)\n\n#     # Fully connected layer (Global Average Pooling)\n#     x = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n#     # Decoder part (You can replace this with any desired architecture)\n#     x = tf.keras.layers.Dense(256, activation='relu')(x)\n#     x = tf.keras.layers.Dense(256 * 256 * 1, activation='sigmoid')(x)\n#     x = tf.keras.layers.Reshape((256, 256, 1))(x)\n\n#     # Create the FCN model\n#     model = tf.keras.Model(inputs=inputs, outputs=x)\n\n#     return model\n\n# # Create the FCN model\n# model = create_fcn_model(input_shape)\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-19T16:45:45.081256Z","iopub.execute_input":"2023-07-19T16:45:45.082042Z","iopub.status.idle":"2023-07-19T16:45:45.517609Z","shell.execute_reply.started":"2023-07-19T16:45:45.082001Z","shell.execute_reply":"2023-07-19T16:45:45.516366Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define accuracy metrics","metadata":{}},{"cell_type":"code","source":"import tensorflow.keras.backend as K\n\n# Define the Dice Loss function\ndef dice_loss(y_true, y_pred):\n    smooth = 1e-5\n    y_true = tf.cast(y_true, tf.float32)  # Convert to float32\n    y_pred = tf.cast(y_pred, tf.float32)  # Convert to float32\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    dice_coeff = (2.0 * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1.0 - dice_coeff\n\n# Define the IoU (Intersection over Union) metric\ndef iou_metric(y_true, y_pred):\n    y_true = tf.cast(y_true, tf.float32)  # Convert to float32\n    y_pred = tf.cast(y_pred, tf.float32)  # Convert to float32\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    union = K.sum(K.maximum(y_true_f, y_pred_f))\n    iou = intersection / union\n    return iou","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:32.437791Z","iopub.execute_input":"2023-08-06T14:09:32.438485Z","iopub.status.idle":"2023-08-06T14:09:32.45034Z","shell.execute_reply.started":"2023-08-06T14:09:32.438453Z","shell.execute_reply":"2023-08-06T14:09:32.449356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.compile(optimizer='adam', loss=dice_loss, metrics=[iou_metric])\n\n# # Get the number of steps per epoch and validation steps\n# steps_per_epoch = len(train_paths) // batch_size\n# validation_steps = len(val_paths) // batch_size\n\n# # Train the model for 10 batches using the data generators\n# model.fit(train_generator,\n#               steps_per_epoch=steps_per_epoch,\n#               epochs=10,\n#               validation_data=val_generator,\n#               validation_steps=validation_steps)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T16:50:44.305837Z","iopub.execute_input":"2023-07-19T16:50:44.306233Z","iopub.status.idle":"2023-07-19T17:04:51.25078Z","shell.execute_reply.started":"2023-07-19T16:50:44.306198Z","shell.execute_reply":"2023-07-19T17:04:51.249403Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#images_batch_val[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-07-19T17:19:28.018265Z","iopub.execute_input":"2023-07-19T17:19:28.018667Z","iopub.status.idle":"2023-07-19T17:19:28.025321Z","shell.execute_reply.started":"2023-07-19T17:19:28.018636Z","shell.execute_reply":"2023-07-19T17:19:28.02395Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pred_image = model.predict(images_batch_val)","metadata":{"execution":{"iopub.status.busy":"2023-07-19T17:20:21.406778Z","iopub.execute_input":"2023-07-19T17:20:21.407314Z","iopub.status.idle":"2023-07-19T17:20:25.540613Z","shell.execute_reply.started":"2023-07-19T17:20:21.407276Z","shell.execute_reply":"2023-07-19T17:20:25.539363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(pred_image[1])","metadata":{"execution":{"iopub.status.busy":"2023-07-19T17:22:33.003073Z","iopub.execute_input":"2023-07-19T17:22:33.004131Z","iopub.status.idle":"2023-07-19T17:22:33.317494Z","shell.execute_reply.started":"2023-07-19T17:22:33.00409Z","shell.execute_reply":"2023-07-19T17:22:33.316255Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(masks_batch_val[1])","metadata":{"execution":{"iopub.status.busy":"2023-07-19T17:22:27.883101Z","iopub.execute_input":"2023-07-19T17:22:27.884004Z","iopub.status.idle":"2023-07-19T17:22:28.110466Z","shell.execute_reply.started":"2023-07-19T17:22:27.883965Z","shell.execute_reply":"2023-07-19T17:22:28.109281Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## U-Net Model","metadata":{}},{"cell_type":"code","source":"# U-net encoder\ndef conv2d_block(input_tensor, n_filters, kernel_size=3):\n    \"\"\"\n    Adds convolutional layers with the parameters passed to it.\n\n    Args:\n    input_tensor (tensor) -- the input tenor\n    n_filters (int) -- number of filters\n    kernel_size (int) -- kernel size of the convolution\n    \"\"\"\n    # first layer \n    x = input_tensor\n    for i in range(2):\n        x = tf.keras.layers.Conv2D(filters=n_filters, kernel_size=(kernel_size, kernel_size),\n                                   kernel_initializer=\"he_normal\", padding=\"same\")(x)\n        x = tf.keras.layers.BatchNormalization()(x)\n        x = tf.keras.layers.Activation(\"relu\")(x)\n\n    return x\n\ndef encoder_block(inputs, n_filters=64, pool_size=(2,2), dropout=0.3):\n    \"\"\"\n    Adds two convolutional blocks and then perform sampling on output of convolution.\n\n    Args:\n    input_tensor (tensor) -- the input tensor\n    n_filters (int) -- number of filters\n    kernel_size (int) -- kernel size of convolution\n\n    Returns:\n    f - the output features of the convolution block\n    p - the maxpooled features with dropout\n    \"\"\"\n\n    f = conv2d_block(inputs, n_filters=n_filters)\n    p = tf.keras.layers.MaxPool2D(pool_size=(2,2))(f)\n    p = tf.keras.layers.Dropout(0.3)(p)\n\n    return f, p\n\n# VGG16 encoder\ndef vgg_encoder(inputs):\n    \"\"\"\n    This function defines the VGG16 encoder.\n\n    Args:\n    inputs (tensor) -- batch of input images\n\n    Returns:\n    p4 - the output max-pooled features of the last encoder block\n    (f1, f2, f3, f4) - the output features of all the encoder blocks\n    \"\"\"\n    base_encoder = tf.keras.applications.VGG16(input_tensor=inputs, include_top=False, weights='imagenet')\n    p1 = base_encoder.get_layer(\"block1_conv2\").output\n    p2 = base_encoder.get_layer(\"block2_conv2\").output\n    p3 = base_encoder.get_layer(\"block3_conv3\").output\n    p4 = base_encoder.get_layer(\"block4_conv3\").output\n\n    return p4, (p1, p2, p3, p4)\n\n# Combine encoder layers and filter\ndef encoder(inputs):\n    \"\"\"\n    This function defines the encoder or downsampling path.\n\n    Args:\n    inputs (tensor) -- batch of input images\n\n    Returns:\n    p4 - the output max-pooled features of the last encoder block\n    (f1, f2, f3, f4) - the output features of all the encoder blocks\n    \"\"\"\n    f1, p1 = encoder_block(inputs, n_filters=64, pool_size=(2, 2), dropout=0.3)\n    f2, p2 = encoder_block(p1, n_filters=128, pool_size=(2, 2), dropout=0.3)\n    f3, p3 = encoder_block(p2, n_filters=256, pool_size=(2, 2), dropout=0.3)\n    f4, p4 = encoder_block(p3, n_filters=512, pool_size=(2, 2), dropout=0.3)\n\n    return p4, (f1, f2, f3, f4)\n\n\n# Bottelneck conv2d\ndef bottleneck(inputs):\n    \"\"\"\n    This function defines the bottleneck convolutions to extract more features before the unsampling layers.\n    \"\"\"\n\n    bottle_neck = conv2d_block(inputs, n_filters=1024)\n\n    return bottle_neck\n\n# Decoder Utilities\n\ndef decoder_block(inputs, conv_output, n_filters=64, kernel_size=3, strides=2, dropout=0.3):\n    \"\"\"\n    Defines the one decoder block of the UNet\n\n    Args:\n    inputs (tensor) -- batch of input features\n    conv_output (tensor) -- features from an encoder block\n    n_filters (int) -- number of filters\n    kernel_size (int) -- kernel size\n    strides (int) -- strides for the deconvolution/upsampling\n    padding (string) -- \"same\" or \"valid\", tells if shape will be preserved by zero padding\n\n    Returns:\n    c (tensor) -- output features of the decoder block\n    \"\"\"\n    u = tf.keras.layers.Conv2DTranspose(filters=n_filters, kernel_size=kernel_size,\n                                      strides=strides, padding=\"same\")(inputs)\n    u = tf.image.resize(u, conv_output.shape[1:3], method='bilinear')\n    c = tf.keras.layers.concatenate([u, conv_output])                                      \n    c = tf.keras.layers.Dropout(dropout)(c)\n    c = conv2d_block(c, n_filters=n_filters, kernel_size=3)\n\n    return c\n\ndef decoder(inputs, convs, output_channels):\n    \"\"\"\n    Defines the decoder of the UNet chaining together 4 decoder blocks. \n\n    Args:\n    inputs (tensor) -- batch of input features\n    convs (tuple) -- features from the encoder blocks\n    output_channels (int) -- number of classes in the label map\n\n    Returns:\n    outputs (tensor) -- the pixel wise label map of the image\n    \"\"\"\n\n    f1, f2, f3, f4 = convs\n    # 5 is the bottleneck if you ask \n    c6 = decoder_block(inputs, conv_output=f4, n_filters=512, kernel_size=(3,3),strides=(2,2), dropout=0.3)\n    c7 = decoder_block(c6, conv_output=f3, n_filters=256, kernel_size=(3,3),strides=(2,2), dropout=0.3)\n    c8 = decoder_block(c7, conv_output=f2, n_filters=128, kernel_size=(3,3), strides=(2,2), dropout=0.3)\n    c9 = decoder_block(c8, conv_output=f1, n_filters=64, kernel_size=(3,3),strides=(2,2), dropout=0.3)\n    outputs = tf.keras.layers.Conv2D(output_channels, (1,1), activation=\"sigmoid\")(c9)\n\n    return outputs","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:36.031202Z","iopub.execute_input":"2023-08-06T14:09:36.032221Z","iopub.status.idle":"2023-08-06T14:09:36.053961Z","shell.execute_reply.started":"2023-08-06T14:09:36.032168Z","shell.execute_reply":"2023-08-06T14:09:36.052748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Redesigned U-Net architecture with VGG16 as the encoder\ndef unet_vgg16(input_shape=(256, 256, 3), output_channels=1):\n    \"\"\"\n    U-Net architecture with VGG16 as the encoder.\n\n    Args:\n    input_shape (tuple) -- shape of the input image\n    output_channels (int) -- number of classes in the label map\n\n    Returns:\n    model (tf.keras.Model) -- the U-Net model\n    \"\"\"\n    inputs = tf.keras.layers.Input(input_shape)\n\n    # Encoder part\n    p4, (p1, p2, p3, _) = vgg_encoder(inputs)\n\n    # Bottleneck part\n    bottle_neck = bottleneck(p4)\n\n    # Decoder part\n    c6 = decoder_block(bottle_neck, p3, n_filters=512, kernel_size=(3, 3), strides=(2, 2), dropout=0.3)\n    c7 = decoder_block(c6, p2, n_filters=256, kernel_size=(3, 3), strides=(2, 2), dropout=0.3)\n    c8 = decoder_block(c7, p1, n_filters=128, kernel_size=(3, 3), strides=(2, 2), dropout=0.3)\n    c9 = decoder_block(c8, inputs, n_filters=64, kernel_size=(3, 3), strides=(2, 2), dropout=0.3)\n\n    # Output\n    outputs = tf.keras.layers.Conv2D(output_channels, (1, 1), activation=\"sigmoid\")(c9)\n\n    model = tf.keras.Model(inputs=inputs, outputs=outputs)\n    return model\n\n# Create the VGG16-based U-Net model\nunet_model_vgg16 = unet_vgg16(input_shape=(256, 256, 3), output_channels=1)\nunet_model_vgg16.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:36.819772Z","iopub.execute_input":"2023-08-06T14:09:36.820451Z","iopub.status.idle":"2023-08-06T14:09:44.095055Z","shell.execute_reply.started":"2023-08-06T14:09:36.820416Z","shell.execute_reply":"2023-08-06T14:09:44.094291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tf.keras.utils.plot_model(unet_model_vgg16, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-24T10:12:30.390827Z","iopub.execute_input":"2023-07-24T10:12:30.391188Z","iopub.status.idle":"2023-07-24T10:12:31.414638Z","shell.execute_reply.started":"2023-07-24T10:12:30.391159Z","shell.execute_reply":"2023-07-24T10:12:31.413774Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-07-24T10:27:32.908553Z","iopub.execute_input":"2023-07-24T10:27:32.909582Z","iopub.status.idle":"2023-07-24T10:27:32.926183Z","shell.execute_reply.started":"2023-07-24T10:27:32.909544Z","shell.execute_reply":"2023-07-24T10:27:32.925079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.mixed_precision.set_global_policy('mixed_float16')\n# complile model\nunet_model_vgg16.compile(optimizer='adam',\n              loss = dice_loss,\n              metrics=[iou_metric])\n\nbatch_size = 8\n# Get the number of steps per epoch and validation steps\nsteps_per_epoch = len(train_paths) // batch_size\nvalidation_steps = len(val_paths) // batch_size\n# Train the model for 10 batches using the data generators\nunet_model_vgg16.fit(train_generator,\n              steps_per_epoch=steps_per_epoch,\n              epochs=10,\n              validation_data=val_generator,\n              validation_steps=validation_steps)","metadata":{"execution":{"iopub.status.busy":"2023-08-06T14:09:57.181663Z","iopub.execute_input":"2023-08-06T14:09:57.182069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unet_model_vgg16.save('unet_vgg16_model_v1.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-06T18:41:45.432019Z","iopub.execute_input":"2023-08-06T18:41:45.432373Z","iopub.status.idle":"2023-08-06T18:41:45.463748Z","shell.execute_reply.started":"2023-08-06T18:41:45.432344Z","shell.execute_reply":"2023-08-06T18:41:45.462603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_mask = unet_model_vgg16.predict(images_batch_val)","metadata":{"execution":{"iopub.status.busy":"2023-07-24T13:48:36.489632Z","iopub.execute_input":"2023-07-24T13:48:36.490685Z","iopub.status.idle":"2023-07-24T13:48:37.081823Z","shell.execute_reply.started":"2023-07-24T13:48:36.490646Z","shell.execute_reply":"2023-07-24T13:48:37.080783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(pred_mask[0])","metadata":{"execution":{"iopub.status.busy":"2023-07-26T03:49:03.982778Z","iopub.execute_input":"2023-07-26T03:49:03.98337Z","iopub.status.idle":"2023-07-26T03:49:04.532068Z","shell.execute_reply.started":"2023-07-26T03:49:03.98333Z","shell.execute_reply":"2023-07-26T03:49:04.530533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(masks_batch_val[0])","metadata":{"execution":{"iopub.status.busy":"2023-07-24T13:51:36.645861Z","iopub.execute_input":"2023-07-24T13:51:36.646234Z","iopub.status.idle":"2023-07-24T13:51:36.92758Z","shell.execute_reply.started":"2023-07-24T13:51:36.646205Z","shell.execute_reply":"2023-07-24T13:51:36.926601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\n# Assuming you have already created and trained the U-Net model (unet_model_vgg16)\nconv_layers = [layer for layer in unet_model_vgg16.layers if isinstance(layer, tf.keras.layers.Conv2D)]\n\n# Visualize the filters in the first convolutional layer\nfilters_layer1 = conv_layers[0].get_weights()[0]\nnum_filters_layer1 = min(filters_layer1.shape[-1], 32)","metadata":{"execution":{"iopub.status.busy":"2023-07-24T14:10:47.386573Z","iopub.execute_input":"2023-07-24T14:10:47.387149Z","iopub.status.idle":"2023-07-24T14:10:47.399847Z","shell.execute_reply.started":"2023-07-24T14:10:47.387106Z","shell.execute_reply":"2023-07-24T14:10:47.398926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_filters_layer1","metadata":{"execution":{"iopub.status.busy":"2023-07-24T14:10:48.073928Z","iopub.execute_input":"2023-07-24T14:10:48.074285Z","iopub.status.idle":"2023-07-24T14:10:48.08303Z","shell.execute_reply.started":"2023-07-24T14:10:48.074257Z","shell.execute_reply":"2023-07-24T14:10:48.0817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 8))\nfor i in range(num_filters_layer1):\n    plt.subplot(4, 8, i+1)\n    plt.imshow(filters_layer1[..., i], cmap='viridis')\n    plt.axis('off')\nplt.suptitle('Filters in the First Convolutional Layer')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-24T14:10:49.40487Z","iopub.execute_input":"2023-07-24T14:10:49.405235Z","iopub.status.idle":"2023-07-24T14:10:51.059788Z","shell.execute_reply.started":"2023-07-24T14:10:49.405206Z","shell.execute_reply":"2023-07-24T14:10:51.058879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming you have already created and trained the U-Net model (unet_model_vgg16)\n# Get the layers whose feature maps you want to visualize (e.g., encoder blocks)\nencoder_layers = [layer.output for layer in unet_model_vgg16.layers if 'block' in layer.name]\n\n# Create a new model that outputs the activations in the encoder blocks\nfeature_map_model = tf.keras.Model(inputs=unet_model_vgg16.input, outputs=encoder_layers)\n\n# Choose an image from your dataset to visualize the feature maps\nimage_to_visualize = images_batch_val[2]\n\n# Preprocess the image and expand dimensions to match the input shape\nimage_to_visualize = tf.image.resize(image_to_visualize, (256, 256))\nimage_to_visualize = np.expand_dims(image_to_visualize, axis=0)\n\n# Get the activations of the encoder blocks for the input image\nactivations = feature_map_model.predict(image_to_visualize)\n\n# Visualize the feature maps in the first encoder block\nplt.figure(figsize=(15, 8))\nfor i in range(len(activations)):\n    plt.subplot(1, len(activations), i+1)\n    plt.imshow(activations[i][0, :, :, 0], cmap='viridis')  # Display only the first channel\n    plt.axis('off')\n    plt.title(f'Encoder Block {i+1}')\nplt.suptitle('Feature Maps in Encoder Blocks')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-24T14:13:09.138025Z","iopub.execute_input":"2023-07-24T14:13:09.138422Z","iopub.status.idle":"2023-07-24T14:13:10.318632Z","shell.execute_reply.started":"2023-07-24T14:13:09.138387Z","shell.execute_reply":"2023-07-24T14:13:10.317556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}