Skip to content

Commit

Permalink
#13477: Yolov7 ttnn implementation and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
HariniMohan0102 authored and vigneshkeerthivasanx committed Feb 21, 2025
1 parent 99e8f45 commit 1c16193
Show file tree
Hide file tree
Showing 14 changed files with 4,080 additions and 0 deletions.
29 changes: 29 additions & 0 deletions models/experimental/functional_yolov7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Yolov7 Model

# Platforms:
WH N300, N150

## command to run the model
Use the following command to run the yolov7 model
```
pytest --disable-warnings tests/ttnn/integration_tests/yolov7/test_ttnn_yolov7.py
```

## Demo
Batch size : 1
Use the following command to run the demo
```
pytest --disable-warnings models/experimental/functional_yolov7/demo/yolov7_demo.py
```

## Outputs
The Demo outputs are saved inside this directory: `models/experimental/functional_yolov7/demo/runs/detect`


Torch ops: mul, pow, add, sub (Overall PCC of the model drops when converted to ttnn)

Pending issues:
[#12786](https://github.com/tenstorrent/tt-metal/issues/12786) - Failing Silu in Yolov7 model
[#17370](https://github.com/tenstorrent/tt-metal/issues/17370) - Issue with repeat_interleave (Handled in torch in pre-processing of the model)
[#17583](https://github.com/tenstorrent/tt-metal/issues/17583) - Yolov7 Trace+2cq fails with Out of Memoy issue
[#17904](https://github.com/tenstorrent/tt-metal/issues/17904) - Detect sub-module fails with low PCC.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions models/experimental/functional_yolov7/demo/yolov7_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# SPDX-FileCopyrightText: © 2024 Tenstorrent Inc.

# SPDX-License-Identifier: Apache-2.0

import pytest
import torch
import ttnn
import numpy as np
from loguru import logger
import sys
from models.experimental.functional_yolov7.reference.model import Yolov7_model
from models.experimental.functional_yolov7.tt.tt_yolov7 import ttnn_yolov7
from ttnn.model_preprocessing import preprocess_model_parameters

from models.experimental.functional_yolov7.ttnn_yolov7_detect import ttnn_detect, parse_opt

from tests.ttnn.integration_tests.yolov7.test_ttnn_yolov7 import create_custom_preprocessor
from models.experimental.functional_yolov7.reference.yolov7_utils import download_yolov7_weights


@pytest.mark.parametrize("device_params", [{"l1_small_size": 16384}], indirect=True)
def test_demo(device, reset_seeds):
sys.modules["models.yolo"] = sys.modules["models.experimental.functional_yolov7.reference.yolov7_model"]
sys.modules["models.common"] = sys.modules["models.experimental.functional_yolov7.reference.yolov7_utils"]

def load_weights(model, weights_path):
ckpt = torch.load(weights_path, map_location="cpu")
state_dict = ckpt["model"].float().state_dict()
model.load_state_dict(state_dict, strict=False)

args = [
"--weights",
"yolov7.pt",
"--source",
"models/experimental/functional_yolov7/demo/horses.jpg",
"--conf-thres",
"0.50",
"--img-size",
"640",
]

opt = parse_opt(args)

torch_model = Yolov7_model()

new_state_dict = {}
keys = [name for name, parameter in torch_model.state_dict().items()]
ds_state_dict = {k: v for k, v in torch_model.state_dict().items()}
values = [parameter for name, parameter in ds_state_dict.items()]
for i in range(len(keys)):
new_state_dict[keys[i]] = values[i]
torch_model.load_state_dict(new_state_dict)
torch_model.eval()

weights_path = "tests/ttnn/integration_tests/yolov7/yolov7.pt"
weights_path = download_yolov7_weights(weights_path)
load_weights(torch_model, weights_path)

parameters = preprocess_model_parameters(
initialize_model=lambda: torch_model, custom_preprocessor=create_custom_preprocessor(None), device=None
)

grid = [torch.randn(1)] * 3
nx_ny = [80, 40, 20]
grid_tensors = []
for i in range(3):
yv, xv = torch.meshgrid([torch.arange(nx_ny[i]), torch.arange(nx_ny[i])])
grid_tensors.append(torch.stack((xv, yv), 2).view((1, 1, nx_ny[i], nx_ny[i], 2)).float())

ttnn_model = ttnn_yolov7(device, parameters, grid_tensors)

ttnn_output = ttnn_detect(opt, ttnn_model, device)

logger.info("Yolov7 Demo completed")
Loading

0 comments on commit 1c16193

Please sign in to comment.