Skip to content

Commit

Permalink
Fix setup and pre-commit.ci (#209)
Browse files Browse the repository at this point in the history
* Fix getting long description in setup.py

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

* Small fixes to the setup and pyproject

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

* Fixing format

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

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

* Fix pre-commit.ci

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 24, 2021
1 parent 0b7012c commit 72b495e
Show file tree
Hide file tree
Showing 56 changed files with 376 additions and 1,104 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
repos:
default_language_version:
python: python3.8

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
Expand Down
12 changes: 3 additions & 9 deletions deployment/ncnn/export_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--weights", type=str, default="./yolov5s.pt", help="weights path"
)
parser.add_argument("--weights", type=str, default="./yolov5s.pt", help="weights path")
parser.add_argument(
"--output_path",
type=str,
Expand All @@ -25,12 +23,8 @@ def get_parser():
)
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("--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")
Expand Down
8 changes: 2 additions & 6 deletions deployment/ncnn/tools/yolort_deploy_friendly.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,13 @@ def yolov5s_r40_deploy_ncnn(
width_multiple = 0.5
version = "r4.0"

backbone = darknet_pan_backbone(
backbone_name, depth_multiple, width_multiple, version=version
)
backbone = darknet_pan_backbone(backbone_name, depth_multiple, width_multiple, version=version)

model = YOLODeployFriendly(backbone, num_classes, **kwargs)
if pretrained:
if model_urls.get(weights_name, None) is None:
raise ValueError(f"No checkpoint is available for model {weights_name}")
state_dict = load_state_dict_from_url(
model_urls[weights_name], progress=progress
)
state_dict = load_state_dict_from_url(model_urls[weights_name], progress=progress)
model.load_state_dict(state_dict)

del model.anchor_generator
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [u"_build", "**.ipynb_checkpoints", "Thumbs.db", ".DS_Store"]
exclude_patterns = ["_build", "**.ipynb_checkpoints", "Thumbs.db", ".DS_Store"]

pygments_style = "sphinx"

Expand Down
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tool.usort]

first_party_detection = false

[tool.black]

line-length = 110
target-version = ["py36"]
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ universal=1

[metadata]
license_file = LICENSE
description-file = README.md

[pep8]
max-line-length = 110
Expand Down
24 changes: 11 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Adopted from TorchVision, see:
https://github.com/pytorch/vision/blob/master/setup.py
"""
import io
import subprocess
from pathlib import Path

Expand All @@ -19,11 +20,7 @@
sha = "Unknown"

try:
sha = (
subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=PATH_ROOT)
.decode("ascii")
.strip()
)
sha = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=PATH_ROOT).decode("ascii").strip()
except Exception:
pass

Expand All @@ -38,9 +35,13 @@ def write_version_file():
f.write(" cuda = _check_cuda_version()\n")


def load_requirements(
path_dir=PATH_ROOT, file_name="requirements.txt", comment_char="#"
):
def get_long_description():
# Get the long description from the README file
with io.open(str(PATH_ROOT / "README.md"), encoding="utf-8") as f:
return f.read()


def load_requirements(path_dir=PATH_ROOT, file_name="requirements.txt", comment_char="#"):
with open(path_dir / file_name, "r", encoding="utf-8", errors="ignore") as file:
lines = [ln.rstrip() for ln in file.readlines() if not ln.startswith("#")]
reqs = []
Expand All @@ -59,16 +60,13 @@ def load_requirements(

write_version_file()

# Get the long description from the README file
long_description = (PATH_ROOT / "README.md").read_text(encoding="utf-8")

setup(
name=PACKAGE_NAME,
version=VERSION,
description="Yet Another YOLOv5 and its Additional Runtime Stack",
description="Yet another yolov5 and its runtime stack for specialized accelerators.",
author="Zhiqiang Wang",
author_email="[email protected]",
long_description=long_description,
long_description=get_long_description(),
long_description_content_type="text/markdown",
url="https://github.com/zhiqwang/yolov5-rt-stack",
license="GPL-3.0",
Expand Down
8 changes: 2 additions & 6 deletions test/test_data_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def test_get_dataset():

def test_get_dataloader():
batch_size = 8
data_loader = data_helper.get_dataloader(
data_root="data-bin", mode="train", batch_size=batch_size
)
data_loader = data_helper.get_dataloader(data_root="data-bin", mode="train", batch_size=batch_size)
# Test the dataloader
images, targets = next(iter(data_loader))

Expand Down Expand Up @@ -67,7 +65,5 @@ def test_prepare_coco128():
data_path = Path("data-bin")
coco128_dirname = "coco128"
data_helper.prepare_coco128(data_path, dirname=coco128_dirname)
annotation_file = (
data_path / coco128_dirname / "annotations" / "instances_train2017.json"
)
annotation_file = data_path / coco128_dirname / "annotations" / "instances_train2017.json"
assert annotation_file.is_file()
4 changes: 1 addition & 3 deletions test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ def test_test_epoch_end():
data_path = Path("data-bin")
coco128_dirname = "coco128"
data_helper.prepare_coco128(data_path, dirname=coco128_dirname)
annotation_file = (
data_path / coco128_dirname / "annotations" / "instances_train2017.json"
)
annotation_file = data_path / coco128_dirname / "annotations" / "instances_train2017.json"

# Get dataloader to test
val_dataloader = data_helper.get_dataloader(data_root=data_path, mode="val")
Expand Down
52 changes: 13 additions & 39 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def get_export_import_copy(m):
results_from_imported = m_import(*args)
tol = 3e-4
try:
torch.testing.assert_close(
results, results_from_imported, atol=tol, rtol=tol
)
torch.testing.assert_close(results, results_from_imported, atol=tol, rtol=tol)
except ValueError:
# custom check for the models that return named tuples:
# we compare field by field while ignoring None as assert_close can't handle None
Expand Down Expand Up @@ -140,9 +138,7 @@ def _get_feature_shapes(self, height, width, width_multiple=0.5, use_p6=False):

return [(c, height // s, width // s) for (c, s) in zip(in_channels, strides)]

def _get_feature_maps(
self, batch_size, height, width, width_multiple=0.5, use_p6=False
):
def _get_feature_maps(self, batch_size, height, width, width_multiple=0.5, use_p6=False):
feature_shapes = self._get_feature_shapes(
height,
width,
Expand All @@ -152,9 +148,7 @@ def _get_feature_maps(
feature_maps = [torch.rand(batch_size, *f_shape) for f_shape in feature_shapes]
return feature_maps

def _get_head_outputs(
self, batch_size, height, width, width_multiple=0.5, use_p6=False
):
def _get_head_outputs(self, batch_size, height, width, width_multiple=0.5, use_p6=False):
feature_shapes = self._get_feature_shapes(
height,
width,
Expand All @@ -163,9 +157,7 @@ def _get_head_outputs(
)

num_outputs = self.num_outputs
head_shapes = [
(batch_size, 3, *f_shape[1:], num_outputs) for f_shape in feature_shapes
]
head_shapes = [(batch_size, 3, *f_shape[1:], num_outputs) for f_shape in feature_shapes]
head_outputs = [torch.rand(*h_shape) for h_shape in head_shapes]

return head_outputs
Expand Down Expand Up @@ -202,9 +194,7 @@ def _init_test_backbone_with_pan(
(0.67, 0.75, "r6.0", False, False),
],
)
@pytest.mark.parametrize(
"batch_size, height, width", [(4, 448, 320), (2, 384, 640)]
)
@pytest.mark.parametrize("batch_size, height, width", [(4, 448, 320), (2, 384, 640)])
def test_backbone_with_pan(
self,
depth_multiple,
Expand All @@ -216,9 +206,7 @@ def test_backbone_with_pan(
height,
width,
):
out_shape = self._get_feature_shapes(
height, width, width_multiple=width_multiple, use_p6=use_p6
)
out_shape = self._get_feature_shapes(height, width, width_multiple=width_multiple, use_p6=use_p6)

x = torch.rand(batch_size, 3, height, width)
model = self._init_test_backbone_with_pan(
Expand All @@ -243,9 +231,7 @@ def _init_test_anchor_generator(self, use_p6=False):
"width_multiple, use_p6",
[(0.5, False), (0.5, True)],
)
@pytest.mark.parametrize(
"batch_size, height, width", [(4, 448, 320), (2, 384, 640)]
)
@pytest.mark.parametrize("batch_size, height, width", [(4, 448, 320), (2, 384, 640)])
def test_anchor_generator(self, width_multiple, use_p6, batch_size, height, width):
feature_maps = self._get_feature_maps(
batch_size, height, width, width_multiple=width_multiple, use_p6=use_p6
Expand Down Expand Up @@ -344,15 +330,9 @@ def test_torchscript(arch):
out = model(x)
out_script = scripted_model(x)

torch.testing.assert_close(
out[0]["scores"], out_script[1][0]["scores"], rtol=0, atol=0
)
torch.testing.assert_close(
out[0]["labels"], out_script[1][0]["labels"], rtol=0, atol=0
)
torch.testing.assert_close(
out[0]["boxes"], out_script[1][0]["boxes"], rtol=0, atol=0
)
torch.testing.assert_close(out[0]["scores"], out_script[1][0]["scores"], rtol=0, atol=0)
torch.testing.assert_close(out[0]["labels"], out_script[1][0]["labels"], rtol=0, atol=0)
torch.testing.assert_close(out[0]["boxes"], out_script[1][0]["boxes"], rtol=0, atol=0)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -403,12 +383,6 @@ def test_load_from_yolov5(
model.eval()
out = model.predict(img_path)

torch.testing.assert_close(
out_from_yolov5[0]["scores"], out[0]["scores"], rtol=0, atol=0
)
torch.testing.assert_close(
out_from_yolov5[0]["labels"], out[0]["labels"], rtol=0, atol=0
)
torch.testing.assert_close(
out_from_yolov5[0]["boxes"], out[0]["boxes"], rtol=0, atol=0
)
torch.testing.assert_close(out_from_yolov5[0]["scores"], out[0]["scores"], rtol=0, atol=0)
torch.testing.assert_close(out_from_yolov5[0]["labels"], out[0]["labels"], rtol=0, atol=0)
torch.testing.assert_close(out_from_yolov5[0]["boxes"], out[0]["boxes"], rtol=0, atol=0)
8 changes: 2 additions & 6 deletions test/test_models_anchor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ def test_anchor_generator(self):
model.eval()
anchors = model(features)

expected_anchor_output = torch.tensor(
[[-0.5, -0.5], [0.5, -0.5], [-0.5, 0.5], [0.5, 0.5]]
)
expected_anchor_output = torch.tensor([[-0.5, -0.5], [0.5, -0.5], [-0.5, 0.5], [0.5, 0.5]])
expected_wh_output = torch.tensor([[4.0], [4.0], [4.0], [4.0]])
expected_xy_output = torch.tensor(
[[6.0, 14.0], [6.0, 14.0], [6.0, 14.0], [6.0, 14.0]]
)
expected_xy_output = torch.tensor([[6.0, 14.0], [6.0, 14.0], [6.0, 14.0], [6.0, 14.0]])

assert len(anchors) == 3
assert tuple(anchors[0].shape) == (4, 2)
Expand Down
4 changes: 1 addition & 3 deletions test/test_models_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from yolort.v5 import focus_transform, space_to_depth


@pytest.mark.parametrize(
"n, b, h, w", [(1, 3, 480, 640), (4, 3, 416, 320), (4, 3, 320, 416)]
)
@pytest.mark.parametrize("n, b, h, w", [(1, 3, 480, 640), (4, 3, 416, 320), (4, 3, 320, 416)])
def test_space_to_depth(n, b, h, w):
tensor_input = torch.rand((n, b, h, w))
out1 = focus_transform(tensor_input)
Expand Down
8 changes: 2 additions & 6 deletions test/test_models_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,5 @@ def test_yolo_transform():
assert targets.shape[1] == 6

# Test annotations after transformation
torch.testing.assert_close(
annotations[0]["boxes"], annotations_copy[0]["boxes"], rtol=0, atol=0
)
torch.testing.assert_close(
annotations[1]["boxes"], annotations_copy[1]["boxes"], rtol=0, atol=0
)
torch.testing.assert_close(annotations[0]["boxes"], annotations_copy[0]["boxes"], rtol=0, atol=0)
torch.testing.assert_close(annotations[1]["boxes"], annotations_copy[1]["boxes"], rtol=0, atol=0)
12 changes: 3 additions & 9 deletions test/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def run_model(
test_ouputs = model(*test_inputs)
if isinstance(test_ouputs, Tensor):
test_ouputs = (test_ouputs,)
self.ort_validate(
onnx_io, test_inputs, test_ouputs, tolerate_small_mismatch
)
self.ort_validate(onnx_io, test_inputs, test_ouputs, tolerate_small_mismatch)

def ort_validate(self, onnx_io, inputs, outputs, tolerate_small_mismatch=False):

Expand All @@ -82,16 +80,12 @@ def to_numpy(tensor):

ort_session = onnxruntime.InferenceSession(onnx_io.getvalue())
# compute onnxruntime output prediction
ort_inputs = dict(
(ort_session.get_inputs()[i].name, inpt) for i, inpt in enumerate(inputs)
)
ort_inputs = dict((ort_session.get_inputs()[i].name, inpt) for i, inpt in enumerate(inputs))
ort_outs = ort_session.run(None, ort_inputs)

for i in range(0, len(outputs)):
try:
torch.testing.assert_close(
outputs[i], ort_outs[i], rtol=1e-03, atol=1e-05
)
torch.testing.assert_close(outputs[i], ort_outs[i], rtol=1e-03, atol=1e-05)
except AssertionError as error:
if tolerate_small_mismatch:
self.assertIn("(0.00%)", str(error), str(error))
Expand Down
13 changes: 3 additions & 10 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ def test_load_from_ultralytics_voc(

torch.testing.assert_allclose(out_from_yolort[0]["boxes"], out_from_yolov5[:, :4])
torch.testing.assert_allclose(out_from_yolort[0]["scores"], out_from_yolov5[:, 4])
torch.testing.assert_allclose(
out_from_yolort[0]["labels"], out_from_yolov5[:, 5].to(dtype=torch.int64)
)
torch.testing.assert_allclose(out_from_yolort[0]["labels"], out_from_yolov5[:, 5].to(dtype=torch.int64))


def test_read_image_to_tensor():
Expand Down Expand Up @@ -194,13 +192,8 @@ def test_feature_extractor(batch_size, height, width, arch, width_multiple):
in_channels = [int(gw * width_multiple) for gw in grow_widths]
strides = [8, 16, 32]
num_outputs = 85
expected_features = [
(batch_size, inc, height // s, width // s)
for inc, s in zip(in_channels, strides)
]
expected_head_outputs = [
(batch_size, c, height // s, width // s, num_outputs) for s in strides
]
expected_features = [(batch_size, inc, height // s, width // s) for inc, s in zip(in_channels, strides)]
expected_head_outputs = [(batch_size, c, height // s, width // s, num_outputs) for s in strides]

model = models.__dict__[arch]()
model = model.train()
Expand Down
4 changes: 1 addition & 3 deletions tools/eval_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def get_parser():
default="./data-bin/coco128/images/train2017",
help="Root path of the dataset containing images",
)
parser.add_argument(
"--annotation_path", default=None, help="Path of the annotation file"
)
parser.add_argument("--annotation_path", default=None, help="Path of the annotation file")
parser.add_argument(
"--eval_type",
default="yolov5",
Expand Down
4 changes: 1 addition & 3 deletions tools/export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ def get_parser():
help="Image size for evaluation (default: 640).",
)
parser.add_argument("--batch_size", default=1, type=int, help="Batch size.")
parser.add_argument(
"--opset", default=DEFAULT_OPSET, type=int, help="opset_version"
)
parser.add_argument("--opset", default=DEFAULT_OPSET, type=int, help="opset_version")
parser.add_argument("--simplify", action="store_true", help="ONNX: simplify model.")

return parser
Expand Down
Loading

0 comments on commit 72b495e

Please sign in to comment.