Example #1
0
def generate_anchors_pre(height,
                         width,
                         feat_stride,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2),
                         frame_scale=1.0):
    """ A wrapper function to generate anchors given different scales
    Also return the number of anchors in variable 'length'
  """
    anchor_scales = np.array(anchor_scales)*frame_scale
    anchor_ratios = np.array(anchor_ratios)
    anchors = generate_anchors(
        ratios=anchor_ratios, scales=anchor_scales)
    A = anchors.shape[0]
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    #Meshgrid output as xy (N2, N1)
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    K = shifts.shape[0]
    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose(
        (1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])

    return anchors, length
Example #2
0
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2)):
    """ A wrapper function to generate anchors given different scales
      Also return the number of anchors in variable 'length'

    ### Params:
        * `height`: Height of the feature map
        * `width`: Width of the feature map
        * `feat_stride`: feature stride
        * `anchor_scales`: default is (8, 16, 32) which means (8^2, 16^2, 32^2)
        * `anchor_ratios`: default is (0.5, 1, 2)

    ### Returns:
        * `anchors`: a float 2D array with shape of [K x A, 4], where K = w x h, 
                     A = len(anchor_ratio) x len(anchor_scale)
        * `length`: length of the all anchors, value = (w x h) x (3 x 3)
    """
    anchors = generate_anchors(ratios=np.array(
        anchor_ratios), scales=np.array(anchor_scales))
    A = anchors.shape[0]    # A = 9
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)

    # ravel(): Return a flattened array.
    # shift length of [x1, y1, x2, y2]
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                        shift_x.ravel(), shift_y.ravel())).transpose()
    K = shifts.shape[0] # w * h
    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])     # K*A = (w*h) * (3*3)

    return anchors, length
Example #3
0
def generate_anchors_pre(height, width, feat_stride=16,
    anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)):
    """
        A wrapper function to generate anchors given different scales
        Also return the number of anchors in variable 'length'
    """

    shift_x = tf.range(width) * feat_stride
    shift_y = tf.range(height) * feat_stride
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)

    sx = tf.reshape(shift_x, shape=(-1,))
    sy = tf.reshape(shift_y, shape=(-1,))

    anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))

    A = anchors.shape[0]
    K = tf.multiply(width, height)
    length = K * A

    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #4
0
def generate_anchors_pre_tf(height,
                            width,
                            feat_stride=16,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):
    shift_x = tf.range(width) * feat_stride  # width
    shift_y = tf.range(height) * feat_stride  # height
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    sx = tf.reshape(shift_x,
                    shape=(-1, ))  # - tf.cast(feat_stride[0]/2, tf.int32)
    sy = tf.reshape(shift_y,
                    shape=(-1, ))  # - tf.cast(feat_stride[0]/2, tf.int32)
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    K = tf.multiply(width, height)
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    A = anchors.shape[0]
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    length = K * A
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #5
0
def generate_anchors_pre_tf(height,
                            width,
                            feat_stride=16,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):
    shift_x = tf.range(width) * feat_stride  # width
    # tf.range创建数字序列,x偏移的距离
    shift_y = tf.range(height) * feat_stride  # height
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    # tf.meshgrid用于从数组a和b产生网格
    # 用法: [A,B]=Meshgrid(a,b),生成size(b)Xsize(a)大小的矩阵A和B。
    # 它相当于a从一行重复增加到size(b)行,把b转置成一列再重复增加到size(a)列。
    sx = tf.reshape(shift_x, shape=(-1, ))  #转换成列向量
    sy = tf.reshape(shift_y, shape=(-1, ))
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    # 把这四个列向量合并tf.transpose转置
    K = tf.multiply(width, height)
    # 两个矩阵中对应元素相乘
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))
    # shape=(?,4,1),?应该是9
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    A = anchors.shape[0]
    # A=9
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)
    # 创建一个常数张量

    length = K * A
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))
    # anchor_constant(9*4)是以[0 0 15 15]为中心生成的9个anchor,shift是偏移规则(9,4,1)
    # tf.cast转换类型
    return tf.cast(anchors_tf, dtype=tf.float32), length
def generate_anchors_pre_tf(clp_filter,
                            height,
                            width,
                            feat_stride=16,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):

    # old code
    # shift_x = tf.range(width) * feat_stride # width
    # shift_y = tf.range(height) * feat_stride # height
    # shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    # sx = tf.reshape(shift_x, shape=(-1,))
    # sy = tf.reshape(shift_y, shape=(-1,))
    # shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    # K = tf.multiply(width, height)
    #
    # shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))
    #
    # anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
    # A = anchors.shape[0]
    # anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)
    #
    # length = K * A
    # anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    #todo: wn modified
    shift_x = tf.range(width) * feat_stride  # width
    shift_y = tf.range(height) * feat_stride  # height
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    sx = tf.reshape(shift_x, shape=(-1, ))
    sy = tf.reshape(shift_y, shape=(-1, ))
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))  # 竖着排。。。。不,是横着
    K = tf.multiply(width, height)  # 原代码

    # 将点云图展开 需要横向展开
    # clp_filter = tf.placeholder(tf.int32, shape=[None, None]) # 参数获取
    # clp_filter_flat = tf.reshape(tf.transpose(clp_filter), (K, -1))  # shape: [23*32, 1]
    clp_filter_flat = tf.reshape(clp_filter, (K, -1))  # 修改后:横着
    clp_filter_index = tf.where(clp_filter_flat)[:, 0]
    # 获取筛选后的坐标点
    shifts_filter = tf.gather(shifts, clp_filter_index)  # shpae: [none, 4] 横着
    K = tf.size(clp_filter_index, out_type=tf.int32)
    shifts = tf.transpose(tf.reshape(shifts_filter, shape=[1, K, 4]),
                          perm=(1, 0, 2))

    # todo: temp change
    # shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))  # 竖着排
    # K = tf.multiply(width, height)
    # shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))  # 生成9个anchors
    A = anchors.shape[0]
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    length = K * A
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #7
0
def generate_anchors_pre(height,
                         width,
                         feat_stride=16,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2)):
    shift_x = tf.range(width) * feat_stride  # width 张量[0,16,32,...]
    shift_y = tf.range(height) * feat_stride  # height 同上
    #tf.meshgrid给定 N 个一维坐标数组 *args,返回 N 维坐标数组的列表输出,用于计算 N 维网格上的表达式。
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    # shift_x [[0,16,32]...]
    #          [0,16,32]...]
    #          .
    #          .
    #          .
    #          [0,16,32]...]]
    #shift_y [[0, 0, 0,...]
    #         [16,16,16,...]
    #         [32,32,32,...]
    #          .
    #          .
    #          .]
    sx = tf.reshape(shift_x, shape=(-1, ))  #转换为一维张量
    sy = tf.reshape(shift_y, shape=(-1, ))
    # tf.stack将秩为 R 的张量列表堆叠成一个秩为 (R+1) 的张量
    # tf.transpose 交换矩阵维度
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    #shifts*T [[0,16,32,...,0,16,32,...]
    #         [0,0,0,...,16,16,16...]
    #         [0,16,32,...,0,16,32,...]
    #         [0,0,0,...,16,16,16...]]

    #
    # shift [[0,0,0,0]
    #       [16,0,16,0]
    #       [32,0,32,0]
    #       ...
    #       [0,16,0,16]
    #       ...
    #       ]

    K = tf.multiply(width, height)
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))
    # shifts.shape [K,1,4]
    # 生成anchor
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    #A为anchor个数
    ## 9个
    A = anchors.shape[0]
    #申明tf图中的常量
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)
    #anchor+偏移量
    #length为总anchor数量
    length = K * A
    ## 原始的9个anchor加偏移量,得到最终anchors
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #8
0
def generate_anchors_pre(height,
                         width,
                         feat_stride,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2)):
    ''' A wrapper function to generate anchors given different scales, 
        Also return the number of anchors in variable 'length'
    '''
    # 锚框记录的两个坐标点是左下角右上角
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    A = anchors.shape[0]
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    # ----------------- #
    # x = np.array([0, 1, 2])
    # y = np.array([0, 1])
    # np.meshgrid()函数说明
    # X, Y = np.meshgrid(x, y)
    # X = [[0 1 2]
    #      [0 1 2]]
    # Y = [[0 0 0]
    #      [1 1 1]]
    # ------------------ # 生成图像每个点坐标位置
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    #******************************#
    # np.hstack() np.vstack()实例说明
    # a = np.array([[1, 2],[2, 3],[3, 4]])
    # b = np.array([[2, 3],[3, 4],[4, 5]])
    # np.hstack((a,b)) =
    #                 array([[1, 2, 2, 3],
    #                     [2, 3, 3, 4],
    #                     [3, 4, 4, 5]])
    # np.vstack((a,b)) =
    #                 array([[1, 2],
    #                     [2, 3],
    #                     [3, 4],
    #                     [2, 3],
    #                     [3, 4],
    #                     [4, 5]])
    # np.ravel() 解释
    # a = arange(12).reshape(3,4) = [[ 0  1  2  3]
    #                                [ 4  5  6  7]
    #                                [ 8  9 10 11]]
    # a.ravel() = [ 0  1  2  3  4  5  6  7  8  9 10 11]
    #******************************#
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    K = shifts.shape[0]

    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose(
        (1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])

    return anchors, length
Example #9
0
def generate_anchors_pre_tf(height,
                            width,
                            feat_stride=16,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):
    """ Generate anchor boxes

    :param height: number of vertical anchor boxes that fit into image dimensions
    :param width: number of vertical anchor boxes that fit into image dimensions
    :param feat_stride: the size of the feature stide
    :param anchor_scales: scales of anchors
    :param anchor_ratios: aspect ratios of anchor boxes
    :return:
    """
    # Generate all of the horizontal and vertical positions for anchors.
    shift_x = tf.range(width) * feat_stride
    shift_y = tf.range(height) * feat_stride

    # Create a meshgrid of the positions of all anchors
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)

    # Flatten meshgrids into 1D arrays
    sx = tf.reshape(shift_x, shape=(-1, ))
    sy = tf.reshape(shift_y, shape=(-1, ))

    # Stack flat meshgrids together twice to be used as increments for anchors shaped like
    # [x_min, y_min, x_max, y_max]
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))

    # Find total number of shifts for anchors
    K = tf.multiply(width, height)

    # Permutate dimensions
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    # Generates all of the anchors of all aspect ratios. Generates (num_ratios x num_scales)
    # matrix of vectors containing [x_min, y_min, x_max, y_max]
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))

    # Get the number of anchors
    A = anchors.shape[0]

    # shape of object storing anchors. In default case dim = (1, 9, 4)
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    # Number of all anchors for all shifts
    length = K * A

    # Initialize positions of all anchors, by incrememnting the base anchor with all points on the
    # meshgrid
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    # return matrix of all anchors as well as number of all anchors
    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #10
0
def generate_anchors_fixate(height, width, h_start, w_start, anchor_stride=(16,), anchor_sizes=(128, 256, 512), anchor_ratios=(0.5, 1, 2)):
    """ A wrapper function to generate anchors given different scales
      Also return the number of anchors in variable 'length'
    """
    anchors = generate_anchors(anchor_stride, anchor_ratios, anchor_sizes)
    A = anchors.shape[0]
    shift_x = (np.arange(0, width) + w_start) * anchor_stride
    shift_y = (np.arange(0, height) + h_start) * anchor_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
    K = shifts.shape[0]
    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])

    return anchors, length
Example #11
0
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2)):
    """ A wrapper function to generate anchors given different scales
      Also return the number of anchors in variable 'length'
    """
    anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
    A = anchors.shape[0]
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
    K = shifts.shape[0]
    # width changes faster, so here it is H, W, C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])

    return anchors, length
Example #12
0
def generate_anchors_pre(height, width,
                         aspect_ratio=[0.5, 1.0, 2.0],
                         aspect_scale=[8, 16, 32],
                         feat_stride=16):
    anchors = generate_anchors()
    anchor_nums = anchors.shape[0]
    width = np.arange(width)*feat_stride
    height = np.arange(height)*feat_stride
    shift_x, shift_y = np.meshgrid(width, height)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
    print(shifts)
    anchors = anchors.reshape(1, anchor_nums, 4) + shifts.reshape(1, shifts.shape[0], 4).transpose([1, 0, 2])
    anchors = anchors.reshape(shifts.shape[0]*anchor_nums, 4).astype(np.float32)
    print(anchors.shape)
    print(anchors)
    length = anchors.shape[0]
    return anchors, length
Example #13
0
def generate_anchors_pre_tf(height, width, feat_stride=16, anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2)):
    shift_x = tf.range(width) * feat_stride  # width
    shift_y = tf.range(height) * feat_stride  # height
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    sx = tf.reshape(shift_x, shape=(-1,))
    sy = tf.reshape(shift_y, shape=(-1,))
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    K = tf.multiply(width, height)
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
    A = anchors.shape[0]
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    length = K * A
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #14
0
def generate_anchors_pre(height,
                         width,
                         aspect_ratio=[0.5, 1.0, 2.0],
                         aspect_scale=[8, 16, 32],
                         feat_stride=16):
    anchors = generate_anchors()
    anchor_nums = anchors.shape[0]
    width = np.arange(width) * feat_stride
    height = np.arange(height) * feat_stride
    shift_x, shift_y = np.meshgrid(width, height)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    print(shifts)
    anchors = anchors.reshape(1, anchor_nums, 4) + shifts.reshape(
        1, shifts.shape[0], 4).transpose([1, 0, 2])
    anchors = anchors.reshape(shifts.shape[0] * anchor_nums,
                              4).astype(np.float32)
    print(anchors.shape)
    print(anchors)
    length = anchors.shape[0]
    return anchors, length
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)):
  """ A wrapper function to generate anchors given different scales
    Also return the number of anchors in variable 'length'
  """
  # 1. 生成9个不同面积不同长宽比的窗口anchors,此时anchor有H, W两个参数
  anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
  A = anchors.shape[0]
  # 2. 根据height, width得到所有中心点坐标shifts
  # todo: 修改中心点坐标与数量
  shift_x = np.arange(0, width) * feat_stride
  shift_y = np.arange(0, height) * feat_stride
  shift_x, shift_y = np.meshgrid(shift_x, shift_y)
  shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
  K = shifts.shape[0]
  # 3. anchors中添加坐标信息,此时anchor有H, W, X, Y四个参数
  # width changes faster, so here it is H, W, C
  anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
  anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
  length = np.int32(anchors.shape[0])

  return anchors, length
Example #16
0
def generate_anchors_pre_tf(height,
                            width,
                            anchor_stride=(16, ),
                            anchor_sizes=(128, 256, 512),
                            anchor_ratios=(0.5, 1, 2)):
    shift_x = tf.range(width) * anchor_stride  # width
    shift_y = tf.range(height) * anchor_stride  # height
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    sx = tf.reshape(shift_x, shape=(-1, ))
    sy = tf.reshape(shift_y, shape=(-1, ))
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))
    K = tf.multiply(width, height)
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchors = generate_anchors(anchor_stride, anchor_ratios, anchor_sizes)
    A = anchors.shape[0]
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    length = K * A
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
def generate_anchors_pre(height,
                         width,
                         feat_stride,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2)):
    """
    A wrapper function to generate anchors given different scales
    Also return the number of anchors in variable 'length'
  """
    #转到generate_anchors。py,得到一系列anchor[x1,y1,x2,y2]左下和右上坐标
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))

    A = anchors.shape[0]  #anchor的数量9个
    #16格一平移
    shift_x = np.arange(0, width) * feat_stride  # feat_stride=[16,]
    shift_y = np.arange(0, height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x,
                                   shift_y)  #生成网格图纵向复制和横向复制均为(w×h)(w×h)
    #shift=[shift_x, shift_y, shift_x, shift_y]     shift_x.ravel[1× W*W]    shift_y.ravel[1× H*H]
    #shift=[w*h, 4]
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    K = shifts.shape[0]  # =w*h

    # width changes faster, so here it is H, W, C
    # transpose做轴对换0轴和1轴对换
    # ---------------A=9 K=W*H----------------------------------
    #[1,9,4]+[k,1,4]=[k,A,4]
    #一个anchor生成4K个坐标,共扫图k次
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose(
        (1, 0, 2))
    #[K*A,4],生成9K个anchor
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    #length=K*A
    length = np.int32(anchors.shape[0])

    return anchors, length
Example #18
0
def generate_anchors_pre(height,
                         width,
                         feat_stride,
                         anchor_scales=(8, 16, 32),
                         anchor_ratios=(0.5, 1, 2)):
    # 一个包装函数,用于生成给定不同比例的锚点 并返回anchors数量
    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))
    A = anchors.shape[0]
    shift_x = np.arange(0, width) * feat_stride
    shift_y = np.arange(0, height) * feat_stride
    # 接受两个一维数组生成两个二维矩阵 维度均为 y.shape x x.shape
    # shift_x 每一行均为shift_x,shift_y每一列均为shift_y
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(),
                        shift_y.ravel())).transpose()
    K = shifts.shape[0]
    # 这里是 H W C
    anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose(
        (1, 0, 2))
    anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
    length = np.int32(anchors.shape[0])
    return anchors, length
Example #19
0
def generate_anchors_pre_tf(height,
                            width,
                            feat_stride=16,
                            anchor_scales=(8, 16, 32),
                            anchor_ratios=(0.5, 1, 2)):
    #生成候选框的思路是计算基础框的9个范围,然后通过加法作为偏移,得到9K个候选框
    shift_x = tf.range(width) * feat_stride  #x偏移量
    shift_y = tf.range(height) * feat_stride  #y偏移量
    shift_x, shift_y = tf.meshgrid(shift_x, shift_y)
    sx = tf.reshape(shift_x, shape=(-1, ))
    sy = tf.reshape(shift_y, shape=(-1, ))
    shifts = tf.transpose(tf.stack([sx, sy, sx, sy]))  #增加一维
    K = tf.multiply(width, height)  #像素点总数
    shifts = tf.transpose(tf.reshape(shifts, shape=[1, K, 4]), perm=(1, 0, 2))

    anchors = generate_anchors(ratios=np.array(anchor_ratios),
                               scales=np.array(anchor_scales))  #9个候选框
    A = anchors.shape[0]  #A=9,共9个候选框
    anchor_constant = tf.constant(anchors.reshape((1, A, 4)), dtype=tf.int32)

    length = K * A  #共9×K个候选框
    anchors_tf = tf.reshape(tf.add(anchor_constant, shifts), shape=(length, 4))

    return tf.cast(anchors_tf, dtype=tf.float32), length
Example #20
0
    im_size_min = min(img.shape)
    im_size_max = max(img.shape)

    im_scale = float(args.scale) / float(im_size_min)
    # Prevent the biggest axis from being more than MAX_SIZE
    if np.round(im_scale * im_size_max) > args.max_scale:
        im_scale = float(args.max_scale) / float(im_size_max)

    img = cv2.resize(img, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR)
    print("Scaled image size")
    print(img.shape)
    width = img.shape[1]
    height = img.shape[0]

    base_anchors = generate_anchors(base_height=11,
                                    num_anchors=10,
                                    anchor_width=16,
                                    h_ratio_step=0.7)

    heights = [x[3] - x[1] for x in base_anchors]

    img = draw_anchors(img, heights, 16, (width // 2, height // 2))
    img = draw_anchors(img, heights, 16, (100, 150))
    img = draw_anchors(img, heights, 16, (width - 300, 150))
    img = draw_anchors(img, heights, 16, (100, height - 150))
    img = draw_anchors(img, heights, 16, (width - 300, height - 150))

    cv2.namedWindow('test')
    cv2.imshow('test', img)
    cv2.waitKey()
Example #21
0
    if np.round(im_scale * im_size_max) > args.max_scale:
        im_scale = float(args.max_scale) / float(im_size_max)

    img = cv2.resize(img,
                     None,
                     None,
                     fx=im_scale,
                     fy=im_scale,
                     interpolation=cv2.INTER_LINEAR)
    print("Scaled image size")
    print(img.shape)
    width = img.shape[1]
    height = img.shape[0]

    base_anchors = generate_anchors(base_height=11,
                                    num_anchors=10,
                                    anchor_width=16,
                                    h_ratio_step=0.7)

    heights = [x[3] - x[1] for x in base_anchors]

    img = draw_anchors(img, heights, 16, (width // 2, height // 2))
    img = draw_anchors(img, heights, 16, (100, 150))
    img = draw_anchors(img, heights, 16, (width - 300, 150))
    img = draw_anchors(img, heights, 16, (100, height - 150))
    img = draw_anchors(img, heights, 16, (width - 300, height - 150))

    cv2.namedWindow('test')
    cv2.imshow('test', img)
    cv2.waitKey()