Skip to content

Commit a7addae

Browse files
committed
fix some bugs
1 parent 86fbd42 commit a7addae

5 files changed

Lines changed: 69 additions & 11 deletions

File tree

infCNN/LayerLib.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def forward(self, x):
1919
return y.T + self.bias.reshape((1, -1))
2020

2121
def load_from_torch(self, weights, bias):
22-
self.K = weights
23-
self.bias = bias
22+
self.K = weights.copy()
23+
self.bias = bias.copy()
2424

2525

2626
class Conv2d(Layer):
@@ -45,11 +45,13 @@ def __init__(self, C_in, C_out, K_s, Stride):
4545

4646
def forward(self, X):
4747
out = img2col(X, self.K)
48-
print(out.shape)
48+
out = out.transpose(0, 2, 3, 1)
49+
out = out + self.bias
50+
return out.transpose(0, 3, 1, 2)
4951

5052
def load_from_torch(self, weights, bias):
51-
self.K = weights
52-
self.bias = bias
53+
self.K = weights.copy()
54+
self.bias = bias.copy()
5355

5456

5557

infCNN/OpLib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .Core import Op
2-
from .im2col import im2col_indices
32
import numpy as np
3+
from .im2col import im2col_indices
44

55

66
class ReLU(Op):

infCNN/conv.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def img2col(img, core, buf=[np.zeros(1, dtype=np.int32)]):
3434

3535
# mark where need
3636
iimg = img.view(dtype=np.int32)
37-
iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
37+
# iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
38+
iimg &= 0xfffffffe
3839
iimg[:,0,:,:] |= 1
3940

4041
# ravel the image
@@ -60,7 +61,8 @@ def img2col(img, core, buf=[np.zeros(1, dtype=np.int32)]):
6061
img.ravel()[:] = np.arange(3*512*512)
6162

6263
iimg = img.view(dtype=np.int32)
63-
iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
64+
# iimg.view(dtype=np.uint8).ravel()[::4]&=0xfe
65+
iimg &= 0xfffffffe
6466
iimg[:,0,:,:] |= 1
6567

6668
core = np.zeros((32, 3, 3, 3), dtype=np.float32)

infCNN/im2col.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import numpy as np
2+
3+
4+
def get_im2col_indices(x_shape, field_height, field_width, padding=1, stride=1):
5+
# First figure out what the size of the output should be
6+
N, C, H, W = x_shape
7+
assert (H + 2 * padding - field_height) % stride == 0
8+
assert (W + 2 * padding - field_height) % stride == 0
9+
out_height = int((H + 2 * padding - field_height) / stride + 1)
10+
out_width = int((W + 2 * padding - field_width) / stride + 1)
11+
12+
i0 = np.repeat(np.arange(field_height), field_width)
13+
i0 = np.tile(i0, C)
14+
i1 = stride * np.repeat(np.arange(out_height), out_width)
15+
j0 = np.tile(np.arange(field_width), field_height * C)
16+
j1 = stride * np.tile(np.arange(out_width), out_height)
17+
i = i0.reshape(-1, 1) + i1.reshape(1, -1)
18+
j = j0.reshape(-1, 1) + j1.reshape(1, -1)
19+
20+
k = np.repeat(np.arange(C), field_height * field_width).reshape(-1, 1)
21+
22+
return (k.astype(int), i.astype(int), j.astype(int))
23+
24+
25+
def im2col_indices(x, field_height, field_width, padding=1, stride=1):
26+
""" An implementation of im2col based on some fancy indexing """
27+
# Zero-pad the input
28+
p = padding
29+
x_padded = np.pad(x, ((0, 0), (0, 0), (p, p), (p, p)), mode='constant')
30+
31+
k, i, j = get_im2col_indices(x.shape, field_height, field_width, padding, stride)
32+
33+
cols = x_padded[:, k, i, j]
34+
C = x.shape[1]
35+
cols = cols.transpose(1, 2, 0).reshape(field_height * field_width * C, -1)
36+
return cols
37+
38+
39+
def col2im_indices(cols, x_shape, field_height=3, field_width=3, padding=1,
40+
stride=1):
41+
""" An implementation of col2im based on fancy indexing and np.add.at """
42+
N, C, H, W = x_shape
43+
H_padded, W_padded = H + 2 * padding, W + 2 * padding
44+
x_padded = np.zeros((N, C, H_padded, W_padded), dtype=cols.dtype)
45+
k, i, j = get_im2col_indices(x_shape, field_height, field_width, padding, stride)
46+
cols_reshaped = cols.reshape(C * field_height * field_width, -1, N)
47+
cols_reshaped = cols_reshaped.transpose(2, 0, 1)
48+
np.add.at(x_padded, (slice(None), k, i, j), cols_reshaped)
49+
if padding == 0:
50+
return x_padded
51+
return x_padded[:, :, padding:-padding, padding:-padding]

net.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ def load_mat(self, fn):
5252

5353
if __name__ == "__main__":
5454
net = LeNet()
55-
data = np.ones((1, 1, 28, 28))
56-
net.load_mat('train/lenet.mat')
55+
data = np.ones((1, 1, 28, 28), dtype='float32')
56+
x = np.ascontiguousarray(data, dtype=np.float32)
57+
print(data.shape, data.dtype)
58+
# net.load_mat('train/lenet.mat')
59+
print(net(x))
5760
start = time()
58-
print(net(data))
61+
print(net(x))
5962
print('infCNN LeNet time:', time()-start)
6063

0 commit comments

Comments
 (0)