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 = utils.get_conv_outsize(H, KH, SH, PH) OW = utils.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 forward(self, x, W, b): xp = cuda.get_array_module(x) Weight = W SH, SW = self.stride PH, PW = self.pad C, OC, KH, KW = Weight.shape N, C, H, W = x.shape if self.outsize is None: out_h = get_deconv_outsize(H, KH, SH, PH) out_w = get_deconv_outsize(W, KW, SW, PW) else: out_h, out_w = _pair(self.outsize) img_shape = (N, OC, out_h, out_w) gcol = xp.tensordot(Weight, x, (0, 1)) gcol = xp.rollaxis(gcol, 3) y = utils.col2im(gcol, img_shape, (KH, KW), self.stride, self.pad, to_matrix=False) # b, k, h, w if b is not None: self.no_bias = True y += b.reshape((1, b.size, 1, 1)) 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 = utils.get_conv_outsize(H, KH, SH, PH) OW = utils.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 _init_W(self, x): self.in_channels = x.shape[1] xp = cuda.get_array_module(x) C, OC = self.in_channels, self.out_channels KH, KW = _pair(self.kernel_size) W_data = xp.random.randn(OC, C, KH, KW).astype(np.float32) * np.sqrt( 1 / C * KH * KW) self.W.data = W_data
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) out_h = (h + ph * 2 - kh) // sh + 1 out_w = (h + pw * 2 - kw) // sw + 1 col = im2col(x, kernel_size, stride, pad) col = col.transpose((0, 4, 5, 1, 2, 3)).reshape((-1, kh * kw)) y = col.max(axis=1) y = y.reshape((n, out_h, out_w, c)) y = y.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 forward(self, gy): n, c, out_h, out_w = gy.shape h, w = self.input_shpae[2:] kh, kw = _pair(self.kernel_size) gcol = np.zeros((n * c * out_h * out_w * kh * kw), dtype=self.dtype) indexes = self.indexes.ravel() + np.arange( 0, self.indexes.size * kh * kw, kh * kw) gcol[indexes] = gy[0].ravel() gcol = gcol.reshape(n, c, out_h, out_w, kh, kw) gcol = np.swapaxes(gcol, 2, 4) gcol = np.swapaxes(gcol, 3, 5) gx = utils.col2im( gcol, (n, c, h, w), self.kernel_size, self.stride, self.pad) # self.sy, self.sx, self.ph, self.pw, h, w) return gx
def forward(self, x, W, b): sy, sx = self.stride ph, pw = self.pad in_c, out_c, kh, kw = W.shape n, in_c, in_h, in_w = x.shape if self.outsize is None: out_h = get_deconv_outsize(in_h, kh, sy, ph) out_w = get_deconv_outsize(in_w, kw, sx, pw) else: out_h, out_w = _pair(self.outsize) img_shape = (n, out_c, out_h, out_w) gcol = np.tensordot(W, x, (0, 1)) gcol = np.rollaxis(gcol, 3) y = utils.col2im(gcol, img_shape, (kh, kw), self.stride, self.pad) # b, k, h, w if b is not None: self.no_bias = True y += b.reshape((1, b.size, 1, 1)) return y
def __init__(self, in_channels, out_channels, kernel_size, stride=1, pad=0, nobias=False): super().__init__() self.kernel_size = _pair(kernel_size) self.stride = stride self.pad = pad I, O = in_channels, out_channels KH, KW = self.kernel_size W_data = np.random.randn(O, I, KH, KW).astype('f') * np.sqrt( 1 / I * KH * KW) self.W = Parameter(W_data, name='W') if nobias: self.b = None else: self.b = Parameter(np.zeros(O).astype('f'), name='b')
def forward(self, gy): xp = cuda.get_array_module(gy) N, C, OH, OW = gy.shape H, W = self.input_shpae[2:] KH, KW = _pair(self.kernel_size) gcol = xp.zeros((N * C * OH * OW * KH * KW), dtype=self.dtype) indexes = self.indexes.ravel() + xp.arange( 0, self.indexes.size * KH * KW, KH * KW) gcol[indexes] = gy[0].ravel() gcol = gcol.reshape(N, C, OH, OW, KH, KW) gcol = xp.swapaxes(gcol, 2, 4) gcol = xp.swapaxes(gcol, 3, 5) gx = utils.col2im(gcol, (N, C, H, W), self.kernel_size, self.stride, self.pad, to_matrix=False) return gx
def __init__(self, stride=1, pad=0, outsize=None): super().__init__() self.stride = _pair(stride) self.pad = _pair(pad) self.outsize = outsize
def __init__(self, stride=1, pad=0): super().__init__() self.stride = _pair(stride) self.pad = _pair(pad)