我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.moveaxis()。
def data_from_grid (cells, gridwidth, gridheight, grid=32): width = cells.shape[4] crop = (width - grid ) // 2 ## for simplicity we are assuming the same crop (and grid) in x & y directions if crop > 0: # do NOT crop with 0 as we get empty cells ... cells = cells[:,:,:,crop:-crop,crop:-crop] shape = cells.shape new_shape_1_dim = shape[0]// (gridwidth * gridheight) # ws // 36 -- Improved on 20170306 new_shape = (gridwidth * gridheight, new_shape_1_dim, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 cells = np.reshape(cells, new_shape) cells = np.moveaxis(cells, 0, -3) shape = cells.shape new_shape2 = tuple([x for x in shape[0:3]]) + (gridheight, gridwidth,) + tuple([x for x in shape[4:]]) cells = np.reshape(cells, new_shape2) cells = cells.swapaxes(-2, -3) shape = cells.shape combine_shape =tuple([x for x in shape[0:3]]) + (shape[-4]*shape[-3], shape[-2]*shape[-1],) cells = np.reshape(cells, combine_shape) return cells
def generateCountMaps(self, coords): '''Generates a count map for the provided list of coordinates. ''' s = self.config['projective_field_size'] unpadded_size = self.config['output_size'] target_size = 3 + unpadded_size + 2 * s countMaps = np.zeros((self.config['cls_nb'], target_size, target_size), dtype=np.int16) for coord in coords: y = coord[1] - self.config['contextual_pad'] x = coord[2] - self.config['contextual_pad'] if y >= 0 and y < self.config['tile_size'] and \ x >= 0 and x < self.config['tile_size']: self.inc_region(countMaps[coord[0]], *self.target_sizes[y, x]) return np.moveaxis(countMaps, 0, -1).astype(np.float32)
def generateCountMaps(self, coords): '''Generates a count map for the provided list of coordinates. It can count at most 256 object within the receptive field. Beyond that it overflows. ''' s = self.config['receptive_field_size'] pad = s // 2 unpadded_size = self.config['tile_size'] target_size = 1 + unpadded_size + 2 * pad countMaps = np.zeros((self.config['cls_nb'], target_size, target_size), dtype=np.int16) y_min = 0 y_max = unpadded_size x_min = 0 x_max = unpadded_size for coord in coords: if coord[1] >= y_min and coord[1] < y_max and coord[2] >= x_min and coord[2] < x_max: self.inc_region(countMaps[coord[0]], coord[1] + pad, coord[2] + pad, s, s) return np.moveaxis(countMaps, 0, -1).astype(np.float32)
def preprocessExample(self, image, coords, angle, shear_x, shear_y, scale): size_in = image.shape[0] size_out = self.config['tile_size'] + 2 * self.config['contextual_pad'] # h = base64.b64encode(struct.pack(">q", hash(image.tostring()))).decode() # data_preparation.imshow(image, coords=coords, save=True, title='%s_preprocessExampleA' %h) image = self.applyLinearTransformToImage(image, angle, shear_x, shear_y, scale, size_out) image = self.applyColorAugmentation(image, self.config['aug_color_std'], \ self.config['aug_gamma_factor']) coords[:, 1:] = self.applyLinearTransformToCoords(coords[:, 1:], angle, shear_x, shear_y, scale, size_in, size_out) target = self.generateCountMaps(coords) large_target = self.generateLargeCountMaps(coords) if self.config['draw_border'] and self.config['contextual_pad'] > 0: image = self.draw_border(image, self.config['contextual_pad'], self.config['tile_size']) # data_preparation.imshow(image, coords=coords, save=True, title='%s_preprocessExampleB' % h) # t = np.concatenate(np.moveaxis(target, -1, 0)) # data_preparation.imshow(t, normalize=True, save=True, title='%s_preprocessExampleC' % h) return image.astype(np.float32), target, large_target
def test_errors(self): x = np.random.randn(1, 2, 3) assert_raises_regex(ValueError, 'invalid axis .* `source`', np.moveaxis, x, 3, 0) assert_raises_regex(ValueError, 'invalid axis .* `source`', np.moveaxis, x, -4, 0) assert_raises_regex(ValueError, 'invalid axis .* `destination`', np.moveaxis, x, 0, 5) assert_raises_regex(ValueError, 'repeated axis in `source`', np.moveaxis, x, [0, 0], [0, 1]) assert_raises_regex(ValueError, 'repeated axis in `destination`', np.moveaxis, x, [0, 1], [1, 1]) assert_raises_regex(ValueError, 'must have the same number', np.moveaxis, x, 0, [0, 1]) assert_raises_regex(ValueError, 'must have the same number', np.moveaxis, x, [0, 1], [0])
def show( scheme, tet=numpy.array([ [+1, 0, -1.0/numpy.sqrt(2.0)], [-1, 0, -1.0/numpy.sqrt(2.0)], [0, +1, +1.0/numpy.sqrt(2.0)], [0, -1, +1.0/numpy.sqrt(2.0)], ]), backend='mpl' ): edges = numpy.array([[tet[i], tet[j]] for i in range(4) for j in range(i)]) edges = numpy.moveaxis(edges, 1, 2) helpers.backend_to_function[backend]( transform(scheme.points.T, tet.T).T, scheme.weights, get_vol(tet), edges ) return
def get_vol(simplex): # Compute the volume via the Cayley-Menger determinant # <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One # advantage is that it can compute the volume of the simplex indenpendent # of the dimension of the space where it's embedded. # compute all edge lengths edges = numpy.subtract(simplex[:, None], simplex[None, :]) ei_dot_ej = numpy.einsum('...k,...k->...', edges, edges) j = simplex.shape[0] - 1 a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:]) a[1:, 1:] = ei_dot_ej a[0, 1:] = 1.0 a[1:, 0] = 1.0 a[0, 0] = 0.0 a = numpy.moveaxis(a, (0, 1), (-2, -1)) det = numpy.linalg.det(a) vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det) return vol
def test_horizontal_flip_with_swap(self): """ Test horizontal flip with swapping axes NB: horizontal flip <=> reverse order on columns """ np.random.seed(0) # 5 images of size 10 x 10 x 3 shape = [5, 10, 10, 3] batch = np.random.random(size=shape).astype(np.float32) # move axis and then flip horizontally flipped_1 = np.moveaxis(batch, 3, 1)[:, :, :, ::-1] # flip horizontally without moving axis flipped_2 = batch[:, :, ::-1, :] for i in range(shape[0]): for j in range(shape[3]): assert np.all(np.isclose(flipped_1[i, j], flipped_2[i, :, :, j]))
def load_train_image(data_dir, img_name, is_hflip=False, hshift=0, vshift=0, rotate=0, scale_size=0, is_color_trans=False, is_fancy_pca_trans=False, is_edge_enh_trans=False, test_time_aug=None, paddings=None, tile_size=None): ''' load a train image ''' img_file_name = tile.get_img_name(img_name) img_ext = 'jpg' img = load_image_file(data_dir, img_file_name, img_ext, rotate) # img.shape: (height, width, 3) if is_color_trans : img = color.transform(img) if is_fancy_pca_trans: img = fancy_pca.rgb_shift(img) if is_edge_enh_trans: img = cv2.detailEnhance(img, sigma_s=5, sigma_r=0.1) img = np.moveaxis(img, 2, 0) # img.shape: (3, height, width) return preprocess(img, img_name, is_hflip, hshift, vshift, scale_size, paddings, tile_size, test_time_aug)
def _do_bprop(self, env, idx): if idx == 0: x = self.inputs[0].get_value() i = self.inputs[1].get_value() g = self.outputs[0].get_grad() xshape = x.shape axis, ndim, alen = self._axis, len(xshape), xshape[self._axis] i_hat = i.reshpae(-1) g_hat = g.reshape(-1) r_hat = np.zeros((i.shape[0], alen), dtype=x.dtype) r_hat[np.arange(r_hat.shape[0]), i_hat] = g_hat r = r_hat.reshape(xshape[:axis] + xshape[axis+1:] + (alen, )) r = np.moveaxis(r, -1, axis) return r else: return 0
def fold(unfolded_tensor, mode, shape): """Refolds the mode-`mode` unfolding into a tensor of shape `shape` In other words, refolds the n-mode unfolded tensor into the original tensor of the specified shape. Parameters ---------- unfolded_tensor : ndarray unfolded tensor of shape ``(shape[mode], -1)`` mode : int the mode of the unfolding shape : tuple shape of the original tensor before unfolding Returns ------- ndarray folded_tensor of shape `shape` """ full_shape = list(shape) mode_dim = full_shape.pop(mode) full_shape.insert(0, mode_dim) return np.moveaxis(unfolded_tensor.reshape(full_shape), 0, mode)
def equalize(**kwargs): """ Equalizes the image histogram, per channel. :param kwargs: Additional arguments for skimage.exposure.equalize_hist. :return: The equalize function. """ def f(x): if keras.backend.image_data_format() == 'channels_last': x = numpy.moveaxis(x, -1, 0) y = numpy.empty_like(x, dtype=numpy.float64) for index, img in enumerate(x): y[index] = skimage.exposure.equalize_hist(img, **kwargs) if keras.backend.image_data_format() == 'channels_last': y = numpy.moveaxis(y, 0, -1) return y return f
def reduce_noise(**kwargs): """ Reduces noise in the image. :param kwargs: Additional arguments for skimage.restoration.denoise_bilateral. :return: The reduce_noise function. """ def f(x): if keras.backend.image_data_format() == 'channels_last': x = numpy.moveaxis(x, -1, 0) y = numpy.empty_like(x, dtype=numpy.float64) for index, img in enumerate(x): y[index] = skimage.restoration.denoise_bilateral(img, **kwargs) if keras.backend.image_data_format() == 'channels_last': y = numpy.moveaxis(y, 0, -1) return y return f
def get_experience(seq, action, reward, hist_len, episode_done): exp_state = list() exp_new_state = list() ''' If we don't have enough images to produce a history ''' if len(seq) < hist_len + 1: num_copy = hist_len - (len(seq) - 1) for i in range(num_copy): exp_state.append(seq[0]) for i in range(len(seq) - 1): exp_state.append(seq[i]) num_copy = hist_len - len(seq) for i in range(num_copy): exp_new_state.append(seq[0]) for i in range(len(seq)): exp_new_state.append(seq[i]) else: exp_state = seq[-hist_len - 1 : -1] exp_new_state = seq[-hist_len:] exp = Experience(state=np.moveaxis(exp_state, 0, -1), action=action, reward=reward, new_state=np.moveaxis(exp_new_state, 0, -1), game_over=episode_done) return exp
def vandermonde_evaluate_expansion_all(self, input_array, output_array): """Naive implementation of evaluate_expansion_all args: input_array (input) Expansion coefficients output_array (output) Function values on quadrature mesh """ assert abs(self.padding_factor-1) < 1e-8 assert self.N == output_array.shape[self.axis] points = self.points_and_weights(self.N)[0] V = self.vandermonde(points) P = self.get_vandermonde_basis(V) if output_array.ndim == 1: output_array = np.dot(P, input_array, out=output_array) else: fc = np.moveaxis(input_array, self.axis, -2) array = np.dot(P, fc) output_array[:] = np.moveaxis(array, 0, self.axis) assert output_array is self.backward.output_array assert input_array is self.backward.input_array return output_array
def matvec(self, v, c, format='self', axis=0): N = self.shape[0] c.fill(0) if format == 'self': if axis > 0: c = np.moveaxis(c, axis, 0) v = np.moveaxis(v, axis, 0) s = (slice(None),) + (np.newaxis,)*(v.ndim-1) # broadcasting ve = v[-2:0:-2].cumsum(axis=0) vo = v[-1:0:-2].cumsum(axis=0) c[-3::-2] = ve*2.0 c[-2::-2] = vo*2.0 if axis > 0: c = np.moveaxis(c, 0, axis) v = np.moveaxis(v, 0, axis) else: c = super(SpectralMatrix, self).matvec(v, c, format=format, axis=axis) return c
def vandermonde_scalar_product(self, input_array, output_array): """Naive implementation of scalar product args: input_array (input) Function values on quadrature mesh output_array (output) Expansion coefficients """ assert abs(self.padding_factor-1) < 1e-8 assert self.N == input_array.shape[self.axis] points, weights = self.points_and_weights(self.N) V = self.vandermonde(points) P = self.get_vandermonde_basis(V) if input_array.ndim == 1: output_array[:] = np.dot(input_array*weights, np.conj(P)) else: # broadcasting bc_shape = [np.newaxis,]*input_array.ndim bc_shape[self.axis] = slice(None) fc = np.moveaxis(input_array*weights[bc_shape], self.axis, -1) output_array[:] = np.moveaxis(np.dot(fc, np.conj(P)), -1, self.axis) assert output_array is self.forward.output_array return output_array
def matvec(self, v, c, format='cython', axis=0): N = self.shape[0] c.fill(0) if format == 'self': if axis > 0: v = np.moveaxis(v, axis, 0) c = np.moveaxis(c, axis, 0) s = (slice(None),) + (np.newaxis,)*(v.ndim-1) # broadcasting c[:N-1] = self[1][s]*v[1:N] c[1:N] += self[-1][s]*v[:(N-1)] if axis > 0: v = np.moveaxis(v, 0, axis) c = np.moveaxis(c, 0, axis) elif format == 'cython' and v.ndim == 3: CDDmat_matvec(self[1], self[-1], v, c, axis) else: c = super(SpectralMatrix, self).matvec(v, c, format=format, axis=axis) return c
def matvec(self, v, c, format='cython', axis=0): N, M = self.shape c.fill(0) if format == 'self': if axis > 0: c = np.moveaxis(c, axis, 0) v = np.moveaxis(v, axis, 0) s = (slice(None),) + (np.newaxis,)*(v.ndim-1) # broadcasting c[1:N] = self[-1][s]*v[:M-3] c[:N] += self[1][s]*v[1:M-1] c[:N-1] += self[3][s]*v[3:M] if axis > 0: c = np.moveaxis(c, 0, axis) v = np.moveaxis(v, 0, axis) elif format == 'cython' and v.ndim == 3: CBD_matvec3D(v, c, self[-1], self[1], self[3], axis) elif format == 'cython' and v.ndim == 1: CBD_matvec(v, c, self[-1], self[1], self[3]) else: c = super(SpectralMatrix, self).matvec(v, c, format=format, axis=axis) return c
def matvec(self, v, c, format='cython', axis=0): N, M = self.shape c.fill(0) if format == 'self': if axis > 0: c = np.moveaxis(c, axis, 0) v = np.moveaxis(v, axis, 0) s = (slice(None),) + (np.newaxis,)*(v.ndim-1) # broadcasting c[3:N] = self[-3][s] * v[:M-1] c[1:N-1] += self[-1][s] * v[:M] c[:N-3] += self[1][s] * v[1:M] if axis > 0: c = np.moveaxis(c, 0, axis) v = np.moveaxis(v, 0, axis) elif format == 'cython' and v.ndim == 3: CDB_matvec3D(v, c, self[-3], self[-1], self[1], axis) else: c = super(SpectralMatrix, self).matvec(v, c, format=format, axis=axis) return c
def data_from_grid (cells, gridwidth, gridheight, grid=32): height = cells.shape[3] # should be 224 for our data width = cells.shape[4] crop = (width - grid ) // 2 ## for simplicity we are assuming the same crop (and grid) vertically and horizontally dspacing = gridwidth * gridheight layers = cells.shape[0] // dspacing if crop > 0: # do NOT crop with 0 as we get empty cells ... cells = cells[:,:,:,crop:-crop,crop:-crop] if crop > 2*grid: print ("data_from_grid Warning, unusually large crop (> 2*grid); crop, & grid, gridwith, gridheight: ", (crop, grid, gridwidth, gridheight)) shape = cells.shape new_shape_1_dim = shape[0]// (gridwidth * gridheight) # ws // 36 -- Improved on 20170306 new_shape = (gridwidth * gridheight, new_shape_1_dim, ) + tuple([x for x in shape][1:]) # was 36, Improved on 20170306 cells = np.reshape(cells, new_shape) cells = np.moveaxis(cells, 0, -3) shape = cells.shape new_shape2 = tuple([x for x in shape[0:3]]) + (gridheight, gridwidth,) + tuple([x for x in shape[4:]]) cells = np.reshape(cells, new_shape2) cells = cells.swapaxes(-2, -3) shape = cells.shape combine_shape =tuple([x for x in shape[0:3]]) + (shape[-4]*shape[-3], shape[-2]*shape[-1],) cells = np.reshape(cells, combine_shape) return cells
def preprocessExample(self, image, coords, angle, shear_x, shear_y, scale): '''This function is meant to be run as a tf.py_func node on a single example. It returns a randomly perturbed and correctly cropped and padded image and generates one or multiple targets. image, target = tf.py_func(preprocessExample, [image, coords, class_ids], [tf.float32, tf.float32]) Args: image: A single training image with value range [0, 1]. Returns: A tuple containing an image and a table of coordinates. ''' size_in = image.shape[0] size_out = self.config['tile_size'] + 2 * self.config['contextual_pad'] # h = base64.b64encode(struct.pack(">q", hash(image.tostring()))).decode() # data_preparation.imshow(image, coords=coords, save=True, title='%s_preprocessExampleA' %h) image = self.applyLinearTransformToImage(image, angle, shear_x, shear_y, scale, size_out) image = self.applyColorAugmentation(image, self.config['aug_color_std'], \ self.config['aug_gamma_factor']) coords[:, 1:] = self.applyLinearTransformToCoords(coords[:, 1:], angle, shear_x, shear_y, scale, size_in, size_out) target = self.generateCountMaps(coords) if self.config['draw_border'] and self.config['contextual_pad'] > 0: image = self.draw_border(image, self.config['contextual_pad'], self.config['tile_size']) # data_preparation.imshow(image, coords=coords, save=True, title='%s_preprocessExampleB' % h) # t = np.concatenate(np.moveaxis(target, -1, 0)) # data_preparation.imshow(t, normalize=True, save=True, title='%s_preprocessExampleC' % h) return image.astype(np.float32), target
def generateLargeCountMaps(self, coords): '''Generates a count map for the provided list of coordinates. ''' c = self.config['target_context_pad'] target_size = 3 + self.config['output_size'] + 2 * c count_maps = np.zeros((self.config['cls_nb'], target_size, target_size), dtype=np.int16) # We want coordinates relative to the fully padded large size. For that we # first get coordinates wrt the unpadded tile and then set the upper left # corner of the large size as the origin. pad = self.config['large_contextual_pad'] shift = - self.config['contextual_pad'] + pad r = self.config['receptive_field_size'] tile_size = self.config['tile_size'] size = tile_size + 2 * pad for coord in coords: y = coord[1] + shift x = coord[2] + shift if (not (y >= pad and y < pad + tile_size and \ x >= pad and x < pad + tile_size)) and \ y >= r and y < size - r and \ x >= r and x < size - r: self.inc_region(count_maps[coord[0]], *self.target_sizes_large[y - r, x - r]) # t = np.concatenate(count_maps) # data_preparation.imshow(t, normalize=True, save=True, title='large') return np.moveaxis(count_maps, 0, -1).astype(np.float32)
def test_move_to_end(self): x = np.random.randn(5, 6, 7) for source, expected in [(0, (6, 7, 5)), (1, (5, 7, 6)), (2, (5, 6, 7)), (-1, (5, 6, 7))]: actual = np.moveaxis(x, source, -1).shape assert_(actual, expected)
def test_move_new_position(self): x = np.random.randn(1, 2, 3, 4) for source, destination, expected in [ (0, 1, (2, 1, 3, 4)), (1, 2, (1, 3, 2, 4)), (1, -1, (1, 3, 4, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
def test_preserve_order(self): x = np.zeros((1, 2, 3, 4)) for source, destination in [ (0, 0), (3, -1), (-1, 3), ([0, -1], [0, -1]), ([2, 0], [2, 0]), (range(4), range(4)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, (1, 2, 3, 4))
def test_move_multiples(self): x = np.zeros((0, 1, 2, 3)) for source, destination, expected in [ ([0, 1], [2, 3], (2, 3, 0, 1)), ([2, 3], [0, 1], (2, 3, 0, 1)), ([0, 1, 2], [2, 3, 0], (2, 3, 0, 1)), ([3, 0], [1, 0], (0, 3, 1, 2)), ([0, 3], [0, 1], (0, 3, 1, 2)), ]: actual = np.moveaxis(x, source, destination).shape assert_(actual, expected)
def tensor_function(self, tensor): tensor = np.asarray(tensor) if tensor.ndim == 3: # There's a channel axis - we move it to front tensor = np.moveaxis(tensor, source=-1, destination=0) elif tensor.ndim == 2: pass else: raise NotImplementedError("Expected tensor to be a 2D or 3D " "numpy array, got a {}D array instead." .format(tensor.ndim)) return tensor