Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.10/dist-packages (4.10.0.82)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python-headless) (1.25.2)
Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.15.0)
Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.4.0)
Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3)
Requirement already satisfied: flatbuffers>=23.5.26 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.3.25)
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.5.4)
Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.9.0)
Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (18.1.1)
Requirement already satisfied: ml-dtypes~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.25.2)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.0)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.20.3)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (67.7.2)
Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.4.0)
Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.12.1)
Requirement already satisfied: wrapt<1.15,>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.14.1)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.37.0)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.64.1)
Requirement already satisfied: tensorboard<2.16,>=2.15 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.2)
Requirement already satisfied: tensorflow-estimator<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: keras<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.43.0)
Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.27.0)
Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (1.2.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.6)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.31.0)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (0.7.2)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.0.3)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (5.3.3)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.4.0)
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (4.9)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (1.3.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2024.6.2)
Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.16,>=2.15->tensorflow) (2.1.5)
Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.6.0)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (3.2.2)
Requirement already satisfied: kaggle in /usr/local/lib/python3.10/dist-packages (1.6.14)
Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.10/dist-packages (from kaggle) (1.16.0)
Requirement already satisfied: certifi>=2023.7.22 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2024.6.2)
Requirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.8.2)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.31.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from kaggle) (4.66.4)
Requirement already satisfied: python-slugify in /usr/local/lib/python3.10/dist-packages (from kaggle) (8.0.4)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.0.7)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from kaggle) (6.1.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->kaggle) (0.5.1)
Requirement already satisfied: text-unidecode>=1.3 in /usr/local/lib/python3.10/dist-packages (from python-slugify->kaggle) (1.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.7)
Dataset URL: https://www.kaggle.com/datasets/kpgeek/kitti-roadlane-detection-dataset-224-x-224
License(s): unknown
Downloading kitti-roadlane-detection-dataset-224-x-224.zip to /content
95% 238M/250M [00:01<00:00, 193MB/s]
100% 250M/250M [00:01<00:00, 158MB/s]
Total image files: 289, Total mask files: 290
Loaded 289 images and 289 masks
Training images: (231, 224, 224, 3), Validation images: (58, 224, 224, 3)
Training masks: (231, 224, 224, 1), Validation masks: (58, 224, 224, 1)
Epoch 1/10
15/15 [==============================] - 72s 2s/step - loss: 4.4682 - accuracy: 0.0121 - val_loss: 0.6355 - val_accuracy: 0.0096
Epoch 2/10
15/15 [==============================] - 9s 627ms/step - loss: 0.6264 - accuracy: 0.0122 - val_loss: 0.6230 - val_accuracy: 0.0096
Epoch 3/10
15/15 [==============================] - 9s 630ms/step - loss: 0.6227 - accuracy: 0.0122 - val_loss: 0.6211 - val_accuracy: 0.0096
Epoch 4/10
15/15 [==============================] - 9s 635ms/step - loss: 0.6215 - accuracy: 0.0122 - val_loss: 0.6208 - val_accuracy: 0.0096
Epoch 5/10
15/15 [==============================] - 10s 641ms/step - loss: 0.6213 - accuracy: 0.0122 - val_loss: 0.6207 - val_accuracy: 0.0096
Epoch 6/10
15/15 [==============================] - 9s 603ms/step - loss: 0.6210 - accuracy: 0.0122 - val_loss: 0.6203 - val_accuracy: 0.0096
Epoch 7/10
15/15 [==============================] - 9s 601ms/step - loss: 0.6206 - accuracy: 0.0122 - val_loss: 0.6198 - val_accuracy: 0.0096
Epoch 8/10
15/15 [==============================] - 9s 598ms/step - loss: 0.6203 - accuracy: 0.0122 - val_loss: 0.6196 - val_accuracy: 0.0096
Epoch 9/10
15/15 [==============================] - 9s 637ms/step - loss: 0.6202 - accuracy: 0.0122 - val_loss: 0.6195 - val_accuracy: 0.0096
Epoch 10/10
15/15 [==============================] - 9s 633ms/step - loss: 0.6200 - accuracy: 0.0122 - val_loss: 0.6193 - val_accuracy: 0.0096
th (1).jpg
th (1).jpg(image/jpeg) - 3856 bytes, last modified: 6/9/2024 - 100% done
Saving th (1).jpg to th (1).jpg
1/1 [==============================] - 2s 2s/step
now the accuracy is less aand the image is not semantic segmented please i need to highlight whole road not just lane lines
Thank you for your patience. Let’s address the issues step-by-step to ensure the U-Net model is correctly trained for semantic segmentation of the entire road, not just the lane lines.

### Key Points to Address:
1. Model Training Issue: The accuracy is low, and the model may not be learning correctly.
2. Semantic Segmentation of Entire Road: Ensure the model is trained to segment the whole road, not just lane lines.

### Steps to Improve the Model:
1. Check Data Preprocessing: Ensure masks are correctly prepared for the entire road.
2. Model Architecture: Ensure the U-Net architecture is suitable for the task.
3. Training Parameters: Adjust learning rate, loss function, and metrics.
4. Data Augmentation: Use data augmentation to improve model generalization.

### Final Script with Improvements

# Install necessary libraries (if not already)
!pip install opencv-python-headless
!pip install tensorflow
!pip install kaggle

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from google.colab import files
import os

# Set up Kaggle API credentials
os.environ[‘KAGGLE_USERNAME’] = ‘your_kaggle_username’
os.environ[‘KAGGLE_KEY’] = ‘your_kaggle_key’

# Download the dataset from Kaggle
!kaggle datasets download -d kpgeek/kitti-roadlane-detection-dataset-224-x-224
!unzip -oq kitti-roadlane-detection-dataset-224-x-224.zip -d kitti_road

######################################################################
# 1. Define U-Net Model
######################################################################

def unet_model(input_size=(224, 224, 3)):
inputs = Input(input_size)
conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(inputs)
conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(pool1)
conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(pool2)
conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(pool3)
conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(pool4)
conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(conv5)

up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)
conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(up6)
conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv6)

up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)
conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(up7)
conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv7)

up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)
conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(up8)
conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv8)

up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)
conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(up9)
conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv9)

conv10 = Conv2D(1, 1, activation=‘sigmoid’)(conv9)

model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=Adam(learning_rate=1e-4), loss=‘binary_crossentropy’, metrics=[‘accuracy’])

return model

# Load the U-Net model
unet = unet_model()

######################################################################
# 2. Load Dataset - ensuring filenames are matched correctly
######################################################################

def load_data(image_dir, mask_dir, img_size=(224, 224)):
images = []
masks = []

image_files = sorted(os.listdir(image_dir)) # Ensure consistent order
mask_files = {os.path.splitext(os.path.basename(f))[0].replace(‘gt_image_’, ‘image_’): f for f in os.listdir(mask_dir)}

print(f"Total image files: {len(image_files)}, Total mask files: {len(mask_files)}“)

for img_file in image_files:
img_path = os.path.join(image_dir, img_file)
img_name, _ = os.path.splitext(img_file)

if img_name in mask_files:
mask_path = os.path.join(mask_dir, mask_files[img_name])

img = cv2.imread(img_path)
if img is None:
print(f"Error loading image: {img_path}”)
continue

img = cv2.resize(img, img_size)
images.append(img)

mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
if mask is None:
print(f"Error loading mask: {mask_path}“)
continue

mask = cv2.resize(mask, img_size)
masks.append(mask)
else:
print(f"Missing file: {img_path} or {os.path.join(mask_dir, mask_files.get(img_name, ‘’))}”)

if not images or not masks:
print(“No images or masks loaded. Check your dataset.”)
return None, None

images = np.array(images)
masks = np.array(masks)
images = images.astype(‘float32’) / 255.0
masks = masks.astype(‘float32’) / 255.0
masks = np.expand_dims(masks, axis=-1)
return images, masks

# Paths updated as per directory structure provided
train_image_dir = ‘kitti_road/data_road_224/training/image_2’
train_mask_dir = ‘kitti_road/data_road_224/training/gt_image_2’

# Load training data
train_images, train_masks = load_data(train_image_dir, train_mask_dir)

# Debug: Check dataset size after loading
if train_images is not None and train_masks is not None:
print(f"Loaded {len(train_images)} images and {len(train_masks)} masks")
else:
print(“Failed to load images or masks.”)

# Split the dataset into training and validation sets
if train_images is not None and train_masks is not None:
train_images, val_images, train_masks, val_masks = train_test_split(train_images, train_masks, test_size=0.2, random_state=42)

# Check sizes of the datasets
print(f"Training images: {train_images.shape}, Validation images: {val_images.shape}“)
print(f"Training masks: {train_masks.shape}, Validation masks: {val_masks.shape}”)

# Train the U-Net model
unet.fit(train_images, train_masks, validation_data=(val_images, val_masks), epochs=10, batch_size=16)

######################################################################
# 3. Define Functions
######################################################################

# Function to dehaze the image using the method described in the paper
def dehaze_image(image):
# Step 1: Apply Improved Retinex Algorithm
def retinex_ssr(img, sigma):
img = img.astype(np.float32) + 1.0

continue lets get that semantic whole road segment as in gt_image_2 dataset with good accuracy like 80 90 % come on
Let’s continue refining the script to improve the accuracy of semantic segmentation for the entire road. We’ll take the following steps:

1. Data Preprocessing: Ensure masks are correctly prepared to segment the entire road.
2. Model Architecture: Keep the U-Net architecture but potentially refine some hyperparameters.
3. Training Parameters: Adjust learning rate, batch size, and loss function.
4. Data Augmentation: Add data augmentation to improve model generalization.
5. Additional Debugging: Check and visualize predictions to debug further.

Here’s an improved version of the script:

### Improved Code Script

# Install necessary libraries (if not already)
!pip install opencv-python-headless
!pip install tensorflow
!pip install kaggle

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from google.colab import files
import os

# Set up Kaggle API credentials
os.environ[‘KAGGLE_USERNAME’] = ‘your_kaggle_username’
os.environ[‘KAGGLE_KEY’] = ‘your_kaggle_key’

# Download the dataset from Kaggle
!kaggle datasets download -d kpgeek/kitti-roadlane-detection-dataset-224-x-224
!unzip -oq kitti-roadlane-detection-dataset-224-x-224.zip -d kitti_road

################################################################################
# 1. Define U-Net Model
################################################################################

def unet_model(input_size=(224, 224, 3)):
    inputs = Input(input_size)
    conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(inputs)
    conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(pool1)
    conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(pool2)
    conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(pool3)
    conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(pool4)
    conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(conv5)

    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)
    conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(up6)
    conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv6)

    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)
    conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(up7)
    conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv7)

    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)
    conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(up8)
    conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv8)

    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)
    conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(up9)
    conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv9)

    conv10 = Conv2D(1, 1, activation=‘sigmoid’)(conv9)

    model = Model(inputs=[inputs], outputs=[conv10])
    model.compile(optimizer=Adam(learning_rate=1e-4), loss=‘binary_crossentropy’, metrics=[‘accuracy’])

    return model

# Load the U-Net model
unet = unet_model()

################################################################################
# 2. Load Dataset - ensuring filenames are matched correctly
################################################################################

def load_data(image_dir, mask_dir, img_size=(224, 224)):
    images = []
    masks = []

    image_files = sorted(os.listdir(image_dir))  # Ensure consistent order
    mask_files = {os.path.splitext(os.path.basename(f))[0].replace(‘gt_image_’, ‘image_’): f for f in os.listdir(mask_dir)}

    print(f"Total image files: {len(image_files)}, Total mask files: {len(mask_files)}“)

    for img_file in image_files:
        img_path = os.path.join(image_dir, img_file)
        img_name, _ = os.path.splitext(img_file)

        if img_name in mask_files:
            mask_path = os.path.join(mask_dir, mask_files[img_name])

            img = cv2.imread(img_path)
            if img is None:
                print(f"Error loading image: {img_path}”)
                continue

            img = cv2.resize(img, img_size)
            images.append(img)

            mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
            if mask is None:
                print(f"Error loading mask: {mask_path}“)
                continue

            mask = cv2.resize(mask, img_size)
            mask = np.where(mask > 0, 1, 0)  # Binary mask
            masks.append(mask)
        else:
            print(f"Missing file: {img_path} or {os.path.join(mask_dir, mask_files.get(img_name, ‘’))}”)

    if not images or not masks:
        print(“No images or masks loaded. Check your dataset.”)
        return None, None

    images = np.array(images)
    masks = np.array(masks)
    images = images.astype(‘float32’) / 255.0
    masks = masks.astype(‘float32’) / 255.0
    masks = np.expand_dims(masks, axis=-1)
    return images, masks

# Paths updated as per directory structure provided
train_image_dir = ‘kitti_road/data_road_224/training/image_2’
train_mask_dir = ‘kitti_road/data_road_224/training/gt_image_2’

# Load training data
train_images, train_masks = load_data(train_image_dir, train_mask_dir)

# Debug: Check dataset size after loading
if train_images is not None and train_masks is not None:
    print(f"Loaded {len(train_images)} images and {len(train_masks)} masks")
else:
    print(“Failed to load images or masks.”)

# Split the dataset into training and validation sets
if train_images is not None and train_masks is not None:
    train_images, val_images, train_masks, val_masks = train_test_split(train_images, train_masks, test_size=0.2, random_state=42)

    # Check sizes of the datasets
    print(f"Training images: {train_images.shape}, Validation images: {val_images.shape}“)
    print(f"Training masks: {train_masks.shape}, Validation masks: {val_masks.shape}”)

    # Train the U-Net model with data augmentation
    datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rotation_range=20,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode=‘nearest’
    )

    # Fit the model
    unet.fit(datagen.flow(train_images, train_masks, batch_size=16), validation_data=(val_images, val_masks), epochs=10)

################################################################################
# 3. Predictions and Visualization
################################################################################

# Function to visualize predictions
def display_mask(mask):
    plt.imshow(mask.squeeze(), cmap=“gray”)
    plt.axis(“off”)
    plt.show()

# Predicting and visualizing a random sample from the validation set
index = np.random.randint(0, len(val_images))
sample_image = val_images[index]
sample_mask = val_masks[index]
predicted_mask = unet.predict(np.expand_dims(sample_image, axis=0))

# Display the sample image
plt.imshow(sample_image)
plt.axis(“off”)
plt.show()

# Display the ground truth mask
display_mask(sample_mask)

# Display the predicted mask
display_mask(predicted_mask)


### Explanation:
1. U-Net Model: The U-Net architecture remains the same.
2. Load Data: Masks are binarized to ensure proper segmentation. We also output debug information to ensure data is loaded correctly.
3. Data Augmentation: Added data augmentation to improve generalization.
4. Training Parameters: Updated the training loop to include data augmentation.

After running the above script, evaluate the model performance. If accuracy improves, but not yet to desired levels, consider extending the number of epochs, or further tuning the hyperparameters, such as learning rate and batch size. Additionally, refining data augmentation strategies or even using more advanced architectures like ResNet-based U-Net could further improve performance.
Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.10/dist-packages (4.10.0.82)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python-headless) (1.25.2)
Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.15.0)
Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.4.0)
Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3)
Requirement already satisfied: flatbuffers>=23.5.26 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.3.25)
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.5.4)
Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.9.0)
Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (18.1.1)
Requirement already satisfied: ml-dtypes~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.25.2)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.0)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.20.3)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (67.7.2)
Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.4.0)
Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.12.1)
Requirement already satisfied: wrapt<1.15,>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.14.1)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.37.0)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.64.1)
Requirement already satisfied: tensorboard<2.16,>=2.15 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.2)
Requirement already satisfied: tensorflow-estimator<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: keras<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.43.0)
Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.27.0)
Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (1.2.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.6)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.31.0)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (0.7.2)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.0.3)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (5.3.3)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.4.0)
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (4.9)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (1.3.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2024.6.2)
Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.16,>=2.15->tensorflow) (2.1.5)
Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.6.0)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (3.2.2)
Requirement already satisfied: kaggle in /usr/local/lib/python3.10/dist-packages (1.6.14)
Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.10/dist-packages (from kaggle) (1.16.0)
Requirement already satisfied: certifi>=2023.7.22 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2024.6.2)
Requirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.8.2)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.31.0)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from kaggle) (4.66.4)
Requirement already satisfied: python-slugify in /usr/local/lib/python3.10/dist-packages (from kaggle) (8.0.4)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.0.7)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from kaggle) (6.1.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->kaggle) (0.5.1)
Requirement already satisfied: text-unidecode>=1.3 in /usr/local/lib/python3.10/dist-packages (from python-slugify->kaggle) (1.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.7)
Dataset URL: https://www.kaggle.com/datasets/kpgeek/kitti-roadlane-detection-dataset-224-x-224
License(s): unknown
Downloading kitti-roadlane-detection-dataset-224-x-224.zip to /content
96% 240M/250M [00:00<00:00, 282MB/s]
100% 250M/250M [00:00<00:00, 272MB/s]
Total image files: 289, Total mask files: 290
Loaded 289 images and 289 masks
Training images: (231, 224, 224, 3), Validation images: (58, 224, 224, 3)
Training masks: (231, 224, 224, 1), Validation masks: (58, 224, 224, 1)
Epoch 1/10
15/15 [==============================] - 105s 5s/step - loss: 0.3845 - accuracy: 0.0116 - val_loss: 0.0785 - val_accuracy: 0.0096
Epoch 2/10
15/15 [==============================] - 9s 585ms/step - loss: 0.0684 - accuracy: 0.0122 - val_loss: 0.0561 - val_accuracy: 0.0096
Epoch 3/10
15/15 [==============================] - 9s 592ms/step - loss: 0.0480 - accuracy: 0.0122 - val_loss: 0.0365 - val_accuracy: 0.0096
Epoch 4/10
15/15 [==============================] - 9s 593ms/step - loss: 0.0330 - accuracy: 0.0122 - val_loss: 0.0288 - val_accuracy: 0.0096
Epoch 5/10
15/15 [==============================] - 9s 596ms/step - loss: 0.0277 - accuracy: 0.0122 - val_loss: 0.0273 - val_accuracy: 0.0096
Epoch 6/10
15/15 [==============================] - 9s 595ms/step - loss: 0.0263 - accuracy: 0.0122 - val_loss: 0.0260 - val_accuracy: 0.0096
Epoch 7/10
15/15 [==============================] - 9s 600ms/step - loss: 0.0258 - accuracy: 0.0122 - val_loss: 0.0257 - val_accuracy: 0.0096
Epoch 8/10
15/15 [==============================] - 10s 646ms/step - loss: 0.0257 - accuracy: 0.0122 - val_loss: 0.0257 - val_accuracy: 0.0096
Epoch 9/10
15/15 [==============================] - 10s 650ms/step - loss: 0.0256 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096
Epoch 10/10
15/15 [==============================] - 9s 613ms/step - loss: 0.0255 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096
1/1 [==============================] - 2s 2s/step

very less accuracy and its not accepting my input image as the previous code
# Install necessary libraries (if not already)
!pip install opencv-python-headless
!pip install tensorflow
!pip install kaggle

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from google.colab import files
import os

# Set up Kaggle API credentials
os.environ[‘KAGGLE_USERNAME’] = ‘saisai6567’
os.environ[‘KAGGLE_KEY’] = ‘a26b5bb35240ac0aa00506ef1db25bb9’

# Download the dataset from Kaggle
!kaggle datasets download -d kpgeek/kitti-roadlane-detection-dataset-224-x-224
!unzip -oq kitti-roadlane-detection-dataset-224-x-224.zip -d kitti_road

######################################################################
# 1. Define U-Net Model
######################################################################

def unet_model(input_size=(224, 224, 3)):
inputs = Input(input_size)
conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(inputs)
conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(pool1)
conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(pool2)
conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(pool3)
conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(pool4)
conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(conv5)

up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)
conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(up6)
conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv6)

up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)
conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(up7)
conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv7)

up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)
conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(up8)
conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv8)

up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)
conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(up9)
conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv9)

conv10 = Conv2D(1, 1, activation=‘sigmoid’)(conv9)

model = Model(inputs=[inputs], outputs=[conv10])
model.compile(optimizer=‘adam’, loss=‘binary_crossentropy’, metrics=[‘accuracy’])

return model

# Load the U-Net model
unet = unet_model()

######################################################################
# 2. Load Dataset - ensuring filenames are matched correctly
######################################################################


def load_data(image_dir, mask_dir, img_size=(224, 224)):
images = []
masks = []

image_files = sorted(os.listdir(image_dir)) # Ensure consistent order
mask_files = {os.path.splitext(os.path.basename(f))[0].replace(‘gt_’, ‘’): f for f in os.listdir(mask_dir)}

print(f"Total image files: {len(image_files)}, Total mask files: {len(mask_files)}“)

for img_file in image_files:
img_path = os.path.join(image_dir, img_file)
img_name, _ = os.path.splitext(img_file)

if img_name in mask_files:
mask_path = os.path.join(mask_dir, mask_files[img_name])

img = cv2.imread(img_path)
if img is None:
print(f"Error loading image: {img_path}”)
continue

img = cv2.resize(img, img_size)
images.append(img)

mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
if mask is None:
print(f"Error loading mask: {mask_path}“)
continue

mask = cv2.resize(mask, img_size)
masks.append(mask)
else:
print(f"Missing file: {img_path} or {os.path.join(mask_dir, mask_files.get(img_name, ‘’))}”)

if not images or not masks:
print(“No images or masks loaded. Check your dataset.”)
return None, None

images = np.array(images)
masks = np.array(masks)
images = images.astype(‘float32’) / 255.0
masks = masks.astype(‘float32’) / 255.0
masks = np.expand_dims(masks, axis=-1)
return images, masks



# Paths updated as per directory structure provided
train_image_dir = ‘/content/kitti_road/data_road_224/training/image_2’
train_mask_dir = ‘/content/kitti_road/data_road_224/training/gt_image_2’

# Load training data
train_images, train_masks = load_data(train_image_dir, train_mask_dir)

# Debug: Check dataset size after loading
if train_images is not None and train_masks is not None:
print(f"Loaded {len(train_images)} images and {len(train_masks)} masks")
else:
print(“Failed to load images or masks.”)

# Split the dataset into training and validation sets
if train_images is not None and train_masks is not None:
train_images, val_images, train_masks, val_masks = train_test_split(train_images, train_masks, test_size=0.2, random_state=42)

# Check sizes of the datasets
print(f"Training images: {train_images.shape}, Validation images: {val_images.shape}“)
print(f"Training masks: {train_masks.shape}, Validation masks: {val_masks.shape}”)

# Train the U-Net model
unet.fit(train_images, train_masks, validation_data=(val_images, val_masks), epochs=10, batch_size=16)

######################################################################
# 3. Define Functions
######################################################################

# Function to dehaze the image using the method described in the paper
def dehaze_image(image):
# Step 1: Apply Improved Retinex Algorithm
def retinex_ssr(img, sigma):
img = img.astype(np.float32) + 1.0
retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
return retinex

def retinex_msr(img, sigma_list):
msr = np.zeros_like(img, dtype=np.float32) # Ensure correct dtype
for sigma in sigma_list:
msr += retinex_ssr(img, sigma) # Accumulate in float32 format
msr = msr / len(sigma_list)
return msr

sigma_list = [15, 80, 250]
img_msr = retinex_msr(image, sigma_list)
img_msr = np.uint8(cv2.normalize(img_msr, None, 0, 255, cv2.NORM_MINMAX))

# Step 2: Apply Hessian Matrix for Lane Line Strengthening
gray = cv2.cvtColor(img_msr, cv2.COLOR_BGR2GRAY)
hessian = cv2.cornerHarris(gray, 2, 3, 0.04)
hessian = cv2.dilate(hessian, None)
img_msr[hessian > 0.01 * hessian.max()] = [0, 0, 255]

return img_msr

# Preprocess the image for segmentation
def preprocess_image_segmentation(image):
image = cv2.resize(image, (224, 224))
image = image.astype(‘float32’) / 255.0
image = np.expand_dims(image, axis=0)
return image

# Perform road segmentation using U-Net
def segment_road(image, model):
image = preprocess_image_segmentation(image)
mask = model.predict(image)
mask = (mask[0] > 0.5).astype(‘uint8’) * 255
mask = cv2.resize(mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST)
return mask

# Combine the original image with the segmentation mask
def highlight_road(image, mask):
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
highlighted_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 3)
return highlighted_image

######################################################################
# 4. Upload and Process the Image
######################################################################

uploaded = files.upload()
image_path = list(uploaded.keys())[0]
image = cv2.imread(image_path)
original_height, original_width = image.shape[:2]

# Dehaze the image
dehazed_image = dehaze_image(image)

# Segment the road in the dehazed image
road_mask = segment_road(dehazed_image, unet)
highlighted_image = highlight_road(dehazed_image, road_mask)

# Display the result
plt.figure(figsize=(15, 15))
plt.subplot(1, 3, 1)
plt.title(“Original Image”)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis(‘off’)

plt.subplot(1, 3, 2)
plt.title(“Dehazed Image”)
plt.imshow(cv2.cvtColor(dehazed_image, cv2.COLOR_BGR2RGB))
plt.axis(‘off’)

plt.subplot(1, 3, 3)
plt.title(“Road Highlighted”)
plt.imshow(cv2.cvtColor(highlighted_image, cv2.COLOR_BGR2RGB))
plt.axis(‘off’)

plt.show()

and its gibberish as accuracy is negligible hope you fix it i mean u net semantic segmentation is like good fit tech do it
Let’s address the issues to improve the U-Net model’s performance for road segmentation. Here are the key steps we will follow:

1. Model Architecture: Ensure the U-Net model is correctly defined.
2. Data Preprocessing: Ensure masks are correctly prepared for the entire road.
3. Training Parameters: Adjust learning rate, batch size, loss function, and metrics.
4. Data Augmentation: Use data augmentation to improve model generalization.
5. Debugging and Visualization: Visualize predictions to debug and check the model’s performance.

### Updated Code Script

# Install necessary libraries (if not already)
!pip install opencv-python-headless
!pip install tensorflow
!pip install kaggle

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from google.colab import files
import os

# Set up Kaggle API credentials
os.environ[‘KAGGLE_USERNAME’] = ‘saisai6567’
os.environ[‘KAGGLE_KEY’] = ‘a26b5bb35240ac0aa00506ef1db25bb9’

# Download the dataset from Kaggle
!kaggle datasets download -d kpgeek/kitti-roadlane-detection-dataset-224-x-224
!unzip -oq kitti-roadlane-detection-dataset-224-x-224.zip -d kitti_road

################################################################################
# 1. Define U-Net Model
################################################################################

def unet_model(input_size=(224, 224, 3)):
    inputs = Input(input_size)
    conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(inputs)
    conv1 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(pool1)
    conv2 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(pool2)
    conv3 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(pool3)
    conv4 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(pool4)
    conv5 = Conv2D(1024, 3, activation=‘relu’, padding=‘same’)(conv5)

    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)
    conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(up6)
    conv6 = Conv2D(512, 3, activation=‘relu’, padding=‘same’)(conv6)

    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)
    conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(up7)
    conv7 = Conv2D(256, 3, activation=‘relu’, padding=‘same’)(conv7)

    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)
    conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(up8)
    conv8 = Conv2D(128, 3, activation=‘relu’, padding=‘same’)(conv8)

    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)
    conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(up9)
    conv9 = Conv2D(64, 3, activation=‘relu’, padding=‘same’)(conv9)

    conv10 = Conv2D(1, 1, activation=‘sigmoid’)(conv9)

    model = Model(inputs=[inputs], outputs=[conv10])
    model.compile(optimizer=Adam(learning_rate=1e-4), loss=‘binary_crossentropy’, metrics=[‘accuracy’])

    return model

# Load the U-Net model
unet = unet_model()

################################################################################
# 2. Load Dataset - ensuring filenames are matched correctly
################################################################################

def load_data(image_dir, mask_dir, img_size=(224, 224)):
    images = []
    masks = []

    image_files = sorted(os.listdir(image_dir))  # Ensure consistent order
    mask_files = {os.path.splitext(os.path.basename(f))[0].replace(‘gt_’, ‘’): f for f in os.listdir(mask_dir)}

    print(f"Total image files: {len(image_files)}, Total mask files: {len(mask_files)}“)

    for img_file in image_files:
        img_path = os.path.join(image_dir, img_file)
        img_name, _ = os.path.splitext(img_file)

        if img_name in mask_files:
            mask_path = os.path.join(mask_dir, mask_files[img_name])

            img = cv2.imread(img_path)
            if img is None:
                print(f"Error loading image: {img_path}”)
                continue

            img = cv2.resize(img, img_size)
            images.append(img)

            mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
            if mask is None:
                print(f"Error loading mask: {mask_path}“)
                continue

            mask = cv2.resize(mask, img_size)
            mask = np.where(mask > 0, 1, 0)  # Binary mask
            masks.append(mask)
        else:
            print(f"Missing file: {img_path} or {os.path.join(mask_dir, mask_files.get(img_name, ‘’))}”)

    if not images or not masks:
        print(“No images or masks loaded. Check your dataset.”)
        return None, None

    images = np.array(images)
    masks = np.array(masks)
    images = images.astype(‘float32’) / 255.0
    masks = masks.astype(‘float32’) / 255.0
    masks = np.expand_dims(masks, axis=-1)
    return images, masks

# Paths updated as per directory structure provided
train_image_dir = ‘/content/kitti_road/data_road_224/training/image_2’
train_mask_dir = ‘/content/kitti_road/data_road_224/training/gt_image_2’

# Load training data
train_images, train_masks = load_data(train_image_dir, train_mask_dir)

# Debug: Check dataset size after loading
if train_images is not None and train_masks is not None:
    print(f"Loaded {len(train_images)} images and {len(train_masks)} masks")
else:
    print(“Failed to load images or masks.”)

# Split the dataset into training and validation sets
if train_images is not None and train_masks is not None:
    train_images, val_images, train_masks, val_masks = train_test_split(train_images, train_masks, test_size=0.2, random_state=42)

    # Check sizes of the datasets
    print(f"Training images: {train_images.shape}, Validation images: {val_images.shape}“)
    print(f"Training masks: {train_masks.shape}, Validation masks: {val_masks.shape}”)

    # Data Augmentation
    datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rotation_range=20,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode=‘nearest’
    )

    # Fit the model
    unet.fit(datagen.flow(train_images, train_masks, batch_size=16), validation_data=(val_images, val_masks), epochs=10)

################################################################################
# 3. Predictions and Visualization
################################################################################

# Function to visualize predictions
def display_mask(mask):
    plt.imshow(mask.squeeze(), cmap=“gray”)
    plt.axis(“off”)
    plt.show()

# Predicting and visualizing a random sample from the validation set
index = np.random.randint(0, len(val_images))
sample_image = val_images[index]
sample_mask = val_masks[index]
predicted_mask = unet.predict(np.expand_dims(sample_image, axis=0))

# Display the sample image
plt.imshow(sample_image)
plt.axis(“off”)
plt.show()

# Display the ground truth mask
display_mask(sample_mask)

# Display the predicted mask
display_mask(predicted_mask)

################################################################################
# 4. Upload and Process the Image
################################################################################

uploaded = files.upload()
image_path = list(uploaded.keys())[0]
image = cv2.imread(image_path)
original_height, original_width = image.shape[:2]

# Preprocess the image for segmentation
def preprocess_image_segmentation(image):
    image = cv2.resize(image, (224, 224))
    image = image.astype(‘float32’) / 255.0
    image = np.expand_dims(image, axis=0)
    return image

# Perform road segmentation using U-Net
def segment_road(image, model):
    image = preprocess_image_segmentation(image)
    mask = model.predict(image)
    mask = (mask[0] > 0.5).astype(‘uint8’) * 255
    mask = cv2.resize(mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST)
    return mask

# Combine the original image with the segmentation mask
def highlight_road(image, mask):
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    highlighted_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 3)
    return highlighted_image

# Segment the road in the uploaded image
road_mask = segment_road(image, unet)
highlighted_image = highlight_road(image, road_mask)

# Display the result
plt.figure(figsize=(15, 15))
plt.subplot(1, 3, 1)
plt.title(“Original Image”)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis(‘off’)

plt.subplot(1, 3, 2)
plt.title(“Segmented Mask”)
plt.imshow(road_mask, cmap=“gray”)
plt.axis(‘off’)

plt.subplot(1, 3, 3)
plt.title(“Road Highlighted”)
plt.imshow(cv2.cvtColor(highlighted_image, cv2.COLOR_BGR2RGB))
plt.axis(‘off’)

plt.show()


### Explanation of the Changes:
1. Model Architecture: Ensured the U-Net model is correctly defined and compiled with a suitable learning rate.
2. Data Preprocessing: Ensured masks are binarized to correctly segment the entire road.
3. Training Parameters: Adjusted learning rate and added data augmentation to improve model generalization.
4. Debugging and Visualization: Added functions to visualize predictions and debug the model’s performance.

Run the updated script and evaluate the model’s performance. If accuracy improves but is not yet at the desired levels, consider further tuning the hyperparameters or extending the number of epochs. Additionally, refining data augmentation strategies or using more advanced architectures like ResNet-based U-Net could further improve performance.
it does nothing the segmented mask is a black blanck image and accuracy is horrible i mean whats happening its unet for semantic segmentation accuracy should be way better and whats that output

Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.10/dist-packages (4.10.0.82) Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python-headless) (1.25.2) Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.15.0) Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.4.0) Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3) Requirement already satisfied: flatbuffers>=23.5.26 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.3.25) Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.5.4) Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0) Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.9.0) Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (18.1.1) Requirement already satisfied: ml-dtypes~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0) Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.25.2) Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0) Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.0) Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.20.3) Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (67.7.2) Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0) Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.4.0) Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.12.1) Requirement already satisfied: wrapt<1.15,>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.14.1) Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.37.0) Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.64.1) Requirement already satisfied: tensorboard<2.16,>=2.15 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.2) Requirement already satisfied: tensorflow-estimator<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0) Requirement already satisfied: keras<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0) Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.43.0) Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.27.0) Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (1.2.0) Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.6) Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.31.0) Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (0.7.2) Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.0.3) Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (5.3.3) Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.4.0) Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (4.9) Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (1.3.1) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.7) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2.0.7) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2024.6.2) Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.16,>=2.15->tensorflow) (2.1.5) Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.6.0) Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (3.2.2) Requirement already satisfied: kaggle in /usr/local/lib/python3.10/dist-packages (1.6.14) Requirement already satisfied: six>=1.10 in /usr/local/lib/python3.10/dist-packages (from kaggle) (1.16.0) Requirement already satisfied: certifi>=2023.7.22 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2024.6.2) Requirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.8.2) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.31.0) Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from kaggle) (4.66.4) Requirement already satisfied: python-slugify in /usr/local/lib/python3.10/dist-packages (from kaggle) (8.0.4) Requirement already satisfied: urllib3 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.0.7) Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from kaggle) (6.1.0) Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->kaggle) (0.5.1) Requirement already satisfied: text-unidecode>=1.3 in /usr/local/lib/python3.10/dist-packages (from python-slugify->kaggle) (1.3) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.7) Dataset URL: https://www.kaggle.com/datasets/kpgeek/kitti-roadlane-detection-dataset-224-x-224 License(s): unknown kitti-roadlane-detection-dataset-224-x-224.zip: Skipping, found more recently modified local copy (use --force to force download) Total image files: 289, Total mask files: 290 Loaded 289 images and 289 masks Training images: (231, 224, 224, 3), Validation images: (58, 224, 224, 3) Training masks: (231, 224, 224, 1), Validation masks: (58, 224, 224, 1) Epoch 1/10 15/15 [==============================] - 14s 610ms/step - loss: 0.3237 - accuracy: 0.0122 - val_loss: 0.0525 - val_accuracy: 0.0096 Epoch 2/10 15/15 [==============================] - 9s 575ms/step - loss: 0.0641 - accuracy: 0.0122 - val_loss: 0.0470 - val_accuracy: 0.0096 Epoch 3/10 15/15 [==============================] - 9s 583ms/step - loss: 0.0429 - accuracy: 0.0122 - val_loss: 0.0341 - val_accuracy: 0.0096 Epoch 4/10 15/15 [==============================] - 9s 586ms/step - loss: 0.0306 - accuracy: 0.0122 - val_loss: 0.0283 - val_accuracy: 0.0096 Epoch 5/10 15/15 [==============================] - 10s 630ms/step - loss: 0.0266 - accuracy: 0.0122 - val_loss: 0.0260 - val_accuracy: 0.0096 Epoch 6/10 15/15 [==============================] - 9s 589ms/step - loss: 0.0259 - accuracy: 0.0122 - val_loss: 0.0257 - val_accuracy: 0.0096 Epoch 7/10 15/15 [==============================] - 10s 636ms/step - loss: 0.0257 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096 Epoch 8/10 15/15 [==============================] - 10s 635ms/step - loss: 0.0256 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096 Epoch 9/10 15/15 [==============================] - 10s 650ms/step - loss: 0.0255 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096 Epoch 10/10 15/15 [==============================] - 9s 594ms/step - loss: 0.0255 - accuracy: 0.0122 - val_loss: 0.0256 - val_accuracy: 0.0096 1/1 [==============================] - 0s 185ms/step th (1).jpg th (1).jpg(image/jpeg) - 3856 bytes, last modified: 6/9/2024 - 100% done