SSD in the anchor understand

import numpy as np
import math
def ssd_anchor_one_layer(img_shape,feat_shape,sizes,ratios,step,offset=0.5,dtype=np.float32):
    """Computer SSD default anchor boxes for one feature layer.

    Determine the relative position grid of the centers, and the relative
    width and height.

    Arguments:
      feat_shape: Feature shape, used for computing relative position grids;
      size: Absolute reference sizes;
      ratios: Ratios to use on these features;
      img_shape: Image shape, used for computing height, width relatively to the
        former;
      offset: Grid offset.

    Return:
      y, x, h, w: Relative x and y grids, and height and width.
    "" " 
    # Position the Compute The Grid: Simple Way. 
    Y, X = np.mgrid [0: feat_shape [0], 0: feat_shape [. 1]] 
    # can obtained on the original, the relative ratio of the size of the original center coordinates of each anchor point x, y 
    # 38 cell corresponding to the anchor frame x, y coordinates of each cell is shifted to the center, and then multiplied by the relative scaling of the original ratio, divided by the original 
    Y = (y.astype (DTYPE) + offset) * STEP / img_shape [0] 
    X = (x.astype (DTYPE) + offset) * STEP / img_shape [. 1]
    Y #, np.mgrid X = [0: feat_shape [0], 0: feat_shape [. 1]] 
    # Y = (y.astype (DTYPE) + offset) / feat_shape [0] 
    # X = (x.astype (DTYPE ) + offset) / feat_shape [. 1] 
    # Weird the using the SSD-Caffe Computation Steps values ... 
    # for the first characteristic diagram (block4: 38x38); y = [[0,0, ...... 0], [1, 1, ...... 1], ...... [37, 37, ......, 37]]; 
    # and x = [[0,1,2 ......, 37 ], [0,1,2 ......, 37], ...... [0,1,2 ......, 37 []] 
    . Easy Support # Expand Broadcasting DIMS to 
    # for the first feature map, y of the shape = 38x38x1; shape x of 38x38x1 = 
    Y = np.expand_dims (Y, Axis -1 =) 
    X = np.expand_dims (X, Axis = -1) 

    # the Compute relative height and width. 
    # Tries to Follow the Original the Order for Implementation of the SSD. 
    # anchor block corresponding to each point on the feature of FIG. number; as: a first feature for each point in FIG. 4 predicted anchor frames (block4: 38x38),
    #num_anchors: = 2 + 2. 4 
    num_anchors = len (sizes) + len (ratios) 
    # For the first characteristic diagram, h the shape = 4x; w = 4x the Shape 
    H = np.zeros ((num_anchors,), DTYPE DTYPE =) 
    W = np.zeros ((num_anchors,), DTYPE = DTYPE)  
    # first Anchor boxes with the Add. 1 = ratio. 
    # high first anchor frame h [ 0] = the initial anchor point high / high original size; for example: H [0] = 21 is / 300 
    H [0] = sizes [0] / img_shape [0]
    # first anchor frame width w [0] = width / original width of the size of the initial anchor; for example: w [0 ] = 21 is / 300 
    W [0] = sizes [0] / img_shape [. 1] 
    DI = the number of anchor width deviation. 1 # 
    IF len (sizes)>. 1: 
        # anchor frames of the second high-h [1 ] = sqrt (* high initial starting anchors anchor width) / high picture size; for example: H [. 1] = sqrt (21 is * 45) / 300 
        H [. 1] = Math.sqrt (sizes [0] * sizes [1] ) / img_shape [0] 
        high w # second anchor frame [1] = sqrt (* high initial starting anchors anchor width) / original width size ; for example: W [. 1] = sqrt (21 is * 45) / 300 
        W [. 1] = Math.sqrt (sizes [0] * sizes [. 1]) / img_shape [. 1] 
        DI + =. 1 
    for I, R & lt in the enumerate (ratios): 
        # traversing aspect ratio, a first feature map, R & lt only two, 2 and 0.5; total of four anchor width size (H [0] ~ H [. 3]) 
        # example: for a first characteristic diagram, h [0 + 2] = h [2] = 21/300 / sqrt (2); W [0 + 2] = W [2] = 45/300 * sqrt (2) 
        # example: for a first characteristic diagram, h [1 + 2] = h [3] = 21 / 300 / sqrt (0.5); w [1 + 2] = w [3] = 45/300 * sqrt (0.5)
        # Return anchor the coordinates and size are not normalized before 
        H [I + DI] sizes = [0] / img_shape [0] / Math.sqrt (R & lt) 
        W [I + DI] sizes = [0] / img_shape [ . 1] * Math.sqrt (R & lt) 
    return Y, X, H, W 
Y, X, H, W = ssd_anchor_one_layer ((300,300), (38, 38), (21 is., 45.), [2, .5 ],. 8) 
Print (H) 
Print (W)

  [0.07       0.10246951  0.04949747  0.09899495]
  [0.07       0.10246951  0.09899495  0.04949747]

Wherein a characteristic diagram of the 38 * 38, each point produces four anchor, respectively, 0.07 and 0.102 of two square, rectangular width and height, respectively, of two 0.049,0.098.

Guess you like

Origin www.cnblogs.com/lzq116/p/12608832.html