From d088d5fca243dd0f04e0d69b9565bf28d41aaf03 Mon Sep 17 00:00:00 2001 From: Zhiqiang Wang Date: Sat, 12 Mar 2022 13:38:08 +0800 Subject: [PATCH] Add PyTorch 1.11.0 in GH Actions (#359) * Add PyTorch 1.11.0 in GH Actions * Add check_version from yolov5 * Compatibility updates * GeneratorExp aren't supported in TorchScript * Apply pre-commit * Minor updates * Upgrade PyTorch minimal version to 1.8.0, add Python 3.10 and remove Python 3.6 * Fix pre-commit --- .github/workflows/ci-test.yml | 6 +++++- README.md | 2 +- setup.py | 4 ++-- yolort/utils/__init__.py | 6 +++++- yolort/utils/dependency.py | 28 ++++++++++++++++++++++++++++ yolort/v5/helper.py | 15 ++++++++------- 6 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 yolort/utils/dependency.py 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 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/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..0d02bb2d --- /dev/null +++ b/yolort/utils/dependency.py @@ -0,0 +1,28 @@ +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 diff --git a/yolort/v5/helper.py b/yolort/v5/helper.py index 2677e6d7..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"] @@ -71,11 +71,12 @@ 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 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 return model