Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conv2d kn2row kn2col #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions dnn/operations/convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,56 @@ def backward(self, grad_outputs):

class Convolution2D_KN2ROW(Convolution2DBase, Operation):

def forward(self, x, kernel):
"""

x.shape -> batch, input_channel, height, width.
kernel.shape -> output_channel, input_channel, kernel_height, kernel_width.
"""
batch = x.shape[0]
input_h = x.shape[2]
input_w = x.shape[3]
out_c = kernel.shape[0]
kernel_h = kernel.shape[2]
kernel_w = kernel.shape[3]
out_h = 1 + ((input_h + 2 * self.pad_h - kernel_h) // self.stride_y)
out_w = 1 + ((input_w + 2 * self.pad_w - kernel_w) // self.stride_x)

# zero padding
x = np.pad(
x,
((0, 0),
(0, 0),
(self.pad_h, self.pad_h + self.stride_y - 1),
(self.pad_w, self.pad_w + self.stride_x - 1)),
mode='constant',
constant_values=(0,))

# 1x1 convolution
# tmp.shape -> (kernel_h, kernel_w, out_channel, batch, input_height+pad, input_widht+pad, )
tmp = np.tensordot(
kernel.transpose((2, 3, 0, 1)), x, ((3, ), (1, )))

# tmp.shape -> (batch, kernel_h, kernel_w, out_channel, input_height+pad, input_widht+pad, )
tmp = np.moveaxis(tmp, 3, 0)

out = np.zeros((batch, out_c, out_h, out_w))
for k_h in range(kernel_h):
for k_w in range(kernel_w):
for h in range(out_h):
for w in range(out_w):
stride_h = h * self.stride_y
stride_w = w * self.stride_x
out[:, :, h, w] += tmp[:, k_h, k_w, :, stride_h+k_h, stride_w+k_w]

return out

def backward(self, grad_outputs):
Exception("Should be implement")


class Convolution2D_KN2COL(Convolution2DBase, Operation):

def forward(self, x, kernel):
"""

Expand Down
11 changes: 8 additions & 3 deletions tests/dnn_tests/operations_tests/test_convolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

from dnn.operations.convolution import (
Convolution2D_IM2COL, Convolution2D_Naive,
Convolution2D_KN2ROW
Convolution2D_KN2ROW, Convolution2D_KN2COL,
)
from dnn.test import check_gradients


def test_compare_convolution2d():
input_shape = (2, 2, 20, 20) # b, c, h, w
input_shape = (1, 32, 20, 20) # b, c, h, w
input_data = np.random.rand(*input_shape)

kernel_shape = (4, 2, 3, 3) # out_c, in_c, h, w,
kernel_shape = (64, 32, 3, 3) # out_c, in_c, h, w,
kernel_data = np.random.rand(*kernel_shape)

pad = 1
Expand All @@ -26,6 +26,11 @@ def test_compare_convolution2d():

assert np.allclose(naive_output.data, kn2row_output.data)

kn2col = Convolution2D_KN2COL(pad=pad, stride=stride)
kn2col_output = kn2col(input_data, kernel_data)

assert np.allclose(naive_output.data, kn2col_output.data)

im2col = Convolution2D_IM2COL(pad=pad, stride=stride)
im2col_output = im2col(input_data, kernel_data)

Expand Down