Caffe2 - Python API
A deep learning, cross platform ML framework
batch_normalization.py
1 from __future__ import absolute_import
2 from __future__ import division
3 from __future__ import print_function
4 from __future__ import unicode_literals
5 
6 from caffe2.python import schema
7 from caffe2.python.layers.layers import ModelLayer
8 
9 import numpy as np
10 
11 
13  def __init__(
14  self,
15  model,
16  input_record,
17  name='batch_normalization',
18  scale_optim=None,
19  bias_optim=None,
20  momentum=0.9,
21  order='NCHW',
22  **kwargs
23  ):
24  super(BatchNormalization, self).__init__(
25  model, name, input_record, **kwargs)
26 
27  assert isinstance(input_record, schema.Scalar), "Incorrect input type"
28 
29  self.input_shape = input_record.field_type().shape
30 
31  if len(self.input_shape) == 3:
32  if order == "NCHW":
33  input_dims = self.input_shape[0]
34  elif order == "NHWC":
35  input_dims = self.input_shape[2]
36  else:
37  raise ValueError("Please specify a correct order")
38  else:
39  assert len(self.input_shape) == 1, (
40  "This layer supports only 4D or 2D tesnors")
41  input_dims = self.input_shape[0]
42 
44  (np.float32, self.input_shape),
45  self.get_next_blob_reference('output')
46  )
47 
48  self.momentum = momentum
49  self.order = order
50 
51  self.scale = self.create_param(param_name='scale',
52  shape=[input_dims],
53  initializer=('ConstantFill', {'value': 1.0}),
54  optimizer=scale_optim)
55  self.bias = self.create_param(param_name='bias',
56  shape=[input_dims],
57  initializer=('ConstantFill', {'value': 0.0}),
58  optimizer=bias_optim)
59  self.rm = self.create_param(param_name='running_mean',
60  shape=[input_dims],
61  initializer=('ConstantFill', {'value': 0.0}),
62  optimizer=model.NoOptim)
63  self.riv = self.create_param(param_name='running_inv_var',
64  shape=[input_dims],
65  initializer=('ConstantFill', {'value': 1.0}),
66  optimizer=model.NoOptim)
67 
68  def _add_ops(self, net, is_test, out_blob=None):
69  original_input_blob = self.input_record.field_blobs()
70  input_blob = net.NextScopedBlob('expand_input')
71  if len(self.input_shape) == 1:
72  input_blob = net.ExpandDims(original_input_blob,
73  dims=[2, 3])
74  else:
75  input_blob = original_input_blob[0]
76 
77  if out_blob is None:
78  bn_output = self.output_schema.field_blobs()
79  else:
80  bn_output = out_blob
81  if is_test:
82  output_blobs = bn_output
83  else:
84  output_blobs = bn_output + [self.rm, self.riv,
85  net.NextScopedBlob('bn_saved_mean'),
86  net.NextScopedBlob('bn_saved_iv')]
87 
88  net.SpatialBN([input_blob, self.scale,
89  self.bias, self.rm, self.riv],
90  output_blobs,
91  momentum=self.momentum,
92  is_test=is_test,
93  order=self.order)
94 
95  if len(self.input_shape) == 1:
96  net.Squeeze(bn_output,
97  bn_output,
98  dims=[2, 3])
99 
100  def add_train_ops(self, net):
101  self._add_ops(net, is_test=False)
102 
103  def add_eval_ops(self, net):
104  self._add_ops(net, is_test=True)
105 
106  def add_ops(self, net):
107  self.add_eval_ops(net)
def get_next_blob_reference(self, name)
Definition: layers.py:349
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
Definition: layers.py:334