Skip to content

Commit

Permalink
Add pre-commit hooks (#182)
Browse files Browse the repository at this point in the history
* Add pre-commit hooks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add ufmt as code formatter

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Move create_small_table to _helper.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use exists to check an existing file

* Add mdformat as markdown formatter

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use exists to check an existing file

* Fixing flake8

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Cleanup codes

* Fixing pre-commit

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixing pre-commit

* Catch exception in v5/utils/downloads.py

* Fixing exception in check_font

* Use exists to check an existing file

* Fixing exception in parse_model

* Fixing format

* Decrease max-line-length in setup.cfg

* Fixing flake8

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
zhiqwang and pre-commit-ci[bot] authored Oct 1, 2021
1 parent 97f507d commit c9b5b6f
Show file tree
Hide file tree
Showing 77 changed files with 4,106 additions and 2,204 deletions.
33 changes: 33 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-docstring-first
- id: check-toml
- id: check-yaml
exclude: packaging/.*
- id: end-of-file-fixer

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
hooks:
- id: flake8
args: [--config=setup.cfg]

- repo: https://github.com/omnilib/ufmt
rev: v1.3.0
hooks:
- id: ufmt
additional_dependencies:
- black == 21.9b0
- usort == 0.6.4

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.9
hooks:
- id: mdformat
additional_dependencies:
- mdformat-gfm
- mdformat-black
- mdformat_frontmatter
30 changes: 15 additions & 15 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ appearance, race, religion, or sexual identity and orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Expand Down Expand Up @@ -70,7 +70,7 @@ members of the project's leadership.
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq

[homepage]: https://www.contributor-covenant.org
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Contributing

When contributing to this repository, please first discuss the change you wish to make via issue,
email, or any other method with the owners of this repository before making a change.
email, or any other method with the owners of this repository before making a change.

Please note we have a code of conduct, please follow it in all your interactions with the project.

## Pull Request Process

1. Ensure any install or build dependencies are removed before the end of the layer when doing a
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
build.
2. Update the README.md with details of changes to the interface, this includes new environment
1. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is <[SemVer](http://semver.org/)>.
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
1. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is \<[SemVer](http://semver.org/)>.
1. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ There are no extra compiled components in `yolort` and package dependencies are
model.eval()
# Perform inference on an image file
predictions = model.predict('bus.jpg')
predictions = model.predict("bus.jpg")
# Perform inference on a list of image files
predictions = model.predict(['bus.jpg', 'zidane.jpg'])
predictions = model.predict(["bus.jpg", "zidane.jpg"])
```

### Loading via `torch.hub`

The models are also available via torch hub, to load `yolov5s` with pretrained weights simply do:

```python
model = torch.hub.load('zhiqwang/yolov5-rt-stack', 'yolov5s', pretrained=True)
model = torch.hub.load("zhiqwang/yolov5-rt-stack", "yolov5s", pretrained=True)
```

### Loading checkpoint from official yolov5
Expand All @@ -113,11 +113,11 @@ The following is the interface for loading the checkpoint weights trained with `
from yolort.models import YOLOv5
# 'yolov5s.pt' is downloaded from https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
ckpt_path_from_ultralytics = 'yolov5s.pt'
ckpt_path_from_ultralytics = "yolov5s.pt"
model = YOLOv5.load_from_yolov5(ckpt_path_from_ultralytics, score_thresh=0.25)
model.eval()
img_path = 'test/assets/bus.jpg'
img_path = "test/assets/bus.jpg"
predictions = model.predict(img_path)
```

Expand Down
64 changes: 32 additions & 32 deletions deployment/libtorch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,55 @@ The LibTorch inference for `yolort`, both GPU and CPU are supported.
- LibTorch 1.8.0 / 1.9.0
- TorchVision 0.9.0 / 0.10.0
- OpenCV 3.4+
- CUDA 10.2 [Optional]
- CUDA 10.2 \[Optional\]

*We didn't impose too strong restrictions on the version of CUDA and Ubuntu systems.*

## Usage

1. First, Setup the environment variables.

```bash
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
```
```bash
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
```

1. Don't forget to compile `TorchVision` using the following scripts.
```bash
git clone https://github.com/pytorch/vision.git
cd vision
git checkout release/0.9 # Double check the version of TorchVision currently in use
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch # Set `-DWITH_CUDA=ON` if you're using GPU
make -j4
sudo make install
```
```bash
git clone https://github.com/pytorch/vision.git
cd vision
git checkout release/0.9 # Double check the version of TorchVision currently in use
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch # Set `-DWITH_CUDA=ON` if you're using GPU
make -j4
sudo make install
```

1. Generate `TorchScript` model

Unlike [ultralytics's](https://github.com/ultralytics/yolov5/blob/8ee9fd1/export.py) `torch.jit.trace` mechanism, We're using `torch.jit.script` to trace the YOLOv5 models which containing the whole pre-processing (especially with the [`letterbox`](https://github.com/ultralytics/yolov5/blob/8ee9fd1/utils/augmentations.py#L85-L115) ops) and post-processing (especially with the `nms` ops) procedures, as such you don't need to rewrite manually the C++ codes of pre-processing and post-processing.
Unlike [ultralytics's](https://github.com/ultralytics/yolov5/blob/8ee9fd1/export.py) `torch.jit.trace` mechanism, We're using `torch.jit.script` to trace the YOLOv5 models which containing the whole pre-processing (especially with the [`letterbox`](https://github.com/ultralytics/yolov5/blob/8ee9fd1/utils/augmentations.py#L85-L115) ops) and post-processing (especially with the `nms` ops) procedures, as such you don't need to rewrite manually the C++ codes of pre-processing and post-processing.
```bash
git clone https://github.com/zhiqwang/yolov5-rt-stack.git
cd yolov5-rt-stack
python -m test.tracing.trace_model
```
```bash
git clone https://github.com/zhiqwang/yolov5-rt-stack.git
cd yolov5-rt-stack
python -m test.tracing.trace_model
```
1. Then compile the source code.
```bash
cd deployment/libtorch
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make
```
```bash
cd deployment/libtorch
mkdir build && cd build
cmake .. -DTorch_DIR=$TORCH_PATH/share/cmake/Torch
make
```
1. Now, you can infer your own images.
```bash
./yolo_inference [--input_source ../../../test/assets/zidane.jpg]
[--checkpoint ../../../test/tracing/yolov5s.torchscript.pt]
[--labelmap ../../../notebooks/assets/coco.names]
[--gpu] # GPU switch, which is optional, and set False as default
```
```bash
./yolo_inference [--input_source ../../../test/assets/zidane.jpg]
[--checkpoint ../../../test/tracing/yolov5s.torchscript.pt]
[--labelmap ../../../notebooks/assets/coco.names]
[--gpu] # GPU switch, which is optional, and set False as default
```
46 changes: 23 additions & 23 deletions deployment/ncnn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ The ncnn inference for `yolort`, both GPU and CPU are supported.

1. First, Setup the environment variables.

```bash
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
```
```bash
export TORCH_PATH=$(dirname $(python -c "import torch; print(torch.__file__)"))
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TORCH_PATH/lib/
```

1. First, compile `ncnn` using the following scripts.

```bash
git clone --recursive [email protected]:Tencent/ncnn.git
cd ncnn
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON .. # Set -DNCNN_VULKAN=ON if you're using VULKAN
make -j4
make install
```
```bash
git clone --recursive [email protected]:Tencent/ncnn.git
cd ncnn
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_SYSTEM_GLSLANG=ON -DNCNN_BUILD_EXAMPLES=ON .. # Set -DNCNN_VULKAN=ON if you're using VULKAN
make -j4
make install
```

Or follow the [official instructions](https://github.com/Tencent/ncnn/wiki/how-to-build) to install ncnn.
Or follow the [official instructions](https://github.com/Tencent/ncnn/wiki/how-to-build) to install ncnn.

1. Then compile the source code.

```bash
cd deployment/ncnn
mkdir build && cd build
cmake .. -Dncnn_DIR=<ncnn_install_dir>/lib/cmake/ncnn/
make
```
```bash
cd deployment/ncnn
mkdir build && cd build
cmake .. -Dncnn_DIR=<ncnn_install_dir>/lib/cmake/ncnn/
make
```

_Note: you have to change <ncnn_install_dir> to your machine's directory, it is the directory that contains ncnnConfig.cmake, if you are following the above operations, you should set it to <./ncnn/build/install>_
_Note: you have to change \<ncnn_install_dir> to your machine's directory, it is the directory that contains ncnnConfig.cmake, if you are following the above operations, you should set it to \<./ncnn/build/install>_
1. Now, you can infer your own images with ncnn.
```bash
./yolort_ncnn ../../../test/assets/zidane.jpg
```
```bash
./yolort_ncnn ../../../test/assets/zidane.jpg
```
52 changes: 30 additions & 22 deletions deployment/ncnn/export_onnx.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
# Copyright (c) 2021, Zhiqiang Wang. All Rights Reserved.
import argparse

import torch
from tools.yolort_deploy_friendly import yolov5s_r40_deploy_ncnn


def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', type=str, default='./yolov5s.pt',
help='weights path')
parser.add_argument('--output_path', type=str, default='./yolov5s.onnx',
help='path of exported onnx')
parser.add_argument('--img_size', nargs='+', type=int, default=[640, 640],
help='image (height, width)')
parser.add_argument('--num_classes', type=int, default=80,
help='number of classes')
parser.add_argument('--batch_size', type=int, default=1,
help='batch size')
parser.add_argument('--device', default='cpu',
help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--half', action='store_true',
help='FP16 half-precision export')
parser.add_argument('--dynamic', action='store_true',
help='ONNX: dynamic axes')
parser.add_argument('--simplify', action='store_true',
help='ONNX: simplify model')
parser.add_argument('--opset', type=int, default=11,
help='ONNX: opset version')
parser.add_argument(
"--weights", type=str, default="./yolov5s.pt", help="weights path"
)
parser.add_argument(
"--output_path",
type=str,
default="./yolov5s.onnx",
help="path of exported onnx",
)
parser.add_argument(
"--img_size",
nargs="+",
type=int,
default=[640, 640],
help="image (height, width)",
)
parser.add_argument("--num_classes", type=int, default=80, help="number of classes")
parser.add_argument("--batch_size", type=int, default=1, help="batch size")
parser.add_argument(
"--device", default="cpu", help="cuda device, i.e. 0 or 0,1,2,3 or cpu"
)
parser.add_argument(
"--half", action="store_true", help="FP16 half-precision export"
)
parser.add_argument("--dynamic", action="store_true", help="ONNX: dynamic axes")
parser.add_argument("--simplify", action="store_true", help="ONNX: simplify model")
parser.add_argument("--opset", type=int, default=11, help="ONNX: opset version")
return parser


Expand Down Expand Up @@ -53,8 +61,8 @@ def export_onnx(args):
verbose=False,
opset_version=args.opset,
do_constant_folding=True,
input_names=['images'],
output_names=['h1', 'h2', 'h3'],
input_names=["images"],
output_names=["h1", "h2", "h3"],
)


Expand Down
Loading

0 comments on commit c9b5b6f

Please sign in to comment.