FCN(全卷及网络)
FCN将CNN最后的全链接层(FC)换成了卷积层,输出为一张已经label好的图
网络结构
分为四个阶段
第一阶段:
全链接层
第二阶段:
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
}
}
第三阶段:
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
}
}
第四阶段:
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(非极大值抑制)
构建一个边框例子
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')