Comments (3)
The SSD is https://github.com/conner99/caffe. The train_voc.py is:`from future import print_function
import argparse
import caffe
from caffe import layers as L
from caffe import params as P
from caffe.proto import caffe_pb2
from caffe.model_libs import CreateAnnotatedDataLayer, check_if_exist, make_if_not_exist
from feature_extractor import CreateMultiBoxHead
from google.protobuf import text_format
from feature_extractor import VGG_RUN, VGG_SSD, Pelee
import math
import os
import shutil
import stat
import subprocess
import sys
model_meta = {
'pelee':Pelee,
'ssd':VGG_SSD,
'run':VGG_RUN
}
Default_weights_file = "F:/data/people/pelee_voc/models/peleenet_inet_acc7243.caffemodel"
parser = argparse.ArgumentParser(description='Pelee Training')
parser.add_argument('--arch', '-a', metavar='ARCH', default='pelee',
choices=model_meta.keys(),
help='model architecture: ' +
' | '.join(model_meta.keys()) +
' (default: pelee)')
parser.add_argument('--lr', '--learning-rate', default=0.005, type=float,
metavar='LR', help='initial learning rate (default: 0.005)')
parser.add_argument('--weight-decay', '--wd', default=0.0005, type=float,
metavar='W', help='weight decay (default: 5e-4)')
parser.add_argument('-b', '--batch-size', default=32, type=int,
metavar='N', help='mini-batch size (default: 32)')
parser.add_argument('-k', '--kernel-size', default=1, type=int,
metavar='K', help='kernel size for CreateMultiBoxHead (default: 1)')
parser.add_argument('--image-size', default=304, type=int,
metavar='IMAGE_SIZE', help='image size (default: 304)')
parser.add_argument('--weights', default=Default_weights_file, type=str,
metavar='WEIGHTS', help='initial weights file (default: {})'.format(Default_weights_file))
parser.add_argument('--run-later', dest='run_soon', action='store_false',
help='start training later after generating all files')
parser.add_argument('--step-value', '-s', nargs='+', type=int, default=[80000, 100000, 120000],
metavar='S', help='step value (default: [80000, 100000, 120000])')
parser.add_argument('--posfix', '-p', metavar='POSFIX', default='', type=str)
parser.set_defaults(run_soon=True)
args = parser.parse_args()
print( 'args:',args)
NetBuilder = model_meta[args.arch]
mbox_source_layers = NetBuilder.mbox_source_layers
model_prefix = '{}{}'.format(args.arch, args.posfix)
Modify the following parameters accordingly
The directory which contains the caffe code.
We assume you are running the script at the CAFFE_ROOT.
caffe_root = os.getcwd()
Set true if you want to start training right after generating all files.
run_soon = True
run_soon = args.run_soon
Set true if you want to load from most recently saved snapshot.
Otherwise, we will load from the pretrain_model defined below.
resume_training = True
If true, Remove old model files.
remove_old_models = False
pretrain_model = args.weights
The database file for training data. Created by data/VOC0712/create_data.sh
train_data = "F:/data/people/mydata_trainval_lmdb"
The database file for testing data. Created by data/VOC0712/create_data.sh
test_data = "F:/data/people/mydata_test_lmdb"
Specify the batch sampler.
resize_width = args.image_size
resize_height = args.image_size
resize = "{}x{}".format(resize_width, resize_height)
batch_sampler = [
{
'sampler': {
},
'max_trials': 1,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'min_jaccard_overlap': 0.1,
},
'max_trials': 50,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'min_jaccard_overlap': 0.3,
},
'max_trials': 50,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'min_jaccard_overlap': 0.5,
},
'max_trials': 50,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'min_jaccard_overlap': 0.7,
},
'max_trials': 50,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'min_jaccard_overlap': 0.9,
},
'max_trials': 50,
'max_sample': 1,
},
{
'sampler': {
'min_scale': 0.3,
'max_scale': 1.0,
'min_aspect_ratio': 0.5,
'max_aspect_ratio': 2.0,
},
'sample_constraint': {
'max_jaccard_overlap': 1.0,
},
'max_trials': 50,
'max_sample': 1,
},
]
train_transform_param = {
'mirror': True,
'scale': 0.017,
'mean_value': [103.94,116.78,123.68],
'resize_param': {
'prob': 1,
'resize_mode': P.Resize.WARP,
'height': resize_height,
'width': resize_width,
'interp_mode': [
P.Resize.LINEAR,
P.Resize.AREA,
P.Resize.NEAREST,
P.Resize.CUBIC,
P.Resize.LANCZOS4,
],
},
'emit_constraint': {
'emit_type': caffe_pb2.EmitConstraint.CENTER,
}
}
test_transform_param = {
'scale': 0.017,
'mean_value': [103.94,116.78,123.68],
'resize_param': {
'prob': 1,
'resize_mode': P.Resize.WARP,
'height': resize_height,
'width': resize_width,
'interp_mode': [P.Resize.LINEAR],
},
}
Modify the job name if you want.
job_name = "SSD_{}".format(resize)
The name of the model. Modify it if you want.
model_name = "{}_{}".format(model_prefix, job_name)
Directory which stores the model .prototxt file.
save_dir = "F:/data/people/pelee_voc/models/{}".format(job_name)
Directory which stores the snapshot of models.
snapshot_dir = "F:/data/people/pelee_voc/models/{}".format(job_name)
Directory which stores the job script and log file.
job_dir = "F:/work/caffe-ssd-microsoft_peleenet/jobs/{}".format(job_name)
Directory which stores the detection results.
output_result_dir = "{}/Main".format( job_name)
model definition files.
train_net_file = "{}/train.prototxt".format(save_dir)
test_net_file = "{}/test.prototxt".format(save_dir)
deploy_net_file = "{}/deploy.prototxt".format(save_dir)
solver_file = "{}/solver.prototxt".format(save_dir)
snapshot prefix.
snapshot_prefix = "{}/{}".format(snapshot_dir, model_name)
job script path.
##job_file = "{}/{}.sh".format(job_dir, model_name)
job_file = "{}/{}_train.bat".format(job_dir, model_name)
Stores the test image names and sizes. Created by data/VOC0712/create_list.sh
name_size_file = "F:/data/people/test_name_size.txt"
pretrain_model = "F:/data/people/pelee_voc/models/pelee_304x304_acc7094.caffemodel"
Stores LabelMapItem.
label_map_file = "F:/data/people/labelmap_mydata.prototxt"
MultiBoxLoss parameters.
num_classes = 2
share_location = True
background_label_id=0
train_on_diff_gt = True
normalization_mode = P.Loss.VALID
code_type = P.PriorBox.CENTER_SIZE
neg_pos_ratio = 3.
loc_weight = (neg_pos_ratio + 1.) / 4.
mining_type = 1 ##P.MultiBoxLoss.MAX_NEGATIVE
multibox_loss_param = {
'loc_loss_type': P.MultiBoxLoss.SMOOTH_L1,
'conf_loss_type': P.MultiBoxLoss.SOFTMAX,
'loc_weight': loc_weight,
'num_classes': num_classes,
'share_location': share_location,
'match_type': P.MultiBoxLoss.PER_PREDICTION,
'overlap_threshold': 0.5,
'use_prior_for_matching': True,
'background_label_id': background_label_id,
'use_difficult_gt': train_on_diff_gt,
'mining_type': mining_type,
'neg_pos_ratio': neg_pos_ratio,
'neg_overlap': 0.5,
'code_type': code_type,
}
loss_param = {
'normalization': normalization_mode,
}
parameters for generating priors.
minimum dimension of input image
min_dim = args.image_size
in percent %
min_ratio = 15 ###20
max_ratio = 95 ##90
step = int(math.floor((max_ratio - min_ratio) / (len(mbox_source_layers) - 2)))
min_sizes = []
max_sizes = []
for ratio in xrange(min_ratio, max_ratio + 1, step):
min_sizes.append(min_dim * ratio / 100.)
max_sizes.append(min_dim * (ratio + step) / 100.)
min_sizes = [min_dim * 7 / 100.] + min_sizes #10
max_sizes = [min_dim * 20 / 100.] + max_sizes
aspect_ratios = [[2,3], [2, 3], [2, 3], [2, 3], [2,3], [2,3]]
normalizations = None #[-1, -1, -1, -1, -1, -1]
variance used to encode/decode prior bboxes.
if code_type == P.PriorBox.CENTER_SIZE:
prior_variance = [0.1, 0.1, 0.2, 0.2]
else:
prior_variance = [0.1]
flip = True
clip = False
Solver parameters.
Defining which GPUs to use.
gpus = "0"
gpulist = gpus.split(",")
num_gpus = len(gpulist)
Divide the mini-batch to different GPUs.
batch_size = 4 ####32
accum_batch_size = args.batch_size
iter_size = accum_batch_size / batch_size
solver_mode = P.Solver.CPU
device_id = 0
batch_size_per_device = batch_size
if num_gpus > 0:
batch_size_per_device = int(math.ceil(float(batch_size) / num_gpus))
iter_size = int(math.ceil(float(accum_batch_size) / (batch_size_per_device * num_gpus)))
solver_mode = P.Solver.GPU
device_id = int(gpulist[0])
Evaluate on whole test set.
num_test_image = 4952
test_batch_size = 1
test_iter = num_test_image / test_batch_size
solver_param = {
'base_lr': args.lr,
'weight_decay': args.weight_decay,
'lr_policy': "multistep",
'stepvalue': args.step_value,
'gamma': 0.1,
'momentum': 0.9,
'iter_size': iter_size,
'max_iter': args.step_value[-1],
'snapshot': 2000,
'display': 10,
'average_loss': 10,
'type': "SGD",
'solver_mode': solver_mode,
'device_id': device_id,
'debug_info': False,
'snapshot_after_train': True,
# Test parameters
'test_iter': [test_iter],
'test_interval': 2000,
'eval_type': "detection",
'ap_version': "11point",
'test_initialization': False,
}
parameters for generating detection output.
det_out_param = {
'num_classes': num_classes,
'share_location': share_location,
'background_label_id': background_label_id,
'nms_param': {'nms_threshold': 0.45, 'top_k': 400},
'save_output_param': {
'output_directory': output_result_dir,
'output_name_prefix': "comp4_det_test_",
'output_format': "VOC",
'label_map_file': label_map_file,
'name_size_file': name_size_file,
'num_test_image': num_test_image,
},
'keep_top_k': 200,
'confidence_threshold': 0.01,
'code_type': code_type,
}
parameters for evaluating detection results.
det_eval_param = {
'num_classes': num_classes,
'background_label_id': background_label_id,
'overlap_threshold': 0.5,
'evaluate_difficult_gt': False,
'name_size_file': name_size_file,
}
Hopefully you don't need to change the following
Check file.
check_if_exist(train_data)
check_if_exist(test_data)
check_if_exist(label_map_file)
check_if_exist(pretrain_model)
make_if_not_exist(save_dir)
make_if_not_exist(job_dir)
make_if_not_exist(snapshot_dir)
Create train net.
net = caffe.NetSpec()
net.data, net.label = CreateAnnotatedDataLayer(train_data, batch_size=batch_size_per_device,
train=True, output_label=True, label_map_file=label_map_file,
transform_param=train_transform_param, batch_sampler=batch_sampler)
NetBuilder(net, from_layer='data')
Don't use batch norm for location/confidence prediction layers.
mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers,
use_batchnorm=False, min_sizes=min_sizes, max_sizes=max_sizes, normalizations=normalizations,
aspect_ratios=aspect_ratios, num_classes=num_classes, share_location=share_location,
flip=flip, clip=clip, prior_variance=prior_variance, kernel_size=args.kernel_size, pad=int(args.kernel_size/2))
Create the MultiBoxLossLayer.
name = "mbox_loss"
mbox_layers.append(net.label)
net[name] = L.MultiBoxLoss(*mbox_layers, multibox_loss_param=multibox_loss_param,
loss_param=loss_param, include=dict(phase=caffe_pb2.Phase.Value('TRAIN')),
propagate_down=[True, True, False, False])
with open(train_net_file, 'w') as f:
print('name: "{}_train"'.format(model_name), file=f)
print(net.to_proto(), file=f)
shutil.copy(train_net_file, job_dir)
Create test net.
net = caffe.NetSpec()
net.data, net.label = CreateAnnotatedDataLayer(test_data, batch_size=test_batch_size,
train=False, output_label=True, label_map_file=label_map_file,
transform_param=test_transform_param)
NetBuilder(net, from_layer='data')
Don't use batch norm for location/confidence prediction layers.
mbox_layers = CreateMultiBoxHead(net, data_layer='data', from_layers=mbox_source_layers,
use_batchnorm=False, min_sizes=min_sizes, max_sizes=max_sizes,normalizations=normalizations,
aspect_ratios=aspect_ratios, num_classes=num_classes, share_location=share_location,
flip=flip, clip=clip, prior_variance=prior_variance, kernel_size=args.kernel_size, pad=int(args.kernel_size/2))
conf_name = "mbox_conf"
if multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.SOFTMAX:
reshape_name = "{}_reshape".format(conf_name)
net[reshape_name] = L.Reshape(net[conf_name], shape=dict(dim=[0, -1, num_classes]))
softmax_name = "{}_softmax".format(conf_name)
net[softmax_name] = L.Softmax(net[reshape_name], axis=2)
flatten_name = "{}_flatten".format(conf_name)
net[flatten_name] = L.Flatten(net[softmax_name], axis=1)
mbox_layers[1] = net[flatten_name]
elif multibox_loss_param["conf_loss_type"] == P.MultiBoxLoss.LOGISTIC:
sigmoid_name = "{}_sigmoid".format(conf_name)
net[sigmoid_name] = L.Sigmoid(net[conf_name])
mbox_layers[1] = net[sigmoid_name]
net.detection_out = L.DetectionOutput(*mbox_layers,
detection_output_param=det_out_param,
include=dict(phase=caffe_pb2.Phase.Value('TEST')))
net.detection_eval = L.DetectionEvaluate(net.detection_out, net.label,
detection_evaluate_param=det_eval_param,
include=dict(phase=caffe_pb2.Phase.Value('TEST')))
with open(test_net_file, 'w') as f:
print('name: "{}_test"'.format(model_name), file=f)
print(net.to_proto(), file=f)
shutil.copy(test_net_file, job_dir)
Create deploy net.
Remove the first and last layer from test net.
deploy_net = net
with open(deploy_net_file, 'w') as f:
net_param = deploy_net.to_proto()
# Remove the first (AnnotatedData) and last (DetectionEvaluate) layer from test net.
del net_param.layer[0]
del net_param.layer[-1]
net_param.name = '{}_deploy'.format(model_name)
net_param.input.extend(['data'])
net_param.input_shape.extend([
caffe_pb2.BlobShape(dim=[1, 3, resize_height, resize_width])])
print(net_param, file=f)
shutil.copy(deploy_net_file, job_dir)
# Create deploy net for CoreML.
coreml_deploy_net_file = "{}/coreml_deploy.prototxt".format(save_dir)
net = caffe.NetSpec()
net.data, net.label = CreateAnnotatedDataLayer(test_data, batch_size=test_batch_size,
train=False, output_label=True, label_map_file=label_map_file,
transform_param=test_transform_param)
NetBuilder(net, from_layer='data')
conf_layers, loc_layers = CreateMultiBoxHeadForCoreML(net, data_layer='data', from_layers=mbox_source_layers,
use_batchnorm=use_batchnorm, min_sizes=min_sizes, max_sizes=max_sizes,normalizations=normalizations,
aspect_ratios=aspect_ratios, num_classes=num_classes, share_location=share_location,
flip=flip, clip=clip, prior_variance=prior_variance, kernel_size=args.kernel_size, pad=int(args.kernel_size/2))
with open(coreml_deploy_net_file, 'w') as f:
net_param = net.to_proto()
# Remove the first (AnnotatedData) from test net.
del net_param.layer[0]
net_param.name = '{}_{}_coreml'.format(model_prefix,resize)
net_param.input.extend(['data'])
net_param.input_shape.extend([
caffe_pb2.BlobShape(dim=[1, 3, resize_height, resize_width])])
print(net_param, file=f)
shutil.copy(deploy_net_file, job_dir)
Create solver.
solver = caffe_pb2.SolverParameter(
train_net=train_net_file,
test_net=[test_net_file],
snapshot_prefix=snapshot_prefix,
**solver_param)
with open(solver_file, 'w') as f:
print(solver, file=f)
shutil.copy(solver_file, job_dir)
max_iter = 0
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(model_name))[1])
if iter > max_iter:
max_iter = iter
train_src_param = '--weights="{}" \\n'.format(pretrain_model)
if resume_training:
if max_iter > 0:
train_src_param = '--snapshot="{}iter{}.solverstate" \\n'.format(snapshot_prefix, max_iter)
if remove_old_models:
Remove any snapshots smaller than max_iter.
for file in os.listdir(snapshot_dir):
if file.endswith(".solverstate"):
basename = os.path.splitext(file)[0]
iter = int(basename.split("{}iter".format(model_name))[1])
if max_iter > iter:
os.remove("{}/{}".format(snapshot_dir, file))
if file.endswith(".caffemodel"):
basename = os.path.splitext(file)[0]
iter = int(basename.split("{}iter".format(model_name))[1])
if max_iter > iter:
os.remove("{}/{}".format(snapshot_dir, file))
Create job file.
with open(job_file, 'w') as f:
f.write('cd {}\n'.format(caffe_root))
f.write('./build/tools/caffe train \\n')
f.write('--solver="{}" \\n'.format(solver_file))
f.write(train_src_param)
if solver_param['solver_mode'] == P.Solver.GPU:
f.write('--gpu {} 2>&1 | tee {}/{}.log\n'.format(gpus, job_dir, model_name))
else:
f.write('2>&1 | tee {}/{}.log\n'.format(job_dir, model_name))
Copy the python script to job_dir.
py_file = os.path.abspath(file)
shutil.copy(py_file, job_dir)
# # Run the job.
os.chmod(job_file, stat.S_IRWXU)
if run_soon:
subprocess.call(job_file, shell=True)
`
from pelee.
You should clone weiliu's ssd-caffe version.
from pelee.
@ujsyehao Thanks! I will try it.
from pelee.
Related Issues (20)
- fine tuning with different number of classes HOT 1
- peele-SSD add_extra_layers_pelee
- About the stanford dog dataset. HOT 3
- 2-way dense layer in code and paper seems mismatch. HOT 2
- how can i get the fps=120 on nvidia tx2? please help me HOT 1
- Calculation of number of parameter, macc, and flops HOT 3
- pytorch pretained model
- max_iter
- Does it support 512 or bigger input size? HOT 1
- question for iteration HOT 1
- can not download the pretrained PeleeNet model
- one question
- Question for peleeNet structure
- How can I train my own model?
- train error
- peleeNet speed in GTX1080ti HOT 1
- Question about 1x1 convolutional kernels to reduce computational cost
- the paper was accepted two years ago???
- Pelee input resolution problem
- question about merge bn?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pelee.