未验证 提交 7901ff90 编写于 作者: C Chang Xu 提交者: GitHub

Revert "speedup sparse block (#1022)" (#1058)

上级 f01fc4f6
...@@ -8,25 +8,11 @@ BLOCK_SPARSE_ACCURATE_THRESHOLD = 0.05 ...@@ -8,25 +8,11 @@ BLOCK_SPARSE_ACCURATE_THRESHOLD = 0.05
def cal_mxn_avg_matrix(mat, m=1, n=1): def cal_mxn_avg_matrix(mat, m=1, n=1):
if m == 1 and n == 1: return copy.deepcopy(mat) if m == 1 and n == 1: return copy.deepcopy(mat)
ori_row, ori_col = mat.shape[0], mat.shape[1]
if len(mat.shape) == 4:
assert mat.shape[2:] == (1, 1), "Only support for (n, n, 1, 1) for now."
mat = mat.reshape(ori_row, ori_col)
res_col = n - len(mat[0]) % n
res_row = m - len(mat) % m
mat = np.pad(mat, ((0, res_col), (0, res_col)), 'reflect')
avg_mat = np.zeros_like(mat) avg_mat = np.zeros_like(mat)
new_shape = [len(mat) // m, len(mat[0]) // n, m, n] rows = len(mat) // m + 1
strides = mat.itemsize * np.array([len(mat) * m, n, len(mat), 1]) cols = len(mat[0]) // n + 1
mat = np.lib.stride_tricks.as_strided(mat, shape=new_shape, strides=strides) for row in range(rows):
mat = mat.mean((2, 3), keepdims=True) for col in range(cols):
mat = np.tile(mat, (1, 1, m, n)) avg_mat[m * row:m * row + m, n * col:n * col + n] = np.mean(mat[
for i in range(len(mat)): m * row:m * row + m, n * col:n * col + n])
avg_mat[i * m:i * m + m] = np.concatenate(list(mat[i]), axis=1)
avg_mat = avg_mat[:ori_row, :ori_col]
if len(mat.shape) == 4:
avg_mat = avg_mat.reshape(ori_row, ori_col, 1, 1)
return avg_mat return avg_mat
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册