Skip to content

Commit

Permalink
Compatibility updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqwang committed Mar 12, 2022
1 parent 8237135 commit e175764
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
7 changes: 6 additions & 1 deletion yolort/models/anchor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]):
Expand All @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions yolort/v5/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e175764

Please sign in to comment.