Ai
107 Star 890 Fork 1.4K

MindSpore/models
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
eval_onnx.py 4.01 KB
一键复制 编辑 原始数据 按行查看 历史
hemaohua 提交于 2023-03-13 09:43 +08:00 . set save_graphs=False
# Copyright 2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Evaluation api."""
import os
import ast
import argparse
import pickle
from mindspore import context
from config import Seq2seqConfig
from src.seq2seq_model import infer_onnx
from src.seq2seq_model.bleu_calculate import bleu_calculate
from src.dataset.tokenizer import Tokenizer
import numpy as np
is_modelarts = False
parser = argparse.ArgumentParser(description='seq2seq')
parser.add_argument("--config", type=str, required=True,
help="model config json file path.")
parser.add_argument("--data_url", type=str, default=None,
help="data address.")
parser.add_argument("--train_url", type=str, default=None,
help="output address.")
parser.add_argument("--test_dataset", type=str, required=True,
help="test dataset address.")
parser.add_argument("--onnx_file", type=str, required=True,
help="existed onnx file path.")
parser.add_argument("--vocab", type=str, required=True,
help="Vocabulary to use.")
parser.add_argument("--bpe_codes", type=str, required=True,
help="bpe codes to use.")
parser.add_argument("--test_tgt", type=str, required=True,
default=None,
help="data file of the test target")
parser.add_argument("--output", type=str, required=False,
default="./output.npz",
help="result file path.")
parser.add_argument("--is_modelarts", type=ast.literal_eval, default=False,
help="running on modelarts")
args, _ = parser.parse_known_args()
if args.is_modelarts:
import moxing as mox
context.set_context(
mode=context.GRAPH_MODE,
device_target="CPU",
reserve_class_name_in_scope=True)
def get_config(config):
config = Seq2seqConfig.from_json_file(config)
config.compute_type = np.float16
config.dtype = np.float32
return config
def _check_args(config):
if not os.path.exists(config):
raise FileNotFoundError("`config` is not existed.")
if not isinstance(config, str):
raise ValueError("`config` must be type of str.")
if __name__ == '__main__':
_check_args(args.config)
_config = get_config(args.config)
if args.is_modelarts:
mox.file.copy_parallel(src_url=args.data_url, dst_url='/cache/dataset_menu/')
_config.test_dataset = '/cache/dataset_menu/newstest2014.en.mindrecord'
_config.onnx_file = '/cache/dataset_menu/seq2seq.onnx'
_config.test_dataset = args.test_dataset
_config.onnx_file = args.onnx_file
result = infer_onnx(_config)
with open(args.output, "wb") as f:
pickle.dump(result, f, 1)
result_npy_addr = args.output
vocab = args.vocab
bpe_codes = args.bpe_codes
test_tgt = args.test_tgt
tokenizer = Tokenizer(vocab, bpe_codes, 'en', 'fr')
scores = bleu_calculate(tokenizer, result_npy_addr, test_tgt)
print(f"BLEU scores is :{scores}")
if args.is_modelarts:
result_npy_addr = output
vocab = '/cache/dataset_menu/vocab.bpe.32000'
bpe_codes = '/cache/dataset_menu/bpe.32000'
test_tgt = '/cache/dataset_menu/newstest2014.fr'
tokenizer = Tokenizer(vocab, bpe_codes, 'en', 'fr')
scores = bleu_calculate(tokenizer, result_npy_addr, test_tgt)
print(f"BLEU scores is :{scores}")
mox.file.copy_parallel(src_url='/cache/infer_output/', dst_url=args.train_url)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mindspore/models.git
git@gitee.com:mindspore/models.git
mindspore
models
models
master

搜索帮助