Skip to content

Commit

Permalink
🏁 添加在 win 上运行的脚本,并优化了脚本内容
Browse files Browse the repository at this point in the history
  • Loading branch information
henryzhuhr committed Jun 6, 2024
1 parent b5cc0b0 commit fbba332
Show file tree
Hide file tree
Showing 19 changed files with 256 additions and 65 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/docs-pnpm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ jobs:
steps:
- name: Checkout this
# @see https://github.com/actions/checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0 # 如果未启用 lastUpdated,则不需要

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 20

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ tmps/
temp/
temps/
resource/
projects/


# Vuepress/Vitepress
Expand Down
13 changes: 11 additions & 2 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ export default defineConfig({
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Home', link: '/' },
{ text: '首页', link: '/' },
{ text: '项目源码', link: 'https://github.com/HenryZhuHR/deep-object-detect-track' },
{
text: '项目文档',
items: [
{ text: '安装环境', link: '/install' },
{ text: '数据集制作', link: '/dataset' },
{ text: '模型训练', link: '/train' },
{ text: '模型部署', link: '/deploy' },
]
}
],

sidebar: [
Expand All @@ -19,7 +29,6 @@ export default defineConfig({
{ text: '数据集制作', link: '/dataset' },
{ text: '模型训练', link: '/train' },
{ text: '模型部署', link: '/deploy' },
// { text: '目标检测YOLOv5项目', link: '/yolo' },
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion docs/dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ outline: deep
DATASET_DIR=/path/to/dataset
```

> 需要注意的是,数据集通常需要放置在项目外的路径,例如 `~/data` (推荐)。如果放置在项目内,导致编辑器对于项目的索引过大,会导致编辑器卡顿
> 需要注意的是,数据集通常需要放置在项目外的路径,例如 `~/data` `$HOME/data` (推荐)(win 下为 `$env:USERPROFILE/data`)。如果放置在项目内,导致编辑器对于项目的索引过大,会导致编辑器卡顿
这里准备好了一个示例数据集,可以下载

Expand Down
16 changes: 14 additions & 2 deletions docs/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ outline: deep

<!--@include: ./download-pretrian.md-->

同时,也提供了一些转化好的模型(从 ultralytics/yolov5(v7.0) 的 [yolov5](https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s) 导出),可以直接使用进行部署

```shell
bash scripts/download-premodels.sh
#zsh scripts/download-premodels.sh # zsh
```

## 导出模型

提供一个导出脚本 `scripts/train.sh`,复制一份到项目目录下进行自定义修改(推荐)
Expand Down Expand Up @@ -285,17 +292,22 @@ python3 export.py \
```
- `--device` 指定 GPU 设备,有多少张卡就写多少张卡,用逗号分隔

修改 `infer.py` 文件,指定模型路径
修改 `infer.py` 文件
```python
## -- TensorRT
args.model=".cache/yolov5/yolov5s.engine"
detector = backends.TensorRTBackend()
```

执行推理
```shell
python infer.py --model .cache/yolov5/yolov5s.engine
```


推理速度

| Tensor 版本 | 模型 | 推理时间(ms) | 设备 |
| ----------- | ------- | ------------ | -------------- |
| 10.0(.1.6) | yolov5s | 3.26 ~ 3.72 | RTX 4090 (24G) |
| 10.0(.1.6) | yolov5s | 10.41 ~ 10.7 | RTX 3050 (8G) |
| 8.6(.1.6) | yolov5s | 3.40 ~ 3.85 | RTX 4090 (24G) |
Binary file added docs/images/添加trt路径.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/编辑环境变量界面.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/编辑用户环境变量.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 21 additions & 6 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,33 @@ cd deep-object-detect-track

> 后续的脚本基于 `deep-object-detect-track` 目录下执行
如果未能获取子模块,可以手动获取
```shell
如果未能获取子模块,可以手动获取,如果 `git submodule` 无法获取,可以使用 `git clone` 获取

::: code-group

```shell [git submodule]
# in deep-object-detect-track directory
git submodule init
git submodule update
```

```shell [git clone]
git clone https://github.com/ultralytics/yolov5.git projects/yolov5
```

:::


## 系统要求

### 操作系统


项目在 Linux(Ubuntu) 和 MacOS 系统并经过测试 ,经过测试的系统:
- Ubuntu 22.04 jammy (CPU & GPU)
- MacOS (CPU)
项目在 Linux(Ubuntu) 和 MacOS 系统并经过测试 ,经过测试的系统:
- Ubuntu 22.04 jammy (CPU & GPU)
- MacOS (CPU)

Windows 系统暂未测试也无适配计划,如果需要在 Windows 系统上运行,可以使用 WSL2 或者根据提供的脚本手动执行,不接受 Windows 系统的问题反馈
> 项目不支持 Windows 系统 ❌ ,如果需要在 Windows 系统上运行,可以使用 WSL2 或者根据提供的脚本手动执行;虽然已经测试通过,但是不保证所有功能都能正常运行,因此不接受 Windows 系统的问题反馈
### GPU

Expand All @@ -63,6 +73,8 @@ export PATH="$CUDA_HOME/bin:$PATH"
export LD_LIBRARY_PATH="$CUDA_HOME/lib64:$LD_LIBRARY_PATH"
```

> 事实上,Pytorch 1.8 开始就会在安装的时候自动安装对应的 CUDA Toolkit,因此不需要手动安装 CUDA Toolkit,因此可以跳过这一步
MacOS 系统不支持 CUDA Toolkit,可以使用 CPU 训练模型 (Yolov5 项目暂不支持 MPS 训练),但是推理过程可以使用 Metal ,参考 [*Introducing Accelerated PyTorch Training on Mac*](https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/#getting-started)[*MPS backend*](https://pytorch.org/docs/stable/notes/mps.html#mps-backend)


Expand Down Expand Up @@ -191,3 +203,6 @@ pip install -r requirements/requirements.train.txt
- `onnx`: ONNX 格式的模型导出支持任意设备,需要取消注释,并且其他导出依赖于 ONNX 模型
- `coremltools`: 必须依赖于 MacOS 系统
- `nvidia-*`: 确保硬件支持 NVIDIA GPU



32 changes: 16 additions & 16 deletions infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from typing import Dict
import cv2
import tqdm
import yaml

from dlinfer.detector import DetectorInferBackends
Expand All @@ -26,25 +27,21 @@ def main() -> int:
## ------ ONNX ------
onnx_backend = backends.ONNXBackend
print("-- Available devices:", providers := onnx_backend.SUPPORTED_DEVICES)
detector = onnx_backend(
device=providers, inputs=["images"], outputs=["output0"]
)
detector = onnx_backend(device=providers, inputs=["images"], outputs=["output0"])

## ------ OpenVINO ------
# ov_backend = backends.OpenVINOBackend
# print("-- Available devices:", ov_backend.query_device())
# detector = ov_backend(device="AUTO")
# detector = ov_backend(device="GPU.0")

## ------ TensorRT ------
# detector = backends.TensorRTBackend()
# =======================================================

detector.load_model(args.model, verbose=True)

with open(".cache/yolov5/yolov5s_openvino_model/yolov5s.yaml", "r") as f:
label_map: Dict[int, str] = yaml.load(f, Loader=yaml.FullLoader)[
"names"
]
with open(".cache/yolov5/coco.yaml", "r") as f:
label_map: Dict[int, str] = yaml.load(f, Loader=yaml.FullLoader)["names"]
label_list = list(label_map.values())
# print(label_list)

Expand All @@ -57,23 +54,26 @@ def main() -> int:

# -- do inference
print("-- do inference")
for i in range(10):
pbar = tqdm.tqdm(range(100))
total_sum_time = 0
_cnt = 0
for i in pbar:
start_time = cv2.getTickCount()
# -- preprocess
input_t, scale_h, scale_w = Process.preprocess(img) # B C H W
# -- inference
output_t = detector.infer(input_t)

end_time = cv2.getTickCount()
infer_time = (end_time - start_time) / cv2.getTickFrequency() * 1000

# -- postprocess
preds = Process.postprocess(output_t)

end_time = cv2.getTickCount()
total_time = (end_time - start_time) / cv2.getTickFrequency() * 1000

print(
f"Time infer/total: {infer_time:.2f}/{total_time:.2f} ms",
)
total_sum_time += total_time
_cnt += 1
pbar.set_description(f"Time infer/total: {infer_time:.2f}/{total_time:.2f} ms")
# -- mark
print(f"-- Average time: {total_sum_time / _cnt:.2f} ms")
Process.mark(img, preds, label_list, scale_h, scale_w)
cv2.imwrite(save_path := "tmp/out.jpg", img)
print(f"-- output saved to '{save_path}'")
Expand Down
38 changes: 38 additions & 0 deletions scripts/base.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# =============== Color Print ===============
$DEFAULT = [char]27 + '[0m'
$RED = [char]27 + '[00;31m'
$GREEN = [char]27 + '[00;32m'
$YELLOW = [char]27 + '[00;33m'
$CYAN = [char]27 + '[00;36m'

function print_base { param($color, $type, $message) Write-Host -NoNewline "$color- [$type] $message"; Write-Host $DEFAULT }
function print_info { param($message) print_base $CYAN "INFO" $message }
function print_tip { param($message) print_base $YELLOW "TIP" $message }
function print_success { param($message) print_base $GREEN "SUCCESS" $message }
function print_warning { param($message) print_base $YELLOW "WARNING" $message }
function print_error { param($message) print_base $RED "ERROR" $message }

function run_script {
param($scriptPath)
if (-not (Test-Path $scriptPath -PathType Leaf)) {
Write-Host "$scriptPath not found"
exit 1
}
. $scriptPath # 指定 powershell 执行脚本
}

$variablesCustomPath = "scripts/variables.custom.ps1"
$variablesDefaultPath = "scripts/variables.ps1"

if (Test-Path $variablesCustomPath -PathType Leaf) {
run_script $variablesCustomPath
print_success "Loaded custom variables: '$variablesCustomPath'"
}
elseif (Test-Path $variablesDefaultPath -PathType Leaf) {
run_script $variablesDefaultPath
print_success "Loaded default variables: '$variablesDefaultPath'"
}
else {
Write-Host "'$variablesCustomPath' or '$variablesDefaultPath' not found"
exit 1
}
68 changes: 68 additions & 0 deletions scripts/create-python-env.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 启动 PowerShell:打开 PowerShell 控制台。可以通过在Windows搜索框中键入“PowerShell”并选择相应的选项来启动。

# 设置执行策略(可选):如果您的系统的执行策略不允许运行脚本,您可能需要先更改执行策略。可以通过在管理员权限下打开的 PowerShell 中运行以下命令来更改执行策略:

# powershell
# 复制代码
# Set-ExecutionPolicy RemoteSigned
# 这将允许运行本地的、来自 Internet 的已签名脚本,但不会执行未签名的本地脚本。

# Check if scripts\base.ps1 exists
if (-not (Test-Path "scripts\base.ps1")) {
Write-Host "scripts\base.ps1 not found"
exit 1
}

. "scripts\base.ps1"

# Check if ENV_PATH is set
if (-not $script:ENV_PATH) {
print_error "ENV_PATH is not set. Please set it in 'scripts\variables.ps1'"
exit 1
}

$script:ENV_PATH = "$script:ENV_PATH.conda"

# Check if ENV_PATH directory exists
if (-not (Test-Path $script:ENV_PATH -PathType Container)) {
$DEFAULT_PYTHON_VERSION = 3.10
if (-not $script:CUSTOM_PYTHON_VERSION) {
$ENV_PYTHON_VERSION = $DEFAULT_PYTHON_VERSION
} else {
$ENV_PYTHON_VERSION = $script:CUSTOM_PYTHON_VERSION
}
conda create -p $script:ENV_PATH -y python=$ENV_PYTHON_VERSION
print_success "Create Python environment in '$script:ENV_PATH'"
} else {
print_info "Conda environment '$script:ENV_PATH' already exists."
}

conda activate $script:ENV_PATH

# 该安装命令参考 https://pytorch.org/get-started/locally/
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
pip install -r projects/yolov5/requirements.txt
pip install -r requirements/requirements.train.txt

conda deactivate

function print_activate_env_message {
Write-Host ""
print_info "Run command below to activate the environment:"
Write-Host ""
Write-Host "source ~/.$(Split-Path -Leaf $PROFILE)"
Write-Host "conda activate $script:ENV_PATH"
Write-Host ""
print_info "Then run command below to deactivate the environment:"
Write-Host ""
Write-Host "source ~/.$(Split-Path -Leaf $PROFILE)"
Write-Host "conda deactivate"
Write-Host ""
}
print_activate_env_message

# cd .\project\deep-object-detect-track\
# conda activate .\.env\deep-object-detect-track.conda\
# python .\export.py --weights "C:\Users\29650\project\deep-object-detect-track\.cache\yolov5\yolov5s.pt" --data data/coco128.yaml --simplify --include onnx openvino engine --device 0
# python .\export.py --weights "C:\Users\29650\project\deep-object-detect-track\.cache\yolov5\yolov5s.pt" --data data/coco128.yaml --include onnx openvino engine --device 0
# python .\export.py --weights "C:\Users\29650\project\deep-object-detect-track\.cache\yolov5\yolov5s.pt" --data data/coco128.yaml --include engine --device 0
3 changes: 2 additions & 1 deletion scripts/create-python-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ case $key in
echo "Usage: create-python-env.sh [OPTIONS]"
echo "Options:"
echo " -e, --env-provider <provider> Set the environment provider (venv or conda)"
echo " -ni, --no-install Skip Python requirements installation"
echo " -i, --install Install Python requirements"
echo " -v, --verbose Enable verbose mode"
echo " -h, --help Show this help message and exit"
exit 0
;;
Expand Down
22 changes: 22 additions & 0 deletions scripts/download-premodels.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if [ ! -f "scripts/base.sh" ]; then
echo "scripts/base.sh not found"
exit 1
fi
source scripts/base.sh

tag_name=v1.0.0
base_url=https://github.com/HenryZhuHR/deep-object-detect-track/releases/download/${tag_name}


weights_dir=$CACHE_DIR/yolov5
[ ! -d ${weights_dir} ] && mkdir -p ${weights_dir}

wget -c ${base_url}/coco.yaml -P ${weights_dir}
wget -c ${base_url}/yolov5s.onnx -P ${weights_dir}


ov_dir=${weights_dir}/yolov5s_openvino_model
[ ! -d ${ov_dir} ] && mkdir -p ${ov_dir}

wget -c ${base_url}/yolov5s.bin -P ${ov_dir}
wget -c ${base_url}/yolov5s.xml -P ${ov_dir}
2 changes: 1 addition & 1 deletion scripts/download-yolov5.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ all_model_list=(
)

weights_dir=$CACHE_DIR/yolov5
mkdir -p ${weights_dir}
[ ! -d ${weights_dir} ] && mkdir -p ${weights_dir}

model_list=($1)

Expand Down
2 changes: 2 additions & 0 deletions scripts/train.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ python3 train.py \
--device $TRAIN_DEVICE \
--workers 8 \
--project $PROJECT_HOME/tmp/runs/train

# python train.py --data $env:USERPROFILE/data/bottle-organized/dataset.yaml --cfg models/yolov5s.yaml --weights "../../.cache/yolov5/yolov5s.pt" --epochs 10 --batch 4 --device 0
Loading

0 comments on commit fbba332

Please sign in to comment.