Corresponding way to load_model to predict of bind


#1

samle code@method 1@succesfull

import mxnet as mx
import os
import numpy as np

def test_lstm():
    print 'test@lstm'
    ctx = mx.cpu()

    file_sym = '/Users/hypergroups/Desktop/wolfram.lstm-symbol.json'
    file_nd = '/Users/hypergroups/Desktop/wolfram.lstm-0000.params'
    _sym = mx.symbol.load(file_sym)

    _nd = mx.nd.load(file_nd)

    input_data = np.array([[1, 2]])

    print input_data

    array = mx.nd.array(input_data)

    _nd["Input"] = array
    _nd['4.State'] = mx.nd.array([[0, 0, 0, 0, 0]])
    _nd['4.CellState'] = mx.nd.array([[0, 0, 0, 0, 0]])

    _e = _sym.bind(ctx, _nd)

    _out = _e.forward()
    prob = _out[0].asnumpy()
    prob = np.squeeze(prob)
    print 'prob', prob


    if __name__ == '__main__':
        test_lstm()

I can run above code well.

my model is in mode-jsonl&&model-params

my above code is also in code above

My question is how can I use load_checkpoint method to load my model? or import the model by gluon

sample code@method2

def test_example_lstm():
    model_prefix = "/Users/hypergroups/Nutstore/ProjectsOnline/MyProjects/MXNet/Resources/models/models_mx.mma11.3.5.raw/example.lstm"
    ctx = mx.cpu()
    data_name = "Input"
    data_shape = (1, 2)
    sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 0)
    mod = mx.mod.Module(symbol=sym, context=ctx,
                        data_names=['Input'],
                        label_names=None)

    mod.bind(for_training=False, data_shapes=[('Input',(1,2))],
             )

    # print 'arg_params', arg_params
    # print 'aux_params', aux_params

    # mod.set_params(arg_params, aux_params)
    #
    # input_data = np.array([[1, 2]])
    # array = mx.nd.array(input_data)
    #
    # Batch = namedtuple('Batch', ['data'])
    # mod.forward(Batch([array]))
    #
    # prob = mod.get_outputs()[0].asnumpy()
    # prob = np.squeeze(prob)
    # print prob

if __name__ == '__main__':
    test_example_lstm()

sample code@method3

    net=gluon.nn.SymbolBlock.imports(sym,["Input"],param_file=param)

#2

Hey @HyperGroups

Did you call model.save_checkpoint after .fit? For example, as here: https://github.com/apache/incubator-mxnet/issues/6951 ?

To load a checkpoint after saving checkpoints (as per https://mxnet.incubator.apache.org/tutorials/python/predict_image.html, which it looks like you’re following)
you’ll need to make sure model_prefix is correct. If you run your code ~/Desktop, it should be wolfram.lstm.

However, when I do try to run the code, it looks like the model wasn’t saved correctly. How did you generate those .json and .params files?

I get

sym, arg_params, aux_params = mx.model.load_checkpoint('wolfram-lstm', 0)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-ce5aa0ac7242> in <module>()
----> 1 sym, arg_params, aux_params = mx.model.load_checkpoint('wolfram', 0)

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/mxnet/model.py in load_checkpoint(prefix, epoch)
    441     aux_params = {}
    442     for k, v in save_dict.items():
--> 443         tp, name = k.split(':', 1)
    444         if tp == 'arg':
    445             arg_params[name] = v

ValueError: not enough values to unpack (expected 2, got 1)

Vishaal


#3

Hi
@VishaalKapoor
The model was generated by Export[“wolfram-lstm”, net,“MXNet”], this is a possible bug of Mathematica 11.3’s NeuralNetworks11.3.5 package-wrapped MXNet 1.0.

SeedRandom[1234];
net = NetInitialize@NetChain[{10, Ramp, ReshapeLayer[{5, 2}], 
                    LongShortTermMemoryLayer[5], 3}, "Input" -> 2]
net[{1, 2}]
Export["wolfram-lstm.json", net, "MXNet"];

cross post___mathematica se

But we can also modify the model.py to deal with this exception, and load the model

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/mxnet/model.py in load_checkpoint(prefix, epoch)
        aux_params = {}
        for k, v in save_dict.items():
                if ':' in k:
-->             tp, name = k.split(':', 1)
                if tp == 'arg':
                    arg_params[name] = v
                else:
                    arg_params[name]=v


I'll update a model fixed by DIY-Export, the 'arg' string  as key of dict of params is missing in the previous params file

#4

The wolfram plugin is written for MXNet 1.0, so likely you’re seeing a discrepancy between the two versions (I tried with 1.3.1). If you do find an error with 1.0 which I would find to be unlikely, it would be a legitimate bug in the plugin.

Vishaal