give me only code part by removingoutputs and errors dont modify code just give it as it is
"Cell In[5], line 263
    260     return np.array([left_coords, right_coords], dtype=np.int32)
    262 image = cv2.imread(test_images[1])
--> 263 predict_lanes(image)

Cell In[5], line 204, in predict_lanes(image)
    201 roi = mask_img[50:220, 10:, :]
    202 image = image[50:220, 10:, :]
--> 204 mask_gray = cv2.cvtColor(roi, cv2.COLOR_RGB2GRAY)
    205 gray = cv2.GaussianBlur(mask_gray, (5, 5), 0)
    206 ret, thresh = cv2.threshold(mask_gray, 100, 255, cv2.THRESH_BINARY)

error: OpenCV(4.10.0) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = cv::impl::<unnamed>::NONE; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1


this was original code
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
#for dirname, _, filenames in os.walk('/kaggle/input'):
 #   for filename in filenames:
  #      print(os.path.join(dirname, filename))

# 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" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
!pip install torch_snippets
Collecting torch_snippets
  Downloading torch_snippets-0.524-py3-none-any.whl.metadata (6.4 kB)
Requirement already satisfied: fastcore in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (1.5.29)
Requirement already satisfied: matplotlib in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (3.7.4)
Requirement already satisfied: Pillow in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (9.5.0)
Requirement already satisfied: altair in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (5.2.0)
Requirement already satisfied: dill in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.3.7)
Requirement already satisfied: ipython in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (8.20.0)
Requirement already satisfied: loguru in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.7.2)
Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (1.24.4)
Requirement already satisfied: pandas in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (2.1.4)
Requirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (4.66.1)
Requirement already satisfied: rich in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (13.7.0)
Requirement already satisfied: PyYAML in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (6.0.1)
Requirement already satisfied: catalogue in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (2.0.10)
Requirement already satisfied: confection in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.1.4)
Requirement already satisfied: pydantic in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (2.5.3)
Collecting typing (from torch_snippets)
  Downloading typing-3.7.4.3.tar.gz (78 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.6/78.6 kB 3.0 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Requirement already satisfied: srsly in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (2.4.8)
Requirement already satisfied: typing-extensions in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (4.9.0)
Requirement already satisfied: wasabi in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (1.1.2)
Collecting jsonlines (from torch_snippets)
  Downloading jsonlines-4.0.0-py3-none-any.whl.metadata (1.6 kB)
Requirement already satisfied: imgaug>=0.4.0 in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.4.0)
Collecting xmltodict (from torch_snippets)
  Downloading xmltodict-0.13.0-py2.py3-none-any.whl.metadata (7.7 kB)
Requirement already satisfied: fuzzywuzzy in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.18.0)
Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (1.2.2)
Requirement already satisfied: nltk in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (3.2.4)
Requirement already satisfied: python-Levenshtein in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (0.23.0)
Collecting pre-commit (from torch_snippets)
  Downloading pre_commit-3.6.2-py2.py3-none-any.whl.metadata (1.3 kB)
Collecting pymupdf (from torch_snippets)
  Downloading PyMuPDF-1.23.26-cp310-none-manylinux2014_x86_64.whl.metadata (3.4 kB)
Requirement already satisfied: nbconvert in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (6.4.5)
Requirement already satisfied: nbformat in /opt/conda/lib/python3.10/site-packages (from torch_snippets) (5.9.2)
Collecting icecream (from torch_snippets)
  Downloading icecream-2.1.3-py2.py3-none-any.whl.metadata (1.4 kB)
Requirement already satisfied: six in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (1.16.0)
Requirement already satisfied: scipy in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (1.11.4)
Requirement already satisfied: scikit-image>=0.14.2 in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (0.22.0)
Requirement already satisfied: opencv-python in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (4.9.0.80)
Requirement already satisfied: imageio in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (2.33.1)
Requirement already satisfied: Shapely in /opt/conda/lib/python3.10/site-packages (from imgaug>=0.4.0->torch_snippets) (1.8.5.post1)
Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from altair->torch_snippets) (3.1.2)
Requirement already satisfied: jsonschema>=3.0 in /opt/conda/lib/python3.10/site-packages (from altair->torch_snippets) (4.20.0)
Requirement already satisfied: packaging in /opt/conda/lib/python3.10/site-packages (from altair->torch_snippets) (21.3)
Requirement already satisfied: toolz in /opt/conda/lib/python3.10/site-packages (from altair->torch_snippets) (0.12.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.10/site-packages (from pandas->torch_snippets) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas->torch_snippets) (2023.3.post1)
Requirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.10/site-packages (from pandas->torch_snippets) (2023.4)
Requirement already satisfied: annotated-types>=0.4.0 in /opt/conda/lib/python3.10/site-packages (from pydantic->torch_snippets) (0.6.0)
Requirement already satisfied: pydantic-core==2.14.6 in /opt/conda/lib/python3.10/site-packages (from pydantic->torch_snippets) (2.14.6)
Requirement already satisfied: pip in /opt/conda/lib/python3.10/site-packages (from fastcore->torch_snippets) (23.3.2)
Requirement already satisfied: colorama>=0.3.9 in /opt/conda/lib/python3.10/site-packages (from icecream->torch_snippets) (0.4.6)
Requirement already satisfied: pygments>=2.2.0 in /opt/conda/lib/python3.10/site-packages (from icecream->torch_snippets) (2.17.2)
Requirement already satisfied: executing>=0.3.1 in /opt/conda/lib/python3.10/site-packages (from icecream->torch_snippets) (2.0.1)
Requirement already satisfied: asttokens>=2.0.1 in /opt/conda/lib/python3.10/site-packages (from icecream->torch_snippets) (2.4.1)
Requirement already satisfied: decorator in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (5.1.1)
Requirement already satisfied: jedi>=0.16 in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (0.19.1)
Requirement already satisfied: matplotlib-inline in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (0.1.6)
Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (3.0.42)
Requirement already satisfied: stack-data in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (0.6.2)
Requirement already satisfied: traitlets>=5 in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (5.9.0)
Requirement already satisfied: exceptiongroup in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (1.2.0)
Requirement already satisfied: pexpect>4.3 in /opt/conda/lib/python3.10/site-packages (from ipython->torch_snippets) (4.8.0)
Requirement already satisfied: attrs>=19.2.0 in /opt/conda/lib/python3.10/site-packages (from jsonlines->torch_snippets) (23.2.0)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib->torch_snippets) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.10/site-packages (from matplotlib->torch_snippets) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib->torch_snippets) (4.47.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib->torch_snippets) (1.4.5)
Requirement already satisfied: pyparsing>=2.3.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib->torch_snippets) (3.1.1)
Requirement already satisfied: mistune<2,>=0.8.1 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.8.4)
Requirement already satisfied: jupyterlab-pygments in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.3.0)
Requirement already satisfied: jupyter-core in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (5.7.1)
Requirement already satisfied: entrypoints>=0.2.2 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.4)
Requirement already satisfied: bleach in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (6.1.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (1.5.0)
Requirement already satisfied: testpath in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.6.0)
Requirement already satisfied: defusedxml in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.7.1)
Requirement already satisfied: beautifulsoup4 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (4.12.2)
Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (0.5.13)
Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from nbconvert->torch_snippets) (2.1.3)
Requirement already satisfied: fastjsonschema in /opt/conda/lib/python3.10/site-packages (from nbformat->torch_snippets) (2.19.1)
Collecting cfgv>=2.0.0 (from pre-commit->torch_snippets)
  Downloading cfgv-3.4.0-py2.py3-none-any.whl.metadata (8.5 kB)
Collecting identify>=1.0.0 (from pre-commit->torch_snippets)
  Downloading identify-2.5.35-py2.py3-none-any.whl.metadata (4.4 kB)
Collecting nodeenv>=0.11.1 (from pre-commit->torch_snippets)
  Downloading nodeenv-1.8.0-py2.py3-none-any.whl.metadata (21 kB)
Requirement already satisfied: virtualenv>=20.10.0 in /opt/conda/lib/python3.10/site-packages (from pre-commit->torch_snippets) (20.21.0)
Collecting PyMuPDFb==1.23.22 (from pymupdf->torch_snippets)
  Downloading PyMuPDFb-1.23.22-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.4 kB)
Requirement already satisfied: Levenshtein==0.23.0 in /opt/conda/lib/python3.10/site-packages (from python-Levenshtein->torch_snippets) (0.23.0)
Requirement already satisfied: rapidfuzz<4.0.0,>=3.1.0 in /opt/conda/lib/python3.10/site-packages (from Levenshtein==0.23.0->python-Levenshtein->torch_snippets) (3.6.1)
Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/conda/lib/python3.10/site-packages (from rich->torch_snippets) (3.0.0)
Requirement already satisfied: joblib>=1.1.1 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->torch_snippets) (1.3.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn->torch_snippets) (3.2.0)
Requirement already satisfied: parso<0.9.0,>=0.8.3 in /opt/conda/lib/python3.10/site-packages (from jedi>=0.16->ipython->torch_snippets) (0.8.3)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /opt/conda/lib/python3.10/site-packages (from jsonschema>=3.0->altair->torch_snippets) (2023.12.1)
Requirement already satisfied: referencing>=0.28.4 in /opt/conda/lib/python3.10/site-packages (from jsonschema>=3.0->altair->torch_snippets) (0.32.1)
Requirement already satisfied: rpds-py>=0.7.1 in /opt/conda/lib/python3.10/site-packages (from jsonschema>=3.0->altair->torch_snippets) (0.16.2)
Requirement already satisfied: mdurl~=0.1 in /opt/conda/lib/python3.10/site-packages (from markdown-it-py>=2.2.0->rich->torch_snippets) (0.1.2)
Requirement already satisfied: jupyter-client>=6.1.5 in /opt/conda/lib/python3.10/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->torch_snippets) (7.4.9)
Requirement already satisfied: nest-asyncio in /opt/conda/lib/python3.10/site-packages (from nbclient<0.6.0,>=0.5.0->nbconvert->torch_snippets) (1.5.8)
Requirement already satisfied: setuptools in /opt/conda/lib/python3.10/site-packages (from nodeenv>=0.11.1->pre-commit->torch_snippets) (69.0.3)
Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.10/site-packages (from pexpect>4.3->ipython->torch_snippets) (0.7.0)
Requirement already satisfied: wcwidth in /opt/conda/lib/python3.10/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython->torch_snippets) (0.2.13)
Requirement already satisfied: networkx>=2.8 in /opt/conda/lib/python3.10/site-packages (from scikit-image>=0.14.2->imgaug>=0.4.0->torch_snippets) (3.2.1)
Requirement already satisfied: tifffile>=2022.8.12 in /opt/conda/lib/python3.10/site-packages (from scikit-image>=0.14.2->imgaug>=0.4.0->torch_snippets) (2023.12.9)
Requirement already satisfied: lazy_loader>=0.3 in /opt/conda/lib/python3.10/site-packages (from scikit-image>=0.14.2->imgaug>=0.4.0->torch_snippets) (0.3)
Requirement already satisfied: distlib<1,>=0.3.6 in /opt/conda/lib/python3.10/site-packages (from virtualenv>=20.10.0->pre-commit->torch_snippets) (0.3.8)
Requirement already satisfied: filelock<4,>=3.4.1 in /opt/conda/lib/python3.10/site-packages (from virtualenv>=20.10.0->pre-commit->torch_snippets) (3.13.1)
Collecting platformdirs<4,>=2.4 (from virtualenv>=20.10.0->pre-commit->torch_snippets)
  Downloading platformdirs-3.11.0-py3-none-any.whl.metadata (11 kB)
Requirement already satisfied: soupsieve>1.2 in /opt/conda/lib/python3.10/site-packages (from beautifulsoup4->nbconvert->torch_snippets) (2.5)
Requirement already satisfied: webencodings in /opt/conda/lib/python3.10/site-packages (from bleach->nbconvert->torch_snippets) (0.5.1)
Requirement already satisfied: pure-eval in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython->torch_snippets) (0.2.2)
Requirement already satisfied: pyzmq>=23.0 in /opt/conda/lib/python3.10/site-packages (from jupyter-client>=6.1.5->nbclient<0.6.0,>=0.5.0->nbconvert->torch_snippets) (24.0.1)
Requirement already satisfied: tornado>=6.2 in /opt/conda/lib/python3.10/site-packages (from jupyter-client>=6.1.5->nbclient<0.6.0,>=0.5.0->nbconvert->torch_snippets) (6.3.3)
Downloading torch_snippets-0.524-py3-none-any.whl (79 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 79.6/79.6 kB 5.0 MB/s eta 0:00:00
Downloading icecream-2.1.3-py2.py3-none-any.whl (8.4 kB)
Downloading jsonlines-4.0.0-py3-none-any.whl (8.7 kB)
Downloading pre_commit-3.6.2-py2.py3-none-any.whl (204 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 204.2/204.2 kB 10.8 MB/s eta 0:00:00
Downloading PyMuPDF-1.23.26-cp310-none-manylinux2014_x86_64.whl (4.4 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 MB 60.1 MB/s eta 0:00:00:00:0100:01
Downloading PyMuPDFb-1.23.22-py3-none-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (30.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.6/30.6 MB 53.9 MB/s eta 0:00:00:00:0100:01
Downloading xmltodict-0.13.0-py2.py3-none-any.whl (10.0 kB)
Downloading cfgv-3.4.0-py2.py3-none-any.whl (7.2 kB)
Downloading identify-2.5.35-py2.py3-none-any.whl (98 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.9/98.9 kB 6.5 MB/s eta 0:00:00
Downloading nodeenv-1.8.0-py2.py3-none-any.whl (22 kB)
Downloading platformdirs-3.11.0-py3-none-any.whl (17 kB)
Building wheels for collected packages: typing
  Building wheel for typing (setup.py) ... done
  Created wheel for typing: filename=typing-3.7.4.3-py3-none-any.whl size=26304 sha256=6adcf44e7ee511355e35e501b07aa877460f600933704ace681cf002b1bcd813
  Stored in directory: /root/.cache/pip/wheels/7c/d0/9e/1f26ebb66d9e1732e4098bc5a6c2d91f6c9a529838f0284890
Successfully built typing
Installing collected packages: xmltodict, typing, PyMuPDFb, platformdirs, nodeenv, jsonlines, identify, cfgv, pymupdf, icecream, pre-commit, torch_snippets
  Attempting uninstall: platformdirs
    Found existing installation: platformdirs 4.1.0
    Uninstalling platformdirs-4.1.0:
      Successfully uninstalled platformdirs-4.1.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
jupyterlab 4.0.11 requires jupyter-lsp>=2.0.0, but you have jupyter-lsp 1.5.1 which is incompatible.
jupyterlab-lsp 5.0.2 requires jupyter-lsp>=2.0.0, but you have jupyter-lsp 1.5.1 which is incompatible.
libpysal 4.9.2 requires packaging>=22, but you have packaging 21.3 which is incompatible.
libpysal 4.9.2 requires shapely>=2.0.1, but you have shapely 1.8.5.post1 which is incompatible.
momepy 0.7.0 requires shapely>=2, but you have shapely 1.8.5.post1 which is incompatible.
spopt 0.6.0 requires shapely>=2.0.1, but you have shapely 1.8.5.post1 which is incompatible.
Successfully installed PyMuPDFb-1.23.22 cfgv-3.4.0 icecream-2.1.3 identify-2.5.35 jsonlines-4.0.0 nodeenv-1.8.0 platformdirs-3.11.0 pre-commit-3.6.2 pymupdf-1.23.26 torch_snippets-0.524 typing-3.7.4.3 xmltodict-0.13.0
from torch_snippets import *
from torchvision import transforms
from sklearn.model_selection import train_test_split
device = 'cuda' if torch.cuda.is_available() else 'cpu'
import cv2 
import glob
tfms = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # imagenet
])
train_images=sorted(glob.glob("/kaggle/input/kitti-roadlane-detection-dataset-224-x-224/data_road_224/training/image_2/*.png"))
train_mask=sorted(glob.glob("/kaggle/input/kitti-roadlane-detection-dataset-224-x-224/data_road_224/training/gt_image_2/*.png"))
test_images=sorted(glob.glob("/kaggle/input/kitti-roadlane-detection-dataset-224-x-224/data_road_224/testing/image_2/*.png"))
class SegData(Dataset):
    def __init__(self,images,labels):
        self.images=images
        self.labels=labels
    def __len__(self):
        return len(self.images)
    def __getitem__(self, ix):
        image = cv2.imread(self.images[ix])
        image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (224,224))
        mask = read(self.labels[ix])
        mask = cv2.resize(mask, (224,224))
        #mask=cv2.cvtColor(mask,cv2.COLOR_BGR2RGB)
        return image, mask
    def choose(self): return self[randint(len(self.images))]
    def __len__(self):
        return len(self.images)
    def collate_fn(self, batch):
        ims, masks = list(zip(*batch))
        ims = torch.cat([tfms(im.copy()/255.)[None] for im in ims]).float().to(device)
        ce_masks = torch.cat([transforms.ToTensor()(mask)[None] for mask in masks]).float().to(device)
        return ims, ce_masks
train_dataset=SegData(train_images,train_mask)
#test_dataset=SegData(test_images,test_mask)
image,mask=train_dataset[100]
print(image.shape)
print(mask.shape)
plt.imshow(image)
plt.figure()
plt.imshow(mask)
(224, 224, 3)
(224, 224, 3)
<matplotlib.image.AxesImage at 0x793844126590>


trn_dl = DataLoader(train_dataset, batch_size=4, shuffle=True, collate_fn=train_dataset.collate_fn,drop_last=True)
#test_dl=DataLoader(test_dataset,batch_size=4,shuffle=True,collate_fn=test_dataset.collate_fn)
_,mask=next(iter(trn_dl))
mask.shape
torch.Size([4, 3, 224, 224])
def conv(in_channels, out_channels):
    return nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=(3,3),padding="same"),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
def up_conv(in_channels, out_channels):
    return nn.Sequential(
        nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True)
    )
class tinyUNet(nn.Module):
    def __init__(self, pretrained=True, out_channels=32):
        super().__init__()
        self.block1=conv(3,64)
        self.block2=conv(64,64)
        self.block3=conv(64,128)
        self.block4=conv(128,128)
        self.block5=conv(128,256)
        self.block6=conv(256,256)
        self.block7=conv(256,512)
        self.block8=conv(512,512)
        self.block9=conv(512,1024)
        self.maxpool=nn.MaxPool2d(kernel_size=(2,2),stride=2,padding=0,dilation=1,ceil_mode=False)
        self.up_conv6 = up_conv(1024, 512)
        self.conv6 = conv(512 + 512, 512)
        self.up_conv7 = up_conv(512, 256)
        self.conv7 = conv(256+256,256)
        self.up_conv8 = up_conv(128+128, 128)
        self.conv8 = conv(128 + 128, 128)
        self.up_conv9 = up_conv(128, 64)
        self.conv9 = conv(64 +64, 64)
        self.up_conv10 = up_conv(64, 32)
        self.conv10 = conv(32 + 64, 32)
        self.conv11 = nn.Conv2d(64, 3, kernel_size=1)
        self.sigmoid=nn.Sigmoid()
    def forward(self, x):
        block1 = self.block1(x)
        block2 = self.maxpool(self.block2(block1))
        block3 = self.block3(block2)
        block4 = self.maxpool(self.block4(block3))
        block5 = self.block5(block4)
        block6 = self.maxpool(self.block6(block5))
        block7 = self.block7(block6)
        block8 = self.maxpool(self.block8(block7))
        block9=self.block9(block8)

        x4 = self.up_conv6(block9)
        x4 = torch.cat([x4,block7],dim=1)
        x4=self.conv6(x4)
        x3 = self.up_conv7(x4)
        
        x3 = torch.cat([x3, block5], dim=1)
        x3 = self.conv7(x3)

        x2 = self.up_conv8(x3)
        x2 = torch.cat([x2, block3], dim=1)
        x2 = self.conv8(x2)

        x1 = self.up_conv9(x2)
        x1 = torch.cat([x1, block1], dim=1)
        x1 = self.conv9(x1)

        #x = self.up_conv10(x1)
        #x = torch.cat([x, block1], dim=1)
        #x = self.conv10(x)

        x = self.sigmoid(self.conv11(x1))

        return x
model=tinyUNet().to(device)
!pip install torchsummary
Collecting torchsummary
  Downloading torchsummary-1.5.1-py3-none-any.whl.metadata (296 bytes)
Downloading torchsummary-1.5.1-py3-none-any.whl (2.8 kB)
Installing collected packages: torchsummary
Successfully installed torchsummary-1.5.1
from torchsummary import summary
summary(model,(3,224,224))
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]           1,792
       BatchNorm2d-2         [-1, 64, 224, 224]             128
              ReLU-3         [-1, 64, 224, 224]               0
            Conv2d-4         [-1, 64, 224, 224]          36,928
       BatchNorm2d-5         [-1, 64, 224, 224]             128
              ReLU-6         [-1, 64, 224, 224]               0
         MaxPool2d-7         [-1, 64, 112, 112]               0
            Conv2d-8        [-1, 128, 112, 112]          73,856
       BatchNorm2d-9        [-1, 128, 112, 112]             256
             ReLU-10        [-1, 128, 112, 112]               0
           Conv2d-11        [-1, 128, 112, 112]         147,584
      BatchNorm2d-12        [-1, 128, 112, 112]             256
             ReLU-13        [-1, 128, 112, 112]               0
        MaxPool2d-14          [-1, 128, 56, 56]               0
           Conv2d-15          [-1, 256, 56, 56]         295,168
      BatchNorm2d-16          [-1, 256, 56, 56]             512
             ReLU-17          [-1, 256, 56, 56]               0
           Conv2d-18          [-1, 256, 56, 56]         590,080
      BatchNorm2d-19          [-1, 256, 56, 56]             512
             ReLU-20          [-1, 256, 56, 56]               0
        MaxPool2d-21          [-1, 256, 28, 28]               0
           Conv2d-22          [-1, 512, 28, 28]       1,180,160
      BatchNorm2d-23          [-1, 512, 28, 28]           1,024
             ReLU-24          [-1, 512, 28, 28]               0
           Conv2d-25          [-1, 512, 28, 28]       2,359,808
      BatchNorm2d-26          [-1, 512, 28, 28]           1,024
             ReLU-27          [-1, 512, 28, 28]               0
        MaxPool2d-28          [-1, 512, 14, 14]               0
           Conv2d-29         [-1, 1024, 14, 14]       4,719,616
      BatchNorm2d-30         [-1, 1024, 14, 14]           2,048
             ReLU-31         [-1, 1024, 14, 14]               0
  ConvTranspose2d-32          [-1, 512, 28, 28]       2,097,664
      BatchNorm2d-33          [-1, 512, 28, 28]           1,024
             ReLU-34          [-1, 512, 28, 28]               0
           Conv2d-35          [-1, 512, 28, 28]       4,719,104
      BatchNorm2d-36          [-1, 512, 28, 28]           1,024
             ReLU-37          [-1, 512, 28, 28]               0
  ConvTranspose2d-38          [-1, 256, 56, 56]         524,544
      BatchNorm2d-39          [-1, 256, 56, 56]             512
             ReLU-40          [-1, 256, 56, 56]               0
           Conv2d-41          [-1, 256, 56, 56]       1,179,904
      BatchNorm2d-42          [-1, 256, 56, 56]             512
             ReLU-43          [-1, 256, 56, 56]               0
  ConvTranspose2d-44        [-1, 128, 112, 112]         131,200
      BatchNorm2d-45        [-1, 128, 112, 112]             256
             ReLU-46        [-1, 128, 112, 112]               0
           Conv2d-47        [-1, 128, 112, 112]         295,040
      BatchNorm2d-48        [-1, 128, 112, 112]             256
             ReLU-49        [-1, 128, 112, 112]               0
  ConvTranspose2d-50         [-1, 64, 224, 224]          32,832
      BatchNorm2d-51         [-1, 64, 224, 224]             128
             ReLU-52         [-1, 64, 224, 224]               0
           Conv2d-53         [-1, 64, 224, 224]          73,792
      BatchNorm2d-54         [-1, 64, 224, 224]             128
             ReLU-55         [-1, 64, 224, 224]               0
           Conv2d-56          [-1, 3, 224, 224]             195
          Sigmoid-57          [-1, 3, 224, 224]               0
================================================================
Total params: 18,468,995
Trainable params: 18,468,995
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 569.62
Params size (MB): 70.45
Estimated Total Size (MB): 640.65
----------------------------------------------------------------
loss_fn=torch.nn.MSELoss()
optimizer=torch.optim.Adam(model.parameters(),lr=1e-4,weight_decay=1e-7)
def train_batch(model, data):
    model.train()
    ims, ce_masks = data
    _masks = model(ims)
    optimizer.zero_grad()
    loss=loss_fn(_masks,ce_masks)
    loss.backward()
    optimizer.step()
    return loss.item()

@torch.no_grad()
def validate_batch(model, data):
    model.eval()
    ims, masks = data
    _masks = model(ims)
    loss=loss_fn(_masks,masks)
    return loss.item()
n_epochs=20
log = Report(n_epochs)
for ex in range(n_epochs):
    N = len(trn_dl)
    for bx, data in enumerate(trn_dl):
        loss= train_batch(model, data)
        log.record(ex+(bx+1)/N, trn_loss=loss, end='\r')
    log.report_avgs(ex+1)
EPOCH: 1.000  trn_loss: 0.167  (9.44s - 179.33s remaining)
EPOCH: 2.000  trn_loss: 0.120  (16.71s - 150.35s remaining)
EPOCH: 3.000  trn_loss: 0.098  (23.99s - 135.92s remaining)
EPOCH: 4.000  trn_loss: 0.080  (31.18s - 124.72s remaining)
EPOCH: 5.000  trn_loss: 0.067  (38.38s - 115.14s remaining)
EPOCH: 6.000  trn_loss: 0.056  (45.57s - 106.34s remaining)
EPOCH: 7.000  trn_loss: 0.047  (52.76s - 97.99s remaining))
EPOCH: 8.000  trn_loss: 0.040  (60.00s - 90.00s remaining)
EPOCH: 9.000  trn_loss: 0.034  (67.22s - 82.15s remaining)
EPOCH: 10.000  trn_loss: 0.029  (74.53s - 74.53s remaining)
EPOCH: 11.000  trn_loss: 0.025  (81.78s - 66.91s remaining)
EPOCH: 12.000  trn_loss: 0.022  (88.99s - 59.33s remaining)
EPOCH: 13.000  trn_loss: 0.019  (96.16s - 51.78s remaining)
EPOCH: 14.000  trn_loss: 0.018  (103.35s - 44.29s remaining)
EPOCH: 15.000  trn_loss: 0.016  (110.55s - 36.85s remaining)
EPOCH: 16.000  trn_loss: 0.015  (117.79s - 29.45s remaining)
EPOCH: 17.000  trn_loss: 0.013  (125.04s - 22.07s remaining)
EPOCH: 18.000  trn_loss: 0.011  (132.31s - 14.70s remaining)
EPOCH: 19.000  trn_loss: 0.012  (139.60s - 7.35s remaining))
EPOCH: 20.000  trn_loss: 0.010  (146.82s - 0.00s remaining)
image=cv2.imread(test_images[-7])
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
image=cv2.resize(image,(224,224))
image_tensor=tfms(image/255.)[None].float().to(device)
with torch.no_grad():
    mask=model(image_tensor)
mask=mask[0].detach().permute(1,2,0).cpu()
plt.subplot(1,2,1)
plt.imshow(image)
plt.subplot(1,2,2)
plt.imshow(mask)
<matplotlib.image.AxesImage at 0x793844046a40>

PATH = "Kitty_lane_detector.pt"
torch.save(model.state_dict(), PATH)
mask_img=np.round(255*mask.numpy()).astype(np.uint8)
#roi=mask_img
roi=mask_img[50:220,10:,:]
image=image[50:220,10:,:]
plt.imshow(roi)
plt.figure()
plt.imshow(image)
<matplotlib.image.AxesImage at 0x793840147e50>


#high_pink=np.array([255,0,255])
#low_pink=np.array([147,112,219])
#mask_thresh=cv2.inRange(mask_img,high_pink,low_pink)
#plt.imshow(mask_thresh)
mask_gray=cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY)
gray=cv2.GaussianBlur(mask_gray,(5,5),0)
ret,thresh=cv2.threshold(mask_gray,100,255,cv2.THRESH_BINARY)
plt.imshow(thresh,cmap='gray')
<matplotlib.image.AxesImage at 0x79381ff57b50>

canny=cv2.Canny(np.array(thresh,dtype=np.uint8),50,255,apertureSize=7)
canny=cv2.dilate(canny,np.ones((5,5)),iterations = 1)

plt.imshow(canny)
<matplotlib.image.AxesImage at 0x79381fff8b80>

lines=cv2.HoughLinesP(canny,2,np.pi/180,threshold=100, lines=np.array([]), minLineLength=40, maxLineGap=5)
if lines is not None:
    for line in lines:
        for x1,y1,x2,y2 in line:
        #ROI=np.array([(x1,y1),((x1+x2)/2,(y2-y1)/2),(x2,y2)],np.int32)
        #lanes=cv2.fillPoly(mask,[ROI],(255,255,255))
            lanes=cv2.line(image,(x1,y1),(x2,y2),(0,255,0),2,cv2.LINE_AA)
    plt.imshow(lanes)
else: 
    print("the is no line")

def make_coordinates(coords):
    y=image.shape[0]
    y2=int(y*(3/5))
    m,b=coords[0],coords[1]
    x=(y-b)/m
    x2=(y2-b)/m
    return np.array([x,y,x2,y2])

def average_lines(lines):
    left_fit=[]
    right_fit=[]
    for line in lines:
            x1,y1,x2,y2=line.reshape(4)
            parameters=np.polyfit((x1,x2),(y1,y2),1)
            slopes=parameters[0]
            intercep=parameters[1]
            if slopes<0:
                left_fit.append((slopes,intercep))
            else:
                right_fit.append((slopes,intercep))
    left_average=np.average(left_fit,axis=0)
    right_average=np.average(right_fit,axis=0)
    left_coords=make_coordinates(left_average)
    right_coords=make_coordinates(right_average)
    return np.array([left_coords,right_coords],dtype=np.int32)
avg_line=average_lines(lines)
if avg_line is not None:
    for line in avg_line:
        x1,y1,x2,y2=line.reshape(4)
        print(line)
        #ROI=np.array([(x1,y1),((x1+x2)/2,(y2-y1)/2),(x2,y2)],np.int32)
        #lanes=cv2.fillPoly(mask,[ROI],(255,255,255))
        lanes=cv2.line(image,(x1,y1),(x2,y2),(0,0,255),2,cv2.LINE_AA)
    plt.imshow(lanes)
else: 
    print("the is no line")
[ 53 170  75 102]
[142 170 114 102]

# Steering Angle
h,w=image.shape[:2]
def compute_steering_angle(avg_line):
    if avg_line is not None and len(avg_line)==2 :
        theta=0
        _,_,x1,_=avg_line[0]
        _,_,x2,_=avg_line[1]
        theta=theta+np.arctan(((x2+x1)/2-int(w/2))/int(h*(3/5)))
        rad=np.rad2deg(theta)+90
    elif avg_line is None:
        theta=0
        theta=theta+np.arctan2(0,int(h/2))
        rad=np.rad2deg(theta)
    else:
        theta=0
        for avg_line in avg_line:
            for x1,y1,x2,y2 in line:
                theta=theta+np.arctan((x2-x1)/(y2-y1))
                rad=np.rad2deg(theta)+90
    print(rad-90)
    print(theta)
compute_steering_angle(avg_line)
-6.9867045366669345
-0.12194099802886278
def predict_lanes(image):
    image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    image=cv2.resize(image,(224,224))
    image_tensor=tfms(image/255.)[None].float().to(device)
    with torch.no_grad():
        mask=model(image_tensor)
    mask=mask[0].detach().permute(1,2,0).cpu()
    mask_img=np.round(255*mask.numpy()).astype(np.uint8)
    #roi=mask_img
    roi=mask_img[50:220,10:,:]
    image=image[50:220,10:,:]
    mask_gray=cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY)
    gray=cv2.GaussianBlur(mask_gray,(5,5),0)
    ret,thresh=cv2.threshold(mask_gray,100,255,cv2.THRESH_BINARY)
    canny=cv2.Canny(np.array(thresh,dtype=np.uint8),50,255,apertureSize=7)
    canny=cv2.dilate(canny,np.ones((5,5)),iterations = 1)
    lines=cv2.HoughLinesP(canny,2,np.pi/180,threshold=100, lines=np.array([]), minLineLength=40, maxLineGap=5)
    avg_line=average_lines(lines)
    if avg_line is not None:
        for line in avg_line:
            x1,y1,x2,y2=line.reshape(4)
        #lanes=cv2.fillPoly(mask,[ROI],(255,255,255))
            lanes=cv2.line(image,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),3,cv2.LINE_AA)
        plt.imshow(lanes)
    else: 
        print("the is no line")
    plt.figure()
    plt.imshow(roi)
    compute_steering_angle(avg_line)
    
image=cv2.imread(test_images[1])
predict_lanes(image)
-27.677450793375783
-0.48306264490312417"