forked from SoonminHwang/caffe-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·275 lines (238 loc) · 11.2 KB
/
train.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import argparse
import caffe
from caffe.proto import caffe_pb2
import os
from os.path import dirname, exists, join
import subprocess
import glob, shutil
# import network
import stat
__author__ = 'Soonmin Hwang'
__email__ = '[email protected]'
__description__ = 'This code is a modified version of F.Yus implementation. \
(https://github.com/fyu/dilated.git) '
def make_solver(options):
solver = caffe_pb2.SolverParameter()
# solver.train_net = options.train_net
# # if options.test_net is not None:
# # solver.test_net.append(options.test_net)
# # solver.test_iter.append(50)
# solver.test_interval = 100
# solver.base_lr = options.lr
# solver.lr_policy = "step"
# solver.gamma = 0.1
# # solver.stepsize = 100000
# solver.stepsize = 20000
# solver.display = 5
# # solver.max_iter = 400000
# solver.max_iter = 100000
# solver.momentum = options.momentum
# solver.weight_decay = 0.0005
# solver.regularization_type = 'L2'
# solver.snapshot = 2000
# solver.solver_mode = solver.GPU
# solver.iter_size = options.iter_size
# solver.snapshot_format = solver.BINARYPROTO
# solver.type = 'SGD'
# solver.snapshot_prefix = options.snapshot_prefix
solver.train_net = options.train_net
# if options.test_net is not None:
# solver.test_net.append(options.test_net)
# solver.test_iter.append(50)
solver.test_interval = 100
solver.base_lr = options.lr
solver.lr_policy = "poly"
solver.power = 0.9
# solver.gamma = 0.1
# solver.stepsize = 100000
solver.stepsize = 20000
solver.display = 5
# solver.max_iter = 400000
solver.max_iter = 20000
solver.momentum = 0.9
solver.weight_decay = 0.0001
solver.regularization_type = 'L2'
solver.snapshot = 5000
solver.solver_mode = solver.GPU
solver.iter_size = options.iter_size
solver.snapshot_format = solver.BINARYPROTO
solver.type = 'SGD'
solver.snapshot_prefix = options.snapshot_prefix
return solver
def process_options(options):
# assert (options.crop_size - 372) % 8 == 0, \
# "The crop size must be a multiple of 8 after removing the margin"
assert len(options.mean) == 3
# assert options.model == 'context' or options.weights is not None, \
# 'Pretrained weights are required for frontend and joint training.'
# assert options.model != 'context' or \
# (options.label_shape is not None and
# len(options.label_shape) == 2), \
# 'Please specify the height and weight of label images ' \
# 'for computing the loss.'
# assert exists(options.train), options.train + 'does not exist'
# assert exists(options.test), options.test + 'does not exist'
# if options.model == 'frontend':
# options.model += '_vgg'
# work_dir = "jobs/{}/{}/".format(options.dataset, options.model)
post_fix = '_' + options.post_fix if not options.post_fix == '' else ''
# work_dir = join('jobs', options.dataset, options.model + post_fix)
work_dir = join('jobs', options.dataset, options.model + post_fix)
options.work_dir = work_dir
# work_dir = options.work_dir
# model = options.model
if not exists(work_dir):
print('Creating working directory', work_dir)
os.makedirs(work_dir)
assert exists(options.train_net), options.train_net + 'does not exist'
shutil.copy(options.train_net, join(work_dir, 'train.prototxt'))
# options.train_net = join(work_dir, model + '_train_net.txt')
# if options.test_batch > 0:
# options.test_net = join(work_dir, model + '_test_net.txt')
# else:
# options.test_net = None
# options.solver_path = join(work_dir, model + '_solver.txt')
options.solver_path = join(work_dir, 'solver.txt')
snapshot_dir = join(work_dir, 'snapshots')
if not exists(snapshot_dir):
os.makedirs(snapshot_dir)
options.snapshot_prefix = join(snapshot_dir, options.model + post_fix)
# if options.up:
# options.label_stride = 1
# else:
# options.label_stride = 8
if options.lr == 0:
options.lr = 0.001
# if options.lr == 0:
# if options.model == 'frontend_vgg':
# options.lr = 0.0001
# elif options.model == 'context':
# options.lr = 0.001
# elif options.model == 'joint':
# options.lr = 0.00001
# if options.momentum == 0:
# options.momentum = 0.9
return options
def train(options):
job_dir = options.work_dir
job_file = "{}/train.sh".format(job_dir)
max_iter = 0
snapshot_dir = "{}/snapshots".format(job_dir)
if options.resume:
post_fix = '_' + options.post_fix if not options.post_fix == '' else ''
# Find most recent snapshot.
for file in os.listdir(snapshot_dir):
if file.endswith(".solverstate"):
basename = os.path.splitext(file)[0]
iter = int(basename.split("{}_iter_".format(options.model + post_fix))[1])
if iter > max_iter:
max_iter = iter
train_src_param = '--weights="{}" \\\n'.format(options.weights)
if options.resume:
if max_iter > 0:
train_src_param = '--snapshot="{}/{}_iter_{}.solverstate" \\\n'.format(snapshot_dir, options.model+post_fix, max_iter)
# Create job file.
with open(job_file, 'w') as f:
f.write('{} train \\\n'.format(options.caffe))
f.write('--solver="{}" \\\n'.format(options.solver_path))
f.write(train_src_param)
if options.resume:
f.write('--gpu {} 2>&1 | tee -a {}/train_{}.log\n'.format(options.gpu, job_dir, options.model))
else:
f.write('--gpu {} 2>&1 | tee {}/train_{}.log\n'.format(options.gpu, job_dir, options.model))
# parsed_log_file = glob.glob("{}*.train".format(job_dir))
# if len(parsed_log_file) > 0 and os.path.exists(parsed_log_file[0]):
# old_dir = "{}old_log".format(job_dir)
# if not os.path.exists(old_dir):
# os.makedirs(old_dir)
# shutil.copy(parsed_log_file[0], old_dir)
# os.remove(parsed_log_file[0])
# parsed_log_file = glob.glob("{}*.test".format(job_dir))
# if len(parsed_log_file) > 0 and os.path.exists(parsed_log_file[0]):
# old_dir = "{}old_log".format(job_dir)
# if not os.path.exists(old_dir):
# os.makedirs(old_dir)
# shutil.copy(parsed_log_file[0], old_dir)
# os.remove(parsed_log_file[0])
os.chmod(job_file, stat.S_IRWXU)
subprocess.call(job_file, shell=True)
def main():
parser = argparse.ArgumentParser()
# parser.add_argument('model', nargs='?',
# choices=['frontend', 'context', 'joint', 'joint_bn'])
parser.add_argument('--model', default='DeepLabv2')
parser.add_argument('--caffe', default='caffe',
help='Path to the caffe binary compiled from '
'https://github.com/fyu/caffe-dilation.')
parser.add_argument('--dataset', type=str, default='pascal_voc',
help='DB name for creating job directory')
parser.add_argument('--post_fix', type=str, default='default',
help='Post fix for creating job directory')
parser.add_argument('--resume', action='store_true', default=False,
help='If true, resume training from latest solverstate.')
parser.add_argument('--train_net', type=str, required=True,
help='Prototxt file for train_net')
parser.add_argument('--weights', default=None,
help='Path to the weights to initialize the model.')
parser.add_argument('--mean', nargs='*', type=float,
default=[102.93, 111.36, 116.52],
help='Mean pixel value (BGR) for the dataset.\n'
'Default is the mean pixel of PASCAL dataset.')
# parser.add_argument('--work_dir', default='training/',
# help='Working dir for training.\nAll the generated '
# 'network and solver configurations will be '
# 'written to this directory, in addition to '
# 'training snapshots.')
# parser.add_argument('--train_set', default='', type=str, required=True,
# help='List file containing image & label paths')
# parser.add_argument('--train_image', default='', required=True,
# help='Path to the training image list')
# parser.add_argument('--train_label', default='', required=True,
# help='Path to the training label list')
# parser.add_argument('--test_image', default='',
# help='Path to the testing image list')
# parser.add_argument('--test_label', default='',
# help='Path to the testing label list')
# parser.add_argument('--train_batch', type=int, default=8,
# help='Training batch size.')
# parser.add_argument('--test_batch', type=int, default=2,
# help='Testing batch size. If it is 0, no test phase.')
# parser.add_argument('--crop_size', type=int, default=500)
parser.add_argument('--lr', type=float, default=0,
help='Solver SGD learning rate')
parser.add_argument('--momentum', type=float, default=0.9,
help='Gradient momentum')
parser.add_argument('--classes', type=int, required=True,
help='Number of categories in the data')
parser.add_argument('--gpu', type=str, default='0',
help='GPU index for training')
# parser.add_argument('--up', action='store_true',
# help='If true, upsampling the final feature map '
# 'before calculating the loss or accuracy')
# parser.add_argument('--layers', type=int, default=8,
# help='Used for training context module.\n'
# 'Number of layers in the context module.')
# parser.add_argument('--label_shape', nargs='*', type=int,
# help='Used for training context module.\n' \
# 'The dimensions of labels for the loss function.')
parser.add_argument('--iter_size', type=int, default=1,
help='Number of passes/batches in each iteration.')
options = process_options(parser.parse_args())
# train_net, test_net = make_nets(options)
solver = make_solver(options)
# print('Writing', options.train_net)
# with open(options.train_net, 'w') as fp:
# fp.write(str(train_net))
# if test_net is not None:
# print('Writing', options.test_net)
# with open(options.test_net, 'w') as fp:
# fp.write(str(test_net))
print('Writing', options.solver_path)
with open(options.solver_path, 'w') as fp:
fp.write(str(solver))
train(options)
if __name__ == '__main__':
main()