mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Using FeedForward and NDArray in Java

Open gaush123 opened this issue 10 years ago • 6 comments

Hi,

Is there a way to use FeedForward function and NDArray to create the model in Java?

Thanks, Gaurav

gaush123 avatar Jun 30 '16 16:06 gaush123

you can refer to https://github.com/dmlc/mxnet/tree/master/scala-package

Ldpe2G avatar Jul 01 '16 05:07 Ldpe2G

@gaush123 You can also try swig.

thirdwing avatar Jul 01 '16 15:07 thirdwing

@thirdwing Could you please just give little more information about swig. Thanks! Also, I would like to run the below code in java/scala, is there any way of accomplishing it. model = mx.model.FeedForward(ctx=mx.cpu(), symbol=SymbolFile, arg_params=AllParams, aux_params={}, num_epoch=1, learning_rate=0.05, momentum=0.9, wd=0.0001)


Okay! What I think would work for me are the lines below, which are implemented in scala:

val model = new FeedForward( modelDef, ctx, numEpoch, epochSize, optimizer, initializer, batchSize, argParams, auxParams, allowExtraParams, beginEpoch)

gaush123 avatar Jul 13 '16 12:07 gaush123

@tqchen @thirdwing @pluskid @sneakerkg Hi, I am trying to create the model using FeedForward using mxnet_predict-all.cc, instead of using it as predictor, as I want to run this process on mobile device instead of a desktop/server.

What I need to know is how do I pass the values of const void* param_bytes, in the function mentioned below. On my local I can do this by passing a dictionary (python) with key as names of the layers and values as NDArray 4D arrays.

How do I achieve the same result by using the fucntion below

  • \brief create a predictor
    • \param symbol_json_str The JSON string of the symbol.
    • \param param_bytes The in-memory raw bytes of parameter ndarray file.
    • \param param_size The size of parameter ndarray file.
    • \param dev_type The device type, 1: cpu, 2:gpu
    • \param dev_id The device id of the predictor.
    • \param num_input_nodes Number of input nodes to the net,
    • For feedforward net, this is 1.
    • \param input_keys The name of input argument.
    • For feedforward net, this is {"data"}
    • \param input_shape_indptr Index pointer of shapes of each input node.
    • The length of this array = num_input_nodes + 1.
    • For feedforward net that takes 4 dimensional input, this is {0, 4}.
    • \param input_shape_data A flatted data of shapes of each input node.
    • For feedforward net that takes 4 dimensional input, this is the shape data.
    • \param out The created predictor handle.
    • \return 0 when success, -1 when failure. */
MXNET_DLL int MXPredCreate(const char* symbol_json_str,
                           const void* param_bytes,
                           int param_size,
                           int dev_type, int dev_id,
                           mx_uint num_input_nodes,
                           const char** input_keys,
                           const mx_uint* input_shape_indptr,
                           const mx_uint* input_shape_data,
                           PredictorHandle* out);

gaush123 avatar Jul 26 '16 12:07 gaush123

@gaush123 Have you ever find a solution in using MXNet in Java?

zihaolucky avatar Aug 27 '16 09:08 zihaolucky

Hi @Ldpe2G I trained the model in Python and now is going to deploy it in Java environment, but encountered some difficulties, could you give me a hint?

The model file is said to be language agnostic, so I write a predict function in Java as below:

public void predict(NDArrayIter dataiter, int batchSize) {

    SGD opt = new SGD(0.0001f, 0.9f, 0.000f, 0.0f, null);
    Uniform uniform = new Uniform(0.1f);
    Context[] ctx = new Context[1];
    ctx[0] = Context.cpu(0);
    FeedForward model = FeedForward.load("prefix", 10, ctx, 200, 32, opt, uniform, 32, false);

    model.predict(dataiter, batchSize);
}

The question is, how to create an NDArrayIter? I was going to start from creating an NDArray but failed at last. I found a quite similar issue here https://github.com/dmlc/mxnet/issues/2583

Thank you~

zihaolucky avatar Aug 28 '16 12:08 zihaolucky