tensorflow

2020-10-09  本文已影响0人  dario_hit

FLAGS.train_image_size = 300

        out_shape = [FLAGS.train_image_size] * 2

        anchor_creator = anchor_manipulator.AnchorCreator(out_shape,

                                                    layers_shapes = ,

                                                    anchor_scales = [(0.1,), (0.2,), (0.375,), (0.55,), (0.725,), (0.9,)],

                                                    extra_anchor_scales = ,

                                                    anchor_ratios = ,

                                                    layer_steps = )

class AnchorCreator(object):

    def __init__(self, img_shape, layers_shapes, anchor_scales, extra_anchor_scales, anchor_ratios, layer_steps):

        super(AnchorCreator, self).__init__()

        # img_shape -> (height, width)

        self._img_shape = img_shape

        self._layers_shapes = layers_shapes

        self._anchor_scales = anchor_scales

        self._extra_anchor_scales = extra_anchor_scales

        self._anchor_ratios = anchor_ratios

        self._layer_steps = layer_steps

        self._anchor_offset = [0.5] * len(self._layers_shapes)

【入参】:

# img_shape: 原始图形尺寸

self._img_shape = [300, 300]

# layers_shapes:输出层特征图尺寸大小

self._layers_shapes = [(38, 38), (19, 19), (10, 10), (5, 5), (3, 3), (1, 1)]

# anchor_scales: 各层锚框的尺度缩小因子

self._anchor_scales = [(0.1,), (0.2,), (0.375,), (0.55,), (0.725,), (0.9,)]

# extra_anchor_scales: 每层方框的额外尺度缩小因子

self._extra_anchor_scales = [(0.1414,), (0.2739,), (0.4541,), (0.6315,), (0.8078,), (0.9836,)]

# anchor_ratios: 每层锚框的宽高比

self._anchor_ratios = [(1., 2., .5), (1., 2., 3., .5, 0.3333), (1., 2., 3., .5, 0.3333), (1., 2., 3., .5, 0.3333), (1., 2., .5), (1., 2., .5)]

# layer_steps:原图与输出层特征图的尺寸比例

self._layer_steps = [8, 16, 32, 64, 100, 300]

# anchor_offset:锚框的起点偏移

self._anchor_offset = [0.5]*6

【出参】:

all_anchors

all_num_anchors_depth

all_num_anchors_spatial

出参初始化

all_anchors = []

all_num_anchors_depth = []

all_num_anchors_spatial = []

【计算过程】:

        for layer_index, layer_shape in enumerate(self._layers_shapes):

            anchors_this_layer = self.get_layer_anchors(layer_shape,

                                                        self._anchor_scales[layer_index],

                                                        self._extra_anchor_scales[layer_index],

                                                        self._anchor_ratios[layer_index],

                                                        self._layer_steps[layer_index],

                                                        self._anchor_offset[layer_index])

            all_anchors.append(anchors_this_layer[:-2])

            all_num_anchors_depth.append(anchors_this_layer[-2])

            all_num_anchors_spatial.append(anchors_this_layer[-1])

以第一层为例:

[参数列表]

layer_shape = [38,38]

layer_index = 0

anchor_scale = (0.1,)

extra_anchor_scale = (0.1414,)

anchor_ratio = (1., 2., .5)

layer_step = 8

offset = 0.5

def get_layer_anchors(self, layer_shape, anchor_scale, extra_anchor_scale, anchor_ratio, layer_step, offset = 0.5):

  with tf.name_scope('get_layer_anchors'):

# x_on_layer, y_on_layer = tf.meshgrid(tf.range(38), tf.range(38))

            x_on_layer, y_on_layer = tf.meshgrid(tf.range(layer_shape[1]), tf.range(layer_shape[0]))

            y_on_image = (tf.cast(y_on_layer, tf.float32) + offset) * layer_step / self._img_shape[0]

            x_on_image = (tf.cast(x_on_layer, tf.float32) + offset) * layer_step / self._img_shape[1]

            num_anchors_along_depth = len(anchor_scale) * len(anchor_ratio) + len(extra_anchor_scale)

            num_anchors_along_spatial = layer_shape[1] * layer_shape[0]

            list_h_on_image = []

            list_w_on_image = []

            global_index = 0

            # for square anchors

            for _, scale in enumerate(extra_anchor_scale):

                list_h_on_image.append(scale)

                list_w_on_image.append(scale)

                global_index += 1

            # for other aspect ratio anchors

            for scale_index, scale in enumerate(anchor_scale):

                for ratio_index, ratio in enumerate(anchor_ratio):

                    list_h_on_image.append(scale / math.sqrt(ratio))

                    list_w_on_image.append(scale * math.sqrt(ratio))

                    global_index += 1

            # shape info:

            # y_on_image, x_on_image: layers_shapes[0] * layers_shapes[1]

            # h_on_image, w_on_image: num_anchors_along_depth

            return tf.expand_dims(y_on_image, axis=-1), tf.expand_dims(x_on_image, axis=-1), \

                    tf.constant(list_h_on_image, dtype=tf.float32), \

                    tf.constant(list_w_on_image, dtype=tf.float32), num_anchors_along_depth, num_anchors_along_spatial

-------------------------------------------------------------------

将入参代入后:

  with tf.name_scope('get_layer_anchors'):

x_on_layer, y_on_layer = tf.meshgrid(tf.range(38), tf.range(38))

# x_on_layer = [

#  [0,1,2,...,37],

#  [0,1,2,...,37],

#  ...

#  [0,1,2,...,37],

# ]

# x_on_layer 共38行

#

# y_on_layer = [

#  [0,0,0,...,0],

#  [1,1,1,...,1],

#  ...

#  [37,37,37,...,37],

# ]

# x_on_layer 共38列

#

y_on_image = (tf.cast(y_on_layer, tf.float32) + offset) * layer_step / self._img_shape[0]

            x_on_image = (tf.cast(x_on_layer, tf.float32) + offset) * layer_step / self._img_shape[1]

# tf.cast(y_on_layer, tf.float32) 该步骤是将坐标转为float类型,方便计算

# (tf.cast(y_on_layer, tf.float32) + offset) * layer_step 该步骤是计算映射回原图后的坐标

# / self._img_shape[0]  该步骤是将坐标归一化

num_anchors_along_depth = len(anchor_scale) * len(anchor_ratio) + len(extra_anchor_scale)

            num_anchors_along_spatial = layer_shape[1] * layer_shape[0]

# num_anchors_along_depth = 4

# num_anchors_along_spatial = 38*38 = 1524

list_h_on_image = []

            list_w_on_image = []

            global_index = 0

            # for square anchors

# extra_anchor_scale = (0.1414,)

            for _, scale in enumerate(extra_anchor_scale):

                list_h_on_image.append(scale)

                list_w_on_image.append(scale)

                global_index += 1

# list_h_on_image = [0.1414]

# list_w_on_image = [0.1414]

# global_index = 1

            # for other aspect ratio anchors

# anchor_scale = (0.1,)

# anchor_ratio = (1., 2., .5)

            for scale_index, scale in enumerate(anchor_scale):

                for ratio_index, ratio in enumerate(anchor_ratio):

                    list_h_on_image.append(scale / math.sqrt(ratio)) # 0.1/sqrt(1)

                    list_w_on_image.append(scale * math.sqrt(ratio))

                    global_index += 1

# list_h_on_image = [0.1414, 0.1, 0.0707, x, x]

# list_w_on_image = [0.1414, y, y, y, y]

# global_index = 4

            # shape info:

            # y_on_image, x_on_image: layers_shapes[0] * layers_shapes[1]

            # h_on_image, w_on_image: num_anchors_along_depth

            return tf.expand_dims(y_on_image, axis=-1), tf.expand_dims(x_on_image, axis=-1), \

                    tf.constant(list_h_on_image, dtype=tf.float32), \

                    tf.constant(list_w_on_image, dtype=tf.float32), num_anchors_along_depth, num_anchors_along_spatial

# x_on_layer = [

#  [0,1,2,...,37],

#  [0,1,2,...,37],

#  ...

#  [0,1,2,...,37],

# ]

# x_on_layer 共38行

#

# y_on_layer = [

#  [0,0,0,...,0],

#  [1,1,1,...,1],

#  ...

#  [37,37,37,...,37],

# ]

x_on_layer.shape = (38,38)

y_on_layer.shape = (38,38)

tf.expand_dims(y_on_image, axis=-1).shape = (38,38,1)

tf.expand_dims(x_on_image, axis=-1).shape = (38,38,1)

list_h_on_image = [0.1414, 0.1, 0.0707, x, x]

list_w_on_image = [0.1414, y, y, y, y]

num_anchors_along_depth = 4

num_anchors_along_spatial = 38*38 = 1444

anchors_this_layer = tf.expand_dims(y_on_image, axis=-1),

tf.expand_dims(x_on_image, axis=-1),

list_h_on_image,

list_w_on_image,

num_anchors_along_depth,

num_anchors_along_spatial

-------------------------------------------------------------------

all_anchors.append(anchors_this_layer[:-2]) = [six elements]

elment = (expand_dim_y_on_image(38,38,1),

  expand_dim_x_on_image(38,38,1),

  list_h_on_image(4,),

  list_h_on_image(4,))

all_num_anchors_depth.append(anchors_this_layer[-2]) = [six elements]

elment = 4

all_num_anchors_spatial.append(anchors_this_layer[-1]) = [six elements]

elment = feature map size.

第二段

【参数】:

all_anchors.append(anchors_this_layer[:-2]) = [six elements]

elment = (expand_dim_y_on_image(38,38,1),

  expand_dim_x_on_image(38,38,1),

  list_h_on_image(4,),

  list_h_on_image(4,))

all_num_anchors_depth.append(anchors_this_layer[-2]) = [six elements]

elment = 4

all_num_anchors_spatial.append(anchors_this_layer[-1]) = [six elements]

elment = feature map size

【代码段】:

      all_anchors, all_num_anchors_depth, all_num_anchors_spatial = anchor_creator.get_all_anchors()

        num_anchors_per_layer = []

        for ind in range(len(all_anchors)):

            num_anchors_per_layer.append(all_num_anchors_depth[ind] * all_num_anchors_spatial[ind])

        anchor_encoder_decoder = anchor_manipulator.AnchorEncoder(allowed_borders = [1.0] * 6,

                                                            positive_threshold = FLAGS.match_threshold,

                                                            ignore_threshold = FLAGS.neg_threshold,

                                                            prior_scaling=[0.1, 0.1, 0.2, 0.2])

【过程参数】:

num_anchors_per_layer = [38*38*4, ] .shape = 6

上一篇下一篇

猜你喜欢

热点阅读