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

CNN实现 #401

Closed
yhx0105 opened this issue Nov 8, 2019 · 2 comments
Closed

CNN实现 #401

yhx0105 opened this issue Nov 8, 2019 · 2 comments

Comments

@yhx0105
Copy link
Contributor

yhx0105 commented Nov 8, 2019

老师你好,我在阅读你们代码的时候遇到了一些问题不太理解。
1.在ch17-CNNBasic MinFramework jit_utility.py 中
`
def img2col(input_data, filter_h, filter_w, stride=1, pad=0):

N, C, H, W = input_data.shape
out_h = (H + 2*pad - filter_h)//stride + 1
out_w = (W + 2*pad - filter_w)//stride + 1
img = input_data
if pad > 0:
    img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))
for i in range(filter_h):
    i_max = i + stride*out_h
    for j in range(filter_w):
        j_max = j + stride*out_w
        col[:, :, i, j, :, :] = img[:, :, i:i_max:stride, j:j_max:stride]
col = np.transpose(col, axes=(0, 4, 5, 1, 2, 3)).reshape(N*out_h*out_w, -1)
return col`

为什么col要变成这个形状(Nout_hout_w, -1)??????

`def col2img(col, input_shape, filter_h, filter_w, stride, pad, out_h, out_w):

N, C, H, W = input_shape
tmp1 = col.reshape(N, out_h, out_w, C, filter_h, filter_w)
tmp2 = np.transpose(tmp1, axes=(0, 3, 4, 5, 1, 2))
img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
for i in range(filter_h):
    i_max = i + stride*out_h
    for j in range(filter_w):
        j_max = j + stride*out_w
        img[:, :, i:i_max:stride, j:j_max:stride] += tmp2[:, :, i, j, :, :]
return img[:, :, pad:H + pad, pad:W + pad]`

为什么初始化img的形状是(N, C, H + 2pad + stride - 1, W + 2pad + stride - 1)

@yhx0105
Copy link
Contributor Author

yhx0105 commented Nov 9, 2019

虽然不知道为什么但是这样矩阵变来变去,居然又变回去了 好神奇啊

@yhx0105 yhx0105 closed this as completed Nov 18, 2019
@xiaowuhu
Copy link
Collaborator

这个在17.2中有详细解释

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

2 participants