Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 32f023f

Browse files
Long Jinfacebook-github-bot
authored andcommitted
Add conv layer and layer tests
Reviewed By: xianjiec Differential Revision: D5569206 fbshipit-source-id: ed836315f3ee4d7983da94f2633a3085fe99194d
1 parent 3383b68 commit 32f023f

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

caffe2/python/layers/conv.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
## @package conv
2+
# Module caffe2.python.layers.conv
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+
from caffe2.python import schema
9+
from caffe2.python.layers.layers import (
10+
ModelLayer,
11+
)
12+
import numpy as np
13+
14+
15+
class Conv(ModelLayer):
16+
"""
17+
Convolutional layer
18+
Input:
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'
26+
"""
27+
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):
33+
34+
super(Conv, self).__init__(model, name, input_record, **kwargs)
35+
assert isinstance(input_record, schema.Scalar), "Incorrect input type"
36+
# input num_channels (C) is needed
37+
input_dims = input_record.field_type().shape
38+
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")
43+
self.kernel_h = kernel_h
44+
self.kernel_w = kernel_w
45+
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")
50+
self.stride_h = stride_h
51+
self.stride_w = stride_w
52+
53+
# output_dim calculation (http://cs231n.github.io/convolutional-networks/)
54+
# output_dim_w = (input_dim_w - kernel_w + pad_r + pad_l) / stride_w + 1
55+
# so, do auto_padding requires
56+
# pad_r, pad_l = [(input_dim_w - 1) * stride_w - input_dim_w + kernel_w] / 2
57+
# similair for pad_t and pad_b to auto pad kernel_h
58+
# here we only do auto padding for stride = 1 case
59+
if stride_h == 1:
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
62+
else:
63+
pad_t = 0 if pad_t is None else pad_t
64+
pad_b = 0 if pad_b is None else pad_b
65+
66+
if stride_w == 1:
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
69+
else:
70+
pad_r = 0 if pad_r is None else pad_r
71+
pad_l = 0 if pad_l is None else pad_l
72+
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"
77+
self.pad_t = pad_t
78+
self.pad_b = pad_b
79+
self.pad_r = pad_r
80+
self.pad_l = pad_l
81+
82+
assert order in ['NHWC', 'NCHW'], "order should either 'NHWC' or 'NCHW'"
83+
self.order = order
84+
85+
if order == 'NHWC':
86+
input_c = input_dims[-1]
87+
kernel_shape = [output_dim, kernel_h, kernel_w, input_c]
88+
elif order == 'NCHW':
89+
input_c = input_dims[0]
90+
kernel_shape = [output_dim, input_c, kernel_h, kernel_w]
91+
assert input_c > 0, (
92+
"Number of input channels in conv parameters should be positive")
93+
94+
kernel_init = kernel_init if kernel_init else (
95+
'XavierFill', {}
96+
)
97+
bias_init = bias_init if bias_init else (
98+
'ConstantFill', {'value': 0.0}
99+
)
100+
101+
self.kernel = self.create_param(
102+
param_name='conv_kernel',
103+
shape=kernel_shape,
104+
initializer=kernel_init,
105+
optimizer=kernel_optim,
106+
)
107+
108+
self.bias = self.create_param(
109+
param_name='conv_bias',
110+
shape=[output_dim],
111+
initializer=bias_init,
112+
optimizer=bias_optim,
113+
)
114+
115+
# the output_schema only has the num of output channels
116+
# output_h and output_w would be inferred internally
117+
self.output_schema = schema.Scalar(
118+
(np.float32, (output_dim,)),
119+
self.get_next_blob_reference('output')
120+
)
121+
122+
def add_ops(self, net):
123+
net.Conv(
124+
self.input_record.field_blobs() + [self.kernel, self.bias],
125+
self.output_schema.field_blobs(),
126+
kernel_h=self.kernel_h,
127+
kernel_w=self.kernel_w,
128+
stride_h=self.stride_h,
129+
stride_w=self.stride_w,
130+
pad_t=self.pad_t,
131+
pad_l=self.pad_l,
132+
pad_b=self.pad_b,
133+
pad_r=self.pad_r,
134+
order=self.order
135+
)

caffe2/python/layers_test.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,3 +1178,76 @@ def _semi_random_hypothesis_test(srf_output, X_full, X_random, rand_w,
11781178
self._test_net(predict_net, ops_list)
11791179
_semi_random_hypothesis_test(srf_output.full(), X_full, X_random,
11801180
rand_w, rand_b, s)
1181+
1182+
def testConv(self):
1183+
batch_size = 50
1184+
H = 1
1185+
W = 10
1186+
C = 50
1187+
output_dims = 32
1188+
kernel_h = 1
1189+
kernel_w = 3
1190+
stride_h = 1
1191+
stride_w = 1
1192+
pad_t = 0
1193+
pad_b = 0
1194+
pad_r = None
1195+
pad_l = None
1196+
1197+
input_record = self.new_record(schema.Scalar((np.float32, (H, W, C))))
1198+
X = np.random.random((batch_size, H, W, C)).astype(np.float32)
1199+
schema.FeedRecord(input_record, [X])
1200+
conv = self.model.Conv(
1201+
input_record,
1202+
output_dims,
1203+
kernel_h=kernel_h,
1204+
kernel_w=kernel_w,
1205+
stride_h=stride_h,
1206+
stride_w=stride_w,
1207+
pad_t=pad_t,
1208+
pad_b=pad_b,
1209+
pad_r=pad_r,
1210+
pad_l=pad_l,
1211+
order='NHWC'
1212+
)
1213+
1214+
self.assertEqual(
1215+
schema.Scalar((np.float32, (output_dims,))),
1216+
conv
1217+
)
1218+
1219+
self.run_train_net_forward_only()
1220+
output_record = schema.FetchRecord(conv)
1221+
# check the number of output channels is the same as input in this example
1222+
assert output_record.field_types()[0].shape == (H, W, output_dims)
1223+
assert output_record().shape == (batch_size, H, W, output_dims)
1224+
1225+
train_init_net, train_net = self.get_training_nets()
1226+
# Init net assertions
1227+
init_ops = self.assertNetContainOps(
1228+
train_init_net,
1229+
[
1230+
OpSpec("XavierFill", None, None),
1231+
OpSpec("ConstantFill", None, None),
1232+
]
1233+
)
1234+
conv_spec = OpSpec(
1235+
"Conv",
1236+
[
1237+
input_record.field_blobs()[0],
1238+
init_ops[0].output[0],
1239+
init_ops[1].output[0],
1240+
],
1241+
conv.field_blobs()
1242+
)
1243+
1244+
# Train net assertions
1245+
self.assertNetContainOps(train_net, [conv_spec])
1246+
1247+
# Predict net assertions
1248+
predict_net = self.get_predict_net()
1249+
self.assertNetContainOps(predict_net, [conv_spec])
1250+
1251+
# Eval net assertions
1252+
eval_net = self.get_eval_net()
1253+
self.assertNetContainOps(eval_net, [conv_spec])

0 commit comments

Comments
 (0)