3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
19 - input_record: at least has the shape info of C (num_channels) 20 - output_dim: number of convolutional filters 21 - kernel_h, kernel_w: kernel size for h and w 22 - stride_h, stride_w: stride for h and w 23 - pad_b, pad_l, pad_r, pad_t: padding sizes, if stride == 1, 24 'None' value will do auto padding 25 - order: either 'NHWC' or 'NCHW' 28 def __init__(self, model, input_record, output_dim, kernel_h, kernel_w,
29 stride_h, stride_w, pad_b=
None, pad_l=
None, pad_r=
None,
30 pad_t=
None, order=
'NHWC', kernel_init=
None, bias_init=
None,
31 kernel_optim=
None, bias_optim=
None,
32 name=
'conv', **kwargs):
34 super(Conv, self).__init__(model, name, input_record, **kwargs)
35 assert isinstance(input_record,
schema.Scalar),
"Incorrect input type" 37 input_dims = input_record.field_type().shape
39 assert (kernel_h > 0
and isinstance(kernel_h, int)), (
40 "kernel_h should be positive integer")
41 assert (kernel_w > 0
and isinstance(kernel_w, int)), (
42 "kernel_w should be positive integer")
46 assert (stride_h > 0
and isinstance(stride_h, int)), (
47 "stride_h should be positive integer")
48 assert (stride_w > 0
and isinstance(stride_w, int)), (
49 "stride_w should be positive integer")
60 pad_t = int((kernel_h - 1) / 2)
if pad_t
is None else pad_t
61 pad_b = int((kernel_h - 1) / 2)
if pad_b
is None else pad_b
63 pad_t = 0
if pad_t
is None else pad_t
64 pad_b = 0
if pad_b
is None else pad_b
67 pad_r = int((kernel_w - 1) / 2)
if pad_r
is None else pad_r
68 pad_l = int((kernel_w - 1) / 2)
if pad_l
is None else pad_l
70 pad_r = 0
if pad_r
is None else pad_r
71 pad_l = 0
if pad_l
is None else pad_l
73 assert (pad_t >= 0
and isinstance(pad_t, int)),
"pad_t should be int >= 0" 74 assert (pad_b >= 0
and isinstance(pad_b, int)),
"pad_b should be int >= 0" 75 assert (pad_r >= 0
and isinstance(pad_r, int)),
"pad_r should be int >= 0" 76 assert (pad_l >= 0
and isinstance(pad_l, int)),
"pad_l should be int >= 0" 82 assert order
in [
'NHWC',
'NCHW'],
"order should either 'NHWC' or 'NCHW'" 86 input_c = input_dims[-1]
87 kernel_shape = [output_dim, kernel_h, kernel_w, input_c]
89 input_c = input_dims[0]
90 kernel_shape = [output_dim, input_c, kernel_h, kernel_w]
92 "Number of input channels in conv parameters should be positive")
94 kernel_init = kernel_init
if kernel_init
else (
97 bias_init = bias_init
if bias_init
else (
98 'ConstantFill', {
'value': 0.0}
101 self.
kernel = self.create_param(
102 param_name=
'conv_kernel',
104 initializer=kernel_init,
105 optimizer=kernel_optim,
108 self.
bias = self.create_param(
109 param_name=
'conv_bias',
111 initializer=bias_init,
112 optimizer=bias_optim,
118 (np.float32, (output_dim,)),
119 self.get_next_blob_reference(
'output')
122 def add_ops(self, net):
124 self.input_record.field_blobs() + [self.
kernel, self.
bias],
125 self.output_schema.field_blobs(),