提交 6ef2da2e 编写于 作者: C chengduoZH

finetune conv2d navie func

上级 92c39441
......@@ -11,19 +11,19 @@ def conv2d_forward_naive(input, filter, group, conv_param):
sub_out_c = out_c / group
stride, pad = conv_param['stride'], conv_param['pad']
out_h = 1 + (in_h + 2 * pad - f_h) / stride
out_w = 1 + (in_w + 2 * pad - f_w) / stride
out_h = 1 + (in_h + 2 * pad[0] - f_h) / stride[0]
out_w = 1 + (in_w + 2 * pad[1] - f_w) / stride[1]
out = np.zeros((in_n, out_c, out_h, out_w))
input_pad = np.pad(input, ((0, ), (0, ), (pad, ), (pad, )),
input_pad = np.pad(input, ((0, ), (0, ), (pad[0], ), (pad[1], )),
mode='constant',
constant_values=0)
for i in range(out_h):
for j in range(out_w):
for g in range(group):
input_pad_masked = input_pad[:, g * f_c:(
g + 1) * f_c, i * stride:i * stride + f_h, j * stride:j *
stride + f_w]
g + 1) * f_c, i * stride[0]:i * stride[0] + f_h, j * stride[
1]:j * stride[1] + f_w]
f_sub = filter[g * sub_out_c:(g + 1) * sub_out_c, :, :, :]
for k in range(sub_out_c):
out[:, g * sub_out_c + k, i, j] = np.sum(input_pad_masked *
......@@ -37,11 +37,14 @@ class TestConv2dOp(OpTest):
def setUp(self):
self.init_groups()
self.op_type = "conv2d"
pad = [0, 0]
stride = [1, 1]
input_size = [2, 3, 5, 5] # NCHW
assert np.mod(input_size[1], self.groups) == 0
f_c = input_size[1] / self.groups
filter_size = [6, f_c, 3, 3]
conv2d_param = {'stride': 1, 'pad': 0}
conv2d_param = {'stride': stride, 'pad': pad}
input = np.random.random(input_size).astype("float32")
filter = np.random.random(filter_size).astype("float32")
......@@ -49,11 +52,7 @@ class TestConv2dOp(OpTest):
output = conv2d_forward_naive(input, filter, self.groups, conv2d_param)
self.inputs = {'Input': input, 'Filter': filter}
self.attrs = {
'strides': [1, 1],
'paddings': [0, 0],
'groups': self.groups
}
self.attrs = {'strides': stride, 'paddings': pad, 'groups': self.groups}
self.outputs = {'Output': output}
def test_check_output(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册