Skip to content

Commit

Permalink
#0:Add Trace Support
Browse files Browse the repository at this point in the history
  • Loading branch information
vguduruTT committed Mar 9, 2025
1 parent 4bc0af8 commit 4ce4829
Show file tree
Hide file tree
Showing 17 changed files with 719 additions and 176 deletions.
18 changes: 14 additions & 4 deletions models/experimental/functional_UFLD_v2/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# Ultra-Fast-Lane-Detection-v2


### Platforms:
WH N300

### Introduction

The Ultra-Fast-Lane-Detection-v2 is a PyTorch-based implementation designed for fast and efficient deep lane detection using hybrid anchor-driven ordinal classification. It enhances the speed and accuracy of lane detection models with a focus on real-time applications.

The repository includes models trained on various datasets such as [CULane](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_culane.py), [CurveLanes](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_curvelanes.py), and [Tusimple](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_tusimple.py)

Resource link - [source](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2)

Dataset Link - [source](https://github.com/TuSimple/tusimple-benchmark/issues/3), [kaggle](https://www.kaggle.com/datasets/manideep1108/tusimple?resource=download)


The repository includes models trained on various datasets such as [CULane](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_culane.py), [CurveLanes](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_curvelanes.py), and [Tusimple](https://github.com/cfzd/Ultra-Fast-Lane-Detection-v2/blob/master/model/model_tusimple.py)



### Model Details

- The entry point to the UFLD_v2 is located at:`models/experimental/functional_UFLD_v2/ttnn/ttnn_UFLD_v2.py`
- The model picks up trained weights from the **tusimple_res34.pth** file located at:`models/experimental/functional_UFLD_v2/reference/tusimple_res34.pth`
- Batch Size :1 (Currently supported)
- Batch Size :2

### UFLD_v2 Demo

Expand All @@ -27,3 +31,9 @@ Dataset Link - [source](https://github.com/TuSimple/tusimple-benchmark/issues/3)
- The output results for both torch and ttnn model will be stored in this directory(.txt files). - `models/experimental/functional_UFLD_v2/demo/results_txt`

#### Note: The post-processing is performed using PyTorch.


- Use the following command to run the UFLD_v2 performant implementation:
`pytest tests/ttnn/integration_tests/UFLD_v2/test_UFLD_v2_performant.py::test_run_ufld_v2_trace_2cq_inference`

### Owner: [Venkatesh](https://github.com/vguduruTT)
51 changes: 17 additions & 34 deletions models/experimental/functional_UFLD_v2/demo/demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# SPDX-FileCopyrightText: © 2025 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

import torch
import pytest
import json
import numpy as np
import os
import ttnn
import gdown
from pathlib import Path
from loguru import logger
from models.experimental.functional_UFLD_v2.reference.UFLD_v2_model import Tu_Simple
from models.experimental.functional_UFLD_v2.demo import model_config as cfg
from models.experimental.functional_UFLD_v2.demo.demo_utils import (
Expand All @@ -16,13 +27,6 @@
preprocess_linear_weight,
preprocess_linear_bias,
)
import json
import numpy as np
import os
import ttnn
import gdown
from pathlib import Path
from loguru import logger


def custom_preprocessor(model, name):
Expand Down Expand Up @@ -294,29 +298,6 @@ def custom_preprocessor(model, name):
return parameters


def attempt_download(file, key="dumps"):
tests = Path(__file__).parent.parent / key
file_path = tests / Path(str(file).strip().replace("'", "").lower())
print("tests and file path is", tests, file_path)
if not file_path.exists():
google_drive_url = "https://drive.google.com/file/d/1pkz8homK433z39uStGK3ZWkDXrnBAMmX/view"

file_id = google_drive_url.split("/d/")[1].split("/")[0]
google_drive_download_url = f"https://drive.google.com/uc?id={file_id}"

try:
print(f"Downloading from Google Drive: {google_drive_download_url} to {file_path}...")
gdown.download(google_drive_download_url, str(file_path), quiet=False)

# Check if the file exists and its size
assert file_path.exists() and file_path.stat().st_size > 1e6, f"Download failed for {file}"

except Exception as e:
print(f"Error downloading from Google Drive: {e}")

return file_path


@pytest.mark.parametrize(
"batch_size,input_channels,height,width",
[
Expand All @@ -327,11 +308,11 @@ def attempt_download(file, key="dumps"):
"use_pretrained_weight",
[
# False,
True # uncomment to run the model for real weights
True
],
ids=[
# "pretrained_weight_false",
"pretrained_weight_true", # uncomment to run the model for real weights
"pretrained_weight_true",
],
)
@pytest.mark.parametrize("device_params", [{"l1_small_size": 79104}], indirect=True)
Expand All @@ -341,8 +322,10 @@ def test_tu_simple_res34_inference(batch_size, input_channels, height, width, de
if use_pretrained_weight:
logger.info(f"Demo Inference using Pre-trained Weights")
file = "tusimple_res34.pth"
downloaded_file_path = attempt_download(file, key="reference")
state_dict = torch.load(downloaded_file_path)
weights_path = "models/experimental/functional_UFLD_v2/tusimple_res34.pth"
if not os.path.exists(weights_path):
os.system("bash models/experimental/functional_UFLD_v2/weights_download.sh")
state_dict = torch.load(weights_path)
new_state_dict = {}
for key, value in state_dict["model"].items():
new_key = key.replace("model.", "res_model.")
Expand Down
18 changes: 8 additions & 10 deletions models/experimental/functional_UFLD_v2/demo/demo_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# SPDX-FileCopyrightText: © 2025 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

import ttnn
import os, json
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
from PIL import Image
from tqdm import tqdm
import numpy as np
from sklearn.linear_model import LinearRegression
import json
import ttnn


def loader_func(path):
Expand Down Expand Up @@ -69,10 +71,7 @@ def bench(pred, gt, y_samples, running_time):

@staticmethod
def bench_one_submit(pred_file, gt_file):
# try:
json_pred = [json.loads(line) for line in open(pred_file, "r").readlines()]
# except BaseException as e:
# raise Exception('Fail to load json file of the prediction.')
json_gt = [json.loads(line) for line in open(gt_file).readlines()]
if len(json_gt) != len(json_pred):
raise Exception("We do not get the predictions of all the test tasks")
Expand Down Expand Up @@ -122,7 +121,7 @@ def __init__(self, path, list_path, img_transform=None, crop_size=None):
self.crop_size = crop_size
with open(list_path, "r") as f:
self.list = f.readlines()
self.list = [l[1:] if l[0] == "/" else l for l in self.list] # exclude the incorrect path prefix '/' of CULane
self.list = [l[1:] if l[0] == "/" else l for l in self.list]

def __getitem__(self, index):
name = self.list[index].split()[0]
Expand Down Expand Up @@ -256,7 +255,6 @@ def generate_tusimple_lines(row_out, row_ext, col_out, col_ext, row_anchor=None,
lanes_on_tusimple[tusimple_h_sample > bot_lim] = -2
all_lanes.append(lanes_on_tusimple.tolist())
else:
# all_lanes.append([-2]*56)
pass
return all_lanes

Expand Down Expand Up @@ -286,7 +284,7 @@ def run_test_tusimple(
else:
imgs = imgs.permute(0, 2, 3, 1)
imgs = ttnn.from_torch(imgs, dtype=ttnn.bfloat16, layout=ttnn.ROW_MAJOR_LAYOUT, device=device)
out, pred = net(imgs)
out, pred = net(imgs, batch_size=batch_size)
out = ttnn.to_torch(out)
pred["loc_row"] = ttnn.to_torch(pred["loc_row"])
pred["loc_col"] = ttnn.to_torch(pred["loc_col"])
Expand Down
4 changes: 4 additions & 0 deletions models/experimental/functional_UFLD_v2/demo/model_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-FileCopyrightText: © 2025 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

dataset = "Tusimple"
data_root = "models/experimental/functional_UFLD_v2/demo" # Need to be modified before running
epoch = 100
Expand Down
Loading

0 comments on commit 4ce4829

Please sign in to comment.