How to use the mxnet.nd function in mxnet

To help you get started, we’ve selected a few mxnet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github awslabs / sockeye / test / unit / test_coverage.py View on Github external
executor = updated_coverage.simple_bind(ctx=mx.cpu(),
                                            source=source_shape,
                                            source_length=source_length_shape,
                                            prev_hidden=prev_hidden_shape,
                                            prev_coverage=prev_coverage_shape,
                                            attention_scores=attention_scores_shape)
    executor.arg_dict["source"][:] = source_data
    executor.arg_dict["source_length"][:] = source_length_data
    executor.arg_dict["prev_hidden"][:] = prev_hidden_data
    executor.arg_dict["prev_coverage"][:] = prev_coverage_data
    executor.arg_dict["attention_scores"][:] = attention_scores_data
    result = executor.forward()
    new_coverage = result[0].asnumpy()
    assert new_coverage.shape == prev_coverage_shape
    # this is needed to modulate the 0 input. The output changes according to the activation type used.
    modulated = mx.nd.Activation(mx.nd.zeros((1, 1)), act_type=act_type).asnumpy()
    assert (np.sum(np.sum(np.isclose(new_coverage, modulated, atol=1.e-6), axis=2) != 0, axis=1) == source_length_data).all()
github BobLiu20 / FacialLandmark_MXNet / testing / predictor.py View on Github external
def preprocess(self, images):
        images_new = []
        for img in images:
            if self._arg_dict['img_format'] == 'RGB':
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = cv2.resize(img, (self._arg_dict['img_size'], self._arg_dict['img_size']))
            images_new.append(img)
        images_new = np.asarray(images_new)
        images_new = nd.array(images_new, ctx=mx.gpu())
        images_new = nd.transpose(images_new.astype('float32'), (0,3,1,2)) / 127.5 - 1.0
        return images_new
github hpi-xnor / BMXNet-v2 / tests / python / unittest / test_optimizer.py View on Github external
grad : NDArray
        grad ndarray

        state : NDArray or other objects returned by init_state
        The auxiliary state used in optimization.
        """
        lr = self._get_lr(index)
        wd = self._get_wd(index)
        self._update_count(index)
        use_multi_precision = isinstance(state, list) or isinstance(state, tuple)

        if not use_multi_precision:
            if self.momentum == 0.0:
                if self.clip_gradient is not None:
                    weight[:] = ((1 - lr*wd)*weight -
                        lr*mx.nd.clip(grad*self.rescale_grad, -self.clip_gradient, self.clip_gradient))
                else:
                    weight[:] = (1 - lr*wd)*weight - lr*self.rescale_grad*grad
            else:
                mom = state
                if self.clip_gradient is not None:
                    mom[:] = (self.momentum*mom - lr*wd*weight -
                        lr*mx.nd.clip(grad*self.rescale_grad, -self.clip_gradient, self.clip_gradient))
                    weight += mom
                else:
                    mom[:] = self.momentum*mom - lr*wd*weight - lr*self.rescale_grad*grad
                    weight += mom
        else:
            grad32 = array(grad, ctx=grad.context, dtype=np.float32)
            mom = state[0]
            weight32 = state[1]
            if self.momentum == 0.0:
github zhaw / neural_style / perceptual / make_image.py View on Github external
def __init__(self, model_prefix, output_shape):
        s1, s0 = output_shape
        s0 = s0//32*32
        s1 = s1//32*32
        self.s0 = s0
        self.s1 = s1
        generator = symbol.generator_symbol()
        args = mx.nd.load('%s_args.nd'%model_prefix)
        auxs = mx.nd.load('%s_auxs.nd'%model_prefix)
        args['data'] = mx.nd.zeros([1,3,s0+128,s1+128], mx.gpu())
        self.gene_executor = generator.bind(ctx=mx.gpu(), args=args, aux_states=auxs)
github Guanghan / mxnet-centernet / models / losses.py View on Github external
def compute_rot_loss(output, target_bin, target_res, mask):
    # output: (B, 128, 8) [bin1_cls[0], bin1_cls[1], bin1_sin, bin1_cos,
    #                 bin2_cls[0], bin2_cls[1], bin2_sin, bin2_cos]
    # target_bin: (B, 128, 2) [bin1_cls, bin2_cls]
    # target_res: (B, 128, 2) [bin1_res, bin2_res]
    # mask: (B, 128, 1)
    output = nd.reshape(output, (-1, 8))
    target_bin = nd.reshape(target_bin, (-1, 2))
    target_res = nd.reshape(target_res, (-1, 2))
    mask = nd.reshape(mask, (-1, 1))
    loss_bin1 = compute_bin_loss(output[:, 0:2], target_bin[:, 0], mask)
    loss_bin2 = compute_bin_loss(output[:, 4:6], target_bin[:, 1], mask)
    loss_res = nd.zeros_like(loss_bin1)

    mask1 = (target_bin[:, 0] > 0).astype('float32')
    if mask1.sum() > 0:
        valid_output1 = output
        valid_target_res1 = target_res

        loss_sin1 = compute_res_loss(valid_output1[:, 2], nd.sin(valid_target_res1[:, 0]), mask1)
        loss_cos1 = compute_res_loss(valid_output1[:, 3], nd.cos(valid_target_res1[:, 0]), mask1)
        loss_res = loss_res + loss_sin1 + loss_cos1

    mask2 = (target_bin[:, 1] > 0).astype('float32')
    if mask2.sum() > 0:
        valid_output2 = output
github deepinx / age-gender-estimation / data.py View on Github external
super(FaceImageIter, self).__init__()
        assert path_imgrec
        logging.info('loading recordio %s...',
                     path_imgrec)
        path_imgidx = path_imgrec[0:-4]+".idx"
        self.imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r')  # pylint: disable=redefined-variable-type
        s = self.imgrec.read_idx(0)
        header, _ = recordio.unpack(s)
        self.imgidx = list(self.imgrec.keys)
        self.seq = self.imgidx

        self.mean = mean
        self.nd_mean = None
        if self.mean:
          self.mean = np.array(self.mean, dtype=np.float32).reshape(1,1,3)
          self.nd_mean = mx.nd.array(self.mean).reshape((1,1,3))

        self.check_data_shape(data_shape)
        self.provide_data = [(data_name, (batch_size,) + data_shape)]
        self.batch_size = batch_size
        self.data_shape = data_shape
        self.shuffle = shuffle
        self.image_size = '%d,%d'%(data_shape[1],data_shape[2])
        self.rand_mirror = rand_mirror
        print('rand_mirror', rand_mirror)
        self.cutoff = cutoff
        self.color_jittering = color_jittering
        self.CJA = mx.image.ColorJitterAug(0.125, 0.125, 0.125)
        self.provide_label = [(label_name, (batch_size,101))]
        #print(self.provide_label[0][1])
        self.cur = 0
        self.nbatch = 0
github hankcs / DeepBiaffineParserMXNet / models / biaffine_parser.py View on Github external
def _emb_mask_generator(seq_len, batch_size):
            wm, tm = nd.zeros((seq_len, batch_size, 1)), nd.zeros((seq_len, batch_size, 1))
            for i in range(seq_len):
                word_mask = np.random.binomial(1, 1. - dropout_dim, batch_size).astype(np.float32)
                tag_mask = np.random.binomial(1, 1. - dropout_dim, batch_size).astype(np.float32)
                scale = 3. / (2. * word_mask + tag_mask + 1e-12)
                word_mask *= scale
                tag_mask *= scale
                word_mask = nd.array(word_mask)
                tag_mask = nd.array(tag_mask)
                wm[i, :, 0] = word_mask
                tm[i, :, 0] = tag_mask
            return wm, tm
github CortexFoundation / CortexTheseus / cvm-runtime / python / mrt / from_tensorflow.py View on Github external
if sutils.is_var(op, params):
            pass
        elif childs is None:
            params[name] = sutils.get_nd_op(op_name)(**attr)
            op = mx.sym.var(name, shape=params[name].shape)
        else:
            childs = [graph[c.attr('name')] for c in childs]
            assert all([sutils.is_params(c, params) for c in childs])
            in_params = [params[c.attr('name')] for c in childs]
            if op_name == "expand_dims" and in_params[0].shape == ():
                params[name] = nd.array([in_params[0].asnumpy()],
                        dtype=in_params[0].dtype)
            elif op_name == "Reshape" and sutils.get_attr(attr, 'shape') == []:
                assert in_params[0].shape == (1,)
                params[name] = nd.array(in_params[0].asnumpy()[0],
                        dtype=in_params[0].dtype)
            else:
                params[name] = sutils.get_nd_op(op_name)(*in_params, **attr)
            op = mx.sym.var(name, shape=params[name].shape)
        graph[name] = op

    assert sutils.is_params(graph[shape.attr('name')], params)
    shape = params[shape.attr('name')].asnumpy().tolist()
    shape[0] = -1 # since dim zero is batch, set -1 for flexiblity.
    return mx.sym.reshape(X, shape)
github TuSimple / simpledet / utils / callback.py View on Github external
def _callback(param):
        if checkpoint_iter == param.locals["total_iter"]:
            arg_params, aux_params = param.locals["self"].get_params()
            save_dict = {('arg:%s' % k) : v.as_in_context(mx.cpu()) for k, v in arg_params.items()}
            save_dict.update({('aux:%s' % k) : v.as_in_context(mx.cpu()) for k, v in aux_params.items()})
            param_name = '%s-iter-%s.params' % (prefix, checkpoint_iter)
            mx.nd.save(param_name, save_dict)
            logging.info('Saved checkpoint to \"%s\"', param_name)
    return _callback
github osmr / imgclsmob / gluon / gluoncv2 / models / res2net.py View on Github external
ctx = mx.cpu()
        if not pretrained:
            net.initialize(ctx=ctx)

        # net.hybridize()
        net_params = net.collect_params()
        weight_count = 0
        for param in net_params.values():
            if (param.shape is None) or (not param._differentiable):
                continue
            weight_count += np.prod(param.shape)
        print("m={}, {}".format(model.__name__, weight_count))
        assert (model != res2net50_w14_s8 or weight_count == 8258356)
        assert (model != res2net50_w26_s8 or weight_count == 11456212)

        x = mx.nd.zeros((1, 3, 224, 224), ctx=ctx)
        y = net(x)
        assert (y.shape == (1, 1000))