Python mxnet.ndarray() Examples
The following are 30
code examples of mxnet.ndarray().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mxnet
, or try the search function
.
Example #1
Source File: danet.py From gluon-cv with Apache License 2.0 | 6 votes |
def hybrid_forward(self, F, x): feat1 = self.conv5a(x) sa_feat = self.sa(feat1) sa_conv = self.conv51(sa_feat) sa_output = self.conv6(sa_conv) feat2 = self.conv5c(x) sc_feat = self.sc(feat2) sc_conv = self.conv52(sc_feat) sc_output = self.conv7(sc_conv) feat_sum = sa_conv + sc_conv sasc_output = self.conv8(feat_sum) output = [sasc_output] output.append(sa_output) output.append(sc_output) return tuple(output)
Example #2
Source File: test_ndarray.py From SNIPER-mxnet with Apache License 2.0 | 6 votes |
def test_broadcast(): sample_num = 1000 def test_broadcast_to(): for i in range(sample_num): ndim = np.random.randint(1, 6) target_shape = np.random.randint(1, 11, size=ndim) shape = target_shape.copy() axis_flags = np.random.randint(0, 2, size=ndim) axes = [] for (axis, flag) in enumerate(axis_flags): if flag: shape[axis] = 1 dat = np.random.rand(*shape) - 0.5 numpy_ret = dat ndarray_ret = mx.nd.array(dat).broadcast_to(shape=target_shape) if type(ndarray_ret) is mx.ndarray.NDArray: ndarray_ret = ndarray_ret.asnumpy() assert (ndarray_ret.shape == target_shape).all() err = np.square(ndarray_ret - numpy_ret).mean() assert err < 1E-8 test_broadcast_to()
Example #3
Source File: inference_parameters.py From MXFusion with Apache License 2.0 | 6 votes |
def get_serializable(self): """ Returns three dicts: 1. MXNet parameters {uuid: mxnet parameters, mx.nd.array}. 2. MXNet constants {uuid: mxnet parameter (only constant types), mx.nd.array} 3. Other constants {uuid: primitive numeric types (int, float)} :returns: Three dictionaries: MXNet parameters, MXNet constants, and other constants (in that order) :rtypes: {uuid: mx.nd.array}, {uuid: mx.nd.array}, {uuid: primitive (int/float)} """ mxnet_parameters = {key: value._reduce() for key, value in self._params.items()} mxnet_constants = {uuid: value for uuid, value in self._constants.items() if isinstance(value, mx.ndarray.ndarray.NDArray)} variable_constants = {uuid: value for uuid, value in self._constants.items() if uuid not in mxnet_constants} return mxnet_parameters, mxnet_constants, variable_constants
Example #4
Source File: summary.py From mxboard with Apache License 2.0 | 6 votes |
def image_summary(tag, image): """Outputs a `Summary` protocol buffer with image(s). Parameters ---------- tag : str A name for the generated summary. Will also serve as a series name in TensorBoard. image : MXNet `NDArray` or `numpy.ndarray` Image data that is one of the following layout: (H, W), (C, H, W), (N, C, H, W). The pixel values of the image are assumed to be normalized in the range [0, 1]. The image will be rescaled to the range [0, 255] and cast to `np.uint8` before creating the image protobuf. Returns ------- A `Summary` protobuf of the image. """ tag = _clean_tag(tag) image = _prepare_image(image) image = _make_image(image) return Summary(value=[Summary.Value(tag=tag, image=image)])
Example #5
Source File: random_gen.py From MXFusion with Apache License 2.0 | 6 votes |
def _sample_univariate(func, shape=None, dtype=None, out=None, ctx=None, F=None, **kwargs): """ Wrapper for univariate sampling functions (Normal, Gamma etc.) :param func: The function to use for sampling, e.g. F.random.normal :param shape: The shape of the samples :param dtype: The data type :param out: Output variable :param ctx: The execution context :param F: MXNet node :param kwargs: keyword arguments for the sampling function (loc, scale etc) :return: Array of samples """ dtype = get_default_dtype() if dtype is None else dtype if F is mx.ndarray: # This is required because MXNet uses _Null instead of None as shape default if shape is None: return func(dtype=dtype, ctx=ctx, out=out, **kwargs) else: return func(shape=shape, dtype=dtype, ctx=ctx, out=out, **kwargs) else: return func(shape=shape, dtype=dtype, out=out, **kwargs)
Example #6
Source File: utils.py From mxboard with Apache License 2.0 | 6 votes |
def _make_metadata_tsv(metadata, save_path): """Given an `NDArray` or a `numpy.ndarray` or a list as metadata e.g. labels, save the flattened array into the file metadata.tsv under the path provided by the user. The labels can be 1D or 2D with multiple labels per data point. Made to satisfy the requirement in the following link: https://www.tensorflow.org/programmers_guide/embedding#metadata""" if isinstance(metadata, NDArray): metadata = metadata.asnumpy() elif isinstance(metadata, list): metadata = np.array(metadata) elif not isinstance(metadata, np.ndarray): raise TypeError('expected NDArray or np.ndarray or 1D/2D list, while received ' 'type {}'.format(str(type(metadata)))) if len(metadata.shape) > 2: raise TypeError('expected a 1D/2D NDArray, np.ndarray or list, while received ' 'shape {}'.format(str(metadata.shape))) if len(metadata.shape) == 1: metadata = metadata.reshape(-1, 1) with open(os.path.join(save_path, 'metadata.tsv'), 'w') as f: for row in metadata: f.write('\t'.join([str(x) for x in row]) + '\n')
Example #7
Source File: ops.py From simpledet with Apache License 2.0 | 6 votes |
def _prepare_anchors(F, feat_list, anchor_list, num_image, num_anchor): """ crop pre-comuputed anchors into the shape of the features inputs: F: symbol or ndarray feat_list: list of symbols or ndarrays, [(#img, #c, #h1, #w1), ...] anchor_list: list of symbols or ndarrays, [(1, 1, #h1', #w1', #anchor * 4), ...] num_image: int num_anchor: int outputs: anchors: symbol or ndarray, (#img, H * W * #anchor, 4) """ lvl_anchors = [] for features, anchors in zip(feat_list, anchor_list): anchors = F.slice_like(anchors, features, axes=(2, 3)) # (1, 1, h, w, #anchor * 4) anchors = F.reshape(anchors, shape=(1, -1, num_anchor * 4)) # (1, h * w, #anchor * 4) lvl_anchors.append(anchors) anchors = F.concat(*lvl_anchors, dim=1) # (1, H * W, #anchor * 4) anchors = F.reshape(anchors, shape=(0, -1, 4)) # (1, H * W * #anchor, 4) anchors = F.broadcast_axis(anchors, axis=0, size=num_image) # (#img, H * W * #anchor, 4) return anchors
Example #8
Source File: utils.py From mxboard with Apache License 2.0 | 6 votes |
def _make_sprite_image(images, save_path): """Given an NDArray as a batch images, make a sprite image out of it following the rule defined in https://www.tensorflow.org/programmers_guide/embedding and save it in sprite.png under the path provided by the user.""" if isinstance(images, np.ndarray): images = nd.array(images, dtype=images.dtype, ctx=current_context()) elif not isinstance(images, (NDArray, np.ndarray)): raise TypeError('images must be an MXNet NDArray or numpy.ndarray,' ' while received type {}'.format(str(type(images)))) assert isinstance(images, NDArray) shape = images.shape nrow = int(np.ceil(np.sqrt(shape[0]))) _save_image( images, os.path.join(save_path, 'sprite.png'), nrow=nrow, padding=0, square_image=True)
Example #9
Source File: trainer.py From STGCN with GNU General Public License v3.0 | 6 votes |
def predict_batch(model, ctx, x, n_pred): ''' Parameters ---------- x: mx.ndarray, shape is (batch_size, 1, n_his, num_of_vertices) Returns ---------- mx.ndarray, shape is (batch_size, 1, n_pred, num_of_vertices) ''' predicts = [] for pred_idx in range(n_pred): x_input = nd.concat(x, *predicts, dim=2)[:, :, - n_pred:, :] predicts.append(model(x_input.as_in_context(ctx)) .as_in_context(mx.cpu())) return nd.concat(*predicts, dim=2)
Example #10
Source File: distribution.py From gluon-ts with Apache License 2.0 | 6 votes |
def event_shape(self) -> Tuple: r""" Shape of each individual event contemplated by the distribution. For example, distributions over scalars have `event_shape = ()`, over vectors have `event_shape = (d, )` where `d` is the length of the vectors, over matrices have `event_shape = (d1, d2)`, and so on. Invoking `sample()` from a distribution yields a tensor of shape `batch_shape + event_shape`. This property is available in general only in mx.ndarray mode, when the shape of the distribution arguments can be accessed. """ raise NotImplementedError()
Example #11
Source File: summary.py From mxboard with Apache License 2.0 | 6 votes |
def histogram_summary(tag, values, bins): """Outputs a `Summary` protocol buffer with a histogram. Adding a histogram summary makes it possible to visualize the data's distribution in TensorBoard. See detailed explanation of the TensorBoard histogram dashboard at https://www.tensorflow.org/get_started/tensorboard_histograms This op reports an `InvalidArgument` error if any value is not finite. Adapted from the TensorFlow function `histogram()` at https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/summary/summary.py Parameters ---------- tag : str A name for the summary of the histogram. Will also serve as a series name in TensorBoard. values : MXNet `NDArray` or `numpy.ndarray` Values for building the histogram. Returns ------- A `Summary` protobuf of the histogram. """ tag = _clean_tag(tag) values = _make_numpy_array(values) hist = _make_histogram(values.astype(float), bins) return Summary(value=[Summary.Value(tag=tag, histo=hist)])
Example #12
Source File: optimization_utils.py From autogluon with Apache License 2.0 | 5 votes |
def _copy_into(ndarray, nparray): """ Copy the values from the given ndarray into the given (preallocated) numpy array. This can be used to avoid extra memory allocation that ndarray.asnumpy() performs. """ assert nparray.size == ndarray.size assert nparray.flags.f_contiguous and nparray.flags.behaved # NOTE: The copy=False variant of NDArray.astype does not seem to work if ndarray.dtype != nparray.dtype: ndarray = ndarray.astype(nparray.dtype) mx.base.check_call(mx.base._LIB.MXNDArraySyncCopyToCPU( ndarray.handle, nparray.ctypes.data_as(ctypes.c_void_p), ctypes.c_size_t(ndarray.size)))
Example #13
Source File: test_ndarray.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def test_ndarray_astype(): x = mx.nd.zeros((2, 3), dtype='int32') y = x.astype('float32') assert (y.dtype==np.float32) # Test that a new ndarray has been allocated assert (id(x) != id(y)) x = mx.nd.zeros((2, 3), dtype='int32') y = x.astype('float32', copy=False) assert (y.dtype==np.float32) # Test that a new ndarray has been allocated assert (id(x) != id(y)) x = mx.nd.zeros((2, 3), dtype='int32') y = x.astype('int32') assert (y.dtype==np.int32) # Test that a new ndarray has been allocated # even though they have same dtype assert (id(x) != id(y)) # Test that a new ndarray has not been allocated x = mx.nd.zeros((2, 3), dtype='int32') y = x.astype('int32', copy=False) assert (id(x) == id(y)) # Test the string version 'int32' # has the same behaviour as the np.int32 x = mx.nd.zeros((2, 3), dtype='int32') y = x.astype(np.int32, copy=False) assert (id(x) == id(y))
Example #14
Source File: test_ndarray.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def test_assign_a_row_to_ndarray(): """Test case from https://github.com/apache/incubator-mxnet/issues/9976""" H, W = 10, 10 dtype = np.float32 a_np = np.random.random((H, W)).astype(dtype) a_nd = mx.nd.array(a_np) # assign directly a_np[0] = a_np[1] a_nd[0] = a_nd[1] assert same(a_np, a_nd.asnumpy()) # assign a list v = np.random.random(W).astype(dtype).tolist() a_np[1] = v a_nd[1] = v assert same(a_np, a_nd.asnumpy()) # assign a np.ndarray v = np.random.random(W).astype(dtype) a_np[2] = v a_nd[2] = v assert same(a_np, a_nd.asnumpy()) # assign by slice a_np[0, :] = a_np[1] a_nd[0, :] = a_nd[1] assert same(a_np, a_nd.asnumpy())
Example #15
Source File: test_ndarray.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def test_ndarray_saveload(): nrepeat = 10 fname = 'tmp_list.bin' for repeat in range(nrepeat): data = [] # test save/load as list for i in range(10): data.append(random_ndarray(np.random.randint(1, 5))) mx.nd.save(fname, data) data2 = mx.nd.load(fname) assert len(data) == len(data2) for x, y in zip(data, data2): assert np.sum(x.asnumpy() != y.asnumpy()) == 0 # test save/load as dict dmap = {'ndarray xx %s' % i : x for i, x in enumerate(data)} mx.nd.save(fname, dmap) dmap2 = mx.nd.load(fname) assert len(dmap2) == len(dmap) for k, x in dmap.items(): y = dmap2[k] assert np.sum(x.asnumpy() != y.asnumpy()) == 0 # test save/load as ndarray # we expect the single ndarray to be converted into a list containing the ndarray single_ndarray = data[0] mx.nd.save(fname, single_ndarray) single_ndarray_loaded = mx.nd.load(fname) assert len(single_ndarray_loaded) == 1 single_ndarray_loaded = single_ndarray_loaded[0] assert np.sum(single_ndarray.asnumpy() != single_ndarray_loaded.asnumpy()) == 0 os.remove(fname)
Example #16
Source File: distribution.py From gluon-ts with Apache License 2.0 | 5 votes |
def batch_shape(self) -> Tuple: r""" Layout of the set of events contemplated by the distribution. Invoking `sample()` from a distribution yields a tensor of shape `batch_shape + event_shape`, and computing `log_prob` (or `loss` more in general) on such sample will yield a tensor of shape `batch_shape`. This property is available in general only in mx.ndarray mode, when the shape of the distribution arguments can be accessed. """ raise NotImplementedError()
Example #17
Source File: test_distribution_inference.py From gluon-ts with Apache License 2.0 | 5 votes |
def test_categorical_likelihood( num_cats: int, cat_probs: np.ndarray, hybridize: bool ): """ Test to check that maximizing the likelihood recovers the parameters """ cat_prob = mx.nd.array(cat_probs) cat_probs = mx.nd.zeros((NUM_SAMPLES, num_cats)) + cat_prob distr = Categorical(cat_probs.log()) samples = distr.sample() cat_prob_init = mx.nd.random_uniform(1 - TOL, 1 + TOL, num_cats) * cat_prob cat_prob_init = cat_prob_init / cat_prob_init.sum() init_biases = [cat_prob_init] cat_log_prob_hat = maximum_likelihood_estimate_sgd( CategoricalOutput(num_cats), samples, init_biases=init_biases, hybridize=hybridize, learning_rate=PositiveFloat(0.05), num_epochs=PositiveInt(25), ) cat_prob_hat = np.exp(cat_log_prob_hat) prob_deviation = np.abs(cat_prob_hat - cat_prob.asnumpy()).flatten() tolerance = (TOL * cat_prob.asnumpy()).flatten() assert np.all( np.less(prob_deviation, tolerance) ), f"cat_prob did not match: cat_prob = {cat_prob}, cat_prob_hat = {cat_prob_hat}"
Example #18
Source File: test_distribution_inference.py From gluon-ts with Apache License 2.0 | 5 votes |
def test_binned_likelihood( num_bins: float, bin_probabilites: np.ndarray, hybridize: bool ): """ Test to check that maximizing the likelihood recovers the parameters """ bin_prob = mx.nd.array(bin_probabilites) bin_center = mx.nd.array(np.logspace(-1, 1, num_bins)) # generate samples bin_probs = mx.nd.zeros((NUM_SAMPLES, num_bins)) + bin_prob bin_centers = mx.nd.zeros((NUM_SAMPLES, num_bins)) + bin_center distr = Binned(bin_probs.log(), bin_centers) samples = distr.sample() # add some jitter to the uniform initialization and normalize bin_prob_init = mx.nd.random_uniform(1 - TOL, 1 + TOL, num_bins) * bin_prob bin_prob_init = bin_prob_init / bin_prob_init.sum() init_biases = [bin_prob_init] bin_log_prob_hat, _ = maximum_likelihood_estimate_sgd( BinnedOutput(bin_center), samples, init_biases=init_biases, hybridize=hybridize, learning_rate=PositiveFloat(0.05), num_epochs=PositiveInt(25), ) bin_prob_hat = np.exp(bin_log_prob_hat) assert all( mx.nd.abs(mx.nd.array(bin_prob_hat) - bin_prob) < TOL * bin_prob ), f"bin_prob did not match: bin_prob = {bin_prob}, bin_prob_hat = {bin_prob_hat}"
Example #19
Source File: age_iter.py From 1.FaceRecognition with MIT License | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) #mx.ndarray return img
Example #20
Source File: utils.py From mxboard with Apache License 2.0 | 5 votes |
def _save_embedding_tsv(data, file_path): """Given a 2D `NDarray` or a `numpy.ndarray` as embeding, save it in tensors.tsv under the path provided by the user.""" if isinstance(data, np.ndarray): data_list = data.tolist() elif isinstance(data, NDArray): data_list = data.asnumpy().tolist() else: raise TypeError('expected NDArray of np.ndarray, while received type {}'.format( str(type(data)))) with open(os.path.join(file_path, 'tensors.tsv'), 'w') as f: for x in data_list: x = [str(i) for i in x] f.write('\t'.join(x) + '\n')
Example #21
Source File: utils.py From mxboard with Apache License 2.0 | 5 votes |
def _prepare_image(img, nrow=8, padding=2, square_image=False): """Given an image of format HW, CHW, or NCHW, returns a image of format HWC. If the input is a batch of images, a grid of images is made by stitching them together. If data type is float, values must be in the range [0, 1], and then they are rescaled to range [0, 255]. If data type is 'uint8`, values are unchanged. """ if isinstance(img, np.ndarray): img = nd.array(img, dtype=img.dtype, ctx=current_context()) if not isinstance(img, NDArray): raise TypeError('expected MXNet NDArray or numpy.ndarray, ' 'while received type {}'.format(str(type(img)))) assert img.ndim == 2 or img.ndim == 3 or img.ndim == 4 if img.dtype == np.uint8: return make_image_grid( img, nrow=nrow, padding=padding, square_image=square_image).transpose((1, 2, 0)) elif img.dtype == np.float32 or img.dtype == np.float64: min_val = img.min().asscalar() max_val = img.max().asscalar() if min_val < 0.0: raise ValueError('expected non-negative min value from img, ' 'while received {}'.format(min_val)) if max_val > 1.0: raise ValueError('expected max value from img not greater than 1, ' 'while received {}'.format(max_val)) img = make_image_grid(img, nrow=nrow, padding=padding, square_image=square_image) * 255.0 return img.astype(np.uint8).transpose((1, 2, 0)) else: raise ValueError('expected input image dtype is one of uint8, float32, ' 'and float64, received dtype {}'.format(str(img.dtype)))
Example #22
Source File: test_docstring.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def test_ndarray(): globs = {'np': numpy, 'mx': mxnet} doctest.testmod(mxnet.ndarray, globs=globs, verbose=True)
Example #23
Source File: test_distribution_inference.py From gluon-ts with Apache License 2.0 | 5 votes |
def inv_softplus(y: NPArrayLike) -> np.ndarray: # y = log(1 + exp(x)) ==> x = log(exp(y) - 1) return np.log(np.exp(y) - 1)
Example #24
Source File: summary.py From mxboard with Apache License 2.0 | 5 votes |
def scalar_summary(tag, scalar): """Outputs a `Summary` protocol buffer containing a single scalar value. The generated Summary has a Tensor.proto containing the input Tensor. Adapted from the TensorFlow function `scalar()` at https://github.com/tensorflow/tensorflow/blob/r1.6/tensorflow/python/summary/summary.py Parameters ---------- tag : str A name for the generated summary. Will also serve as the series name in TensorBoard. scalar : int, MXNet `NDArray`, or `numpy.ndarray` A scalar value or an ndarray of shape (1,). Returns ------- A `Summary` protobuf of the `scalar` value. Raises ------ ValueError: If the scalar has the wrong shape or type. """ tag = _clean_tag(tag) scalar = _make_numpy_array(scalar) assert(scalar.squeeze().ndim == 0), 'scalar should be 0D' scalar = float(scalar) return Summary(value=[Summary.Value(tag=tag, simple_value=scalar)])
Example #25
Source File: image_iter.py From MaskInsightface with Apache License 2.0 | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) # mx.ndarray return img
Example #26
Source File: data.py From MaskInsightface with Apache License 2.0 | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) #mx.ndarray return img
Example #27
Source File: config.py From MXFusion with Apache License 2.0 | 5 votes |
def get_default_MXNet_mode(): """ Return the default MXNet mode :returns: an MXNet ndarray or symbol, indicating the default mode. :rtype: :class:`mxnet.symbol` or :class:`mxnet.ndarray` """ return MXNET_DEFAULT_MODE
Example #28
Source File: image_iter.py From 1.FaceRecognition with MIT License | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) #mx.ndarray return img
Example #29
Source File: data.py From 1.FaceRecognition with MIT License | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) #mx.ndarray return img
Example #30
Source File: data.py From 1.FaceRecognition with MIT License | 5 votes |
def imdecode(self, s): """Decodes a string or byte string to an NDArray. See mx.img.imdecode for more details.""" img = mx.image.imdecode(s) #mx.ndarray return img