Caffe2 - Python API
A deep learning, cross platform ML framework
conv.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package conv
17 # Module caffe2.python.layers.conv
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 from caffe2.python import schema
24 from caffe2.python.layers.layers import (
25  ModelLayer,
26 )
27 import numpy as np
28 
29 
30 class Conv(ModelLayer):
31  """
32  Convolutional layer
33  Input:
34  - input_record: at least has the shape info of C (num_channels)
35  - output_dim: number of convolutional filters
36  - kernel_h, kernel_w: kernel size for h and w
37  - stride_h, stride_w: stride for h and w
38  - pad_b, pad_l, pad_r, pad_t: padding sizes, if stride == 1,
39  'None' value will do auto padding
40  - order: either 'NHWC' or 'NCHW'
41  """
42 
43  def __init__(self, model, input_record, output_dim, kernel_h, kernel_w,
44  stride_h, stride_w, pad_b=None, pad_l=None, pad_r=None,
45  pad_t=None, order='NHWC', kernel_init=None, bias_init=None,
46  kernel_optim=None, bias_optim=None,
47  name='conv', **kwargs):
48 
49  super(Conv, self).__init__(model, name, input_record, **kwargs)
50  assert isinstance(input_record, schema.Scalar), "Incorrect input type"
51  # input num_channels (C) is needed
52  input_dims = input_record.field_type().shape
53 
54  assert (kernel_h > 0 and isinstance(kernel_h, int)), (
55  "kernel_h should be positive integer")
56  assert (kernel_w > 0 and isinstance(kernel_w, int)), (
57  "kernel_w should be positive integer")
58  self.kernel_h = kernel_h
59  self.kernel_w = kernel_w
60 
61  assert (stride_h > 0 and isinstance(stride_h, int)), (
62  "stride_h should be positive integer")
63  assert (stride_w > 0 and isinstance(stride_w, int)), (
64  "stride_w should be positive integer")
65  self.stride_h = stride_h
66  self.stride_w = stride_w
67 
68  # output_dim calculation (http://cs231n.github.io/convolutional-networks/)
69  # output_dim_w = (input_dim_w - kernel_w + pad_r + pad_l) / stride_w + 1
70  # so, do auto_padding requires
71  # pad_r, pad_l = [(input_dim_w - 1) * stride_w - input_dim_w + kernel_w] / 2
72  # similair for pad_t and pad_b to auto pad kernel_h
73  # here we only do auto padding for stride = 1 case
74  if stride_h == 1:
75  pad_t = int((kernel_h - 1) / 2) if pad_t is None else pad_t
76  pad_b = int((kernel_h - 1) / 2) if pad_b is None else pad_b
77  else:
78  pad_t = 0 if pad_t is None else pad_t
79  pad_b = 0 if pad_b is None else pad_b
80 
81  if stride_w == 1:
82  pad_r = int((kernel_w - 1) / 2) if pad_r is None else pad_r
83  pad_l = int((kernel_w - 1) / 2) if pad_l is None else pad_l
84  else:
85  pad_r = 0 if pad_r is None else pad_r
86  pad_l = 0 if pad_l is None else pad_l
87 
88  assert (pad_t >= 0 and isinstance(pad_t, int)), "pad_t should be int >= 0"
89  assert (pad_b >= 0 and isinstance(pad_b, int)), "pad_b should be int >= 0"
90  assert (pad_r >= 0 and isinstance(pad_r, int)), "pad_r should be int >= 0"
91  assert (pad_l >= 0 and isinstance(pad_l, int)), "pad_l should be int >= 0"
92  self.pad_t = pad_t
93  self.pad_b = pad_b
94  self.pad_r = pad_r
95  self.pad_l = pad_l
96 
97  assert order in ['NHWC', 'NCHW'], "order should either 'NHWC' or 'NCHW'"
98  self.order = order
99 
100  if order == 'NHWC':
101  input_c = input_dims[-1]
102  kernel_shape = [output_dim, kernel_h, kernel_w, input_c]
103  elif order == 'NCHW':
104  input_c = input_dims[0]
105  kernel_shape = [output_dim, input_c, kernel_h, kernel_w]
106  assert input_c > 0, (
107  "Number of input channels in conv parameters should be positive")
108 
109  kernel_init = kernel_init if kernel_init else (
110  'XavierFill', {}
111  )
112  bias_init = bias_init if bias_init else (
113  'ConstantFill', {'value': 0.0}
114  )
115 
116  self.kernel = self.create_param(
117  param_name='conv_kernel',
118  shape=kernel_shape,
119  initializer=kernel_init,
120  optimizer=kernel_optim,
121  )
122 
123  self.bias = self.create_param(
124  param_name='conv_bias',
125  shape=[output_dim],
126  initializer=bias_init,
127  optimizer=bias_optim,
128  )
129 
130  # the output_schema only has the num of output channels
131  # output_h and output_w would be inferred internally
133  (np.float32, (output_dim,)),
134  self.get_next_blob_reference('output')
135  )
136 
137  def add_ops(self, net):
138  net.Conv(
139  self.input_record.field_blobs() + [self.kernel, self.bias],
140  self.output_schema.field_blobs(),
141  kernel_h=self.kernel_h,
142  kernel_w=self.kernel_w,
143  stride_h=self.stride_h,
144  stride_w=self.stride_w,
145  pad_t=self.pad_t,
146  pad_l=self.pad_l,
147  pad_b=self.pad_b,
148  pad_r=self.pad_r,
149  order=self.order
150  )