Caffe2 - Python API
A deep learning, cross platform ML framework
net_construct_bench.py
1 ## @package net_construct_bench
2 # Module caffe2.experiments.python.net_construct_bench
3 from __future__ import absolute_import
4 from __future__ import division
5 from __future__ import print_function
6 from __future__ import unicode_literals
7 
8 import argparse
9 import logging
10 import time
11 
12 from caffe2.python import workspace, data_parallel_model
13 from caffe2.python import cnn
14 
15 import caffe2.python.models.resnet as resnet
16 
17 '''
18 Simple benchmark that creates a data-parallel resnet-50 model
19 and measurs the time.
20 '''
21 
22 
23 logging.basicConfig()
24 log = logging.getLogger("net_construct_bench")
25 log.setLevel(logging.DEBUG)
26 
27 
28 def AddMomentumParameterUpdate(train_model, LR):
29  '''
30  Add the momentum-SGD update.
31  '''
32  params = train_model.GetParams()
33  assert(len(params) > 0)
34  ONE = train_model.param_init_net.ConstantFill(
35  [], "ONE", shape=[1], value=1.0,
36  )
37  NEGONE = train_model.param_init_net.ConstantFill(
38  [], 'NEGONE', shape=[1], value=-1.0,
39  )
40 
41  for param in params:
42  param_grad = train_model.param_to_grad[param]
43  param_momentum = train_model.param_init_net.ConstantFill(
44  [param], param + '_momentum', value=0.0
45  )
46 
47  # Update param_grad and param_momentum in place
48  train_model.net.MomentumSGD(
49  [param_grad, param_momentum, LR],
50  [param_grad, param_momentum],
51  momentum=0.9,
52  nesterov=1
53  )
54 
55  # Update parameters by applying the moment-adjusted gradient
56  train_model.WeightedSum(
57  [param, ONE, param_grad, NEGONE],
58  param
59  )
60 
61 
62 def Create(args):
63  gpus = list(range(args.num_gpus))
64  log.info("Running on gpus: {}".format(gpus))
65 
66  # Create CNNModeLhelper object
67  train_model = cnn.CNNModelHelper(
68  order="NCHW",
69  name="resnet50",
70  use_cudnn=True,
71  cudnn_exhaustive_search=False
72  )
73 
74  # Model building functions
75  def create_resnet50_model_ops(model, loss_scale):
76  [softmax, loss] = resnet.create_resnet50(
77  model,
78  "data",
79  num_input_channels=3,
80  num_labels=1000,
81  label="label",
82  )
83  model.Accuracy([softmax, "label"], "accuracy")
84  return [loss]
85 
86  # SGD
87  def add_parameter_update_ops(model):
88  model.AddWeightDecay(1e-4)
89  ITER = model.Iter("ITER")
90  stepsz = int(30)
91  LR = model.net.LearningRate(
92  [ITER],
93  "LR",
94  base_lr=0.1,
95  policy="step",
96  stepsize=stepsz,
97  gamma=0.1,
98  )
100 
101  def add_image_input(model):
102  pass
103 
104  start_time = time.time()
105 
106  # Create parallelized model
107  data_parallel_model.Parallelize_GPU(
108  train_model,
109  input_builder_fun=add_image_input,
110  forward_pass_builder_fun=create_resnet50_model_ops,
111  param_update_builder_fun=add_parameter_update_ops,
112  devices=gpus,
113  )
114 
115  ct = time.time() - start_time
116  train_model.net._CheckLookupTables()
117 
118  log.info("Model create for {} gpus took: {} secs".format(len(gpus), ct))
119 
120 
121 def main():
122  # TODO: use argv
123  parser = argparse.ArgumentParser(
124  description="Caffe2: Benchmark for net construction"
125  )
126  parser.add_argument("--num_gpus", type=int, default=1,
127  help="Number of GPUs.")
128  args = parser.parse_args()
129 
130  Create(args)
131 
132 
133 if __name__ == '__main__':
134  workspace.GlobalInit(['caffe2', '--caffe2_log_level=2'])
135  import cProfile
136 
137  cProfile.run('main()', sort="cumulative")
def AddMomentumParameterUpdate(train_model, LR)