SSD 單發多框檢測

2022-04-01 06:39:15 字數 3955 閱讀 2501

其實現在用的最多的是faster rcnn,等下再弄專案~~~

%matplotlib inline

import

gluonbook as gb

from mxnet import

autograd,gluon,image,init,nd,contrib

from mxnet.gluon import

loss as gloss,nn

import

time

#類別**層

defcls_predictor(num_anchors,num_classes):

return nn.conv2d(num_anchors*(num_classes+1),kernel_size=3,padding=1)

#邊框**層

defbbox_predictor(num_anchors):

return nn.conv2d(num_anchors*4,kernel_size=3,padding=1)

#鏈結多尺度

defforward(x,block):

block.initialize()

return

block(x)

y1 = forward(nd.zeros((2,8,20,20)),cls_predictor(5,10))

y2 = forward(nd.zeros((2,16,10,10)),cls_predictor(3,10))

y1.shape,y2.shape

defflatten_pred(pred):

return pred.transpose((0,2,3,1)).flatten()

defconcat_preds(preds):

return nd.concat(*[flatten_pred(p) for p in preds],dim=1)

concat_preds([y1,y2]).shape

#減半模組

defdown_sample_blk(num_channels):

blk =nn.sequential()

for _ in range(2):

blk.add(nn.conv2d(num_channels,kernel_size=3,padding=1),

nn.batchnorm(in_channels=num_channels),

nn.activation(

'relu'))

blk.add(nn.maxpool2d(2))

return

blkblk = down_sample_blk(10)

blk.initialize()

x = nd.zeros((2,3,20,20))

y =blk(x)

y.shape

#主體網路塊

defbase_net():

blk =nn.sequential()

for num_filters in [16,32,64]:

blk.add(down_sample_blk(num_filters))

return

blkbnet =base_net()

bnet.initialize()

x = nd.random.uniform(shape=(2,3,256,256))

y =bnet(x)

y.shape

#完整的模型

defget_blk(i):

if i==0: #

0 基礎網路模組

blk =base_net()

elif i==4: #

4 全域性最大池化層模組,將高寬降到1

blk =nn.globalmaxpool2d()

else: #

1 ,2 ,3 高寬減半模組

blk = down_sample_blk(128)

return

blkdef

blk_forward(x,blk,size,ratio,cls_predictor,bbox_predictor):

y =blk(x)

anchors = contrib.nd.multiboxprior(y,sizes=size,ratios=ratio)

cls_preds =cls_predictor(y)

bbox_preds =bbox_predictor(y)

return

(y, anchors, cls_preds,bbox_preds)

sizes = [[0.2, 0.272], [0.37, 0.447], [0.54, 0.619], [0.71, 0.79],

[0.88, 0.961]]

ratios = [[1, 2, 0.5]] * 5num_anchors = len(sizes[0]) + len(ratios[0]) - 1

#完整的tinyssd

class

tinyssd(nn.block):

def__init__(self, num_classes, **kwargs):

super(tinyssd, self).

__init__(**kwargs)

self.num_classes =num_classes

for i in range(5):

#賦值語句 self.blk_i = get_blk(i)

setattr(self, '

blk_%d

' %i,get_blk(i))

setattr(self,

'cls_%d

' %i,cls_predictor(num_anchors,num_classes))

setattr(self,

'bbox_%d

' %i,bbox_predictor(num_anchors))

defforward(self, x):

anchors, cls_preds, bbox_preds = [none]*5,[none]*5,[none]*5

for i in range(5):

#getattr(self, 'blk_%d' % i ) 即訪問 self.blk_i

x, anchors[i], cls_preds[i], bbox_preds[i] =blk_forward(

x, getattr(self,

'blk_%d

' %i), sizes[i], ratios[i],

getattr(self,

'cls_%d

' % i), getattr(self, '

bbox_%d

' %i))

return (nd.concat(*anchors, dim=1),

concat_preds(cls_preds).reshape(

(0, -1, self.num_classes + 1)), concat_preds(bbox_preds))

#測試形狀

net = tinyssd(num_classes=1)

net.initialize()

x = nd.zeros((32,3,256,256))

anchors, cls_preds, bbox_preds =net(x)

print('

output anchors:

',anchors.shape)

print('

output class preds:

',cls_preds.shape)

print('

output bbox preds:

',bbox_preds.shape)

單階多層檢測器 SSD(一)

對於物體檢測任務,第4章的faster rcnn演算法採用了兩階的檢測架構,即首先利用rpn網路進行感興趣區域生成,然後再對該區域進行類別的分類與位置的回歸,這種方法雖然顯著提公升了精度,但也限制了檢測速度。yolo演算法利用回歸的思想,使用一階網路直接完成了物體檢測,速度很快,但是精度有了明顯的下...

單網絡卡多網路

需求 有幾個業務的系統在機房。我自己的電腦經樓層交換機在辦公室。業務系統裡面有獨立的區域網。我想在我辦公室的電腦裡面直接管理這個幾個業務系統。我電腦所在網路與業務系統自身網路相互獨立,且沒有任何物理連線。我也無法將業務系統的網路接入到辦公網路的核心交換機上 我也不想這麼幹 有乙個有利的優勢是,機房跟...

單頁面多頁面區別

單頁面與多頁面的區別 單頁面多頁面 組成 有乙個主要頁面和其他片段頁面組成 由多個完整頁面組成 css js公用載入 每個頁面都分開載入 url模式 a.com pageone a.com pagetwo url模式 a.com pageone.html a.com pagetwo.html 重新整...