Skip to content

Commit aa39be7

Browse files
JiayuXu0ZwwWayne
authored andcommitted
Fix floordiv warning. (#8648)
* Fix floordiv warning. * Add floordiv wrapper.
1 parent 7ec952f commit aa39be7

File tree

3 files changed

+64
-21
lines changed

3 files changed

+64
-21
lines changed

mmdet/models/dense_heads/solo_head.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mmdet.core import InstanceData, mask_matrix_nms, multi_apply
1010
from mmdet.core.utils import center_of_mass, generate_coordinate
1111
from mmdet.models.builder import HEADS, build_loss
12+
from mmdet.utils.misc import floordiv
1213
from .base_mask_head import BaseMaskHead
1314

1415

@@ -375,27 +376,41 @@ def _get_targets_single(self,
375376
center_h, center_w = center_of_mass(gt_mask)
376377

377378
coord_w = int(
378-
(center_w / upsampled_size[1]) // (1. / num_grid))
379+
floordiv((center_w / upsampled_size[1]), (1. / num_grid),
380+
rounding_mode='trunc'))
379381
coord_h = int(
380-
(center_h / upsampled_size[0]) // (1. / num_grid))
382+
floordiv((center_h / upsampled_size[0]), (1. / num_grid),
383+
rounding_mode='trunc'))
381384

382385
# left, top, right, down
383386
top_box = max(
384387
0,
385-
int(((center_h - pos_h_range) / upsampled_size[0]) //
386-
(1. / num_grid)))
388+
int(
389+
floordiv(
390+
(center_h - pos_h_range) / upsampled_size[0],
391+
(1. / num_grid),
392+
rounding_mode='trunc')))
387393
down_box = min(
388394
num_grid - 1,
389-
int(((center_h + pos_h_range) / upsampled_size[0]) //
390-
(1. / num_grid)))
395+
int(
396+
floordiv(
397+
(center_h + pos_h_range) / upsampled_size[0],
398+
(1. / num_grid),
399+
rounding_mode='trunc')))
391400
left_box = max(
392401
0,
393-
int(((center_w - pos_w_range) / upsampled_size[1]) //
394-
(1. / num_grid)))
402+
int(
403+
floordiv(
404+
(center_w - pos_w_range) / upsampled_size[1],
405+
(1. / num_grid),
406+
rounding_mode='trunc')))
395407
right_box = min(
396408
num_grid - 1,
397-
int(((center_w + pos_w_range) / upsampled_size[1]) //
398-
(1. / num_grid)))
409+
int(
410+
floordiv(
411+
(center_w + pos_w_range) / upsampled_size[1],
412+
(1. / num_grid),
413+
rounding_mode='trunc')))
399414

400415
top = max(top_box, coord_h - 1)
401416
down = min(down_box, coord_h + 1)

mmdet/models/dense_heads/solov2_head.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from mmdet.core import InstanceData, mask_matrix_nms, multi_apply
1313
from mmdet.core.utils import center_of_mass, generate_coordinate
1414
from mmdet.models.builder import HEADS
15+
from mmdet.utils.misc import floordiv
1516
from .solo_head import SOLOHead
1617

1718

@@ -382,27 +383,41 @@ def _get_targets_single(self,
382383
center_h, center_w = center_of_mass(gt_mask)
383384

384385
coord_w = int(
385-
(center_w / upsampled_size[1]) // (1. / num_grid))
386+
floordiv((center_w / upsampled_size[1]), (1. / num_grid),
387+
rounding_mode='trunc'))
386388
coord_h = int(
387-
(center_h / upsampled_size[0]) // (1. / num_grid))
389+
floordiv((center_h / upsampled_size[0]), (1. / num_grid),
390+
rounding_mode='trunc'))
388391

389392
# left, top, right, down
390393
top_box = max(
391394
0,
392-
int(((center_h - pos_h_range) / upsampled_size[0]) //
393-
(1. / num_grid)))
395+
int(
396+
floordiv(
397+
(center_h - pos_h_range) / upsampled_size[0],
398+
(1. / num_grid),
399+
rounding_mode='trunc')))
394400
down_box = min(
395401
num_grid - 1,
396-
int(((center_h + pos_h_range) / upsampled_size[0]) //
397-
(1. / num_grid)))
402+
int(
403+
floordiv(
404+
(center_h + pos_h_range) / upsampled_size[0],
405+
(1. / num_grid),
406+
rounding_mode='trunc')))
398407
left_box = max(
399408
0,
400-
int(((center_w - pos_w_range) / upsampled_size[1]) //
401-
(1. / num_grid)))
409+
int(
410+
floordiv(
411+
(center_w - pos_w_range) / upsampled_size[1],
412+
(1. / num_grid),
413+
rounding_mode='trunc')))
402414
right_box = min(
403415
num_grid - 1,
404-
int(((center_w + pos_w_range) / upsampled_size[1]) //
405-
(1. / num_grid)))
416+
int(
417+
floordiv(
418+
(center_w + pos_w_range) / upsampled_size[1],
419+
(1. / num_grid),
420+
rounding_mode='trunc')))
406421

407422
top = max(top_box, coord_h - 1)
408423
down = min(down_box, coord_h + 1)

mmdet/utils/misc.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import warnings
66

77
import mmcv
8-
from mmcv.utils import print_log
8+
import torch
9+
from mmcv.utils import TORCH_VERSION, digit_version, print_log
910

1011

1112
def find_latest_checkpoint(path, suffix='pth'):
@@ -74,3 +75,15 @@ def update(cfg, src_str, dst_str):
7475

7576
update(cfg.data, cfg.data_root, dst_root)
7677
cfg.data_root = dst_root
78+
79+
80+
_torch_version_div_indexing = (
81+
'parrots' not in TORCH_VERSION
82+
and digit_version(TORCH_VERSION) >= digit_version('1.8'))
83+
84+
85+
def floordiv(dividend, divisor, rounding_mode='trunc'):
86+
if _torch_version_div_indexing:
87+
return torch.div(dividend, divisor, rounding_mode=rounding_mode)
88+
else:
89+
return dividend // divisor

0 commit comments

Comments
 (0)