Skip to content

Commit

Permalink
Meshgrid indexing='ij' for PyTorch 1.10 (ultralytics#5309)
Browse files Browse the repository at this point in the history
* Meshgrid `indexing='ij'` for PyTorch 1.10

Will not merge currently as breaks backwards compatibility.

* Meshgrid `indexing='ij'` for PyTorch 1.10

Will not merge currently as breaks backwards compatibility.

* Add check_version hard argument

* Update comment
  • Loading branch information
glenn-jocher committed Nov 1, 2021
1 parent dc2e587 commit 2bb3ac0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions models/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from models.common import *
from models.experimental import *
from utils.autoanchor import check_anchor_order
from utils.general import check_yaml, make_divisible, print_args, set_logging
from utils.general import check_yaml, make_divisible, print_args, set_logging, check_version
from utils.plots import feature_visualization
from utils.torch_utils import copy_attr, fuse_conv_and_bn, initialize_weights, model_info, scale_img, \
select_device, time_sync
Expand Down Expand Up @@ -74,7 +74,10 @@ def forward(self, x):

def _make_grid(self, nx=20, ny=20, i=0):
d = self.anchors[i].device
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)])
if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)], indexing='ij')
else:
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)])
grid = torch.stack((xv, yv), 2).expand((1, self.na, ny, nx, 2)).float()
anchor_grid = (self.anchors[i].clone() * self.stride[i]) \
.view((1, self.na, 1, 1, 2)).expand((1, self.na, ny, nx, 2)).float()
Expand Down
2 changes: 1 addition & 1 deletion utils/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
self.transform = None
try:
import albumentations as A
check_version(A.__version__, '1.0.3') # version requirement
check_version(A.__version__, '1.0.3', hard=True) # version requirement

self.transform = A.Compose([
A.Blur(p=0.01),
Expand Down
11 changes: 7 additions & 4 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,17 @@ def check_git_status():

def check_python(minimum='3.6.2'):
# Check current python version vs. required python version
check_version(platform.python_version(), minimum, name='Python ')
check_version(platform.python_version(), minimum, name='Python ', hard=True)


def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False):
def check_version(current='0.0.0', minimum='0.0.0', name='version ', pinned=False, hard=False):
# Check version vs. required version
current, minimum = (pkg.parse_version(x) for x in (current, minimum))
result = (current == minimum) if pinned else (current >= minimum)
assert result, f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed'
result = (current == minimum) if pinned else (current >= minimum) # bool
if hard: # assert min requirements met
assert result, f'{name}{minimum} required by YOLOv5, but {name}{current} is currently installed'
else:
return result


@try_except
Expand Down

0 comments on commit 2bb3ac0

Please sign in to comment.