【问题标题】:How can I make a predict with a lstm-ctc checkpoint in mxnet?如何在 mxnet 中使用 lstm-ctc 检查点进行预测?
【发布时间】:2016-12-10 02:00:36
【问题描述】:

我按照 example/warpctc/lstm_ocr.py 训练模型。现在我保存了一个检查点 mymodel-0100.params 和 mymodel-symbol.json。
那么,如何使用此检查点仅使​​用一张图像进行预测?

我已经厌倦了使用 Predictor 界面,代码如下:

# Load the pre-trained model
symbol_file = "mymodel-symbol.json"
param_file = "mymodel-0100.params"
predictor = Predictor(open(symbol_file).read(),
    open(param_file).read(),
    {'data':(80, 30)})

但数据形状总是会引发错误,我不知道如何设置此值。谁能帮帮我,谢谢。

不过,我也尝试了另一种方法: 在mxnet/example/warpctc/lstm_ocr.py末尾添加一行代码:

model = mx.model.FeedForward(ctx=contexts,
                             symbol=symbol,
                             num_epoch=num_epoch,
                             learning_rate=learning_rate,
                             momentum=momentum,
                             wd=0.00001,
                             initializer=mx.init.Xavier(factor_type="in", magnitude=2.34))

model.fit(X=data_train, eval_data=data_val,
          eval_metric = mx.metric.np(Accuracy),
          batch_end_callback=mx.callback.Speedometer(BATCH_SIZE, 50),)

model.save("ocr")

# add new line for predict
model.predict(data_val)

但总是错误输出:

Traceback (most recent call last):
File "lstm_ctc_ocr.py", line 211, in <module>
training_all()
File "lstm_ctc_ocr.py", line 188, in training_all
model.predict(data_val)
File "/home/bobliu/Work/code/DL/mxnet/python/mxnet/model.py", line 618, in predict
self._init_predictor(data_shapes, type_dict)
File "/home/bobliu/Work/code/DL/mxnet/python/mxnet/model.py", line 541, in _init_predictor
self.ctx[0], grad_req='null', type_dict=type_dict, **dict(input_shapes))
File "/home/bobliu/Work/code/DL/mxnet/python/mxnet/symbol.py", line 685, in simple_bind
arg_types, _, aux_types = self.infer_type(**type_dict)
File "/home/bobliu/Work/code/DL/mxnet/python/mxnet/symbol.py", line 417, in infer_type
ctypes.byref(complete)))
File "/home/bobliu/Work/code/DL/mxnet/python/mxnet/base.py", line 77, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: InferType Error in reshape0: [21:39:03] src/operator/./reshape-inl.h:345: Check failed: (dtype) != (-1) First input must have specified type

【问题讨论】:

    标签: lstm mxnet


    【解决方案1】:

    看完源码。我现在知道我有什么问题了。

    1。降级为 predictor() 函数。

    我们必须使用完整的符号设置数据形状。
    在这个例子中:

        init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h
    
        all_shapes = [('data', (batch_size, 100 * 30))] + init_states + [('label', (batch_size, num_label))]
    

    所以,你必须为所有形状的数据设置。

    2。降级为model.predict

    我认为源代码中出现了一个错误,但我不确定我是否错了。
    请检查 model.py 文件中的预测函数。
    更改以下代码(注意 X.provide_label):

        data_shapes = X.provide_data + X.provide_label
        data_names = [x[0] for x in data_shapes]
        type_dict = dict((key, value.dtype) for (key, value) in self.arg_params.items())
        for x in X.provide_data + X.provide_label:
            if isinstance(x, DataDesc):
                type_dict[x.name] = x.dtype
            else:
                type_dict[x[0]] = mx_real_t
    

    还有这一点:

            for o_list, o_nd in zip(output_list, self._pred_exec.outputs[0:real_size]):
                o_list.append(o_nd.asnumpy())
    

    现在,一切都很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 2018-09-24
      • 1970-01-01
      • 2021-09-15
      • 2018-03-02
      • 1970-01-01
      相关资源
      最近更新 更多