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

col2im関数のimg配列の初期化サイズについて #46

Open
pometa0507 opened this issue Jan 1, 2020 · 0 comments
Open

col2im関数のimg配列の初期化サイズについて #46

pometa0507 opened this issue Jan 1, 2020 · 0 comments

Comments

@pometa0507
Copy link

def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
"""
Parameters
----------
col :
input_shape : 入力データの形状(例:(10, 1, 28, 28))
filter_h :
filter_w
stride
pad
Returns
-------
"""
N, C, H, W = input_shape
out_h = (H + 2*pad - filter_h)//stride + 1
out_w = (W + 2*pad - filter_w)//stride + 1
col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2)
img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
for y in range(filter_h):
y_max = y + stride*out_h
for x in range(filter_w):
x_max = x + stride*out_w
img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]
return img[:, :, pad:H + pad, pad:W + pad]

上記コードでimgの初期化部分
img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
について、stride>1のときに余分なゼロ要素を生成している気がします。

return時のスライスにて消去されるので結果には影響ないかと思いますが、
余分なメモリ使用やコード理解の妨げになるのではと思い投稿させていただきました。

image

改善案として、img初期化時の配列サイズは下記でよいかと思うのですがいかがでしょうか。
img = np.zeros((N, C, H + 2*pad, W + 2*pad))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant