本文整理汇总了Python中mxnet.nd.zeros方法的典型用法代码示例。如果您正苦于以下问题:Python nd.zeros方法的具体用法?Python nd.zeros怎么用?Python nd.zeros使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet.nd
的用法示例。
在下文中一共展示了nd.zeros方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def forward(self, pos):
r"""Memory allocation and sampling
Parameters
----------
pos : tensor
The positional tensor of shape (B, N, C)
Returns
-------
tensor of shape (B, self.npoints)
The sampled indices in each batch.
"""
ctx = pos.context
B, N, C = pos.shape
pos = pos.reshape(-1, C)
dist = nd.zeros((B * N), dtype=pos.dtype, ctx=ctx)
start_idx = nd.random.randint(0, N - 1, (B, ), dtype=np.int, ctx=ctx)
result = nd.zeros((self.npoints * B), dtype=np.int, ctx=ctx)
farthest_point_sampler(pos, B, self.npoints, dist, start_idx, result)
return result.reshape(B, self.npoints)
示例2: cv_rotate
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def cv_rotate(img, rot, resW, resH):
cv2 = try_import_cv2()
center = np.array((resW - 1, resH - 1)) / 2
rot_rad = np.pi * rot / 180
src_dir = get_dir([0, (resH - 1) * -0.5], rot_rad)
dst_dir = np.array([0, (resH - 1) * -0.5], np.float32)
src = np.zeros((3, 2), dtype=np.float32)
dst = np.zeros((3, 2), dtype=np.float32)
src[0, :] = center
src[1, :] = center + src_dir
dst[0, :] = [(resW - 1) * 0.5, (resH - 1) * 0.5]
dst[1, :] = np.array([(resW - 1) * 0.5, (resH - 1) * 0.5]) + dst_dir
src[2:, :] = get_3rd_point(src[0, :], src[1, :])
dst[2:, :] = get_3rd_point(dst[0, :], dst[1, :])
trans = cv2.getAffineTransform(np.float32(src), np.float32(dst))
dst_img = cv2.warpAffine(img, trans,
(resW, resH), flags=cv2.INTER_LINEAR)
return dst_img
示例3: transformBoxInvert
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def transformBoxInvert(pt, ul, br, resH, resW):
# type: (Tensor, Tensor, Tensor, float, float, float, float) -> Tensor
center = mx.nd.zeros(2)
center[0] = (br[0] - 1 - ul[0]) / 2
center[1] = (br[1] - 1 - ul[1]) / 2
lenH = max(br[1] - ul[1], (br[0] - ul[0]) * resH / resW)
lenW = lenH * resW / resH
_pt = (pt * lenH) / resH
if bool(((lenW - 1) / 2 - center[0]) > 0):
_pt[0] = _pt[0] - ((lenW - 1) / 2 - center[0]).asscalar()
if bool(((lenH - 1) / 2 - center[1]) > 0):
_pt[1] = _pt[1] - ((lenH - 1) / 2 - center[1]).asscalar()
new_point = mx.nd.zeros(2)
new_point[0] = _pt[0] + ul[0]
new_point[1] = _pt[1] + ul[1]
return new_point
示例4: load_data_fashion_mnist
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def load_data_fashion_mnist(batch_size, resize=None, root="~/.mxnet/datasets/fashion-mnist"):
"""download the fashion mnist dataest and then load into memory"""
def transform_mnist(data, label):
# Transform a batch of examples.
if resize:
n = data.shape[0]
new_data = nd.zeros((n, resize, resize, data.shape[3]))
for i in range(n):
new_data[i] = image.imresize(data[i], resize, resize)
data = new_data
# change data from batch x height x width x channel to batch x channel x height x width
return nd.transpose(data.astype('float32'), (0, 3, 1, 2)) / 255, label.astype('float32')
mnist_train = gluon.data.vision.FashionMNIST(root=root, train=True, transform=None)
mnist_test = gluon.data.vision.FashionMNIST(root=root, train=False, transform=None)
# Transform later to avoid memory explosion.
train_data = DataLoader(mnist_train, batch_size, shuffle=True, transform=transform_mnist)
test_data = DataLoader(mnist_test, batch_size, shuffle=False, transform=transform_mnist)
return (train_data, test_data)
示例5: load_data_mnist
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def load_data_mnist(batch_size, resize=None, root="~/.mxnet/datasets/mnist"):
"""download the fashion mnist dataest and then load into memory"""
def transform_mnist(data, label):
# Transform a batch of examples.
if resize:
n = data.shape[0]
new_data = nd.zeros((n, resize, resize, data.shape[3]))
for i in range(n):
new_data[i] = image.imresize(data[i], resize, resize)
data = new_data
# change data from batch x height x width x channel to batch x channel x height x width
return nd.transpose(data.astype('float32'), (0, 3, 1, 2)) / 255, label.astype('float32')
mnist_train = gluon.data.vision.MNIST(root=root, train=True, transform=None)
mnist_test = gluon.data.vision.MNIST(root=root, train=False, transform=None)
# Transform later to avoid memory explosion.
train_data = DataLoader(mnist_train, batch_size, shuffle=True, transform=transform_mnist)
test_data = DataLoader(mnist_test, batch_size, shuffle=False, transform=transform_mnist)
return (train_data, test_data)
示例6: predict_rnn
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def predict_rnn(rnn, prefix, num_chars, params, hidden_dim, ctx, idx_to_char,
char_to_idx, get_inputs, is_lstm=False):
"""Predict the next chars given the prefix."""
prefix = prefix.lower()
state_h = nd.zeros(shape=(1, hidden_dim), ctx=ctx)
if is_lstm:
state_c = nd.zeros(shape=(1, hidden_dim), ctx=ctx)
output = [char_to_idx[prefix[0]]]
for i in range(num_chars + len(prefix)):
X = nd.array([output[-1]], ctx=ctx)
if is_lstm:
Y, state_h, state_c = rnn(get_inputs(X), state_h, state_c, *params)
else:
Y, state_h = rnn(get_inputs(X), state_h, *params)
if i < len(prefix) - 1:
next_input = char_to_idx[prefix[i + 1]]
else:
next_input = int(Y[0].argmax(axis=1).asscalar())
output.append(next_input)
return ''.join([idx_to_char[i] for i in output])
示例7: test_get_nonzero
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def test_get_nonzero():
'''
feat = nd.zeros(shape = (4, 2))
feat[0, 0] = 1
feat[1, 1] = 1
feat[2, 1] = 1
feat[3, 0] = 1
feat = nd.array([[0.6, 0.0, 0.0, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]])
feat = nd.array([[[1,1,1,0,1],[1,0,0,0,1]],
[[1,1,1,0,1],[1,0,0,0,1]]])
'''
feat = nd.zeros(shape = (4, ))
feat[2] = 1
print(feat)
feat_sparse = feat.tostype('csr')
print(feat_sparse)
indices = feat_sparse.indices
print(indices)
return
示例8: __init__
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def __init__(self, options):
if options.gpus[0] >= 0:
try:
self.ctx = mx.gpu()
_ = nd.zeros((1,), ctx=self.ctx)
except mx.base.MXNetError:
print("No GPU available. Use CPU instead.")
self.ctx = mx.cpu()
else:
self.ctx = mx.cpu()
print("Creating model...")
self.model = create_model(options.arch, options.heads, options.head_conv, ctx = self.ctx)
if options.load_model_path != '':
self.model = load_model(self.model, options.load_model_path, ctx = self.ctx)
self.mean = np.array(options.mean, dtype=np.float32).reshape(1, 1, 3)
self.std = np.array(options.std, dtype=np.float32).reshape(1, 1, 3)
self.max_per_image = 100
self.num_classes = options.num_classes
self.scales = options.test_scales
self.opt = options
self.pause = True
示例9: __iter__
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def __iter__(self):
sample_indices = []
label_counter = np.zeros(self._classes)
shuffle_indices = np.arange(len(self._labels))
np.random.shuffle(shuffle_indices)
for idx in shuffle_indices:
label = self._labels[idx]
if label_counter[label] < self._num_per_class:
sample_indices.append(idx)
label_counter[label] += 1
if label_counter.sum() == self._classes * self._num_per_class:
break
for idx, cnt in enumerate(label_counter):
if cnt < self._num_per_class:
raise ValueError("Number of samples for class {} is {} < {}".format(idx, cnt, self._num_per_class))
return iter(sample_indices)
示例10: test_export
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def test_export():
print("<<TEST: hybridize and export>>")
bn_string_in_json_file = '"op": "BatchNorm"'
export_file_name = "/tmp/test_merge_bn-" + time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))
net = mobilenet1_0(pretrained=True)
merge_bn(net)
print("merge_bn ...[ok]")
net.hybridize()
print("hybridize ...[ok]")
_ = net(nd.zeros(shape=(1, 3, 224, 224)))
print("run hybrid graph forward ...[ok]")
net.export(export_file_name, 0)
print("export to", export_file_name, "...[ok]")
with open(export_file_name+"-symbol.json", "r") as f:
s = f.read()
if bn_string_in_json_file not in s:
print('[OK] op "BatchNorm" is not in exported file')
else:
print('[Error] op "BatchNorm" is in exported file')
print()
示例11: test_quantize_symbol
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def test_quantize_symbol():
print("<<TEST: Quantize symbol for mobilenet_v1_1_0>>")
export_file_name = "/tmp/test_freeze_utils-" + time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
in_file_name = export_file_name + "-symbol.json"
out_file_name = export_file_name + "-qsymbol.json"
net = mobilenet1_0(pretrained=True)
net.hybridize()
_ = net(nd.zeros(shape=(1, 3, 224, 224)))
net.export(export_file_name, 0)
mobilenet_sym = sym.load(in_file_name)
qsym = quantize_symbol(mobilenet_sym)
qsym.save()
print('Quantized symbol has saved to ' + out_file_name)
print()
return out_file_name
示例12: evaluate_accuracy_multi
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def evaluate_accuracy_multi(data_iterator, net, ctx):
data_iterator.reset()
acc = 0
dummy_label = np.zeros((0,6))
dummy_pred = np.zeros((0,6))
t1 = time.time()
for i, batch in enumerate(data_iterator):
data, label = _get_batch_multi(batch, ctx, False)
# acc += np.mean([accuracy(net(X), Y) for X, Y in zip(data, label)])
# acc += np.mean([roc_auc_score(Y.asnumpy(), net(X).asnumpy()) for X, Y in zip(data, label)])
output = np.vstack((net(X).asnumpy() for X in data))
labels = np.vstack((Y.asnumpy() for Y in label))
dummy_label = np.vstack((dummy_label, labels))
dummy_pred = np.vstack((dummy_pred, output))
# return acc / (i+1)
# print dummy_label.shape, dummy_pred.shape
dummy_pred_label = dummy_pred > 0.5
for i in range(dummy_label.shape[1]):
print i, confusion_matrix(dummy_label[:,i], dummy_pred_label[:,i])
return roc_auc_score(dummy_label, dummy_pred), accuracy(dummy_pred, dummy_label), time.time() - t1
示例13: Route
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def Route(self, x):
# b_mat = nd.repeat(self.b_mat.data(), repeats=x.shape[0], axis=0)#nd.stop_gradient(nd.repeat(self.b_mat.data(), repeats=x.shape[0], axis=0))
b_mat = nd.zeros((x.shape[0],1,self.num_cap, self.num_locations), ctx=x.context)
x_expand = nd.expand_dims(nd.expand_dims(x, axis=2),2)
w_expand = nd.repeat(nd.expand_dims(self.w_ij.data(x.context),axis=0), repeats=x.shape[0], axis=0)
u_ = w_expand*x_expand
# u_ = nd.abs(w_expand - x_expand)
u = nd.sum(u_, axis = 1)
u_no_gradient = nd.stop_gradient(u)
for i in range(self.route_num):
c_mat = nd.softmax(b_mat, axis=2)
if i == self.route_num -1:
s = nd.sum(u * c_mat, axis=-1)
else:
s = nd.sum(u_no_gradient * c_mat, axis=-1)
v = squash(s, 1)
v1 = nd.expand_dims(v, axis=-1)
if i != self.route_num - 1:
update_term = nd.sum(u_no_gradient*v1, axis=1, keepdims=True)
b_mat = b_mat + update_term
return v
示例14: load_data_fashion_mnist
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def load_data_fashion_mnist(batch_size, resize=None, root="~/.mxnet/datasets/fashion-mnist"):
"""download the fashion mnist dataest and then load into memory"""
def transform_mnist(data, label):
# Transform a batch of examples.
if resize:
n = data.shape[0]
new_data = nd.zeros((n, resize, resize, data.shape[3]))
for i in range(n):
new_data[i] = image.imresize(data[i], resize, resize)
data = new_data
# change data from batch x height x width x channel to batch x channel x height x width
return nd.transpose(data.astype('float32'), (0,3,1,2))/255, label.astype('float32')
mnist_train = gluon.data.vision.FashionMNIST(root=root, train=True, transform=None)
mnist_test = gluon.data.vision.FashionMNIST(root=root, train=False, transform=None)
# Transform later to avoid memory explosion.
train_data = DataLoader(mnist_train, batch_size, shuffle=True, transform=transform_mnist)
test_data = DataLoader(mnist_test, batch_size, shuffle=False, transform=transform_mnist)
return (train_data, test_data)
示例15: predict_rnn
# 需要导入模块: from mxnet import nd [as 别名]
# 或者: from mxnet.nd import zeros [as 别名]
def predict_rnn(rnn, prefix, num_chars, params, hidden_dim, ctx, idx_to_char,
char_to_idx, get_inputs, is_lstm=False):
"""Predict the next chars given the prefix."""
prefix = prefix.lower()
state_h = nd.zeros(shape=(1, hidden_dim), ctx=ctx)
if is_lstm:
state_c = nd.zeros(shape=(1, hidden_dim), ctx=ctx)
output = [char_to_idx[prefix[0]]]
for i in range(num_chars + len(prefix)):
X = nd.array([output[-1]], ctx=ctx)
if is_lstm:
Y, state_h, state_c = rnn(get_inputs(X), state_h, state_c, *params)
else:
Y, state_h = rnn(get_inputs(X), state_h, *params)
if i < len(prefix)-1:
next_input = char_to_idx[prefix[i+1]]
else:
next_input = int(Y[0].argmax(axis=1).asscalar())
output.append(next_input)
return ''.join([idx_to_char[i] for i in output])