0 SSD演算法原始碼解讀 anchor是如何生成的

2021-09-29 01:51:46 字數 2801 閱讀 4180

ssd演算法中預設框的好壞是非常重要的,好的anchor降低回歸的難度,更容易訓練出更好的模型,下面我們根據pytorch**講講預設框是怎麼生成的,**非常簡單,我對**做出必要的說明,希望可以幫助大家理解anchor。

注意,以下**所有引數針對 ssd 512*512,至於ssd 300*300等其他大小的ssd網路,大家同比計算一下即可

這裡我們使用proprbox類來生成預設anchor,初始化的時候就設定好了預設各項指標,我們看看初始化需要哪些引數

class

priorbox

:def

__init__

(self, cfg)

: self.image_size = cfg.input.image_size

prior_config = cfg.model.priors

self.feature_maps = prior_config.feature_maps

self.min_sizes = prior_config.min_sizes

self.max_sizes = prior_config.max_sizes

self.strides = prior_config.strides

self.aspect_ratios = prior_config.aspect_ratios

self.clip = prior_config.clip

下面我們看看這些引數都是什麼意思

cfg是乙個引數集合,也就是類初始化的傳入引數,我們看看各個引數的意義

其他的同理類推

大家對照講解和**來看很容易理解

from itertools import product

import torch

from math import sqrt

#用於生成anchor的類

class

priorbox

:def

__init__

(self, cfg)

: self.image_size = cfg.input.image_size

prior_config = cfg.model.priors

self.feature_maps = prior_config.feature_maps

self.min_sizes = prior_config.min_sizes

self.max_sizes = prior_config.max_sizes

self.strides = prior_config.strides

self.aspect_ratios = prior_config.aspect_ratios

self.clip = prior_config.clip

def__call__

(self)

:"""generate ssd prior boxes.

it returns the center, height and width of the priors. the values are relative to the image size

returns:

priors (num_priors, 4): the prior boxes represented as [[center_x, center_y, w, h]]. all the values

are relative to the image size.

"""priors =

for k, f in

enumerate

(self.feature_maps)

: scale = self.image_size / self.strides[k]

for i, j in product(

range

(f), repeat=2)

:# unit center x,y

cx =

(j +

0.5)

/ scale

cy =

(i +

0.5)

/ scale

# small sized square box

size = self.min_sizes[k]

h = w = size / self.image_size

[cx, cy, w, h]

)# big sized square box

size = sqrt(self.min_sizes[k]

* self.max_sizes[k]

) h = w = size / self.image_size

[cx, cy, w, h]

)# change h/w ratio of the small sized box

size = self.min_sizes[k]

h = w = size / self.image_size

for ratio in self.aspect_ratios[k]

: ratio = sqrt(ratio)

[cx, cy, w * ratio, h / ratio]

)[cx, cy, w / ratio, h * ratio]

) priors = torch.tensor(priors)

if self.clip:

priors.clamp_(

max=1,

min=0)

return priors

SSD原始碼解讀 prior box layer

一直不是很理解檢測結果是怎麼出來的,學習最快的方法就是看原始碼啦,今天先從prior box層開始。這層的作用就是對不同位置的每個特徵點產生不同大小的default box,這些box的大小 形狀由prototxt的prior box param來控制。隨便拿出乙個模型的prior box層來舉例 ...

jemalloc原始碼解讀(四)長度對齊演算法

在現代計算架構中,從記憶體中讀取資料,基本上都是按2 n個位元組來從主存載入cpu中。這個值,基本是cache line的大小。也就是說,如果一塊記憶體是是在同一塊cache line之內是最快的。目前來說,多數pc的cache line值是128個位元組。對於首位址也是一樣的。在32位機器上,如果...

原始碼解讀 RT Thread多任務排程演算法

本文依據 rt thread 當時最新版本 4.0.1 版本原始碼 rt thread作業系統是一款基於優先順序和時間片輪轉的多工實時作業系統。其排程演算法採用256個優先順序,並支援相同優先順序的任務存在。不同優先順序的任務採用優先順序排程,而相同優先順序的任務則採用時間片輪轉排程。其實這種排程演...