Skip to content

Commit f318332

Browse files
committed
Change scales formula for Default Boxes.
1 parent d17eb6c commit f318332

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed
Binary file not shown.

torchvision/models/detection/anchor_utils.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,19 @@ class DefaultBoxGenerator(nn.Module):
138138
Args:
139139
aspect_ratios (List[List[int]]): A list with all the aspect ratios used in each feature map.
140140
min_ratio (float): The minimum scale :math:`\text{s}_{\text{min}}` of the default boxes used in the estimation
141-
of the scales of each feature map.
141+
of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.
142142
max_ratio (float): The maximum scale :math:`\text{s}_{\text{max}}` of the default boxes used in the estimation
143-
of the scales of each feature map.
143+
of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.
144+
scales (List[float]], optional): The scales of the default boxes. If not provided it will be estimated using
145+
the ``min_ratio`` and ``max_ratio`` parameters.
144146
steps (List[int]], optional): It's a hyper-parameter that affects the tiling of defalt boxes. If not provided
145147
it will be estimated from the data.
146148
clip (bool): Whether the standardized values of default boxes should be clipped between 0 and 1. The clipping
147149
is applied while the boxes are encoded in format ``(cx, cy, w, h)``.
148150
"""
149151

150152
def __init__(self, aspect_ratios: List[List[int]], min_ratio: float = 0.15, max_ratio: float = 0.9,
151-
steps: Optional[List[int]] = None, clip: bool = True):
153+
scales: Optional[List[float]] = None, steps: Optional[List[int]] = None, clip: bool = True):
152154
super().__init__()
153155
if steps is not None:
154156
assert len(aspect_ratios) == len(steps)
@@ -158,15 +160,12 @@ def __init__(self, aspect_ratios: List[List[int]], min_ratio: float = 0.15, max_
158160
num_outputs = len(aspect_ratios)
159161

160162
# Estimation of default boxes scales
161-
# Inspired from https://github.com/weiliu89/caffe/blob/ssd/examples/ssd/ssd_pascal.py#L311-L317
162-
min_centile = int(100 * min_ratio)
163-
max_centile = int(100 * max_ratio)
164-
conv4_centile = min_centile // 2 # assume half of min_ratio as in paper
165-
step = (max_centile - min_centile) // (num_outputs - 2)
166-
centiles = [conv4_centile, min_centile]
167-
for c in range(min_centile, max_centile + 1, step):
168-
centiles.append(c + step)
169-
self.scales = [c / 100 for c in centiles]
163+
if scales is None:
164+
range_ratio = max_ratio - min_ratio
165+
self.scales = [min_ratio + range_ratio * k / (num_outputs - 1.0) for k in range(num_outputs)]
166+
self.scales.append(1.0)
167+
else:
168+
self.scales = scales
170169

171170
self._wh_pairs = []
172171
for k in range(num_outputs):

torchvision/models/detection/ssd.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,9 @@ def ssd300_vgg16(pretrained: bool = False, progress: bool = True, num_classes: i
552552
pretrained_backbone = False
553553

554554
backbone = _vgg_extractor("vgg16_features", False, progress, pretrained_backbone, trainable_backbone_layers, True)
555-
anchor_generator = DefaultBoxGenerator([[2], [2, 3], [2, 3], [2, 3], [2], [2]], steps=[8, 16, 32, 64, 100, 300])
555+
anchor_generator = DefaultBoxGenerator([[2], [2, 3], [2, 3], [2, 3], [2], [2]],
556+
scales=[0.07, 0.15, 0.33, 0.51, 0.69, 0.87, 1.05],
557+
steps=[8, 16, 32, 64, 100, 300])
556558
model = SSD(backbone, anchor_generator, (300, 300), num_classes,
557559
image_mean=[0.48235, 0.45882, 0.40784], image_std=[1., 1., 1.], **kwargs)
558560
if pretrained:

0 commit comments

Comments
 (0)