models icon indicating copy to clipboard operation
models copied to clipboard

transformer 中 inference_model.py产出的模型应该怎样调用呢?有没有示例脚本

Open wwjjy opened this issue 5 years ago • 10 comments

如题,辛苦帮忙看下~

wwjjy avatar Feb 12 '20 11:02 wwjjy

您是指预测部署的问题吗?您看下下面的文档能否满足您需求? https://www.paddlepaddle.org.cn/documentation/docs/zh/advanced_usage/deploy/index_cn.html image

chenwhql avatar Feb 12 '20 12:02 chenwhql

我使用如下的方式来调用save_inference_model来保存预测模型,但是有报错 export FLAGS_eager_delete_tensor_gb=0.0 cd work/transformer/ && python -u main.py \ --do_save_inference_model True \ --src_vocab_fpath ../../gen_data/wmt16_ende_data_bpe_clean/vocab_all.bpe.32000 \ --trg_vocab_fpath ../../gen_data/wmt16_ende_data_bpe_clean/vocab_all.bpe.32000 \ --special_token '<s>' '<e>' '<unk>' \ --init_from_params trained_params/latest_checkpoint \ --beam_size 5 \ --max_out_len 255 \ 报错信息如下: W0213 16:32:39.803478 701 device_context.cc:236] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0 W0213 16:32:39.807530 701 device_context.cc:244] device: 0, cuDNN Version: 7.3. Traceback (most recent call last): File "main.py", line 46, in do_save_inference_model(args) File "/home/aistudio/work/transformer/inference_model.py", line 124, in do_save_inference_model init_from_params(args, exe, test_prog) File "/home/aistudio/work/transformer/inference_model.py", line 72, in init_from_params filename="params.pdparams") File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/io.py", line 798, in load_params filename=filename) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/io.py", line 682, in load_vars filename=filename) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/io.py", line 741, in load_vars format(orig_shape, each_var.name, new_shape)) RuntimeError: Shape not matching: the Program requires a parameter with a shape of ((10000L, 512L)), while the loaded parameter (namely [ src_word_emb_table ]) has a shape of ((36556, 512)). 看起来像是这个脚本没有考虑vocab_size,所以使用了默认的10000,和训练出来的模型参数的shape不匹配导致的报错,求看一下是我调用的方式哪里不对引起的吗

wwjjy avatar Feb 13 '20 08:02 wwjjy

您是要保存模型还是要部署模型,我看您同时使用了do_save_inference_model和init_from_params,是要用已经训练好的参数,继续训练,然后保存吗

chenwhql avatar Feb 13 '20 09:02 chenwhql

这个main函数有do_train、do__predict和do_save_inference_model三种参数,我看do_save_inference_model这个应该是用来保存模型的吧,init_from_params指向的是训练好的参数,就是先加载已经训练好的参数,然后save_inference_model。现在应该是在加载参数的时候就报错了

wwjjy avatar Feb 13 '20 09:02 wwjjy

您好,我再确认下您需求,去找模型负责人确认一下。 你现在是要把模型参数导进去,然后直接跑一轮保存模型吗?不训练也不预测?

chenwhql avatar Feb 13 '20 09:02 chenwhql

你看一下 inference_model.py 就理解我的意思了,这里面就是先用fluid.io.load_params加载模型 然后 在用fluid.io.save_inference_model 来保存模型的一个过程,没有训练也没有预测

wwjjy avatar Feb 13 '20 09:02 wwjjy

抱歉这里是有些问题,我们这边修复下,麻烦还请先行参照train.py这里 https://github.com/PaddlePaddle/models/blob/develop/PaddleNLP/PaddleMT/transformer/train.py#L161 在inference_model.py这里加上一行对词典大小的设置

guoshengCS avatar Feb 13 '20 12:02 guoshengCS

@guoshengCS 这里我加了,我觉得train里面的方法太繁琐了,我是这样加的,目前跑下来没有问题,代码如下,求帮忙看下,这样有问题吗? src_vocab = reader.DataProcessor.load_dict(args.src_vocab_fpath) trg_vocab = reader.DataProcessor.load_dict(args.trg_vocab_fpath) # print(len(src_vocab)) args.src_vocab_size = len(src_vocab) args.trg_vocab_size = len(trg_vocab) 还有就是这个save_inference_model 产出的模型,如何加载进来使用呢?我仿照predict.py写的,主要是将https://github.com/PaddlePaddle/models/blob/1078acc2054807b6a0d65a3716bc90a7d1f1443c/PaddleNLP/PaddleMT/transformer/predict.py#L182 这里的test_prog换为使用load_inference_model加载的形式,而不是实例化一个新的网络结构然后加载参数的方法,但是目前代码还会报错,请问这里需要怎么使用呢?有参考脚本吗?

wwjjy avatar Feb 13 '20 12:02 wwjjy

目前报出的错误是什么?load_inference_model的官方文档能帮到您吗? https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/io_cn/load_inference_model_cn.html

chenwhql avatar Feb 14 '20 08:02 chenwhql

我也有类似的问题,建议增加load_inference_model加载模型预测的示例,方便部署的时候进行参考。

hong19860320 avatar Apr 13 '20 14:04 hong19860320