当前位置:网站首页>Anchor of SSD_ Box calculation

Anchor of SSD_ Box calculation

2022-06-21 12:01:00 m0_ thirty-seven million one hundred and eighty-eight thousand

from :http://www.pianshen.com/article/129360549/
Well written and clear . Anti deletion and anti forgetting , If you have any questions, please contact

seen SSD Of tensorflow The implementation partner must be right anchor_box My calculations are curious , There are also various explanations on the Internet , Today, I will explain the source code and principles .

default_params = SSDParams(
        img_shape=(300, 300),
        num_classes=21,
        no_annotation_label=21,
        feat_layers=['block4', 'block7', 'block8', 'block9', 'block10', 'block11'],
        feat_shapes=[(38, 38), (19, 19), (10, 10), (5, 5), (3, 3), (1, 1)],
        anchor_size_bounds=[0.15, 0.90],
        # anchor_size_bounds=[0.20, 0.90],
        anchor_sizes=[(21., 45.),
                      (45., 99.),
                      (99., 153.),
                      (153., 207.),
                      (207., 261.),
                      (261., 315.)],
        # anchor_sizes=[(30., 60.),
        #               (60., 111.),
        #               (111., 162.),
        #               (162., 213.),
        #               (213., 264.),
        #               (264., 315.)],
        anchor_ratios=[[2, .5],
                       [2, .5, 3, 1./3],
                       [2, .5, 3, 1./3],
                       [2, .5, 3, 1./3],
                       [2, .5],
                       [2, .5]],
        anchor_steps=[8, 16, 32, 64, 100, 300],
        anchor_offset=0.5,
        normalizations=[20, -1, -1, -1, -1, -1],
        prior_scaling=[0.1, 0.1, 0.2, 0.2]
        )

This anchor_box The calculation and anchor_sizes and anchor_ratios of .
Explain first anchor_box The origin of . We assume that the current layer is block4, So our feat_shape Namely (38, 38),
You can understand it as 38*38 individual cell unit , Each unit needs to predict several anchor_box, Different layers , Theoretically speaking, this number is 6 individual , (paper That's what it says on the ). Every box The aspect ratio of is ratios Decisive , paper There are two 1 , as well as 4 Other values . But the author's source code has been mischievously changed .
Look at our aspect ratio , There are no those two 1, Because they are set separately :

anchor_ratios=[[2, .5],
               [2, .5, 3, 1./3],
               [2, .5, 3, 1./3],
               [2, .5, 3, 1./3],
               [2, .5],
               [2, .5]],

We see the first floor and the last two , There are only two aspect ratios , So there are only three floors 4 Kind of box.
 Insert picture description here
This picture can be said to be fairly accurate , There are mistakes on the Internet , Subject to this . With block4 Layer as an example , Each of him cell The unit has only 4 individual box, Including two squares, one large and one small , And two rectangles .
For small squares , Its side length is min_size, This min_size What is it? ? It is the... In the previous parameters anchor_sizes:

anchor_sizes=[(30., 60.),
   			(60., 111.),
   			(111., 162.),
   			(162., 213.),
   			(213., 264.),
   			(264., 315.)]

Each of these acts is a group , Each row corresponds to the corresponding feature layer , The first line corresponds to block4, By analogy , about (30, 60), 30 Namely min_size, 60 Namely max_size.
The side length of the large square is :
 Insert picture description here
Then there are two other rectangles , This is the time anchor_ratios I'll be on my way , There is a formula for the length and width of a rectangle .
 Insert picture description here
This ratio Namely anchor_ratios Inside 2, 5, 3, 1/3 And so on. .
such , On the principle of box The size of is introduced . But don't forget that this size is the size on the original drawing , We need the scale of this dimension relative to the original drawing .
Take a look at this part of the source code :

	# Add first anchor boxes with ratio=1.
	# sizes Namely (30, 60), img_shape Is the size of the original drawing (300, 300)
    h[0] = sizes[0] / img_shape[0]
    w[0] = sizes[0] / img_shape[1]
    
    di = 1
    if len(sizes) > 1:
        h[1] = math.sqrt(sizes[0] * sizes[1]) / img_shape[0]
        w[1] = math.sqrt(sizes[0] * sizes[1]) / img_shape[1]
        di += 1
    # r  Namely [2, 5, 3, 1/3]    
    for i, r in enumerate(ratios):
        h[i+di] = sizes[0] / img_shape[0] / math.sqrt(r)
        w[i+di] = sizes[0] / img_shape[1] * math.sqrt(r)

Last , Now that we understand anchor_size, This parameter directly determines the current feature layer box Size , You can see that the closer you get to the input layer , box The smaller it is , Closer to the output layer , box The bigger it is , therefore SSD The bottom layer is used to detect small targets , High level is used to detect large targets .

原网站

版权声明
本文为[m0_ thirty-seven million one hundred and eighty-eight thousand ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211156089825.html