Skip to content

Commit

Permalink
upgrade pytorch lightning version (#458)
Browse files Browse the repository at this point in the history
* upgrade lightning version
  • Loading branch information
RangiLyu committed Aug 26, 2022
1 parent 0f4d8f1 commit fa2c9b2
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Keep in sync with setup.cfg which is used for source packages.

[flake8]
ignore = W503, E203, E221, C901, C408, E741, C407, E741, B006, B007, B017, B950, C416
ignore = W503, E203, E221, C901, C408, E741, C407, E741, B006, B007, B017, B950, C416, E203
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
Expand Down
26 changes: 14 additions & 12 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.6
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8==3.9.2 flake8-bugbear flake8-comprehensions isort==5.8.0
python -m pip install black==21.6b0
python -m pip install flake8==5.0.4 isort==5.10.1
python -m pip install black==22.6.0
flake8 --version
- name: Lint
run: |
Expand All @@ -34,21 +34,23 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
torch: [1.7.0, 1.8.1, 1.9.0]
torch: [1.9.0, 1.10.1, 1.11.0, 1.12.1]
include:
- torch: 1.7.0
torchvision: 0.8.1
- torch: 1.8.1
torchvision: 0.9.1
- torch: 1.9.0
torchvision: 0.10.0
- torch: 1.10.1
torchvision: 0.11.2
- torch: 1.11.0
torchvision: 0.12.0
- torch: 1.12.1
torchvision: 0.13.1
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python 3.6
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install -U pip
Expand All @@ -65,7 +67,7 @@ jobs:
coverage report -m
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
if: matrix.torch == '1.9.0'
if: matrix.torch == '1.12.1'
with:
file: ./coverage.xml
flags: unittests
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ repos:
- id: requirements-txt-fixer

- repo: https://github.com/pycqa/isort
rev: 5.8.0
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://github.com/psf/black
rev: 21.6b0
rev: 22.6.0
hooks:
- id: black

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
rev: 5.0.4
hooks:
- id: flake8
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ MobileDet | 320*320 | 25.6 | - | -

## NEWS!!!

* [2022.08.26] Upgrade to [pytorch-lightning-1.7](https://lightning.ai/). The minimum PyTorch version is upgraded to 1.9. To use previous version of PyTorch, please install [NanoDet <= v1.0.0-alpha-1](https://github.com/RangiLyu/nanodet/tags)

* [2021.12.25] **NanoDet-Plus** release! Adding **AGM**(Assign Guidance Module) & **DSLA**(Dynamic Soft Label Assigner) to improve **7 mAP** with only a little cost.

Find more update notes in [Update notes](docs/update.md).
Expand Down Expand Up @@ -139,7 +141,7 @@ Besides, We provide a notebook [here](./demo/demo-inference-with-pytorch.ipynb)
* Linux or MacOS
* CUDA >= 10.0
* Python >= 3.6
* Pytorch >= 1.7
* Pytorch >= 1.9
* experimental support Windows (Notice: Windows not support distributed training before pytorch1.7)

### Step
Expand Down Expand Up @@ -169,7 +171,7 @@ cd nanodet
```shell script
pip install -r requirements.txt
```

5. Setup NanoDet
```shell script
python setup.py develop
Expand Down
8 changes: 4 additions & 4 deletions nanodet/model/loss/iou_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def diou_loss(pred, target, eps=1e-7):
cw = enclose_wh[:, 0]
ch = enclose_wh[:, 1]

c2 = cw ** 2 + ch ** 2 + eps
c2 = cw**2 + ch**2 + eps

b1_x1, b1_y1 = pred[:, 0], pred[:, 1]
b1_x2, b1_y2 = pred[:, 2], pred[:, 3]
Expand Down Expand Up @@ -326,7 +326,7 @@ def ciou_loss(pred, target, eps=1e-7):
cw = enclose_wh[:, 0]
ch = enclose_wh[:, 1]

c2 = cw ** 2 + ch ** 2 + eps
c2 = cw**2 + ch**2 + eps

b1_x1, b1_y1 = pred[:, 0], pred[:, 1]
b1_x2, b1_y2 = pred[:, 2], pred[:, 3]
Expand All @@ -340,11 +340,11 @@ def ciou_loss(pred, target, eps=1e-7):
right = ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
rho2 = left + right

factor = 4 / math.pi ** 2
factor = 4 / math.pi**2
v = factor * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)

# CIoU
cious = ious - (rho2 / c2 + v ** 2 / (1 - ious + v))
cious = ious - (rho2 / c2 + v**2 / (1 - ious + v))
loss = 1 - cious
return loss

Expand Down
6 changes: 3 additions & 3 deletions nanodet/trainer/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def on_train_start(self) -> None:
if self.current_epoch > 0:
self.lr_scheduler.last_epoch = self.current_epoch - 1

def on_pretrain_routine_end(self) -> None:
def on_fit_start(self) -> None:
if "weight_averager" in self.cfg.model:
self.logger.info("Weight Averaging is enabled")
if self.weight_averager and self.weight_averager.has_inited():
Expand All @@ -327,10 +327,10 @@ def on_pretrain_routine_end(self) -> None:
)
self.weight_averager.load_from(self.model)

def on_epoch_start(self):
def on_train_epoch_start(self):
self.model.set_epoch(self.current_epoch)

def on_train_batch_end(self, outputs, batch, batch_idx, dataloader_idx) -> None:
def on_train_batch_end(self, outputs, batch, batch_idx) -> None:
if self.weight_averager:
self.weight_averager.update(self.model, self.global_step)

Expand Down
30 changes: 15 additions & 15 deletions nanodet/util/flops_counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,21 @@ def flops_to_string(flops, units="GFLOPs", precision=2):
'3e-09 FLOPs'
"""
if units is None:
if flops // 10 ** 9 > 0:
return str(round(flops / 10.0 ** 9, precision)) + " GFLOPs"
elif flops // 10 ** 6 > 0:
return str(round(flops / 10.0 ** 6, precision)) + " MFLOPs"
elif flops // 10 ** 3 > 0:
return str(round(flops / 10.0 ** 3, precision)) + " KFLOPs"
if flops // 10**9 > 0:
return str(round(flops / 10.0**9, precision)) + " GFLOPs"
elif flops // 10**6 > 0:
return str(round(flops / 10.0**6, precision)) + " MFLOPs"
elif flops // 10**3 > 0:
return str(round(flops / 10.0**3, precision)) + " KFLOPs"
else:
return str(flops) + " FLOPs"
else:
if units == "GFLOPs":
return str(round(flops / 10.0 ** 9, precision)) + " " + units
return str(round(flops / 10.0**9, precision)) + " " + units
elif units == "MFLOPs":
return str(round(flops / 10.0 ** 6, precision)) + " " + units
return str(round(flops / 10.0**6, precision)) + " " + units
elif units == "KFLOPs":
return str(round(flops / 10.0 ** 3, precision)) + " " + units
return str(round(flops / 10.0**3, precision)) + " " + units
else:
return str(flops) + " FLOPs"

Expand All @@ -170,17 +170,17 @@ def params_to_string(num_params, units=None, precision=2):
'3e-09'
"""
if units is None:
if num_params // 10 ** 6 > 0:
return str(round(num_params / 10 ** 6, precision)) + " M"
elif num_params // 10 ** 3:
return str(round(num_params / 10 ** 3, precision)) + " k"
if num_params // 10**6 > 0:
return str(round(num_params / 10**6, precision)) + " M"
elif num_params // 10**3:
return str(round(num_params / 10**3, precision)) + " k"
else:
return str(num_params)
else:
if units == "M":
return str(round(num_params / 10.0 ** 6, precision)) + " " + units
return str(round(num_params / 10.0**6, precision)) + " " + units
elif units == "K":
return str(round(num_params / 10.0 ** 3, precision)) + " " + units
return str(round(num_params / 10.0**3, precision)) + " " + units
else:
return str(num_params)

Expand Down
4 changes: 2 additions & 2 deletions nanodet/util/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import time

import numpy as np
from pytorch_lightning.loggers import LightningLoggerBase
from pytorch_lightning.loggers.base import rank_zero_experiment
from pytorch_lightning.loggers import Logger as LightningLoggerBase
from pytorch_lightning.loggers.logger import rank_zero_experiment
from pytorch_lightning.utilities import rank_zero_only
from pytorch_lightning.utilities.cloud_io import get_filesystem
from termcolor import colored
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ onnx-simplifier
opencv-python
pyaml
pycocotools
pytorch-lightning>=1.4.0
pytorch-lightning>=1.7.0
tabulate
tensorboard
termcolor
torch>=1.7
torch>=1.9
torchmetrics
torchvision
tqdm
2 changes: 1 addition & 1 deletion tests/test_models/test_fpn/test_fpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_fpn():
"""Tests fpn."""
s = 64
in_channels = [8, 16, 32, 64]
feat_sizes = [s // 2 ** i for i in range(4)] # [64, 32, 16, 8]
feat_sizes = [s // 2**i for i in range(4)] # [64, 32, 16, 8]
out_channels = 8
# `num_outs` is not equal to len(in_channels) - start_level
with pytest.raises(AssertionError):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_models/test_fpn/test_ghost_pan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_ghost_pan():
"""Tests GhostPAN."""
s = 64
in_channels = [8, 16, 32, 64]
feat_sizes = [s // 2 ** i for i in range(4)] # [64, 32, 16, 8]
feat_sizes = [s // 2**i for i in range(4)] # [64, 32, 16, 8]
out_channels = 8
# `num_extra_level` >= 0
with pytest.raises(AssertionError):
Expand All @@ -34,7 +34,7 @@ def test_ghost_pan():
assert len(outs) == len(in_channels) + 1
for i in range(len(in_channels)):
assert outs[i].shape[1] == out_channels
assert outs[i].shape[2] == outs[i].shape[3] == s // (2 ** i)
assert outs[i].shape[2] == outs[i].shape[3] == s // (2**i)
assert outs[-1].shape[1] == out_channels
assert outs[-1].shape[2] == outs[-1].shape[3] == s // 2 ** len(in_channels)

Expand All @@ -61,6 +61,6 @@ def test_ghost_pan():
assert len(outs) == len(in_channels) + 1
for i in range(len(in_channels)):
assert outs[i].shape[1] == out_channels
assert outs[i].shape[2] == outs[i].shape[3] == s // (2 ** i)
assert outs[i].shape[2] == outs[i].shape[3] == s // (2**i)
assert outs[-1].shape[1] == out_channels
assert outs[-1].shape[2] == outs[-1].shape[3] == s // 2 ** len(in_channels)
2 changes: 1 addition & 1 deletion tests/test_models/test_fpn/test_pan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_pan():
"""Tests PAN."""
s = 64
in_channels = [8, 16, 32, 64]
feat_sizes = [s // 2 ** i for i in range(4)] # [64, 32, 16, 8]
feat_sizes = [s // 2**i for i in range(4)] # [64, 32, 16, 8]
out_channels = 8
# `num_outs` is not equal to len(in_channels) - start_level
with pytest.raises(AssertionError):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models/test_fpn/test_tan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_tan():
"""Tests TAN."""
s = 64
in_channels = [8, 16, 32]
feat_sizes = [s // 2 ** i for i in range(3)] # [64, 32, 16]
feat_sizes = [s // 2**i for i in range(3)] # [64, 32, 16]
out_channels = 8

with pytest.raises(AssertionError):
Expand Down Expand Up @@ -41,4 +41,4 @@ def test_tan():
assert len(outs) == 3
for i in range(3):
assert outs[i].shape[1] == out_channels
assert outs[i].shape[2] == outs[i].shape[3] == s // (2 ** i)
assert outs[i].shape[2] == outs[i].shape[3] == s // (2**i)
6 changes: 3 additions & 3 deletions tests/test_models/test_modules/test_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def test_build_norm_layer():
cfg = dict(type=type_name)
if type_name == "GN":
cfg["num_groups"] = 2
name, layer = build_norm_layer(cfg, 3, postfix=postfix)
name, layer = build_norm_layer(cfg, 4, postfix=postfix)
assert name == abbr_mapping[type_name] + str(postfix)
assert isinstance(layer, module)
if type_name == "GN":
assert layer.num_channels == 3
assert layer.num_channels == 4
assert layer.num_groups == cfg["num_groups"]
elif type_name != "LN":
assert layer.num_features == 3
assert layer.num_features == 4
4 changes: 2 additions & 2 deletions tests/test_trainer/test_lightning_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DummyTrainer(nn.Module):
global_step = 0
local_rank = 0
use_ddp = False
logger = NanoDetLightningLogger(tempfile.TemporaryDirectory().name)
loggers = [NanoDetLightningLogger(tempfile.TemporaryDirectory().name)]

def save_checkpoint(self, *args, **kwargs):
pass
Expand All @@ -24,7 +24,7 @@ def __init__(self, task):
self.task = task

def test(self):
self.task.trainer = DummyTrainer()
self.task._trainer = DummyTrainer()

optimizer = self.task.configure_optimizers()

Expand Down
10 changes: 8 additions & 2 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ def main(args):
ckpt = convert_old_model(ckpt)
task.load_state_dict(ckpt["state_dict"])

if cfg.device.gpu_ids == -1:
logger.info("Using CPU training")
accelerator, devices = "cpu", None
else:
accelerator, devices = "gpu", cfg.device.gpu_ids

trainer = pl.Trainer(
default_root_dir=cfg.save_dir,
gpus=cfg.device.gpu_ids,
accelerator="ddp",
accelerator=accelerator,
devices=devices,
log_every_n_steps=cfg.log.interval,
num_sanity_val_steps=0,
logger=logger,
Expand Down
Loading

0 comments on commit fa2c9b2

Please sign in to comment.