From e2c4eff91b0150b9f44d9a9795e21d7984c56c90 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 12 Oct 2021 17:57:39 +0530 Subject: [PATCH 1/5] Add typing to anchor utils --- torchvision/models/detection/anchor_utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/torchvision/models/detection/anchor_utils.py b/torchvision/models/detection/anchor_utils.py index 2e433958715..a002fee8a47 100644 --- a/torchvision/models/detection/anchor_utils.py +++ b/torchvision/models/detection/anchor_utils.py @@ -1,5 +1,5 @@ import math -from typing import List, Optional +from typing import List, Optional, Tuple import torch from torch import nn, Tensor @@ -34,9 +34,10 @@ class AnchorGenerator(nn.Module): def __init__( self, - sizes=((128, 256, 512),), - aspect_ratios=((0.5, 1.0, 2.0),), - ): + sizes: Tuple[Tuple[int, int, int]] = ((128, 256, 512),), + aspect_ratios: Tuple[Tuple[float, float, float]] = ((0.5, 1.0, 2.0),), + ) -> None: + super(AnchorGenerator, self).__init__() if not isinstance(sizes[0], (list, tuple)): @@ -63,7 +64,7 @@ def generate_anchors( aspect_ratios: List[float], dtype: torch.dtype = torch.float32, device: torch.device = torch.device("cpu"), - ): + ) -> Tensor: scales = torch.as_tensor(scales, dtype=dtype, device=device) aspect_ratios = torch.as_tensor(aspect_ratios, dtype=dtype, device=device) h_ratios = torch.sqrt(aspect_ratios) @@ -75,10 +76,10 @@ def generate_anchors( base_anchors = torch.stack([-ws, -hs, ws, hs], dim=1) / 2 return base_anchors.round() - def set_cell_anchors(self, dtype: torch.dtype, device: torch.device): + def set_cell_anchors(self, dtype: torch.dtype, device: torch.device) -> None: self.cell_anchors = [cell_anchor.to(dtype=dtype, device=device) for cell_anchor in self.cell_anchors] - def num_anchors_per_location(self): + def num_anchors_per_location(self) -> List[int]: return [len(s) * len(a) for s, a in zip(self.sizes, self.aspect_ratios)] # For every combination of (a, (g, s), i) in (self.cell_anchors, zip(grid_sizes, strides), 0:2), @@ -162,7 +163,7 @@ def __init__( scales: Optional[List[float]] = None, steps: Optional[List[int]] = None, clip: bool = True, - ): + ) -> None: super().__init__() if steps is not None: assert len(aspect_ratios) == len(steps) @@ -204,7 +205,7 @@ def _generate_wh_pairs( _wh_pairs.append(torch.as_tensor(wh_pairs, dtype=dtype, device=device)) return _wh_pairs - def num_anchors_per_location(self): + def num_anchors_per_location(self) -> List[int]: # Estimate num of anchors based on aspect ratios: 2 default boxes + 2 * ratios of feaure map. return [2 + 2 * len(r) for r in self.aspect_ratios] From 607a70cdd8b8732eab839356ae9c36ffcd68c555 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 12 Oct 2021 18:29:03 +0530 Subject: [PATCH 2/5] Try variable tuple --- torchvision/models/detection/anchor_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchvision/models/detection/anchor_utils.py b/torchvision/models/detection/anchor_utils.py index a002fee8a47..44ba5ac0b46 100644 --- a/torchvision/models/detection/anchor_utils.py +++ b/torchvision/models/detection/anchor_utils.py @@ -34,8 +34,8 @@ class AnchorGenerator(nn.Module): def __init__( self, - sizes: Tuple[Tuple[int, int, int]] = ((128, 256, 512),), - aspect_ratios: Tuple[Tuple[float, float, float]] = ((0.5, 1.0, 2.0),), + sizes: Tuple[Tuple[int, ...]] = ((128, 256, 512),), + aspect_ratios: Tuple[Tuple[float, ...]] = ((0.5, 1.0, 2.0),), ) -> None: super(AnchorGenerator, self).__init__() From a951312cdd2123395e01a4dbcbee3baeab805309 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 12 Oct 2021 19:49:16 +0530 Subject: [PATCH 3/5] Open up mypy, fix mypy bugs --- mypy.ini | 4 ---- torchvision/models/detection/anchor_utils.py | 14 +++++++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/mypy.ini b/mypy.ini index 0c46a09610b..6d2e9c0afc9 100644 --- a/mypy.ini +++ b/mypy.ini @@ -17,10 +17,6 @@ ignore_errors = True ignore_errors=True -[mypy-torchvision.models.detection.anchor_utils] - -ignore_errors = True - [mypy-torchvision.models.detection.backbone_utils] ignore_errors = True diff --git a/torchvision/models/detection/anchor_utils.py b/torchvision/models/detection/anchor_utils.py index 44ba5ac0b46..3953e2e1014 100644 --- a/torchvision/models/detection/anchor_utils.py +++ b/torchvision/models/detection/anchor_utils.py @@ -42,9 +42,9 @@ def __init__( if not isinstance(sizes[0], (list, tuple)): # TODO change this - sizes = tuple((s,) for s in sizes) + sizes = tuple((s,) for s in sizes) # type: ignore[assignment] if not isinstance(aspect_ratios[0], (list, tuple)): - aspect_ratios = (aspect_ratios,) * len(sizes) + aspect_ratios = (aspect_ratios,) * len(sizes) # type: ignore[assignment] assert len(sizes) == len(aspect_ratios) @@ -60,8 +60,8 @@ def __init__( # This method assumes aspect ratio = height / width for an anchor. def generate_anchors( self, - scales: List[int], - aspect_ratios: List[float], + scales: Tuple[int, ...], + aspect_ratios: Tuple[float, ...], dtype: torch.dtype = torch.float32, device: torch.device = torch.device("cpu"), ) -> Tensor: @@ -117,7 +117,7 @@ def grid_anchors(self, grid_sizes: List[List[int]], strides: List[List[Tensor]]) return anchors def forward(self, image_list: ImageList, feature_maps: List[Tensor]) -> List[Tensor]: - grid_sizes = [feature_map.shape[-2:] for feature_map in feature_maps] + grid_sizes = [list(feature_map.shape[-2:]) for feature_map in feature_maps] image_size = image_list.tensors.shape[-2:] dtype, device = feature_maps[0].dtype, feature_maps[0].device strides = [ @@ -248,8 +248,8 @@ def __repr__(self) -> str: return s.format(**self.__dict__) def forward(self, image_list: ImageList, feature_maps: List[Tensor]) -> List[Tensor]: - grid_sizes = [feature_map.shape[-2:] for feature_map in feature_maps] - image_size = image_list.tensors.shape[-2:] + grid_sizes = [list(feature_map.shape[-2:]) for feature_map in feature_maps] + image_size = list(image_list.tensors.shape[-2:]) dtype, device = feature_maps[0].dtype, feature_maps[0].device default_boxes = self._grid_default_boxes(grid_sizes, image_size, dtype=dtype) default_boxes = default_boxes.to(device) From 5eaa55bfeffe08c2161314f248acde56210272d0 Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 12 Oct 2021 21:51:49 +0530 Subject: [PATCH 4/5] Use the list alternative --- torchvision/models/detection/anchor_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/models/detection/anchor_utils.py b/torchvision/models/detection/anchor_utils.py index 3953e2e1014..3cd3ea0fb37 100644 --- a/torchvision/models/detection/anchor_utils.py +++ b/torchvision/models/detection/anchor_utils.py @@ -34,8 +34,8 @@ class AnchorGenerator(nn.Module): def __init__( self, - sizes: Tuple[Tuple[int, ...]] = ((128, 256, 512),), - aspect_ratios: Tuple[Tuple[float, ...]] = ((0.5, 1.0, 2.0),), + sizes: Tuple[List[int]] = ([128, 256, 512],), + aspect_ratios: Tuple[List[float]] = ([0.5, 1.0, 2.0],), ) -> None: super(AnchorGenerator, self).__init__() @@ -60,8 +60,8 @@ def __init__( # This method assumes aspect ratio = height / width for an anchor. def generate_anchors( self, - scales: Tuple[int, ...], - aspect_ratios: Tuple[float, ...], + scales: List[int], + aspect_ratios: List[float], dtype: torch.dtype = torch.float32, device: torch.device = torch.device("cpu"), ) -> Tensor: From 242c8502f316432956a0eee0d391eb5465f2d7ee Mon Sep 17 00:00:00 2001 From: Aditya Oke Date: Tue, 12 Oct 2021 22:06:38 +0530 Subject: [PATCH 5/5] Use variable tuple : --- torchvision/models/detection/anchor_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/torchvision/models/detection/anchor_utils.py b/torchvision/models/detection/anchor_utils.py index 3cd3ea0fb37..3953e2e1014 100644 --- a/torchvision/models/detection/anchor_utils.py +++ b/torchvision/models/detection/anchor_utils.py @@ -34,8 +34,8 @@ class AnchorGenerator(nn.Module): def __init__( self, - sizes: Tuple[List[int]] = ([128, 256, 512],), - aspect_ratios: Tuple[List[float]] = ([0.5, 1.0, 2.0],), + sizes: Tuple[Tuple[int, ...]] = ((128, 256, 512),), + aspect_ratios: Tuple[Tuple[float, ...]] = ((0.5, 1.0, 2.0),), ) -> None: super(AnchorGenerator, self).__init__() @@ -60,8 +60,8 @@ def __init__( # This method assumes aspect ratio = height / width for an anchor. def generate_anchors( self, - scales: List[int], - aspect_ratios: List[float], + scales: Tuple[int, ...], + aspect_ratios: Tuple[float, ...], dtype: torch.dtype = torch.float32, device: torch.device = torch.device("cpu"), ) -> Tensor: