From dabebe8617469fce12f94ebc7025538a326d2ce6 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 12:14:27 +0800 Subject: [PATCH 1/8] Add PyTorch 1.11.0 in GH Actions --- .github/workflows/ci-test.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-test.yml b/.github/workflows/ci-test.yml index 297b9ff9..de1f1376 100644 --- a/.github/workflows/ci-test.yml +++ b/.github/workflows/ci-test.yml @@ -15,7 +15,7 @@ jobs: fail-fast: false matrix: image: [ 'ubuntu-latest' ] - torch: [ 'PyTorch 1.9.1+cpu', 'PyTorch 1.10.2+cpu' ] + torch: [ 'PyTorch 1.9.1+cpu', 'PyTorch 1.10.2+cpu', 'PyTorch 1.11.0+cpu' ] include: - torch: 'PyTorch 1.9.1+cpu' torch_address: torch==1.9.1+cpu torchvision==0.10.1+cpu -f https://download.pytorch.org/whl/torch_stable.html @@ -25,6 +25,10 @@ jobs: torch_address: torch==1.10.2+cpu torchvision==0.11.3+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html unittest_type: -v --cov=test --cov-report=xml torchvision: release/0.11 + - torch: 'PyTorch 1.11.0+cpu' + torch_address: torch==1.11.0+cpu torchvision==0.12.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html + unittest_type: -v --cov=test --cov-report=xml + torchvision: release/0.12 steps: - name: Clone repository From 8237135d7a1653faeeb23422bccced0c867e3e97 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 12:26:43 +0800 Subject: [PATCH 2/8] Add check_version from yolov5 --- yolort/utils/__init__.py | 6 +++++- yolort/utils/dependency.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 yolort/utils/dependency.py diff --git a/yolort/utils/__init__.py b/yolort/utils/__init__.py index ea8479ef..45904df8 100644 --- a/yolort/utils/__init__.py +++ b/yolort/utils/__init__.py @@ -1,3 +1,5 @@ +# Copyright (c) 2020, yolort team. All rights reserved. + from typing import Callable, Dict, Mapping, Sequence, Union try: @@ -5,6 +7,7 @@ except ImportError: from torch.utils.model_zoo import load_url as load_state_dict_from_url +from .dependency import check_version from .hooks import FeatureExtractor from .image_utils import cv2_imshow, get_image_from_url, read_image_to_tensor from .update_module_state import convert_yolov5_to_yolort, load_from_ultralytics @@ -12,7 +15,7 @@ __all__ = [ - "FeatureExtractor", + "check_version", "cv2_imshow", "get_image_from_url", "get_callable_dict", @@ -20,6 +23,7 @@ "load_from_ultralytics", "load_state_dict_from_url", "read_image_to_tensor", + "FeatureExtractor", "Visualizer", ] diff --git a/yolort/utils/dependency.py b/yolort/utils/dependency.py new file mode 100644 index 00000000..2a26b1a5 --- /dev/null +++ b/yolort/utils/dependency.py @@ -0,0 +1,27 @@ +import logging +import pkg_resources as pkg + +logger = logging.getLogger(__name__) + + +def check_version( + current: str = "0.0.0", + minimum: str = "0.0.0", + name: str = "version ", + pinned: bool = False, + hard: bool = False, + verbose: bool = False, +): + """ + Check version vs. required version. + Adapted from https://github.com/ultralytics/yolov5/blob/c6b4f84/utils/general.py#L293 + """ + + current, minimum = (pkg.parse_version(x) for x in (current, minimum)) + result = (current == minimum) if pinned else (current >= minimum) # bool + verbose_info = f"{name}{minimum} required by yolort, but {name}{current} is currently installed" + if hard: + assert result, verbose_info # assert min requirements met + if verbose and not result: + logger.warning(verbose_info) + return result From e175764606f946d4d38171f3ecc40bee6ef31f56 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 12:40:57 +0800 Subject: [PATCH 3/8] Compatibility updates --- yolort/models/anchor_utils.py | 7 ++++++- yolort/v5/helper.py | 14 ++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/yolort/models/anchor_utils.py b/yolort/models/anchor_utils.py index b444df59..6aff8aaa 100644 --- a/yolort/models/anchor_utils.py +++ b/yolort/models/anchor_utils.py @@ -5,6 +5,8 @@ import torch from torch import nn, Tensor +from yolort.utils import check_version + class AnchorGenerator(nn.Module): def __init__(self, strides: List[int], anchor_grids: List[List[float]]): @@ -29,7 +31,10 @@ def _generate_grids( widths = torch.arange(width, dtype=torch.int32, device=device).to(dtype=dtype) heights = torch.arange(height, dtype=torch.int32, device=device).to(dtype=dtype) - shift_y, shift_x = torch.meshgrid(heights, widths) + if check_version(torch.__version__, "1.10.0"): + shift_y, shift_x = torch.meshgrid(heights, widths, indexing="ij") + else: + shift_y, shift_x = torch.meshgrid(heights, widths) grid = torch.stack((shift_x, shift_y), 2).expand((1, self.num_anchors, height, width, 2)) grids.append(grid) diff --git a/yolort/v5/helper.py b/yolort/v5/helper.py index 2677e6d7..a2a92be6 100644 --- a/yolort/v5/helper.py +++ b/yolort/v5/helper.py @@ -71,11 +71,13 @@ def load_yolov5_model(checkpoint_path: str, fuse: bool = False): model = ckpt["ema" if ckpt.get("ema") else "model"].float().eval() # Compatibility updates - for m in model.modules(): - if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model]: - if isinstance(m, Detect): - if not isinstance(m.anchor_grid, list): # new Detect Layer compatibility - delattr(m, "anchor_grid") - setattr(m, "anchor_grid", [torch.zeros(1)] * m.nl) + for sub_module in model.modules(): + if type(sub_module) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model]: + if isinstance(sub_module, Detect): + if not isinstance(sub_module.anchor_grid, list): # new Detect Layer compatibility + delattr(sub_module, "anchor_grid") + setattr(sub_module, "anchor_grid", [torch.zeros(1)] * sub_module.nl) + elif isinstance(sub_module, nn.Upsample) and not hasattr(sub_module, "recompute_scale_factor"): + sub_module.recompute_scale_factor = None # torch 1.11.0 compatibility return model From 3c48668ebe1bfc9415a44b1b708f730a43fe546f Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 12:53:58 +0800 Subject: [PATCH 4/8] GeneratorExp aren't supported in TorchScript --- yolort/models/anchor_utils.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/yolort/models/anchor_utils.py b/yolort/models/anchor_utils.py index 6aff8aaa..b444df59 100644 --- a/yolort/models/anchor_utils.py +++ b/yolort/models/anchor_utils.py @@ -5,8 +5,6 @@ import torch from torch import nn, Tensor -from yolort.utils import check_version - class AnchorGenerator(nn.Module): def __init__(self, strides: List[int], anchor_grids: List[List[float]]): @@ -31,10 +29,7 @@ def _generate_grids( widths = torch.arange(width, dtype=torch.int32, device=device).to(dtype=dtype) heights = torch.arange(height, dtype=torch.int32, device=device).to(dtype=dtype) - if check_version(torch.__version__, "1.10.0"): - shift_y, shift_x = torch.meshgrid(heights, widths, indexing="ij") - else: - shift_y, shift_x = torch.meshgrid(heights, widths) + shift_y, shift_x = torch.meshgrid(heights, widths) grid = torch.stack((shift_x, shift_y), 2).expand((1, self.num_anchors, height, width, 2)) grids.append(grid) From 52c235354a9f1d9bc40d1d1018dc20b9f664d7f4 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 12:58:00 +0800 Subject: [PATCH 5/8] Apply pre-commit --- yolort/utils/dependency.py | 1 + 1 file changed, 1 insertion(+) diff --git a/yolort/utils/dependency.py b/yolort/utils/dependency.py index 2a26b1a5..0d02bb2d 100644 --- a/yolort/utils/dependency.py +++ b/yolort/utils/dependency.py @@ -1,4 +1,5 @@ import logging + import pkg_resources as pkg logger = logging.getLogger(__name__) From 8d211db5bb3cf649b280ffd089e5fefee871adf2 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 13:12:30 +0800 Subject: [PATCH 6/8] Minor updates --- yolort/v5/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yolort/v5/helper.py b/yolort/v5/helper.py index a2a92be6..171304a6 100644 --- a/yolort/v5/helper.py +++ b/yolort/v5/helper.py @@ -77,7 +77,7 @@ def load_yolov5_model(checkpoint_path: str, fuse: bool = False): if not isinstance(sub_module.anchor_grid, list): # new Detect Layer compatibility delattr(sub_module, "anchor_grid") setattr(sub_module, "anchor_grid", [torch.zeros(1)] * sub_module.nl) - elif isinstance(sub_module, nn.Upsample) and not hasattr(sub_module, "recompute_scale_factor"): + if isinstance(sub_module, nn.Upsample) and not hasattr(sub_module, "recompute_scale_factor"): sub_module.recompute_scale_factor = None # torch 1.11.0 compatibility return model From bf1890650338ddea2bb1dae1f5a307817ed95c29 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 13:18:50 +0800 Subject: [PATCH 7/8] Upgrade PyTorch minimal version to 1.8.0, add Python 3.10 and remove Python 3.6 --- README.md | 2 +- setup.py | 4 ++-- yolort/v5/helper.py | 9 ++++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 86dcf75f..01951e97 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ There are no extra compiled components in `yolort` and package dependencies are ### Installation and Inference Examples -- Above all, follow the [official instructions](https://pytorch.org/get-started/locally/) to install PyTorch 1.7.0+ and torchvision 0.8.1+ +- Above all, follow the [official instructions](https://pytorch.org/get-started/locally/) to install PyTorch 1.8.0+ and torchvision 0.9.0+ - Installation via pip diff --git a/setup.py b/setup.py index f86140a1..b47ed333 100644 --- a/setup.py +++ b/setup.py @@ -99,10 +99,10 @@ def load_requirements(path_dir=PATH_ROOT, file_name="requirements.txt", comment_ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", # Specify the Python versions you support here. "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", ], install_requires=load_requirements(), # This field adds keywords for your project which will appear on the @@ -116,7 +116,7 @@ def load_requirements(path_dir=PATH_ROOT, file_name="requirements.txt", comment_ # 'Programming Language' classifiers above, 'pip install' will check this # and refuse to install the project if the version does not match. See # https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires - python_requires=">=3.6.2", + python_requires=">=3.7", # List additional URLs that are relevant to your project as a dict. # # This field corresponds to the "Project-URL" metadata fields: diff --git a/yolort/v5/helper.py b/yolort/v5/helper.py index 171304a6..4d53e1e1 100644 --- a/yolort/v5/helper.py +++ b/yolort/v5/helper.py @@ -72,11 +72,10 @@ def load_yolov5_model(checkpoint_path: str, fuse: bool = False): # Compatibility updates for sub_module in model.modules(): - if type(sub_module) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model]: - if isinstance(sub_module, Detect): - if not isinstance(sub_module.anchor_grid, list): # new Detect Layer compatibility - delattr(sub_module, "anchor_grid") - setattr(sub_module, "anchor_grid", [torch.zeros(1)] * sub_module.nl) + if isinstance(sub_module, Detect): + if not isinstance(sub_module.anchor_grid, list): # new Detect Layer compatibility + delattr(sub_module, "anchor_grid") + setattr(sub_module, "anchor_grid", [torch.zeros(1)] * sub_module.nl) if isinstance(sub_module, nn.Upsample) and not hasattr(sub_module, "recompute_scale_factor"): sub_module.recompute_scale_factor = None # torch 1.11.0 compatibility From 8f8ffcbac00afed022e0eb28dc08986b2d6afcd5 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 13:20:09 +0800 Subject: [PATCH 8/8] Fix pre-commit --- yolort/v5/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yolort/v5/helper.py b/yolort/v5/helper.py index 4d53e1e1..27ebdcc8 100644 --- a/yolort/v5/helper.py +++ b/yolort/v5/helper.py @@ -6,7 +6,7 @@ import torch from torch import nn -from .models.yolo import Detect, Model +from .models.yolo import Detect from .utils import attempt_download __all__ = ["add_yolov5_context", "load_yolov5_model", "get_yolov5_size"]