def _dot_convolve(a1, a2, mode): if a1.size == 0 or a2.size == 0: raise ValueError('Array arguments cannot be empty') is_inverted = False if a1.size < a2.size: a1, a2 = a2, a1 is_inverted = True dtype = cupy.result_type(a1, a2) n1, n2 = a1.size, a2.size a1 = a1.astype(dtype, copy=False) a2 = a2.astype(dtype, copy=False) if mode == 'full': out_size = n1 + n2 - 1 a1 = cupy.pad(a1, n2 - 1) elif mode == 'same': out_size = n1 pad_size = (n2 - 1) // 2 a1 = cupy.pad(a1, (n2 - 1 - pad_size, pad_size)) elif mode == 'valid': out_size = n1 - n2 + 1 stride = a1.strides[0] a1 = stride_tricks.as_strided(a1, (out_size, n2), (stride, stride)) output = _dot_kernel(a1, a2[::-1], axis=1) return is_inverted, output
def _dot_convolve(a1, a2, mode): offset = 0 if a1.size < a2.size: a1, a2 = a2, a1 offset = 1 - a2.size % 2 dtype = cupy.result_type(a1, a2) n1, n2 = a1.size, a2.size a1 = a1.astype(dtype, copy=False) a2 = a2.astype(dtype, copy=False) if mode == 'full': out_size = n1 + n2 - 1 a1 = cupy.pad(a1, n2 - 1) elif mode == 'same': out_size = n1 pad_size = (n2 - 1) // 2 + offset a1 = cupy.pad(a1, (n2 - 1 - pad_size, pad_size)) elif mode == 'valid': out_size = n1 - n2 + 1 stride = a1.strides[0] a1 = stride_tricks.as_strided(a1, (out_size, n2), (stride, stride)) output = _dot_kernel(a1, a2[::-1], axis=1) return output
def calculate_Bz(config, jx, jy): """ Calculate Bz as iDCT2D(dirichlet_matrix * DCT2D(djx/dy - djy/dx)). """ # 0. Calculate RHS. # NOTE: use gradient instead if available (cupy doesn't have gradient yet). djx_dy = jx[1:-1, 2:] - jx[1:-1, :-2] djy_dx = jy[2:, 1:-1] - jy[:-2, 1:-1] djx_dy = cp.pad(djx_dy, 1, 'constant', constant_values=0) djy_dx = cp.pad(djy_dx, 1, 'constant', constant_values=0) rhs = -(djx_dy - djy_dx) / (config.grid_step_size * 2) # -? # As usual, the boundary conditions are zero # (otherwise add them to boundary cells, divided by grid_step_size/2 # 1. Apply DST-Type1-2D (Discrete Sine Transform Type 1 2D) to the RHS. f = dct2d(rhs) # 2. Multiply f by the special matrix that does the job and normalizes. f *= neumann_matrix(config.grid_steps, config.grid_step_size) # 3. Apply iDCT-Type1-2D (Inverse Discrete Cosine Transform Type 1 2D). # We don't have to define a separate iDCT function, because # unnormalized DCT-Type1 is its own inverse, up to a factor 2(N+1) # and we take all scaling matters into account with a single factor # hidden inside neumann_matrix. Bz = dct2d(f) numba.cuda.synchronize() Bz -= Bz.mean() # Integral over Bz must be 0. return Bz
def __call__(self, x): # npad = ((0, 0), (0, 0), (1, 1), (0, 0)) # x = cp.pad(x, pad_width=npad, mode="constant") if self.comm.rank == 0: # npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) # x = chainer.functions.pad(x, pad_width=npad, mode="constant") if hasattr(x, "array"): npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) x.array = cp.pad(x.array, pad_width=npad, mode="constant") else: npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) x = cp.pad(x, pad_width=npad, mode="constant") if self.comm.rank == 3: # npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) # x = chainer.functions.pad(x, pad_width=npad, mode="constant") if hasattr(x, "array"): npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) x.array = cp.pad(x.array, pad_width=npad, mode="constant") else: npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) x = cp.pad(x, pad_width=npad, mode="constant") x = self.halo_exchange_forward(x) x = self.halo_exchange_backward(x) y = super(SpatialConvolution2D, self).__call__(x) return y
def backpropogate(self, vth, hn, xn, y_index): xn=cp.pad(xn, (1,0), 'constant', constant_values=1.0) #set up yn yn=cp.zeros(vth.shape) yn[y_index]=1 #set up dlda (dl with respect to activation) dlda=-1*(yn-vth) #compute dldv (dl with respect to v) dldv=cp.expand_dims(dlda, axis=1)*hn.T #step down gradient for layer 2 self.v_matrix-=self.learning_rate*dldv #set up matrix for computing layer 1 derivatives xn_matrix=cp.tile(xn, (self.h_matrix.shape[0],1)) f_prime=self.tanh_derivative(xn) f_prime=cp.pad(f_prime, ((1,0), (0,0)), 'constant', constant_values=1.0) #set up l1 gradient matrix l1_gradient_matrix=cp.zeros(self.h_matrix.shape) for i, vi in enumerate(self.v_matrix): grad=((dlda[i]*vi)*f_prime.T[0])[1:] grad=cp.expand_dims(grad, axis=1)*xn_matrix l1_gradient_matrix+=grad #step dow gradient for layer 1 self.h_matrix-=l1_gradient_matrix
def __call__(self, x): # npad = ((0, 0), (0, 0), (1, 1), (0, 0)) # x = cp.pad(x, pad_width=npad, mode="constant") if self.comm.rank == 0: # npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) # x = chainer.functions.pad(x, pad_width=npad, mode="constant") if hasattr(x, "array"): npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) x.array = cp.pad(x.array, pad_width=npad, mode="constant") else: npad = ((0, 0), (0, 0), (0, 0), (self.halo_size, 0)) x = cp.pad(x, pad_width=npad, mode="constant") if self.comm.rank == 3: # npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) # x = chainer.functions.pad(x, pad_width=npad, mode="constant") if hasattr(x, "array"): npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) x.array = cp.pad(x.array, pad_width=npad, mode="constant") else: npad = ((0, 0), (0, 0), (0, 0), (0, self.halo_size)) x = cp.pad(x, pad_width=npad, mode="constant") x = self.halo_exchange_forward(x) x = self.halo_exchange_backward(x) ys = chainermn.functions.allgather(self.comm, x) return F.concat(ys, axis=-1)
def pad(self, x): if len(x.shape) == 2: out = np.pad(x, ([self.py, self.py], [self.px, self.px]), mode='constant') elif len(x.shape) == 3: out = np.pad(x, ([self.py, self.py], [self.px, self.px], [0, 0]), mode='constant') return out
def test_structure_tensor_eigenvalues_3d(): image = cp.pad(cube(9), 5, mode='constant') * 1000 boundary = (cp.pad(cube(9), 5, mode='constant') - cp.pad(cube(7), 6, mode='constant')).astype(bool) A_elems = structure_tensor(image, sigma=0.1) e0, e1, e2 = structure_tensor_eigenvalues(A_elems) # e0 should detect facets assert np.all(e0[boundary] != 0)
def d_TSV(mat): dif_c = 2*cp.diff(mat,axis=1) dif_1 = cp.pad(dif_c, [(0,0),(1,0)], mode = 'constant') dif_2 = cp.pad(-dif_c, [(0,0),(0,1)], mode = 'constant') dif_c= 2*cp.diff(mat,axis=0) dif_3 = cp.pad(dif_c, [(1,0),(0,0)], mode = 'constant') dif_4 = cp.pad(-dif_c, [(0,1),(0,0)], mode = 'constant') return dif_1 + dif_2 + dif_3 + dif_4
def training(sample, test=False): global CNN_layer1_map if test is True: layer1_input = cp.pad(te_dat[sample], ((1, 1), (1, 1)), 'constant', constant_values=((0, 0), (0, 0))) else: # zero padding, constant_values default is zero layer1_input = cp.pad(tr_dat[sample], ((1, 1), (1, 1)), 'constant', constant_values=((0, 0), (0, 0))) # first CNN layer, index from 1 starts for index_y in range(8): for index_x in range(8): in_x = index_x * CNN_layer1_stride + 1 in_y = index_y * CNN_layer1_stride + 1 # for both channels output_mat = cp.stack([ele[in_x - 1: in_x + 2] for ele in layer1_input[in_y - 1: in_y + 2]]) *\ cp.stack([single_w for single_w in CNN_layer1_weight]) z = cp.sum(output_mat, axis=(1, 2)) + cp.stack( [single_b[index_y][index_x] for single_b in CNN_layer1_bias]) # with activation function for ch in range(layer1_ch): CNN_layer1_map[ch][index_y][index_x] = sigmoid(z[ch]) # print(output_mat, z, ' ', index_y, index_x, sep='') # zero padding, now layer2_input has two channels layer2_input = cp.pad(CNN_layer1_map, ((0, 0), (2, 2), (2, 2)), 'constant') # second CNN layer global CNN_layer2_map for index_y in range(4): for index_x in range(4): in_x = index_x * CNN_layer2_stride + 2 in_y = index_y * CNN_layer2_stride + 2 output_mat = cp.stack([ cp.stack([ cp.stack([ ele[in_x - 2:in_x + 3] for ele in layer2_input[l1_ch][in_y - 2:in_y + 3] ]) for l1_ch in range(layer1_ch) ]) * CNN_layer2_weight[l2_ch] for l2_ch in range(layer2_ch) ]) z = cp.sum(output_mat, axis=(1, 2, 3)) + cp.stack( [single_b[index_y][index_x] for single_b in CNN_layer2_bias]) # with activation function for ch in range(layer2_ch): CNN_layer2_map[ch][index_y][index_x] = sigmoid(z[ch]) # print(output_mat, z, ' ', index_y, index_x, sep='') # FC layer global FC_a FC_input = CNN_layer2_map.reshape(4, -1) FC_z = cp.sum(cp.dot(FC_input, FC_w), axis=0) + FC_b FC_a = sigmoid(FC_z) return FC_a
def convolution_matrix(a, n, mode='full'): """Construct a convolution matrix. Constructs the Toeplitz matrix representing one-dimensional convolution. Args: a (cupy.ndarray): The 1-D array to convolve. n (int): The number of columns in the resulting matrix. It gives the length of the input to be convolved with ``a``. This is analogous to the length of ``v`` in ``numpy.convolve(a, v)``. mode (str): This must be one of (``'full'``, ``'valid'``, ``'same'``). This is analogous to ``mode`` in ``numpy.convolve(v, a, mode)``. Returns: cupy.ndarray: The convolution matrix whose row count k depends on ``mode``: =========== ========================= ``mode`` k =========== ========================= ``'full'`` m + n - 1 ``'same'`` max(m, n) ``'valid'`` max(m, n) - min(m, n) + 1 =========== ========================= .. seealso:: :func:`cupyx.scipy.linalg.toeplitz` .. seealso:: :func:`scipy.linalg.convolution_matrix` """ if n <= 0: raise ValueError('n must be a positive integer.') if a.ndim != 1: raise ValueError('convolution_matrix expects a one-dimensional ' 'array as input') if a.size == 0: raise ValueError('len(a) must be at least 1.') if mode not in ('full', 'valid', 'same'): raise ValueError( '`mode` argument must be one of (\'full\', \'valid\', \'same\')') # create zero padded versions of the array az = cupy.pad(a, (0, n - 1), 'constant') raz = cupy.pad(a[::-1], (0, n - 1), 'constant') if mode == 'same': trim = min(n, a.size) - 1 tb = trim // 2 te = trim - tb col0 = az[tb:az.size - te] row0 = raz[-n - tb:raz.size - tb] elif mode == 'valid': tb = min(n, a.size) - 1 te = tb col0 = az[tb:az.size - te] row0 = raz[-n - tb:raz.size - tb] else: # 'full' col0 = az row0 = raz[-n:] return toeplitz(col0, row0)
def surface_area(img, phases, periodic=False): """ Calculate interfacial surface area between two phases or the total surface area of one phase :param img: :param phases: list of phases to calculate SA, if lenght 1 calculate total SA, if length 2 calculate inerfacial SA :return: the surface area in faces per unit volume """ shape = img.shape int_not_in_img = np.unique(img).min() - 1 dim = len(shape) img = cp.asarray(img) # finding an int that is not in the img for padding: if periodic: pad = [(int(not x), int(not x)) for x in periodic] img = cp.pad(img, pad, 'constant', constant_values=int_not_in_img) else: img = cp.pad(img, 1, 'constant', constant_values=int_not_in_img) periodic = [0] * dim SA_map = cp.zeros_like(img) if not isinstance(phases, list): phases = [phases] for i in range(dim): for j in [1, -1]: i_rolled = cp.roll(img, j, i) if len(phases) == 2: SA_map[(i_rolled == phases[0]) & (img == phases[1])] += 1 else: SA_map[(i_rolled == phases[0]) & (img != phases[0])] += 1 # remove padding if not periodic[0]: SA_map = SA_map[1:-1, :] if not periodic[1]: SA_map = SA_map[:, 1:-1] x, y = shape[0], shape[1] # scale factor calculated by taking into account edges periodic_mask = [not x for x in periodic] if dim == 3: z = shape[2] if not periodic[2]: SA_map = SA_map[:, :, 1:-1] sf = cp.sum( cp.array([x, y, z])[periodic_mask] * cp.roll(cp.array([x, y, z])[periodic_mask], 1)) total_faces = 3 * (x * y * z) - sf elif dim == 2: sf = cp.sum(cp.array([x, y])[periodic_mask]) total_faces = 2 * (x + 1) * (y + 1) - (x + 1) - (y + 1) - 2 * sf else: total_faces = SA_map.size sa = cp.sum(SA_map) / total_faces return sa
def padsize(a, b): # Make both tensor the same size if a.size == b.size: return a, b diff = abs(a.size - b.size) if a.size < b.size: return cp.pad(a, (diff, 0)), b elif b.size < a.size: return a, cp.pad(b, (diff, 0))
def prepare_morph(img, p, operation): window_size = 2 * p - 1 pad_size = int((p - 1) / 2) if operation == 'dilation': pad_value = 0 else: pad_value = 255 img = cp.pad(img, ((0, 0), (pad_size, pad_size)), constant_values=pad_value) reconstruction_shape = (img.shape[0], img.shape[1]) img = img.reshape(-1) n_window = int(np.floor(img.shape[0] / p)) out = cp.zeros_like(img) if p % 2 == 0 and operation == 'erosion': img = cp.pad(img, (int(p / 2) - pad_size, 0), constant_values=pad_value) required_padding = (2 * p - 1) - cp.size(img[(p * n_window - 1):-1]) if required_padding > 0: img = cp.pad(img, (0, required_padding), constant_values=pad_value) required_blocks = int((n_window / 512) + 1) original_num_window = n_window if n_window > 512: thread_per_block = 512 n_window = 512 else: thread_per_block = n_window if 2 * n_window * p * 4 > dilation_cuda.max_dynamic_shared_size_bytes: max_window = int( np.floor(dilation_cuda.max_dynamic_shared_size_bytes / (2 * p * 4))) required_blocks = int((original_num_window / max_window) + 1) n_window = max_window thread_per_block = max_window return [ img, window_size, reconstruction_shape, pad_size, n_window, out, required_blocks, thread_per_block ]
def test_extensions(self, mode): """Test vs. manually computed results for modes not in numpy's pad.""" x = cp.array([1, 2, 3, 1], dtype=float) npre, npost = 6, 6 y = _pad_test(x, npre=npre, npost=npost, mode=mode) if mode == "antisymmetric": y_expected = cp.asarray( [3, 1, -1, -3, -2, -1, 1, 2, 3, 1, -1, -3, -2, -1, 1, 2] ) elif mode == "antireflect": y_expected = cp.asarray( [1, 2, 3, 1, -1, 0, 1, 2, 3, 1, -1, 0, 1, 2, 3, 1] ) elif mode == "smooth": y_expected = cp.asarray( [-5, -4, -3, -2, -1, 0, 1, 2, 3, 1, -1, -3, -5, -7, -9, -11] ) elif mode == "line": lin_slope = (x[-1] - x[0]) / (len(x) - 1) left = x[0] + cp.arange(-npre, 0, 1) * lin_slope right = x[-1] + cp.arange(1, npost + 1) * lin_slope y_expected = cp.concatenate((left, x, right)) else: y_expected = cp.pad(x, (npre, npost), mode=mode) assert_allclose(y, y_expected)
def backward(self, loss): # batch, height, width, num_filter input_padded = np.pad(self.inputt, pad_width=((0, 0), (self.padding_shape[0], self.padding_shape[0]), (self.padding_shape[1], self.padding_shape[1]), (0, 0))) output = np.zeros_like(input_padded) for i in range(loss.shape[1]): for j in range(loss.shape[2]): height_start = i * self.stride height_end = height_start + self.weights.shape[0] width_start = j * self.stride width_end = width_start + self.weights.shape[1] output[:, height_start:height_end, width_start:width_end, :] += np.sum( self.weights[np.newaxis, :, :, :, :] * loss[:, i:i + 1, j:j + 1, np.newaxis, :], axis=4) self.dweights += np.sum( input_padded[:, height_start:height_end, width_start:width_end, :, np.newaxis] * loss[:, i:i + 1, j:j + 1, np.newaxis, :], axis=0) self.dweights /= self.inputt.shape[0] return output[:, self.padding_shape[0]:self.padding_shape[0] + self.inputt.shape[1], self.padding_shape[1]:self.padding_shape[1] + self.inputt.shape[2], :]
def create_csr_matrix_from_count_df(count_df, empty_doc_ids, n_doc, n_features, dtype=cp.float32): """ Create a sparse matrix from the count of tokens by document Parameters ---------- count_df = cudf.DataFrame({'count':..., 'doc_id':.., 'token':.. }) sorted by doc_id and token empty_doc_ids = cupy array containing doc_ids with no tokens n_doc: Total number of documents n_features: Number of features dtype: Output dtype """ data = count_df["count"].values indices = count_df["token"].values doc_token_counts = count_df["doc_id"].value_counts().reset_index() del count_df doc_token_counts = doc_token_counts.rename( {"doc_id": "token_counts", "index": "doc_id"}, axis=1 ).sort_values(by="doc_id") token_counts = _insert_zeros( doc_token_counts["token_counts"], empty_doc_ids ) indptr = token_counts.cumsum() indptr = cp.pad(indptr, (1, 0), "constant") return cupyx.scipy.sparse.csr_matrix( arg1=(data, indices, indptr), dtype=dtype, shape=(n_doc, n_features) )
def forward(self, is_training: bool = True): W, b = self.variables # pad inputs = cp.pad(self.input_tensor, ((0, 0), (0, 0), self.pad_size, self.pad_size), 'constant') # padded size batch_nums = inputs.shape[0] _, n_C, n_H, n_W = self.output_shape col = im2col(inputs, self.output_shape, self.filter_size, self.stride) col_W = W.output_tensor.reshape(self.filter_nums, -1).T output = col.dot(col_W) + b.output_tensor output = output.reshape(batch_nums, n_H, n_W, -1).transpose(0, 3, 1, 2) self.output_tensor = self.activator._forward(output) #store it for bp if is_training: self.input_shape = self.input_tensor.shape self.cols = col.T self.col_W = col_W if self.require_grads: self.grads = cp.zeros_like(self.output_tensor) super().forward(is_training)
def calculate_Ez(config, jx, jy): """ Calculate Ez as iDST2D(dirichlet_matrix * DST2D(djx/dx + djy/dy)). """ # 0. Calculate RHS (NOTE: it is smaller by 1 on each side). # NOTE: use gradient instead if available (cupy doesn't have gradient yet). djx_dx = jx[2:, 1:-1] - jx[:-2, 1:-1] djy_dy = jy[1:-1, 2:] - jy[1:-1, :-2] rhs_inner = -(djx_dx + djy_dy) / (config.grid_step_size * 2) # -? # 1. Apply DST-Type1-2D (Discrete Sine Transform Type 1 2D) to the RHS. f = dst2d(rhs_inner) # 2. Multiply f by the special matrix that does the job and normalizes. f *= dirichlet_matrix(config.grid_steps, config.grid_step_size) # 3. Apply iDST-Type1-2D (Inverse Discrete Sine Transform Type 1 2D). # We don't have to define a separate iDST function, because # unnormalized DST-Type1 is its own inverse, up to a factor 2(N+1) # and we take all scaling matters into account with a single factor # hidden inside dirichlet_matrix. Ez_inner = dst2d(f) Ez = cp.pad(Ez_inner, 1, 'constant', constant_values=0) numba.cuda.synchronize() return Ez
def _prepad_for_spline_filter(input, mode, cval): if mode in ["nearest", "grid-constant"]: npad = 12 if mode == "grid-constant": padded = cupy.pad(input, npad, mode="constant", constant_values=cval) elif mode == "nearest": padded = cupy.pad(input, npad, mode="edge") else: # other modes have exact boundary conditions implemented so # no prepadding is needed npad = 0 padded = input return padded, npad
def im2col(input_data, filter_h: int, filter_w: int, stride=1, pad=0): ''' Given 1. input_data: input data consisting of a 4-D array of (#observations, #channels, height, width) 2. filter_h: filter height 3. filter_w: filter width 4. stride: stride 5. pad: padding Return col: 2-D array ''' 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 = 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 y in range(filter_h): y_max = y + stride * out_h for x in range(filter_w): x_max = x + stride * out_w col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride] col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N * out_h * out_w, -1) return col
def __init__(self, dim_in, f, c, stride, pad, activation='relu'): """Initialise the convolutional layer of the neural network. """ self.dim_in = dim_in self.dim_out = (dim_in[0], 1 + int( (dim_in[1] - f + 2 * pad) / stride), 1 + int( (dim_in[2] - f + 2 * pad) / stride), c) self.f = f self.n_c = c self.stride = stride self.pad = pad self.activation = activation self.lamb = 0 self.X_pad = np.pad(np.zeros(dim_in), ((0, 0), (pad, pad), (pad, pad), (0, 0)), mode='constant', constant_values=(0, 0)) self.Z = np.zeros(self.dim_out) self.W = self.__init_weights(f, c, dim_in) self.b = np.zeros((1, 1, 1, c)) self.dX = np.zeros(dim_in) self.dW = np.zeros(self.W.shape) self.db = np.zeros(self.b.shape) self.vW = np.zeros(self.W.shape) self.vb = np.zeros(self.b.shape) self.sW = np.zeros(self.W.shape) self.sb = np.zeros(self.b.shape) self.dZ_pad = self._allocate_dZ_pad(dim_in[0])
def __init__(self, env_arr, seed=300): """Initialize environment. Pad env with NaN in every axis. It will help us to find neighbours of cells even if they are edges. env should have 3 dimensions (XYZ). {_agents_positions} is an array of current XYZ position for every agent. {agents_state} is an array of agents states. 1 - live, 0 - dead {is_available_env} - env-like boolean array, where True indicates free cell :param env_arr: array of environment, where agents born. move and die. """ self._raw_env = env_arr self.env = cp.pad(cp.array(env_arr).astype(cp.float), 1, mode='constant', constant_values=cp.NaN) self.agents_state = None self._agents_positions = None self._agents_positions_all_time = [] self.is_available_env = ~cp.isnan(self.env) self._rng = np.random.default_rng(seed) np.random.seed(seed) cp.random.seed(seed)
def _pad_to(arr, shape): """Pad an array with trailing zeros to a given target shape. Parameters ---------- arr : ndarray The input array. shape : tuple The target shape. Returns ------- padded : ndarray The padded array. Examples -------- >>> _pad_to(np.ones((1, 1), dtype=int), (1, 3)) array([[1, 0, 0]]) """ if not all(s >= i for s, i in zip(shape, arr.shape)): raise ValueError("Target shape must be strictly greater " "than input shape.") padding = [(0, s - i) for s, i in zip(shape, arr.shape)] return cp.pad(arr, pad_width=padding, mode="constant", constant_values=0)
def o_f4(imgs, hres_size, row=None, do_fil=False, show=False): """ Fourier Spectrum Initialization #1 for V2 ---------------------------------- Mean Image > sqrt > Ft > (optional) filter Ft with 2 * cutoff_freq > pad Ft > Return Ft """ im = cp.array(imgs).mean(0) f = cp_forward_fourier(cp.sqrt(im)) if do_fil: _orig = int(im.shape[0]) // 2 - 1 CUTOFF_FREQ_px = get_cutoff(row) fil = np.zeros((int(im.shape[0]), int(im.shape[0]))) fil = cp.array( cv2.circle(fil, (_orig, _orig), 2 * CUTOFF_FREQ_px, 1, -1)) f = f * fil pad = (hres_size[0] - imgs[0].shape[0]) // 2 f = cp.pad(f, [(pad, pad), (pad, pad)]) if show: plt.imshow(cp.asnumpy(cp.log(abs(f) + 1e-7))) plt.title(f'o_f1 {"without" if not do_fil else "with"} filtering') plt.show() return f
def conv_backward(self, dA): """Naive backward propagation implementation Args: dA (np.array): gradient of output values Returns: np.array: dX gradient of input values """ self.dW[:, :, :, :] = 0 self.db[:, :, :, :] = 0 (m, n_h, n_w, n_c) = dA.shape dx_pad = np.pad(self.dX, ((0, 0), (self.pad, self.pad), (self.pad, self.pad), (0, 0)), mode='constant', constant_values=(0, 0)) dZ = dA * self._deriv_relu(self.Z) for h in range(n_h): v_s = h * self.stride v_e = h * self.stride + self.f for w in range(n_w): h_s = w * self.stride h_e = w * self.stride + self.f for c in range(n_c): dx_pad[:, v_s:v_e, h_s:h_e, :] += self.W[:, :, :, c] \ * dZ[:, h, w, c].reshape(dZ.shape[0], 1, 1, 1) self.dW[:, :, :, c] += np.sum( self.X_pad[:, v_s:v_e, h_s:h_e] * dZ[:, h, w, c].reshape(dZ.shape[0], 1, 1, 1), axis=0) self.db[:, :, :, c] += np.sum(dZ[:, h, w, c]) self.dX[:, :, :, :] = dx_pad[:, self.pad:-self.pad, self.pad:-self.pad, :] self.dW += self.lamb / self.dim_in[0] * self.W return self.dX
def im2col(input_data, filter_h, filter_w, stride=1, pad=0, fill=0): """ Parameters ---------- input_data : (データ数, チャンネル, 高さ, 幅)の4次元配列からなる入力データ filter_h : フィルターの高さ filter_w : フィルターの幅 stride : ストライド pad : パディング fill : パディングを埋める値 Returns ------- col : 2次元配列 """ 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 = cp.pad(input_data, [(0, 0), (0, 0), (pad, pad), (pad, pad)], 'constant', constant_values=(fill, fill)) col = cp.zeros((N, C, filter_h, filter_w, out_h, out_w), dtype=np.float32) for y in range(filter_h): y_max = y + stride * out_h for x in range(filter_w): x_max = x + stride * out_w col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride] col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N * out_h * out_w, -1) return col
def forward_prop(self, x): """ Forward propagation. Parameters ---------- x : cp.array of floats, shape (nr_examples,) + self.input_size Inputs. Returns ------- cp.array of floats, shape (nr_examples,) + self.output_size Outputs. """ # Add padding. x_pad = cp.pad(x, self.padding, 'constant', constant_values=0) # Keep track of dimensions. nr_examples, _, _, k = x.shape m, n, c = self.output_size p, q = self.filter_size # Create x_cache and z_cache. x_pad_times_d_x = cp.tensordot(x_pad, self.d_x, axes=((1, ), (0, ))) self.x_cache = cp.tensordot(x_pad_times_d_x, self.d_y, axes=((1, ), (0, ))) self.z_cache = cp.tensordot( self.x_cache, self.weights, axes=((1, 2, 4), (0, 1, 2))) + self.bias return self.ac_func(self.z_cache)
def o_f3(imgs, hres_size, row=None, do_fil=False, show=False): """ Fourier Spectrum Initialization #3 ---------------------------------- Mean Image > pad with reflect padding > sqrt > Ft > (optional) filter Ft with 2 * cutoff_freq > Return Ft """ im = cp.array(imgs).mean(0) pad = (hres_size[0] - imgs[0].shape[0]) // 2 im = cp.pad(cp.array(im), [(pad, pad), (pad, pad)], mode='reflect') f = Ft(cp.sqrt(im)) if do_fil: _orig = hres_size[0] // 2 - 1 CUTOFF_FREQ_px = get_cutoff(row) fil = np.zeros(hres_size) fil = cp.array( cv2.circle(fil, (_orig, _orig), 2 * CUTOFF_FREQ_px, 1, -1)) f = f * fil if show: plt.imshow(cp.asnumpy(cp.log(abs(f) + 1e-7))) plt.title(f'o_f3 {"without" if not do_fil else "with"} filtering') plt.show() return f
def map_coordinates(input, coordinates, output=None, order=3, mode='constant', cval=0.0, prefilter=True): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation, default is 3. Must be in the range 0-5. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'``, ``'reflect'``, ``'wrap'``, ``'grid-mirror'``, ``'grid-wrap'``, ``'grid-constant'`` or ``'opencv'``). cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter('map_coordinates', order, mode) if mode == 'opencv' or mode == '_opencv_edge': input = cupy.pad(input, [(1, 1)] * input.ndim, 'constant', constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = 'constant' ret = _util._get_output(output, input, coordinates.shape[1:]) integer_output = ret.dtype.kind in 'iu' _util._check_cval(mode, cval, integer_output) if input.dtype.kind in 'iu': input = input.astype(cupy.float32) coordinates = _check_coordinates(coordinates, order) filtered, nprepad = _filter_input(input, prefilter, mode, cval, order) large_int = max(_prod(input.shape), coordinates.shape[0]) > 1 << 31 kern = _interp_kernels._get_map_kernel( input.ndim, large_int, yshape=coordinates.shape, mode=mode, cval=cval, order=order, integer_output=integer_output, nprepad=nprepad) kern(filtered, coordinates, ret) return ret