FCN(全卷及網絡)
FCN將CNN最後的全連接層(FC)換成了卷積層,輸出爲一張已經label好的圖網絡
網絡結構app
分爲四個階段ide
第一階段:spa
全連接層code
第二階段:圖片
layer { name: "upscore" type: "Deconvolution" bottom: "score_fr" top: "upscore" param { lr_mult: 0 } convolution_param { num_output: 21 bias_term: false kernel_size: 64 stride: 32 } } layer { name: "score" type: "Crop" bottom: "upscore" bottom: "data" top: "score" crop_param { axis: 2 offset: 19 } }
第三階段:ip
layer { name: "upscore16" type: "Deconvolution" bottom: "fuse_pool4" top: "upscore16" param { lr_mult: 0 } convolution_param { num_output: 21 bias_term: false kernel_size: 32 stride: 16 } } layer { name: "score" type: "Crop" bottom: "upscore16" bottom: "data" top: "score" crop_param { axis: 2 offset: 27 } }
第四階段:it
layer { name: "upscore8" type: "Deconvolution" bottom: "fuse_pool3" top: "upscore8" param { lr_mult: 0 } convolution_param { num_output: 21 bias_term: false kernel_size: 16 stride: 8 } } layer { name: "score" type: "Crop" bottom: "upscore8" bottom: "data" top: "score" crop_param { axis: 2 offset: 31 } }
NMS(非極大值抑制)io
構建一個邊框例子class
import numpy as np import matplotlib.pyplot as plt boxes=np.array([[100,100,210,210,0.7], [250,250,420,420,0.8], [230,230,320,330,0.9], [100,100,210,210,0.7], [230,240,325,330,0.8], [220,230,315,340,0.9]]) def plot_bbox(dets, c='k'): x1 = dets[:,0] y1 = dets[:,1] x2 = dets[:,2] y2 = dets[:,3] plt.plot([x1,x2], [y1,y1], c) plt.plot([x1,x1], [y1,y2], c) plt.plot([x1,x2], [y2,y2], c) plt.plot([x2,x2], [y1,y2], c) plt.title("Initial border") plot_bbox(boxes,'k')
def py_cpu_nms(dets, thresh): x1 = dets[:,0] y1 = dets[:,1] x2 = dets[:,2] y2 = dets[:,3] areas = (y2-y1+1) * (x2-x1+1) # 求像素點的面積因此加一 scores = dets[:,4] keep = [] index = scores.argsort()[::-1] while index.size >0: i = index[0] keep.append(i) x11 = np.maximum(x1[i], x1[index[1:]]) # 計算窗口i與其餘因此窗口的交疊部分的面積 y11 = np.maximum(y1[i], y1[index[1:]]) x22 = np.minimum(x2[i], x2[index[1:]]) y22 = np.minimum(y2[i], y2[index[1:]]) w = np.maximum(0, x22-x11+1) h = np.maximum(0, y22-y11+1) overlaps = w*h ious = overlaps / (areas[i]+areas[index[1:]] - overlaps) idx = np.where(ious<=thresh)[0] index = index[idx+1] return keep
keep = py_cpu_nms(boxes, thresh=0.6) plt.title("After nms") plot_bbox(boxes[keep], 'r')