Skip to content

Commit 2bb3ac0

Browse files
authored
Meshgrid indexing='ij' for PyTorch 1.10 (ultralytics#5309)
* 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
1 parent dc2e587 commit 2bb3ac0

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

models/yolo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from models.common import *
2121
from models.experimental import *
2222
from utils.autoanchor import check_anchor_order
23-
from utils.general import check_yaml, make_divisible, print_args, set_logging
23+
from utils.general import check_yaml, make_divisible, print_args, set_logging, check_version
2424
from utils.plots import feature_visualization
2525
from utils.torch_utils import copy_attr, fuse_conv_and_bn, initialize_weights, model_info, scale_img, \
2626
select_device, time_sync
@@ -74,7 +74,10 @@ def forward(self, x):
7474

7575
def _make_grid(self, nx=20, ny=20, i=0):
7676
d = self.anchors[i].device
77-
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)])
77+
if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
78+
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)], indexing='ij')
79+
else:
80+
yv, xv = torch.meshgrid([torch.arange(ny).to(d), torch.arange(nx).to(d)])
7881
grid = torch.stack((xv, yv), 2).expand((1, self.na, ny, nx, 2)).float()
7982
anchor_grid = (self.anchors[i].clone() * self.stride[i]) \
8083
.view((1, self.na, 1, 1, 2)).expand((1, self.na, ny, nx, 2)).float()

utils/augmentations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self):
2020
self.transform = None
2121
try:
2222
import albumentations as A
23-
check_version(A.__version__, '1.0.3') # version requirement
23+
check_version(A.__version__, '1.0.3', hard=True) # version requirement
2424

2525
self.transform = A.Compose([
2626
A.Blur(p=0.01),

utils/general.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,17 @@ def check_git_status():
220220

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

225225

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

232235

233236
@try_except

0 commit comments

Comments
 (0)