def forward(self, x): ker_size = self.ker_size stride = self.stride pad = self.pad ## allocate spaces for the output N, m, n, D = x.shape rows = (m + pad * 2 - ker_size) // stride + 1 cols = (n + pad * 2 - ker_size) // stride + 1 # pad the image if pad != 0: x = F.pad2d(x, pad) ## store input shape for backpropgation self.input_shape = x.shape # get position map maps = F.get_pos(x.shape, (ker_size, ker_size), stride) # store maps to do backpropgation self.maps = maps # get im2col self.X = F.im2col(x, maps) out = np.matmul(self.X, self.W) + self.B.reshape(1, -1) return out.reshape(N, rows, cols, -1)
def forward(self, x): n_bt = x.shape[0] x_ch, x_h, x_w, pool, pad = self.params y_ch, y_h, y_w = self.y_ch, self.y_h, self.y_w # 입력 이미지를 행렬로 변환 cols = fn.im2col(x, pool, pool, y_h, y_w, pool, pad) cols = cols.T.reshape(n_bt * y_h * y_w * x_ch, pool * pool) # 출력 계산: 맥스풀링 y = np.max(cols, axis=1) self.y = y.reshape(n_bt, y_h, y_w, x_ch).transpose(0, 3, 1, 2) # 최대값 인덱스 저장 self.max_index = np.argmax(cols, axis=1)
def forward(self, x): N, C, H, W = x.shape out_h = int(1 + (H - self.pool_h) / self.stride) out_w = int(1 + (W - self.pool_w) / self.stride) col = im2col(x, self.pool_h, self.pool_w, self.stride, self.pad) col = col.reshape(-1, self.pool_h * self.pool_w) arg_max = np.argmax(col, axis=1) out = np.max(col, axis=1) out = out.reshape(N, out_h, out_w, C).transpose(0, 3, 1, 2) self.x = x self.arg_max = arg_max return out
def forward(self, x): FN, C, FH, FW = self.W.shape N, C, H, W = x.shape out_h = 1 + int((H + 2 * self.pad - FH) / self.stride) out_w = 1 + int((W + 2 * self.pad - FW) / self.stride) col = im2col(x, FH, FW, self.stride, self.pad) col_W = self.W.reshape(FN, -1).T out = np.dot(col, col_W) + self.b out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2) self.x = x self.col = col self.col_W = col_W return out
def forward(self, x): n_bt = x.shape[0] x_ch, x_h, x_w, n_flt, flt_h, flt_w, stride, pad = self.params y_ch, y_h, y_w = self.y_ch, self.y_h, self.y_w #input,필터 전처리(im2col) self.cols = fn.im2col(x, flt_h, flt_w, y_h, y_w, stride=stride, pad=pad) self.w_col = self.w.reshape(n_flt, x_ch * flt_h * flt_w) #출력 u = (self.w_col @ self.cols).T + self.b self.u = u.reshape(n_bt, y_h, y_w, y_ch).transpose(0, 3, 1, 2) self.y = fn.relu(self.u)
def forward(self, x): s = self.kersize N, m, n, D = x.shape assert (m % s == 0) assert (n % s == 0) ## calculate the output rows orows = m // s ocols = n // s maps = F.get_pos(x.shape, (s, s), s) im2col = F.im2col(x, maps) rows, _ = im2col.shape im2col = im2col.reshape(rows, -1, D) im_max = np.max(im2col, 1) ## store which elements are biggest self.weights = np.argmax(im2col, 1) return im_max.reshape(N, orows, ocols, D)