@@ -138,17 +138,19 @@ class DefaultBoxGenerator(nn.Module):
138
138
Args:
139
139
aspect_ratios (List[List[int]]): A list with all the aspect ratios used in each feature map.
140
140
min_ratio (float): The minimum scale :math:`\t ext{s}_{\t ext{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.
142
142
max_ratio (float): The maximum scale :math:`\t ext{s}_{\t ext{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.
144
146
steps (List[int]], optional): It's a hyper-parameter that affects the tiling of defalt boxes. If not provided
145
147
it will be estimated from the data.
146
148
clip (bool): Whether the standardized values of default boxes should be clipped between 0 and 1. The clipping
147
149
is applied while the boxes are encoded in format ``(cx, cy, w, h)``.
148
150
"""
149
151
150
152
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 ):
152
154
super ().__init__ ()
153
155
if steps is not None :
154
156
assert len (aspect_ratios ) == len (steps )
@@ -158,15 +160,15 @@ def __init__(self, aspect_ratios: List[List[int]], min_ratio: float = 0.15, max_
158
160
num_outputs = len (aspect_ratios )
159
161
160
162
# 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
+ if num_outputs > 1 :
165
+ range_ratio = max_ratio - min_ratio
166
+ self . scales = [ min_ratio + range_ratio * k / ( num_outputs - 1.0 ) for k in range ( num_outputs )]
167
+ self . scales . append ( 1.0 )
168
+ else :
169
+ self . scales = [ min_ratio , max_ratio ]
170
+ else :
171
+ self .scales = scales
170
172
171
173
self ._wh_pairs = []
172
174
for k in range (num_outputs ):
@@ -207,9 +209,17 @@ def forward(self, image_list: ImageList, feature_maps: List[Tensor]) -> List[Ten
207
209
for k , f_k in enumerate (grid_sizes ):
208
210
# Now add the default boxes for each width-height pair
209
211
for j in range (f_k [0 ]):
210
- cy = (j + 0.5 ) / (float (f_k [0 ]) if self .steps is None else image_size [1 ] / self .steps [k ])
212
+ if self .steps is not None :
213
+ y_f_k = image_size [1 ] / self .steps [k ]
214
+ else :
215
+ y_f_k = float (f_k [0 ])
216
+ cy = (j + 0.5 ) / y_f_k
211
217
for i in range (f_k [1 ]):
212
- cx = (i + 0.5 ) / (float (f_k [1 ]) if self .steps is None else image_size [0 ] / self .steps [k ])
218
+ if self .steps is not None :
219
+ x_f_k = image_size [0 ] / self .steps [k ]
220
+ else :
221
+ x_f_k = float (f_k [1 ])
222
+ cx = (i + 0.5 ) / x_f_k
213
223
default_boxes .extend ([[cx , cy , w , h ] for w , h in self ._wh_pairs [k ]])
214
224
215
225
dboxes = []
0 commit comments