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])
python类moveaxis()的实例源码
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 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 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 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 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