-
Notifications
You must be signed in to change notification settings - Fork 10
/
DeepID.py
307 lines (231 loc) · 10.4 KB
/
DeepID.py
1
2
3
4
5
6
7
8
9
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
Create on Tues 2015-11-24
@author: hqli
'''
#运行前,先将caffe安装好,放在~/caffe-mater
import os
class DeepID():
#基本属性
prj=''#项目名称
caffepath=''#caffe主目录路径
prjpath=''#工程的位置
datapath=''#数据的路径
num=0#人数多少
# ratio=0#训练数:测试
# max_iter=0#迭代次数(用于lfw验证)
# snapshot=0#迭代多少次保存一次
data_train=''#用于训练的数据列表
data_test=''#用于测试的数据列表
net_proto=''#caffe的Net
net_proto_model=''
solver_proto=''#caffe的solver
solver_proto_model=''
deploy_proto=''#caffe的deploy
snapshot_pre=''#训练好的模型的前缀
lmdb_train=''#训练集的lmdb
lmdb_test=''#测试集的lmdb
imgmean=''#图像均值
netimg=''#net的结构图像
log_train=''#caffe的训练日志
log_test=''#caffe的测试日志
log_create=''#转换lmdb的日志
#可执行文件
shcreate=''#将图像和标签转换成lmdb格式
shimgmean=''#计算图像均值
shdrawnet=''#画Net结构图
shtrain=''#训练
shtest=''#测试
def __init__(self,prj,caffepath,prjpath,datapath,num):
self.prj=prj
self.caffepath=caffepath
self.prjpath=self.prjpath
self.datapath=datapath
self.num=num
# self.ratio=ratio
# self.itera=max_iter
# self.snapshot=snapshot
#在prjpath下新建名为num的文件夹,将配置以及生成的文件放在该文件夹下
#在prjpath下新建num文件夹
if not os.path.exists(prjpath+str(num)):
os.makedirs(prjpath+str(num))
self.net_proto_model=prjpath+prj+'_train_test.prototxt'
self.solver_proto_model=prjpath+prj+'_solver.prototxt'
self.netimg=prjpath+prj+'_net.png'
self.shdrawnet=prjpath+prj+'_drawnet.sh'#画Net结构图
rspath=prjpath+str(num)+'/'
strnum='_'+str(num)+'_'
self.data_train=rspath+prj+'_train_'+str(num)+'.txt'
self.data_test=rspath+prj+'_val_'+str(num)+'.txt'
self.net_proto=rspath+prj+strnum+'train_test.prototxt'
self.solver_proto=rspath+prj+strnum+'solver.prototxt'
self.deploy_proto=rspath+prj+'_deploy.prototxt'
self.snapshot_pre=rspath+prj+'_'+str(num)
self.lmdb_train=rspath+prj+strnum+'train_lmdb'
self.lmdb_test=rspath+prj+strnum+'test_lmdb'
self.imgmean=rspath+prj+strnum+'mean.binaryproto'
self.log_train=rspath+prj+strnum+'train.log'
self.log_test=rspath+prj+strnum+'test.log'
self.log_create=rspath+prj+strnum+'create.log'
#可执行文件
self.shcreate=rspath+prj+strnum+'create.sh'#将图像和标签转换成lmdb格式
self.shimgmean=rspath+prj+strnum+'imgmean.sh'#计算图像均值
self.shtrain=rspath+prj+strnum+'train.sh'#训练deepID
self.shtest=rspath+prj+strnum+'test.sh'#测试deepID
@staticmethod
def fileopt(filename,content):
fp=open(filename,'w')
fp.write(content)
fp.close()
def div_data(self,ratio):
'''
@brief: 将数据集分为训练集和测试集,保存在prjpath下
'''
# if not os.path.exists(savepath):
# os.makedirs(savepath)
dirlists=os.listdir(self.datapath)
dict_id_num={}
for subdir in dirlists:
dict_id_num[subdir]=len(os.listdir(os.path.join(self.datapath,subdir)))
#sorted(dict_id_num.items, key=lambda dict_id_num:dict_id_num[1])
sorted_num_id=sorted([(v, k) for k, v in dict_id_num.items()], reverse=True)
select_ids=sorted_num_id[0:self.num]
fid_train=open(self.data_train,'w')
fid_test=open(self.data_test,'w')
pid=0
for select_id in select_ids:
subdir=select_id[1]
filenamelist=os.listdir(os.path.join(self.datapath,subdir))
num=1
for filename in filenamelist :
#print select_ids[top_num-1]
if num>select_ids[self.num-1][0]:
break
if num%ratio!=0:
fid_train.write(os.path.join(subdir,filename)+'\t'+str(pid)+'\n')
else:
fid_test.write(os.path.join(subdir,filename)+'\t'+str(pid)+'\n')
num=num+1
pid=pid+1
fid_train.close()
fid_test.close()
def create(self,height,width):
'''
@brief :
@param :
'''
_command='rm -rf '+self.lmdb_train+'\n'
_command+='rm -rf '+self.lmdb_train+'\n'
_command+='echo "Creating train lmdb..."\n'
_command+=self.caffepath+'build/tools/convert_imageset '
_command+='--resize_height='+str(height)+' '
_command+='--resize_width='+str(width)+' '
_command+='--shuffle '
_command+='--backend="lmdb" '
_command+=self.datapath+' '
_command+=self.data_train+' '
_command+=self.lmdb_train
out=r' 2>&1 |tee '+self.log_create
_command+=out+'\n'
_command+='echo "Creating train lmdb..."\n'
_command+=self.caffepath+'build/tools/convert_imageset '
_command+='--resize_height='+str(height)+' '
_command+='--resize_width='+str(width)+' '
_command+='--shuffle '
_command+='--backend="lmdb" '
_command+=self.datapath+' '
_command+=self.data_test+' '
_command+=self.lmdb_test
out=r' 2>&1 |tee -a '+self.log_create
_command+=out+'\n'
_command+='echo "Done"'
DeepID.fileopt(self.shcreate,_command)
os.system(_command)
def compute_imgmean(self):
_command=self.caffepath+'build/tools/compute_image_mean '+self.lmdb_train+' '+self.imgmean
DeepID.fileopt(self.shimgmean,_command)
os.system(_command)
def draw_net(self):
_command='python '+self.caffepath+'python/draw_net.py '+self.net_proto_model+' '+self.netimg
DeepID.fileopt(self.shdrawnet,_command)
os.system(_command)
def train(self):
#将project_train_test.prototxt中lmdb和imgmean替换
#重新写入到num文件夹下/project_num_train_test.prototxt
lmdb_train=self.lmdb_train.replace('/','\\/')
lmdb_test=self.lmdb_test.replace('/','\\/')
imgmean=self.imgmean.replace('/','\\/')
lmdb_train=' source: \\\"'+lmdb_train+'\\\"'
lmdb_test=' source: \\\"'+lmdb_test+'\\\"'
imgmean=' mean_file: \\\"'+imgmean+'\\\"'
print lmdb_train
num_train=9
num_train_mean=14
num_test=26
num_test_mean=31
_command='cp '+self.net_proto_model+' '+self.net_proto+'\n'
_command+='cp '+self.solver_proto_model+' '+self.solver_proto+'\n'
_command+='sed -i -e \"'+str(num_train)+'c\\'+lmdb_train+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_train_mean)+'c\\'+imgmean+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_test)+'c\\'+lmdb_test+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_test_mean)+'c\\'+imgmean+'\" '+self.net_proto+'\n'
num_net=1
num_snapshot_pre=15
net_proto=self.net_proto.replace('/','\\/')
snapshot_pre=self.snapshot_pre.replace('/','\\/')
net_proto='net: \\\"'+net_proto+'\\\"'
snapshot_pre='snapshot_prefix: \\\"'+snapshot_pre+'\\\"'
_command+='sed -i -e \"'+str(num_net)+'c\\'+net_proto+'\" '+self.solver_proto+'\n'
_command+='sed -i -e \"'+str(num_snapshot_pre)+'c\\'+snapshot_pre+'\" '+self.solver_proto+'\n'
out=r' 2>&1 |tee '
_command+=self.caffepath+'/build/tools/caffe train '+'--solver='+self.solver_proto+' '+out+self.log_train
DeepID.fileopt(self.shtrain,_command)
os.system(_command)
def resume(self,num):
#将project_train_test.prototxt中lmdb和imgmean替换
#重新写入到num文件夹下/project_num_train_test.prototxt
lmdb_train=self.lmdb_train.replace('/','\\/')
lmdb_test=self.lmdb_test.replace('/','\\/')
imgmean=self.imgmean.replace('/','\\/')
lmdb_train=' source: \\\"'+lmdb_train+'\\\"'
lmdb_test=' source: \\\"'+lmdb_test+'\\\"'
imgmean=' mean_file: \\\"'+imgmean+'\\\"'
print lmdb_train
num_train=9
num_train_mean=14
num_test=26
num_test_mean=31
_command='cp '+self.net_proto_model+' '+self.net_proto+'\n'
_command+='cp '+self.solver_proto_model+' '+self.solver_proto+'\n'
_command+='sed -i -e \"'+str(num_train)+'c\\'+lmdb_train+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_train_mean)+'c\\'+imgmean+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_test)+'c\\'+lmdb_test+'\" '+self.net_proto+'\n'
_command+='sed -i -e \"'+str(num_test_mean)+'c\\'+imgmean+'\" '+self.net_proto+'\n'
num_net=1
num_snapshot_pre=15
net_proto=self.net_proto.replace('/','\\/')
snapshot_pre=self.snapshot_pre.replace('/','\\/')
net_proto='net: \\\"'+net_proto+'\\\"'
snapshot_pre='snapshot_prefix: \\\"'+snapshot_pre+'\\\"'
_command+='sed -i -e \"'+str(num_net)+'c\\'+net_proto+'\" '+self.solver_proto+'\n'
_command+='sed -i -e \"'+str(num_snapshot_pre)+'c\\'+snapshot_pre+'\" '+self.solver_proto+'\n'
out=r' 2>&1 |tee '
_command+=self.caffepath+'/build/tools/caffe train '+'--solver='+self.solver_proto+' '+out+self.log_train
DeepID.fileopt(self.shtrain,_command)
def test(self,iternum):
out=r' 2>&1 |tee '
_command=self.caffepath+'/build/tools/caffe test --model='+self.net_proto+' --weights='+self.snapshot_pre+'_iter_'+str(iternum)+'.caffemodel'+out+log_test
DeepID.fileopt(self.shtest,_command)
os.system(_command)
def demo(num):
deepID=DeepID('deepID','/home/ikode/caffe-master/','/home/ikode/caffe-master/examples/deepID/','/media/ikode/Document/big_materials/document/deep_learning/caffe/face_datasets/webface/croped/',num)
ratio=9
deepID.div_data(ratio)
deepID.create(64,64)
deepID.compute_imgmean()
# deepID.draw_net()
deepID.train()
if __name__=='__main__':
demo(1000)
#demo后面的数字是训练的人数(1-10575)