-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.py
163 lines (144 loc) · 5.93 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import numpy as np
import multiprocessing
class Conv2d_MULTITHREADS:
'''
使用矩阵乘法实现卷积操作
'''
def __init__(self, k, s, c_in, c_out, mode='same', weight=None):
self.k, self.s, self.c_in, self.c_out, self.mode = k, s, c_in, c_out, mode
if weight is None:
self.weight = np.random.random((c_in, k, k, c_out))
else:
self.weight = weight
assert mode in ['same', 'full', 'valid']
def workers(self, out_h_start, out_h_end, out_w, c, padded_img, RES, ind=0, PID=0):
image_mat = np.zeros(((out_h_end - out_h_start) * out_w, self.k * self.k * c))
for i in range(out_h_start, out_h_end):
for j in range(out_w):
window = padded_img[:, i * self.s:(i * self.s + self.k), j * self.s:(j * self.s + self.k)]
image_mat[ind] = window.flatten()
ind += 1
RES[PID] = image_mat
def __call__(self, image):
assert image.shape[
0] == self.c_in, f"image in channel {image.shape[0]} not equal to weight channel {self.weight.shape[0]}"
c, h, w = image.shape
if self.mode == "same":
p_h = (self.s * (h - 1) + self.k - h) // 2
p_w = (self.s * (w - 1) + self.k - w) // 2
elif self.mode == 'valid':
p_h, p_w = 0, 0
elif self.mode == 'full':
p_h, p_w = self.k - 1, self.k - 1
else:
assert False, 'error mode'
out_h = (h + 2 * p_h - self.k) // self.s + 1
out_w = (w + 2 * p_w - self.k) // self.s + 1
# 填充后的image
padded_img = np.zeros((c, h + 2 * p_h, w + 2 * p_w))
padded_img[:, p_h:p_h + h, p_w:p_w + w] = image
# image_mat
# image_mat = np.zeros((out_h * out_w, self.k * self.k * c))
# row = 0
# for i in range(out_h):
# for j in range(out_w):
# window = padded_img[:, i * self.s:(i * self.s + self.k), j * self.s:(j * self.s + self.k)]
# image_mat[row] = window.flatten()
# row += 1
# 多线程
RES = multiprocessing.Manager().dict()
p0 = multiprocessing.Process(target=self.workers, args=(0, out_h // 2, out_w, c, padded_img, RES, 0, 0))
p1 = multiprocessing.Process(target=self.workers, args=(out_h // 2, out_h, out_w, c, padded_img, RES, 0, 1))
p0.start()
p1.start()
p0.join()
p1.join()
# print(RES[0].shape)
# print(RES[1].shape)
image_mat = np.concatenate((RES[0], RES[1]))
# 矩阵乘法
res = np.dot(image_mat, self.weight.reshape(-1, self.c_out))
res = np.reshape(res, (out_h, out_w, self.c_out))
return np.transpose(res, (2, 0, 1))
class Conv2d:
'''
使用矩阵乘法实现卷积操作
'''
def __init__(self, k, s, c_in, c_out, mode='same', weight=None):
'''
:param k: kernel size
:param s: stride
:param c_in: in feature maps
:param c_out: out feature maps
:param mode: choose in ['same','valid','full']
:param weight: kernel data
'''
self.k, self.s, self.c_in, self.c_out, self.mode = k, s, c_in, c_out, mode
if weight is None:
self.weight = np.random.random((c_in, k, k, c_out))
else:
self.weight = weight
assert mode in ['same', 'full', 'valid']
def filter(self, image):
h, w = image.shape
if self.mode == "same":
p_h = (self.s * (h - 1) + self.k - h) // 2
p_w = (self.s * (w - 1) + self.k - w) // 2
elif self.mode == 'valid':
p_h, p_w = 0, 0
elif self.mode == 'full':
p_h, p_w = self.k - 1, self.k - 1
else:
assert False, 'error mode'
out_h = (h + 2 * p_h - self.k) // self.s + 1
out_w = (w + 2 * p_w - self.k) // self.s + 1
# 填充后的image
padded_img = np.zeros((h + 2 * p_h, w + 2 * p_w))
padded_img[p_h:p_h + h, p_w:p_w + w] = image
# image_mat
image_mat = np.zeros((out_h * out_w, self.k * self.k))
row = 0
for i in range(out_h):
for j in range(out_w):
window = padded_img[i * self.s:(i * self.s + self.k), j * self.s:(j * self.s + self.k)]
image_mat[row] = window.flatten()
row += 1
# 矩阵乘法
res = np.dot(image_mat, self.weight.flatten())
res = np.reshape(res, (out_h, out_w))
return res
def __call__(self, image):
assert image.shape[
0] == self.c_in, f"image in channel {image.shape[0]} not equal to weight channel {self.weight.shape[0]}"
c, h, w = image.shape
if self.mode == "same":
p_h = (self.s * (h - 1) + self.k - h) // 2
p_w = (self.s * (w - 1) + self.k - w) // 2
elif self.mode == 'valid':
p_h, p_w = 0, 0
elif self.mode == 'full':
p_h, p_w = self.k - 1, self.k - 1
else:
assert False, 'error mode'
out_h = (h + 2 * p_h - self.k) // self.s + 1
out_w = (w + 2 * p_w - self.k) // self.s + 1
# 填充后的image
padded_img = np.zeros((c, h + 2 * p_h, w + 2 * p_w))
padded_img[:, p_h:p_h + h, p_w:p_w + w] = image
# image_mat
image_mat = np.zeros((out_h * out_w, self.k * self.k * c))
row = 0
for i in range(out_h):
for j in range(out_w):
window = padded_img[:, i * self.s:(i * self.s + self.k), j * self.s:(j * self.s + self.k)]
image_mat[row] = window.flatten()
row += 1
# 矩阵乘法
res = np.dot(image_mat, self.weight.reshape(-1, self.c_out))
res = np.reshape(res, (out_h, out_w, self.c_out))
return np.transpose(res, (2, 0, 1))
if __name__ == "__main__":
image = np.ones((3, 10, 10))
conv2d = Conv2d(3, 1, 3, 16, 'valid')
res = conv2d(image)
print(res.shape)