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