def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
python类copyto()的实例源码
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
visualization_utils.py 文件源码
项目:satellite-image-object-detection
作者: marcbelmont
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def draw_keypoints_on_image_array(image,
keypoints,
color='red',
radius=2,
use_normalized_coordinates=True):
"""Draws keypoints on an image (numpy array).
Args:
image: a numpy array with shape [height, width, 3].
keypoints: a numpy array with shape [num_keypoints, 2].
color: color to draw the keypoints with. Default is red.
radius: keypoint radius. Default value is 2.
use_normalized_coordinates: if True (default), treat keypoint values as
relative to the image. Otherwise treat them as absolute.
"""
image_pil = Image.fromarray(np.uint8(image)).convert('RGB')
draw_keypoints_on_image(image_pil, keypoints, color, radius,
use_normalized_coordinates)
np.copyto(image, np.array(image_pil))
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
def quantize_net(net, codebook):
layers = codebook.keys()
codes_W = {}
print "================Perform quantization=============="
for layer in layers:
print "Quantize layer:", layer
W = net.params[layer][0].data
codes, _ = scv.vq(W.flatten(), codebook[layer]) # ???????????
# codes = stochasitc_quantize2(W.flatten(), codebook[layer]) # ?????????
codes = np.reshape(codes, W.shape)
codes_W[layer] = np.array(codes, dtype=np.uint32)
# ?????????????
W_q = np.reshape(codebook[layer][codes], W.shape)
np.copyto(net.params[layer][0].data, W_q)
return codes_W
def upload_indices(self, context):
'''
Upload indices to graphic card
*Parameters:*
- `context`: `VulkContext`
**Note: Mesh must be indexed**
'''
if not self.has_indices:
raise Exception('No index in this mesh')
if not self.dirty_indices:
return
self.dirty_indices = False
with self.indices_buffer.bind(context) as b:
np.copyto(np.array(b, copy=False),
self.indices_array.view(dtype=np.uint8),
casting='no')
def upload_vertices(self, context):
'''
Upload vertices to graphic card
*Parameters:*
- `context`: `VulkContext`
'''
if not self.dirty_vertices:
return
self.dirty_vertices = False
with self.vertices_buffer.bind(context) as b:
np.copyto(np.array(b, copy=False),
self.vertices_array.view(dtype=np.uint8),
casting='no')
def copy_parameters_from(self, params):
"""Copies parameters from another source without reallocation.
Args:
params (Iterable): Iterable of parameter arrays.
"""
for dst, src in zip(self.parameters, params):
if isinstance(dst, numpy.ndarray):
if isinstance(src, numpy.ndarray):
numpy.copyto(dst, src)
else:
dst[:] = src.get()
elif isinstance(src, numpy.ndarray):
dst.set(src)
else:
cuda.copy(src, out=dst)
test_api.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
core.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 38
收藏 0
点赞 0
评论 0
def outer(self, a, b):
"""
Return the function applied to the outer product of a and b.
"""
(da, db) = (getdata(a), getdata(b))
d = self.f.outer(da, db)
ma = getmask(a)
mb = getmask(b)
if ma is nomask and mb is nomask:
m = nomask
else:
ma = getmaskarray(a)
mb = getmaskarray(b)
m = umath.logical_or.outer(ma, mb)
if (not m.ndim) and m:
return masked
if m is not nomask:
np.copyto(d, da, where=m)
if not d.shape:
return d
masked_d = d.view(get_masked_subclass(a, b))
masked_d._mask = m
masked_d._update_from(d)
return masked_d
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
def _sample_words(model, c, maxlen, V_C, K=20):
def predict(samples):
context = np.array([c] * len(samples))
prev_chars = np.zeros((len(samples), maxlen), dtype=np.int32)
probs = np.zeros((len(samples), V_C.size), dtype=np.float32)
for i, prev in enumerate(samples):
for j, ch in enumerate(prev):
prev_chars[i, j + 1] = ch + 1
preds = model.predict_chars(context, prev_chars)
for i, prev in enumerate(samples):
np.copyto(probs[i], preds[i, len(prev)])
return probs
eow = V_C.get_index(EOW)
best_chars, losses = beamsearch(predict, eow, k=K, maxsample=maxlen)
best_words = []
for word_chars in best_chars:
word = ""
for ch in word_chars:
if ch == eow:
break
word += V_C.get_token(ch)
best_words.append(word)
probs = 1. / np.exp(np.array(losses))
return best_words, probs
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
def outer(self, a, b):
"""
Return the function applied to the outer product of a and b.
"""
(da, db) = (getdata(a), getdata(b))
d = self.f.outer(da, db)
ma = getmask(a)
mb = getmask(b)
if ma is nomask and mb is nomask:
m = nomask
else:
ma = getmaskarray(a)
mb = getmaskarray(b)
m = umath.logical_or.outer(ma, mb)
if (not m.ndim) and m:
return masked
if m is not nomask:
np.copyto(d, da, where=m)
if not d.shape:
return d
masked_d = d.view(get_masked_subclass(a, b))
masked_d._mask = m
return masked_d
def fake_blackbox_optimizer(self):
true_grads, losses, l2s, loss1, loss2, scores, nimgs = self.sess.run([self.grad_op, self.loss, self.l2dist, self.loss1, self.loss2, self.output, self.newimg], feed_dict={self.modifier: self.real_modifier})
# ADAM update
grad = true_grads[0].reshape(-1)
# print(true_grads[0])
epoch = self.adam_epoch[0]
mt = self.beta1 * self.mt + (1 - self.beta1) * grad
vt = self.beta2 * self.vt + (1 - self.beta2) * np.square(grad)
corr = (math.sqrt(1 - self.beta2 ** epoch)) / (1 - self.beta1 ** epoch)
# print(grad.shape, mt.shape, vt.shape, self.real_modifier.shape)
# m is a *view* of self.real_modifier
m = self.real_modifier.reshape(-1)
# this is in-place
m -= self.LEARNING_RATE * corr * (mt / (np.sqrt(vt) + 1e-8))
self.mt = mt
self.vt = vt
# m -= self.LEARNING_RATE * grad
if not self.use_tanh:
m_proj = np.maximum(np.minimum(m, self.modifier_up), self.modifier_down)
np.copyto(m, m_proj)
self.adam_epoch[0] = epoch + 1
return losses[0], l2s[0], loss1[0], loss2[0], scores[0], nimgs[0]
def add_data(self, input_cube, index):
"""Add data to a larger cube (this instance) from a smaller cube (input_cube)
Assumes all time samples are present in the smaller cube
Args:
input_cube (spdb.cube.Cube): Input Cube instance from which to merge data
index: relative morton ID indicating where to insert the data
Returns:
None
"""
x_offset = index[0] * input_cube.x_dim
y_offset = index[1] * input_cube.y_dim
z_offset = index[2] * input_cube.z_dim
np.copyto(self.data[input_cube.time_range[0] - self.time_range[0]:input_cube.time_range[1] - self.time_range[0],
z_offset:z_offset + input_cube.z_dim,
y_offset:y_offset + input_cube.y_dim,
x_offset:x_offset + input_cube.x_dim], input_cube.data[:, :, :, :])
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
def lb(self, lb):
if self.equality.any():
raise ValueError(
"The lb array can not be set "
"when there are indices of the "
"equality array that are True")
if lb is None:
lb = -numpy.inf
if isinstance(lb, numpy.ndarray):
numpy.copyto(self._lb, lb)
elif isinstance(lb, NumericValue):
raise ValueError("lb must be set to "
"a simple numeric type "
"or a numpy array")
else:
self._lb.fill(lb)
def ub(self, ub):
if self.equality.any():
raise ValueError(
"The ub array can not be set "
"when there are indices of the "
"equality array that are True")
if ub is None:
ub = numpy.inf
if isinstance(ub, numpy.ndarray):
numpy.copyto(self._ub, ub)
elif isinstance(ub, NumericValue):
raise ValueError("ub must be set to "
"a simple numeric type "
"or a numpy array")
else:
self._ub.fill(ub)
def rhs(self, rhs):
if rhs is None:
# None has a different meaning depending on the
# context (lb or ub), so there is no way to
# interpret this
raise ValueError(
"Constraint right-hand side can not "
"be assigned a value of None.")
elif isinstance(rhs, NumericValue):
raise ValueError("rhs must be set to "
"a simple numeric type "
"or a numpy array")
elif isinstance(rhs, numpy.ndarray):
numpy.copyto(self._lb, rhs)
numpy.copyto(self._ub, rhs)
else:
self._lb.fill(rhs)
self._ub.fill(rhs)
self._equality.fill(True)
def draw_keypoints_on_image_array(image,
keypoints,
color='red',
radius=2,
use_normalized_coordinates=True):
"""Draws keypoints on an image (numpy array).
Args:
image: a numpy array with shape [height, width, 3].
keypoints: a numpy array with shape [num_keypoints, 2].
color: color to draw the keypoints with. Default is red.
radius: keypoint radius. Default value is 2.
use_normalized_coordinates: if True (default), treat keypoint values as
relative to the image. Otherwise treat them as absolute.
"""
image_pil = Image.fromarray(np.uint8(image)).convert('RGB')
draw_keypoints_on_image(image_pil, keypoints, color, radius,
use_normalized_coordinates)
np.copyto(image, np.array(image_pil))
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2, 3)
# Simple copy
np.copyto(a, 1.5)
assert_equal(a, 1.5)
np.copyto(a.T, 2.5)
assert_equal(a, 2.5)
# Where-masked copy
mask = np.array([[0, 1, 0], [0, 0, 1]], dtype='?')
np.copyto(a, 3.5, where=mask)
assert_equal(a, [[2.5, 3.5, 2.5], [2.5, 2.5, 3.5]])
mask = np.array([[0, 1], [1, 1], [1, 0]], dtype='?')
np.copyto(a.T, 4.5, where=mask)
assert_equal(a, [[2.5, 4.5, 4.5], [4.5, 4.5, 3.5]])
def __ipow__(self, other):
"""
Raise self to the power other, in place.
"""
other_data = getdata(other)
other_mask = getmask(other)
with np.errstate(divide='ignore', invalid='ignore'):
self._data.__ipow__(np.where(self._mask, self.dtype.type(1),
other_data))
invalid = np.logical_not(np.isfinite(self._data))
if invalid.any():
if self._mask is not nomask:
self._mask |= invalid
else:
self._mask = invalid
np.copyto(self._data, self.fill_value, where=invalid)
new_mask = mask_or(other_mask, invalid)
self._mask = mask_or(self._mask, new_mask)
return self
def inject_noise_sample(self, data, noise_path, noise_level):
noise_src = load_audio(noise_path)
noise_offset_fraction = np.random.rand()
noise_dst = np.zeros_like(data)
src_offset = int(len(noise_src) * noise_offset_fraction)
src_left = len(noise_src) - src_offset
dst_offset = 0
dst_left = len(data)
while dst_left > 0:
copy_size = min(dst_left, src_left)
np.copyto(noise_dst[dst_offset:dst_offset + copy_size],
noise_src[src_offset:src_offset + copy_size])
if src_left > dst_left:
dst_left = 0
else:
dst_left -= copy_size
dst_offset += copy_size
src_left = len(noise_src)
src_offset = 0
data += noise_level * noise_dst
return data
def inject_noise_sample(self, data, noise_path, noise_level):
noise_src = load_audio(noise_path)
noise_offset_fraction = np.random.rand()
noise_dst = np.zeros_like(data)
src_offset = int(len(noise_src) * noise_offset_fraction)
src_left = len(noise_src) - src_offset
dst_offset = 0
dst_left = len(data)
while dst_left > 0:
copy_size = min(dst_left, src_left)
np.copyto(noise_dst[dst_offset:dst_offset + copy_size],
noise_src[src_offset:src_offset + copy_size])
if src_left > dst_left:
dst_left = 0
else:
dst_left -= copy_size
dst_offset += copy_size
src_left = len(noise_src)
src_offset = 0
data += noise_level * noise_dst
return data
def normalise_data(self, timestamp, data):
""" Convert the data if needed """
if self._passthrough:
return
i = 0
for datum in data:
if self.needsfixup[i] is None:
i += 1
continue
if len(datum) == 0:
# Ignore entries with no data - this typically occurs when the
# plugin requests multiple metrics and the metrics do not all appear
# at every timestep
i += 1
continue
if self.accumulator[i] is None:
self.accumulator[i] = numpy.array(datum)
self.last[i] = numpy.array(datum)
else:
self.accumulator[i] += (datum - self.last[i]) % numpy.uint64(1L << self.needsfixup[i]['range'])
numpy.copyto(self.last[i], datum)
numpy.copyto(datum, self.accumulator[i])
i += 1
def soften_targets(array, low=0.1, high=0.9):
assert list(set(np.unique(array)) ^ {0, 1}) == [], 'Targets must be binary'
array_new = np.empty_like(array)
array_new = np.copyto(array_new, array)
array_new[array == 0] = low
array_new[array == 1] = high
return array_new
# misc