"...controlnet_pytorch.git" did not exist on "e2696ece051bc73e8c85fdf4d941704ee38817fd"
Commit 55565793 authored by Vivek Rathod's avatar Vivek Rathod
Browse files

Allow ssd anchor generator to specify scales and

add interpolated scale at a give aspect ratio. This change breaks
the existing checkpoints because the anchor orders are different.

This change should be merged along newly generated frozen ssd models.
parent 205c5e07
...@@ -38,6 +38,8 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -38,6 +38,8 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
def __init__(self, def __init__(self,
box_specs_list, box_specs_list,
base_anchor_size=None, base_anchor_size=None,
anchor_strides=None,
anchor_offsets=None,
clip_window=None): clip_window=None):
"""Constructs a MultipleGridAnchorGenerator. """Constructs a MultipleGridAnchorGenerator.
...@@ -58,7 +60,26 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -58,7 +60,26 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
outside list having the same number of entries as feature_map_shape_list outside list having the same number of entries as feature_map_shape_list
(which is passed in at generation time). (which is passed in at generation time).
base_anchor_size: base anchor size as [height, width] base_anchor_size: base anchor size as [height, width]
(length-2 float tensor, default=[256, 256]). (length-2 float tensor, default=[1.0, 1.0]).
The height and width values are normalized to the
minimum dimension of the input height and width, so that
when the base anchor height equals the base anchor
width, the resulting anchor is square even if the input
image is not square.
anchor_strides: list of pairs of strides in pixels (in y and x directions
respectively). For example, setting anchor_strides=[(25, 25), (50, 50)]
means that we want the anchors corresponding to the first layer to be
strided by 25 pixels and those in the second layer to be strided by 50
pixels in both y and x directions. If anchor_strides=None, they are set
to be the reciprocal of the corresponding feature map shapes.
anchor_offsets: list of pairs of offsets in pixels (in y and x directions
respectively). The offset specifies where we want the center of the
(0, 0)-th anchor to lie for each layer. For example, setting
anchor_offsets=[(10, 10), (20, 20)]) means that we want the
(0, 0)-th anchor of the first layer to lie at (10, 10) in pixel space
and likewise that we want the (0, 0)-th anchor of the second layer to
lie at (25, 25) in pixel space. If anchor_offsets=None, then they are
set to be half of the corresponding anchor stride.
clip_window: a tensor of shape [4] specifying a window to which all clip_window: a tensor of shape [4] specifying a window to which all
anchors should be clipped. If clip_window is None, then no clipping anchors should be clipped. If clip_window is None, then no clipping
is performed. is performed.
...@@ -76,6 +97,8 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -76,6 +97,8 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
if base_anchor_size is None: if base_anchor_size is None:
base_anchor_size = tf.constant([256, 256], dtype=tf.float32) base_anchor_size = tf.constant([256, 256], dtype=tf.float32)
self._base_anchor_size = base_anchor_size self._base_anchor_size = base_anchor_size
self._anchor_strides = anchor_strides
self._anchor_offsets = anchor_offsets
if clip_window is not None and clip_window.get_shape().as_list() != [4]: if clip_window is not None and clip_window.get_shape().as_list() != [4]:
raise ValueError('clip_window must either be None or a shape [4] tensor') raise ValueError('clip_window must either be None or a shape [4] tensor')
self._clip_window = clip_window self._clip_window = clip_window
...@@ -90,6 +113,18 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -90,6 +113,18 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
self._scales.append(scales) self._scales.append(scales)
self._aspect_ratios.append(aspect_ratios) self._aspect_ratios.append(aspect_ratios)
for arg, arg_name in zip([self._anchor_strides, self._anchor_offsets],
['anchor_strides', 'anchor_offsets']):
if arg and not (isinstance(arg, list) and
len(arg) == len(self._box_specs)):
raise ValueError('%s must be a list with the same length '
'as self._box_specs' % arg_name)
if arg and not all([
isinstance(list_item, tuple) and len(list_item) == 2
for list_item in arg
]):
raise ValueError('%s must be a list of pairs.' % arg_name)
def name_scope(self): def name_scope(self):
return 'MultipleGridAnchorGenerator' return 'MultipleGridAnchorGenerator'
...@@ -102,12 +137,7 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -102,12 +137,7 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
""" """
return [len(box_specs) for box_specs in self._box_specs] return [len(box_specs) for box_specs in self._box_specs]
def _generate(self, def _generate(self, feature_map_shape_list, im_height=1, im_width=1):
feature_map_shape_list,
im_height=1,
im_width=1,
anchor_strides=None,
anchor_offsets=None):
"""Generates a collection of bounding boxes to be used as anchors. """Generates a collection of bounding boxes to be used as anchors.
The number of anchors generated for a single grid with shape MxM where we The number of anchors generated for a single grid with shape MxM where we
...@@ -133,25 +163,6 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -133,25 +163,6 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
im_height and im_width are 1, the generated anchors default to im_height and im_width are 1, the generated anchors default to
normalized coordinates, otherwise absolute coordinates are used for the normalized coordinates, otherwise absolute coordinates are used for the
grid. grid.
anchor_strides: list of pairs of strides (in y and x directions
respectively). For example, setting
anchor_strides=[(.25, .25), (.5, .5)] means that we want the anchors
corresponding to the first layer to be strided by .25 and those in the
second layer to be strided by .5 in both y and x directions. By
default, if anchor_strides=None, then they are set to be the reciprocal
of the corresponding grid sizes. The pairs can also be specified as
dynamic tf.int or tf.float numbers, e.g. for variable shape input
images.
anchor_offsets: list of pairs of offsets (in y and x directions
respectively). The offset specifies where we want the center of the
(0, 0)-th anchor to lie for each layer. For example, setting
anchor_offsets=[(.125, .125), (.25, .25)]) means that we want the
(0, 0)-th anchor of the first layer to lie at (.125, .125) in image
space and likewise that we want the (0, 0)-th anchor of the second
layer to lie at (.25, .25) in image space. By default, if
anchor_offsets=None, then they are set to be half of the corresponding
anchor stride. The pairs can also be specified as dynamic tf.int or
tf.float numbers, e.g. for variable shape input images.
Returns: Returns:
boxes: a BoxList holding a collection of N anchor boxes boxes: a BoxList holding a collection of N anchor boxes
...@@ -168,13 +179,25 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -168,13 +179,25 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
if not all([isinstance(list_item, tuple) and len(list_item) == 2 if not all([isinstance(list_item, tuple) and len(list_item) == 2
for list_item in feature_map_shape_list]): for list_item in feature_map_shape_list]):
raise ValueError('feature_map_shape_list must be a list of pairs.') raise ValueError('feature_map_shape_list must be a list of pairs.')
if not anchor_strides:
anchor_strides = [(tf.to_float(im_height) / tf.to_float(pair[0]), im_height = tf.to_float(im_height)
tf.to_float(im_width) / tf.to_float(pair[1])) im_width = tf.to_float(im_width)
if not self._anchor_strides:
anchor_strides = [(1.0 / tf.to_float(pair[0]), 1.0 / tf.to_float(pair[1]))
for pair in feature_map_shape_list] for pair in feature_map_shape_list]
if not anchor_offsets: else:
anchor_strides = [(tf.to_float(stride[0]) / im_height,
tf.to_float(stride[1]) / im_width)
for stride in self._anchor_strides]
if not self._anchor_offsets:
anchor_offsets = [(0.5 * stride[0], 0.5 * stride[1]) anchor_offsets = [(0.5 * stride[0], 0.5 * stride[1])
for stride in anchor_strides] for stride in anchor_strides]
else:
anchor_offsets = [(tf.to_float(offset[0]) / im_height,
tf.to_float(offset[1]) / im_width)
for offset in self._anchor_offsets]
for arg, arg_name in zip([anchor_strides, anchor_offsets], for arg, arg_name in zip([anchor_strides, anchor_offsets],
['anchor_strides', 'anchor_offsets']): ['anchor_strides', 'anchor_offsets']):
if not (isinstance(arg, list) and len(arg) == len(self._box_specs)): if not (isinstance(arg, list) and len(arg) == len(self._box_specs)):
...@@ -185,8 +208,13 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -185,8 +208,13 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
raise ValueError('%s must be a list of pairs.' % arg_name) raise ValueError('%s must be a list of pairs.' % arg_name)
anchor_grid_list = [] anchor_grid_list = []
min_im_shape = tf.to_float(tf.minimum(im_height, im_width)) min_im_shape = tf.minimum(im_height, im_width)
base_anchor_size = min_im_shape * self._base_anchor_size scale_height = min_im_shape / im_height
scale_width = min_im_shape / im_width
base_anchor_size = [
scale_height * self._base_anchor_size[0],
scale_width * self._base_anchor_size[1]
]
for grid_size, scales, aspect_ratios, stride, offset in zip( for grid_size, scales, aspect_ratios, stride, offset in zip(
feature_map_shape_list, self._scales, self._aspect_ratios, feature_map_shape_list, self._scales, self._aspect_ratios,
anchor_strides, anchor_offsets): anchor_strides, anchor_offsets):
...@@ -204,12 +232,9 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -204,12 +232,9 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
if num_anchors is None: if num_anchors is None:
num_anchors = concatenated_anchors.num_boxes() num_anchors = concatenated_anchors.num_boxes()
if self._clip_window is not None: if self._clip_window is not None:
clip_window = tf.multiply(
tf.to_float([im_height, im_width, im_height, im_width]),
self._clip_window)
concatenated_anchors = box_list_ops.clip_to_window( concatenated_anchors = box_list_ops.clip_to_window(
concatenated_anchors, clip_window, filter_nonoverlapping=False) concatenated_anchors, self._clip_window, filter_nonoverlapping=False)
# TODO: make reshape an option for the clip_to_window op # TODO(jonathanhuang): make reshape an option for the clip_to_window op
concatenated_anchors.set( concatenated_anchors.set(
tf.reshape(concatenated_anchors.get(), [num_anchors, 4])) tf.reshape(concatenated_anchors.get(), [num_anchors, 4]))
...@@ -223,8 +248,12 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): ...@@ -223,8 +248,12 @@ class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator):
def create_ssd_anchors(num_layers=6, def create_ssd_anchors(num_layers=6,
min_scale=0.2, min_scale=0.2,
max_scale=0.95, max_scale=0.95,
aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), scales=None,
aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3),
interpolated_scale_aspect_ratio=1.0,
base_anchor_size=None, base_anchor_size=None,
anchor_strides=None,
anchor_offsets=None,
reduce_boxes_in_lowest_layer=True): reduce_boxes_in_lowest_layer=True):
"""Creates MultipleGridAnchorGenerator for SSD anchors. """Creates MultipleGridAnchorGenerator for SSD anchors.
...@@ -244,9 +273,33 @@ def create_ssd_anchors(num_layers=6, ...@@ -244,9 +273,33 @@ def create_ssd_anchors(num_layers=6,
grid sizes passed in at generation time) grid sizes passed in at generation time)
min_scale: scale of anchors corresponding to finest resolution (float) min_scale: scale of anchors corresponding to finest resolution (float)
max_scale: scale of anchors corresponding to coarsest resolution (float) max_scale: scale of anchors corresponding to coarsest resolution (float)
scales: As list of anchor scales to use. When not None and not emtpy,
min_scale and max_scale are not used.
aspect_ratios: list or tuple of (float) aspect ratios to place on each aspect_ratios: list or tuple of (float) aspect ratios to place on each
grid point. grid point.
interpolated_scale_aspect_ratio: An additional anchor is added with this
aspect ratio and a scale interpolated between the scale for a layer
and the scale for the next layer (1.0 for the last layer).
This anchor is not included if this value is 0.
base_anchor_size: base anchor size as [height, width]. base_anchor_size: base anchor size as [height, width].
The height and width values are normalized to the minimum dimension of the
input height and width, so that when the base anchor height equals the
base anchor width, the resulting anchor is square even if the input image
is not square.
anchor_strides: list of pairs of strides in pixels (in y and x directions
respectively). For example, setting anchor_strides=[(25, 25), (50, 50)]
means that we want the anchors corresponding to the first layer to be
strided by 25 pixels and those in the second layer to be strided by 50
pixels in both y and x directions. If anchor_strides=None, they are set to
be the reciprocal of the corresponding feature map shapes.
anchor_offsets: list of pairs of offsets in pixels (in y and x directions
respectively). The offset specifies where we want the center of the
(0, 0)-th anchor to lie for each layer. For example, setting
anchor_offsets=[(10, 10), (20, 20)]) means that we want the
(0, 0)-th anchor of the first layer to lie at (10, 10) in pixel space
and likewise that we want the (0, 0)-th anchor of the second layer to lie
at (25, 25) in pixel space. If anchor_offsets=None, then they are set to
be half of the corresponding anchor stride.
reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3
boxes per location is used in the lowest layer. boxes per location is used in the lowest layer.
...@@ -257,8 +310,14 @@ def create_ssd_anchors(num_layers=6, ...@@ -257,8 +310,14 @@ def create_ssd_anchors(num_layers=6,
base_anchor_size = [1.0, 1.0] base_anchor_size = [1.0, 1.0]
base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32) base_anchor_size = tf.constant(base_anchor_size, dtype=tf.float32)
box_specs_list = [] box_specs_list = []
scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) if scales is None or not scales:
for i in range(num_layers)] + [1.0] scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1)
for i in range(num_layers)] + [1.0]
else:
# Add 1.0 to the end, which will only be used in scale_next below and used
# for computing an interpolated scale for the largest scale in the list.
scales += [1.0]
for layer, scale, scale_next in zip( for layer, scale, scale_next in zip(
range(num_layers), scales[:-1], scales[1:]): range(num_layers), scales[:-1], scales[1:]):
layer_box_specs = [] layer_box_specs = []
...@@ -267,7 +326,13 @@ def create_ssd_anchors(num_layers=6, ...@@ -267,7 +326,13 @@ def create_ssd_anchors(num_layers=6,
else: else:
for aspect_ratio in aspect_ratios: for aspect_ratio in aspect_ratios:
layer_box_specs.append((scale, aspect_ratio)) layer_box_specs.append((scale, aspect_ratio))
if aspect_ratio == 1.0: # Add one more anchor, with a scale between the current scale, and the
layer_box_specs.append((np.sqrt(scale*scale_next), 1.0)) # scale for the next layer, with a specified aspect ratio (1.0 by
# default).
if interpolated_scale_aspect_ratio > 0.0:
layer_box_specs.append((np.sqrt(scale*scale_next),
interpolated_scale_aspect_ratio))
box_specs_list.append(layer_box_specs) box_specs_list.append(layer_box_specs)
return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size)
return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size,
anchor_strides, anchor_offsets)
...@@ -32,22 +32,21 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -32,22 +32,21 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
[-25, -131, 39, 125], [-57, -259, 71, 253], [-25, -131, 39, 125], [-57, -259, 71, 253],
[-121, -515, 135, 509]] [-121, -515, 135, 509]]
base_anchor_size = tf.constant([256, 256], dtype=tf.float32)
box_specs_list = [[(.5, .25), (1.0, .25), (2.0, .25), box_specs_list = [[(.5, .25), (1.0, .25), (2.0, .25),
(.5, 1.0), (1.0, 1.0), (2.0, 1.0), (.5, 1.0), (1.0, 1.0), (2.0, 1.0),
(.5, 4.0), (1.0, 4.0), (2.0, 4.0)]] (.5, 4.0), (1.0, 4.0), (2.0, 4.0)]]
anchor_generator = ag.MultipleGridAnchorGenerator( anchor_generator = ag.MultipleGridAnchorGenerator(
box_specs_list, base_anchor_size) box_specs_list,
anchors = anchor_generator.generate(feature_map_shape_list=[(1, 1)], base_anchor_size=tf.constant([256, 256], dtype=tf.float32),
anchor_strides=[(16, 16)], anchor_strides=[(16, 16)],
anchor_offsets=[(7, -3)]) anchor_offsets=[(7, -3)])
anchors = anchor_generator.generate(feature_map_shape_list=[(1, 1)])
anchor_corners = anchors.get() anchor_corners = anchors.get()
with self.test_session(): with self.test_session():
anchor_corners_out = anchor_corners.eval() anchor_corners_out = anchor_corners.eval()
self.assertAllClose(anchor_corners_out, exp_anchor_corners) self.assertAllClose(anchor_corners_out, exp_anchor_corners)
def test_construct_anchor_grid(self): def test_construct_anchor_grid(self):
base_anchor_size = tf.constant([10, 10], dtype=tf.float32)
box_specs_list = [[(0.5, 1.0), (1.0, 1.0), (2.0, 1.0)]] box_specs_list = [[(0.5, 1.0), (1.0, 1.0), (2.0, 1.0)]]
exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.], exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.],
...@@ -58,10 +57,11 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -58,10 +57,11 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
[14., 14., 24, 24], [9., 9., 29, 29]] [14., 14., 24, 24], [9., 9., 29, 29]]
anchor_generator = ag.MultipleGridAnchorGenerator( anchor_generator = ag.MultipleGridAnchorGenerator(
box_specs_list, base_anchor_size) box_specs_list,
anchors = anchor_generator.generate(feature_map_shape_list=[(2, 2)], base_anchor_size=tf.constant([10, 10], dtype=tf.float32),
anchor_strides=[(19, 19)], anchor_strides=[(19, 19)],
anchor_offsets=[(0, 0)]) anchor_offsets=[(0, 0)])
anchors = anchor_generator.generate(feature_map_shape_list=[(2, 2)])
anchor_corners = anchors.get() anchor_corners = anchors.get()
with self.test_session(): with self.test_session():
...@@ -69,13 +69,12 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -69,13 +69,12 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
self.assertAllClose(anchor_corners_out, exp_anchor_corners) self.assertAllClose(anchor_corners_out, exp_anchor_corners)
def test_construct_anchor_grid_non_square(self): def test_construct_anchor_grid_non_square(self):
base_anchor_size = tf.constant([1, 1], dtype=tf.float32)
box_specs_list = [[(1.0, 1.0)]] box_specs_list = [[(1.0, 1.0)]]
exp_anchor_corners = [[0., -0.25, 1., 0.75], [0., 0.25, 1., 1.25]] exp_anchor_corners = [[0., -0.25, 1., 0.75], [0., 0.25, 1., 1.25]]
anchor_generator = ag.MultipleGridAnchorGenerator(box_specs_list, anchor_generator = ag.MultipleGridAnchorGenerator(
base_anchor_size) box_specs_list, base_anchor_size=tf.constant([1, 1], dtype=tf.float32))
anchors = anchor_generator.generate(feature_map_shape_list=[(tf.constant( anchors = anchor_generator.generate(feature_map_shape_list=[(tf.constant(
1, dtype=tf.int32), tf.constant(2, dtype=tf.int32))]) 1, dtype=tf.int32), tf.constant(2, dtype=tf.int32))])
anchor_corners = anchors.get() anchor_corners = anchors.get()
...@@ -84,14 +83,13 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -84,14 +83,13 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
anchor_corners_out = anchor_corners.eval() anchor_corners_out = anchor_corners.eval()
self.assertAllClose(anchor_corners_out, exp_anchor_corners) self.assertAllClose(anchor_corners_out, exp_anchor_corners)
def test_construct_anchor_grid_unnormalized(self): def test_construct_anchor_grid_normalized(self):
base_anchor_size = tf.constant([1, 1], dtype=tf.float32)
box_specs_list = [[(1.0, 1.0)]] box_specs_list = [[(1.0, 1.0)]]
exp_anchor_corners = [[0., 0., 320., 320.], [0., 320., 320., 640.]] exp_anchor_corners = [[0., 0., 1., 0.5], [0., 0.5, 1., 1.]]
anchor_generator = ag.MultipleGridAnchorGenerator(box_specs_list, anchor_generator = ag.MultipleGridAnchorGenerator(
base_anchor_size) box_specs_list, base_anchor_size=tf.constant([1, 1], dtype=tf.float32))
anchors = anchor_generator.generate( anchors = anchor_generator.generate(
feature_map_shape_list=[(tf.constant(1, dtype=tf.int32), tf.constant( feature_map_shape_list=[(tf.constant(1, dtype=tf.int32), tf.constant(
2, dtype=tf.int32))], 2, dtype=tf.int32))],
...@@ -104,7 +102,6 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -104,7 +102,6 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
self.assertAllClose(anchor_corners_out, exp_anchor_corners) self.assertAllClose(anchor_corners_out, exp_anchor_corners)
def test_construct_multiple_grids(self): def test_construct_multiple_grids(self):
base_anchor_size = tf.constant([1.0, 1.0], dtype=tf.float32)
box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)],
[(1.0, 1.0), (1.0, 0.5)]] [(1.0, 1.0), (1.0, 0.5)]]
...@@ -125,11 +122,11 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -125,11 +122,11 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
[.125-.5*h, .125-.5*w, .125+.5*h, .125+.5*w],] [.125-.5*h, .125-.5*w, .125+.5*h, .125+.5*w],]
anchor_generator = ag.MultipleGridAnchorGenerator( anchor_generator = ag.MultipleGridAnchorGenerator(
box_specs_list, base_anchor_size) box_specs_list,
anchors = anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)], base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.25, .25), (.5, .5)], anchor_strides=[(.25, .25), (.5, .5)],
anchor_offsets=[(.125, .125), anchor_offsets=[(.125, .125), (.25, .25)])
(.25, .25)]) anchors = anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)])
anchor_corners = anchors.get() anchor_corners = anchors.get()
with self.test_session(): with self.test_session():
...@@ -141,7 +138,6 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -141,7 +138,6 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
self.assertAllClose(big_grid_corners, exp_big_grid_corners) self.assertAllClose(big_grid_corners, exp_big_grid_corners)
def test_construct_multiple_grids_with_clipping(self): def test_construct_multiple_grids_with_clipping(self):
base_anchor_size = tf.constant([1.0, 1.0], dtype=tf.float32)
box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)],
[(1.0, 1.0), (1.0, 0.5)]] [(1.0, 1.0), (1.0, 0.5)]]
...@@ -159,7 +155,9 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -159,7 +155,9 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
clip_window = tf.constant([0, 0, 1, 1], dtype=tf.float32) clip_window = tf.constant([0, 0, 1, 1], dtype=tf.float32)
anchor_generator = ag.MultipleGridAnchorGenerator( anchor_generator = ag.MultipleGridAnchorGenerator(
box_specs_list, base_anchor_size, clip_window=clip_window) box_specs_list,
base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
clip_window=clip_window)
anchors = anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)]) anchors = anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)])
anchor_corners = anchors.get() anchor_corners = anchors.get()
...@@ -181,48 +179,64 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase): ...@@ -181,48 +179,64 @@ class MultipleGridAnchorGeneratorTest(tf.test.TestCase):
ag.MultipleGridAnchorGenerator(box_specs_list) ag.MultipleGridAnchorGenerator(box_specs_list)
def test_invalid_generate_arguments(self): def test_invalid_generate_arguments(self):
base_anchor_size = tf.constant([1.0, 1.0], dtype=tf.float32)
box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)],
[(1.0, 1.0), (1.0, 0.5)]] [(1.0, 1.0), (1.0, 0.5)]]
anchor_generator = ag.MultipleGridAnchorGenerator(
box_specs_list, base_anchor_size)
# incompatible lengths with box_specs_list # incompatible lengths with box_specs_list
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.25, .25)], box_specs_list,
anchor_offsets=[(.125, .125), (.25, .25)]) base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.25, .25)],
anchor_offsets=[(.125, .125), (.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)])
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2), (1, 1)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.25, .25), (.5, .5)], box_specs_list,
anchor_offsets=[(.125, .125), (.25, .25)]) base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.25, .25), (.5, .5)],
anchor_offsets=[(.125, .125), (.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2), (1, 1)])
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.5, .5)], box_specs_list,
anchor_offsets=[(.25, .25)]) base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.5, .5)],
anchor_offsets=[(.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)])
# not pairs # not pairs
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4, 4, 4), (2, 2)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.25, .25), (.5, .5)], box_specs_list,
anchor_offsets=[(.125, .125), (.25, .25)]) base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.25, .25), (.5, .5)],
anchor_offsets=[(.125, .125), (.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4, 4, 4), (2, 2)])
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.25, .25, .1), (.5, .5)], box_specs_list,
anchor_offsets=[(.125, .125), base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
(.25, .25)]) anchor_strides=[(.25, .25, .1), (.5, .5)],
anchor_offsets=[(.125, .125), (.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)])
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
anchor_generator.generate(feature_map_shape_list=[(4), (2, 2)], anchor_generator = ag.MultipleGridAnchorGenerator(
anchor_strides=[(.25, .25), (.5, .5)], box_specs_list,
anchor_offsets=[(.125), (.25)]) base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32),
anchor_strides=[(.25, .25), (.5, .5)],
anchor_offsets=[(.125, .125), (.25, .25)])
anchor_generator.generate(feature_map_shape_list=[(4), (2, 2)])
class CreateSSDAnchorsTest(tf.test.TestCase): class CreateSSDAnchorsTest(tf.test.TestCase):
def test_create_ssd_anchors_returns_correct_shape(self): def test_create_ssd_anchors_returns_correct_shape(self):
anchor_generator = ag.create_ssd_anchors( anchor_generator = ag.create_ssd_anchors(
num_layers=6, min_scale=0.2, max_scale=0.95, num_layers=6,
aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), min_scale=0.2,
max_scale=0.95,
aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3),
reduce_boxes_in_lowest_layer=True) reduce_boxes_in_lowest_layer=True)
feature_map_shape_list = [(38, 38), (19, 19), (10, 10), feature_map_shape_list = [(38, 38), (19, 19), (10, 10),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment