def _im2col_gpu(img, kernel_size, stride, pad): """im2col function for GPU. This code is ported from Chainer: https://github.com/chainer/chainer/blob/v6.4.0/chainer/utils/conv.py """ n, c, h, w = img.shape kh, kw = pair(kernel_size) sy, sx = pair(stride) ph, pw = pair(pad) out_h = get_conv_outsize(h, kh, sy, ph) out_w = get_conv_outsize(w, kw, sx, pw) dy, dx = 1, 1 col = cuda.cupy.empty((n, c, kh, kw, out_h, out_w), dtype=img.dtype) cuda.cupy.ElementwiseKernel( 'raw T img, int32 h, int32 w, int32 out_h, int32 out_w,' 'int32 kh, int32 kw, int32 sy, int32 sx, int32 ph, int32 pw,' 'int32 dy, int32 dx', 'T col', ''' int c0 = i / (kh * kw * out_h * out_w); int ky = i / (kw * out_h * out_w) % kh; int kx = i / (out_h * out_w) % kw; int out_y = i / out_w % out_h; int out_x = i % out_w; int in_y = ky * dy + out_y * sy - ph; int in_x = kx * dx + out_x * sx - pw; if (in_y >= 0 && in_y < h && in_x >= 0 && in_x < w) { col = img[in_x + w * (in_y + h * c0)]; } else { col = 0; } ''', 'im2col')(img.reduced_view(), h, w, out_h, out_w, kh, kw, sy, sx, ph, pw, dy, dx, col) return col
def im2col_array(img, kernel_size, stride, pad, to_matrix=True): N, C, H, W = img.shape KH, KW = pair(kernel_size) SH, SW = pair(stride) PH, PW = pair(pad) OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) img = np.pad(img, ((0, 0), (0, 0), (PH, PH + SH - 1), (PW, PW + SW - 1)), mode='constant', constant_values=(0, )) col = np.ndarray((N, C, KH, KW, OH, OW), dtype=img.dtype) # fig 57-1 mini-batch verion for j in range(KH): j_lim = j + SH * OH for i in range(KW): i_lim = i + SW * OW # assign data applied by kernel at j, i. # https://qiita.com/jun40vn/items/d2e8711cabc9cfb1e0d5 # added batch dim and channel dim col[:, :, j, i, :, :] = img[:, :, j:j_lim:SH, i:i_lim:SW] # reshape method if to_matrix: # fig 57-1 col = col.transpose((0, 4, 5, 1, 2, 3)).reshape((N * OH * OW, -1)) return col
def im2col_array(img, kernel_size, stride, pad, to_matrix=True): N, C, H, W = img.shape KH, KW = pair(kernel_size) SH, SW = pair(stride) PH, PW = pair(pad) OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) xp = cuda.get_array_module(img) if xp != np: col = _im2col_gpu(img, kernel_size, stride, pad) else: img = np.pad(img, ((0, 0), (0, 0), (PH, PH + SH - 1), (PW, PW + SW - 1)), mode='constant', constant_values=(0, )) col = np.ndarray((N, C, KH, KW, OH, OW), dtype=img.dtype) for j in range(KH): j_lim = j + SH * OH for i in range(KW): i_lim = i + SW * OW col[:, :, j, i, :, :] = img[:, :, j:j_lim:SH, i:i_lim:SW] if to_matrix: col = col.transpose((0, 4, 5, 1, 2, 3)).reshape((N * OH * OW, -1)) return col
def pooling_simple(x, kernel_size, stride=1, pad=0): x = as_variable(x) N, C, H, W = x.shape KH, KW = pair(kernel_size) PH, PW = pair(pad) SH, SW = pair(stride) OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) col = im2col(x, kernel_size, stride, pad, to_matrix=True) col = col.reshape(-1, KH * KW) y = col.max(axis=1) y = y.reshape(N, OH, OW, C).transpose(0, 3, 1, 2) return y
def conv2d_simple(x, W, b=None, stride=1, pad=0): x, W = as_variable(x), as_variable(W) Weight = W N, C, H, W = x.shape OC, C, KH, KW = Weight.shape SH, SW = pair(stride) PH, PW = pair(pad) OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) col = im2col(x, (KH, KW), stride, pad, to_matrix=True) Weight = Weight.reshape(OC, -1).transpose() t = linear(col, Weight, b) y = t.reshape(N, OH, OW, OC).transpose(0, 3, 1, 2) return y
def conv2d_simple(x, W, b=None, stride=1, pad=0): x, W = as_variable(x), as_variable(W) n, c, h, w = x.shape out_c, c, kh, kw = W.shape sh, sw = _pair(stride) ph, pw = _pair(pad) out_h = utils.get_conv_outsize(h, kh, sh, ph) out_w = utils.get_conv_outsize(w, kw, sw, pw) col = im2col(x, (kh, kw), stride, pad) col = col.transpose((0, 4, 5, 1, 2, 3)).reshape((n * out_h * out_w, -1)) W = W.reshape((out_c, -1)).transpose() t = linear(col, W, b) y = t.reshape((n, out_h, out_w, -1)).transpose((0, 3, 1, 2)) return y
def col2im_array(col, img_shape, kernel_size, stride, pad, to_matrix=True): N, C, H, W = img_shape KH, KW = pair(kernel_size) SH, SW = pair(stride) PH, PW = pair(pad) OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) if to_matrix: col = col.reshape(N, OH, OW, C, KH, KW).transpose(0, 3, 4, 5, 1, 2) img = np.zeros((N, C, H + 2 * PH + SH - 1, W + 2 * PW + SW - 1), dtype=col.dtype) for j in range(KH): j_lim = j + SH * OH for i in range(KW): i_lim = i + SW * OW img[:, :, j:j_lim:SH, i:i_lim:SW] += col[:, :, j, i, :, :] return img[:, :, PH:H + PH, PW:W + PW]
if '__file__' in globals(): import os, sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from dezero.utils import get_conv_outsize H, W = 4, 4 # input shape KH, KW = 3, 3 # kernel shape SH, SW = 1, 1 # stride PH, PW = 1, 1 # padding OH = get_conv_outsize(H, KH, SH, PH) OW = get_conv_outsize(W, KW, SW, PW) print(OH, OW)