diff --git a/model_zoo/official/cv/faster_rcnn/export.py b/model_zoo/official/cv/faster_rcnn/export.py index cc37345d58..999868bb78 100644 --- a/model_zoo/official/cv/faster_rcnn/export.py +++ b/model_zoo/official/cv/faster_rcnn/export.py @@ -22,20 +22,22 @@ from mindspore import Tensor, load_checkpoint, load_param_into_net, export from src.FasterRcnn.faster_rcnn_r50 import Faster_Rcnn_Resnet50 from src.config import config -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='fasterrcnn_export') - parser.add_argument('--ckpt_file', type=str, default='', help='fasterrcnn ckpt file.') - parser.add_argument('--output_file', type=str, default='', help='fasterrcnn output air name.') - args_opt = parser.parse_args() +parser = argparse.ArgumentParser(description='fasterrcnn_export') +parser.add_argument('--ckpt_file', type=str, default='', help='fasterrcnn ckpt file.') +parser.add_argument('--output_file', type=str, default='', help='fasterrcnn output air name.') +parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format') +args = parser.parse_args() +if __name__ == '__main__': net = Faster_Rcnn_Resnet50(config=config) - param_dict = load_checkpoint(args_opt.ckpt_file) + param_dict = load_checkpoint(args.ckpt_file) load_param_into_net(net, param_dict) - img = Tensor(np.random.uniform(0.0, 1.0, size=[1, 3, 768, 1280]), ms.float16) - img_shape = Tensor(np.random.uniform(0.0, 1.0, size=[768, 1280, 1]), ms.float16) - gt_bboxes = Tensor(np.random.uniform(0.0, 1.0, size=[1, 128]), ms.float16) - gt_label = Tensor(np.random.uniform(0.0, 1.0, size=[1, 128]), ms.int32) - gt_num = Tensor(np.random.uniform(0.0, 1.0, size=[1, 128]), ms.bool) - export(net, img, img_shape, gt_bboxes, gt_label, gt_num, file_name=args_opt.output_file, file_format="AIR") + img = Tensor(np.zeros([config.test_batch_size, 3, config.img_height, config.img_width]), ms.float16) + img_metas = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, 4]), ms.float16) + gt_bboxes = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.float16) + gt_label = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.int32) + gt_num = Tensor(np.random.uniform(0.0, 1.0, size=[config.test_batch_size, config.num_gts]), ms.bool_) + + export(net, img, img_metas, gt_bboxes, gt_label, gt_num, file_name=args.output_file, file_format=args.file_format) diff --git a/model_zoo/official/cv/inceptionv3/export.py b/model_zoo/official/cv/inceptionv3/export.py index 10b1b3b310..c7fa16cd56 100644 --- a/model_zoo/official/cv/inceptionv3/export.py +++ b/model_zoo/official/cv/inceptionv3/export.py @@ -13,7 +13,7 @@ # limitations under the License. # ============================================================================ """ -##############export checkpoint file into air and onnx models################# +export checkpoint file into models """ import argparse import numpy as np @@ -24,16 +24,19 @@ from mindspore import Tensor, load_checkpoint, load_param_into_net, export from src.config import config_gpu as cfg from src.inception_v3 import InceptionV3 +parser = argparse.ArgumentParser(description='inceptionv3 export') +parser.add_argument('--ckpt_file', type=str, required=True, help='inceptionv3 ckpt file.') +parser.add_argument('--output_file', type=str, default='inceptionv3.air', help='inceptionv3 output air name.') +parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format') +parser.add_argument('--width', type=int, default=299, help='input width') +parser.add_argument('--height', type=int, default=299, help='input height') +args = parser.parse_args() if __name__ == '__main__': - parser = argparse.ArgumentParser(description='checkpoint export') - parser.add_argument('--checkpoint', type=str, default='', help='checkpoint of inception-v3 (Default: None)') - args_opt = parser.parse_args() - net = InceptionV3(num_classes=cfg.num_classes, is_training=False) - param_dict = load_checkpoint(args_opt.checkpoint) + param_dict = load_checkpoint(args.ckpt_file) load_param_into_net(net, param_dict) - input_arr = Tensor(np.random.uniform(0.0, 1.0, size=[1, 3, 299, 299]), ms.float32) - export(net, input_arr, file_name=cfg.onnx_filename, file_format="ONNX") - export(net, input_arr, file_name=cfg.air_filename, file_format="AIR") + input_arr = Tensor(np.random.uniform(0.0, 1.0, size=[cfg.batch_size, 3, args.width, args.height]), ms.float32) + + export(net, input_arr, file_name=args.output_file, file_format=args.file_format) diff --git a/model_zoo/official/cv/vgg16/export.py b/model_zoo/official/cv/vgg16/export.py new file mode 100644 index 0000000000..1cdc732823 --- /dev/null +++ b/model_zoo/official/cv/vgg16/export.py @@ -0,0 +1,61 @@ +# Copyright 2020 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. +# ============================================================================ +"""export checkpoint file into models""" +import argparse +import numpy as np + +from mindspore import Tensor, context +import mindspore.common.dtype as mstype +from mindspore.train.serialization import load_checkpoint, export + +from src.vgg import vgg16 + +context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") + +parser = argparse.ArgumentParser(description='VGG16 export') +parser.add_argument('--dataset', type=str, choices=["cifar10", "imagenet2012"], default="cifar10", help='ckpt file') +parser.add_argument('--ckpt_file', type=str, required=True, help='vgg16 ckpt file.') +parser.add_argument('--output_file', type=str, default='vgg16.air', help='vgg16 output air name.') +parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format') +args = parser.parse_args() + +if args.dataset == "cifar10": + from src.config import cifar_cfg as cfg +else: + from src.config import imagenet_cfg as cfg + +args.num_classes = cfg.num_classes +args.pad_mode = cfg.pad_mode +args.padding = cfg.padding +args.has_bias = cfg.has_bias +args.initialize_mode = cfg.initialize_mode +args.batch_norm = cfg.batch_norm +args.has_dropout = cfg.has_dropout +args.image_size = list(map(int, cfg.image_size.split(','))) + + +if __name__ == '__main__': + if args.dataset == "cifar10": + net = vgg16(num_classes=args.num_classes, args=args) + else: + net = vgg16(args.num_classes, args, phase="test") + net.add_flags_recursive(fp16=True) + + load_checkpoint(args.ckpt_file, net=net) + net.set_train(False) + + input_data = Tensor(np.zeros([cfg.batch_size, 3, args.image_size[0], args.image_size[1]]), mstype.float32) + + export(net, input_data, file_name=args.output_file, file_format=args.file_format) diff --git a/model_zoo/official/nlp/bert/export.py b/model_zoo/official/nlp/bert/export.py new file mode 100644 index 0000000000..b77fa08fa0 --- /dev/null +++ b/model_zoo/official/nlp/bert/export.py @@ -0,0 +1,74 @@ +# Copyright 2020 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. +# ============================================================================ +"""export checkpoint file into models""" +import argparse +import numpy as np + +from mindspore import Tensor, context +import mindspore.common.dtype as mstype +from mindspore.train.serialization import load_checkpoint, export + +from src.finetune_eval_model import BertCLSModel, BertSquadModel, BertNERModel +from src.finetune_eval_config import optimizer_cfg, bert_net_cfg +from src.utils import convert_labels_to_index + +context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") + +parser = argparse.ArgumentParser(description='Bert export') +parser.add_argument('--use_crf', type=str, default="false", help='Use cfg, default is false.') +parser.add_argument('--downstream_task', type=str, choices=["NER", "CLS", "SQUAD"], default="NER", + help='at present,support NER only') +parser.add_argument('--num_class', type=int, default=2, help='The number of class, default is 2.') +parser.add_argument('--label_file_path', type=str, default="", help='label file path, used in clue benchmark.') +parser.add_argument('--ckpt_file', type=str, required=True, help='Bert ckpt file.') +parser.add_argument('--output_file', type=str, default='Bert.air', help='bert output air name.') +parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='AIR', help='file format') +args = parser.parse_args() + +label_list = [] +with open(args.label_file_path) as f: + for label in f: + label_list.append(label.strip()) + +tag_to_index = convert_labels_to_index(label_list) + +if args.use_crf.lower() == "true": + max_val = max(tag_to_index.values()) + tag_to_index[""] = max_val + 1 + tag_to_index[""] = max_val + 2 + number_labels = len(tag_to_index) +else: + number_labels = args.num_class + + +if __name__ == '__main__': + if args.downstream_task == "NER": + net = BertNERModel(bert_net_cfg, False, number_labels, use_crf=(args.use_crf.lower() == "true")) + elif args.downstream_task == "CLS": + net = BertCLSModel(bert_net_cfg, False, num_labels=number_labels) + elif args.downstream_task == "SQUAD": + net = BertSquadModel(bert_net_cfg, False) + else: + raise ValueError("unsupported downstream task") + + load_checkpoint(args.ckpt_file, net=net) + net.set_train(False) + + input_ids = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32) + input_mask = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32) + token_type_id = Tensor(np.zeros([optimizer_cfg.batch_size, bert_net_cfg.seq_length]), mstype.int32) + + input_data = [input_ids, input_mask, token_type_id] + export(net, *input_data, file_name=args.output_file, file_format=args.file_format)