01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/usr/bin/env sh
# Create the image to lmdb inputs
TOOLS=/home/crw/caffe-master/.build_release/tools
#图像文件的存放位置
TRAIN_DATA_ROOT=/media/crw/MyBook/Dataset/faceImages/
VAL_DATA_ROOT=/media/crw/MyBook/Dataset/faceImages/
IMAGE_LIST_ROOT=./
#LMDB文件的存放位置
ROOT_LMDB=/media/crw/MyBook/TrainData/LMDB/FaceDetection/50000_32X32
# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
#是否剪切为相同的大小
RESIZE=
true
if
$RESIZE; then
RESIZE_HEIGHT=32
RESIZE_WIDTH=32
else
RESIZE_HEIGHT=0
RESIZE_WIDTH=0
fi
if
[ ! -d
"$TRAIN_DATA_ROOT"
]; then
echo
"Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
echo
"Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path"
\
"where the ImageNet training data is stored."
exit
1
fi
if
[ ! -d
"$VAL_DATA_ROOT"
]; then
echo
"Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
echo
"Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path"
\
"where the ImageNet validation data is stored."
exit
1
fi
echo
"Creating train lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
--gray \
$TRAIN_DATA_ROOT \
$IMAGE_LIST_ROOT/train_2.list \
$ROOT_LMDB/train
echo
"Creating val lmdb..."
GLOG_logtostderr=1 $TOOLS/convert_imageset \
--resize_height=$RESIZE_HEIGHT \
--resize_width=$RESIZE_WIDTH \
--shuffle \
--gray \
$VAL_DATA_ROOT \
$IMAGE_LIST_ROOT/val_2.list \
$ROOT_LMDB/val
$TOOLS/compute_image_mean $ROOT_LMDB/train \
$ROOT_LMDB/mean.binaryproto
echo
"Done."
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
def convert_full_conv(model_define,model_weight,model_define_fc,model_weight_fc):
''
'
@breif : 将原始网络转换为全卷积模型
@param: model_define,二分类网络定义文件
@param: model_weight,二分类网络训练好的参数
@param: model_define_fc,生成的全卷积网络定义文件
@param: model_weight_fc,转化好的全卷积网络的参数
''
'
net = caffe.Net(model_define, model_weight, caffe.TEST)
fc_params = {
pr
: (net.params[
pr
][0].data, net.params[
pr
][1].data)
for
pr
in
params}
net_fc = caffe.Net(model_define_fc, model_weight, caffe.TEST)
conv_params = {
pr
: (net_fc.params[
pr
][0].data, net_fc.params[
pr
][1].data)
for
pr
in
params_fc}
for
pr
, pr_conv
in
zip(params, params_fc):
conv_params[pr_conv][0].flat = fc_params[
pr
][0].flat
# flat unrolls the arrays
conv_params[pr_conv][1][...] = fc_params[
pr
][1]
net_fc.save(model_weight_fc)
print
'convert done!'
return
net_fc
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
def face_detection_image(net,net_vf,image_name):
''
'
@检测单张人脸图像
''
'
scales = []
imgs = skimage.io.imread(image_name)
if
imgs.ndim==3:
rows,cols,ch = imgs.shape
else
:
rows,cols = imgs.shape
#计算需要的检测的尺度因子
min = rows
if
rows<=cols
else
cols
max = rows
if
rows>=cols
else
cols
# 放大的尺度
delim = 2500
/max
while
(delim >= 1):
scales.append(delim)
delim=delim-0.5
#缩小的尺度
min = min * factor
factor_count = 1
while
(min >= face_w):
scale = pow(factor, factor_count)
scales.append(scale)
min = min * factor
factor_count += 1
#=========================
#scales.append(1)
total_boxes = []
###显示热图用
num_scale = len(scales)
s1=int(np.sqrt(num_scale))+1
tt=1
plt.subplot(s1, s1+1, tt)
plt.axis(
'off'
)
plt.title(
"Input Image"
)
im=caffe.io.load_image(image_name)
plt.imshow(im)
#============
for
scale
in
scales:
w,h = int(rows* scale),int(cols* scale)
scale_img= tf.resize(imgs,(w,h))
#更改网络输入data图像的大小
net.blobs[
'data'
].reshape(1,channel,w,h)
#转换结构
transformer = caffe.io.Transformer({
'data'
: net.blobs[
'data'
].data.shape})
#transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1))
transformer.set_transpose(
'data'
, (2,0,1))
transformer.set_channel_swap(
'data'
, (2,1,0))
transformer.set_raw_scale(
'data'
, raw_scale)
#前馈一次
out = net.forward_all(data=np.asarray([transformer.preprocess(
'data'
, scale_img)]))
###显示热图用
tt=tt+1
plt.subplot(s1, s1+1, tt)
plt.axis(
'off'
)
plt.title(
"sacle: "
+
"%.2f"
%scale)
plt.imshow(out[
'prob'
][0,map_idx])
#===========
boxes = generateBoundingBox(out[
'prob'
][0,map_idx], scale)
if
(boxes):
total_boxes.extend(boxes)
#非极大值抑制
boxes_nms = np.array(total_boxes)
true_boxes1 = nms_max(boxes_nms, overlapThresh=0.3)
true_boxes = nms_average(np.array(true_boxes1), overlapThresh=0.07)
#===================
plt.savefig(
'heatmap/'
+image_name.
split
(
'/'
)[-1])
#在图像中画出检测到的人脸框
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(imgs)
for
box
in
true_boxes:
im_crop = im[box[0]:box[2],box[1]:box[3],:]
if
im_crop.shape[0] == 0 or im_crop.shape[1] == 0:
continue
if
re_verify(net_vf, im_crop) == True:
rect = mpatches.Rectangle((box[0], box[1]), box[2]-box[0], box[3]-box[1],
fill=False, edgecolor=
'red'
, linewidth=1)
ax.text(box[0], box[1]+20,
"{0:.3f}"
.
format
(box[4]),color=
'white'
, fontsize=6)
ax.add_patch(rect)
plt.savefig(
'result/'
+image_name.
split
(
'/'
)[-1])
plt.close()
return
out[
'prob'
][0,map_idx]
|