From 5b4d25bc6d6fac2847e0fce91c266cb747c54fe8 Mon Sep 17 00:00:00 2001 From: yuzhenhua Date: Sat, 12 Dec 2020 11:10:48 +0800 Subject: [PATCH] add export file for bgcf,maskrcnn_mobilenetv1,deepfm --- .../cv/maskrcnn_mobilenetv1/export.py | 51 +++++++++++++++ model_zoo/official/cv/mobilenetv1/export.py | 57 ++++++++++++++++ model_zoo/official/gnn/bgcf/export.py | 65 +++++++++++++++++++ model_zoo/official/recommend/deepfm/export.py | 50 ++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 model_zoo/official/cv/maskrcnn_mobilenetv1/export.py create mode 100644 model_zoo/official/cv/mobilenetv1/export.py create mode 100644 model_zoo/official/gnn/bgcf/export.py create mode 100644 model_zoo/official/recommend/deepfm/export.py diff --git a/model_zoo/official/cv/maskrcnn_mobilenetv1/export.py b/model_zoo/official/cv/maskrcnn_mobilenetv1/export.py new file mode 100644 index 0000000000..23ab45ee4e --- /dev/null +++ b/model_zoo/official/cv/maskrcnn_mobilenetv1/export.py @@ -0,0 +1,51 @@ +# 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. +# ============================================================================ + +import argparse +import numpy as np + +from mindspore import Tensor, context, load_checkpoint, export + +from src.maskrcnn_mobilenetv1.mask_rcnn_mobilenetv1 import Mask_Rcnn_Mobilenetv1 +from src.config import config + +parser = argparse.ArgumentParser(description="maskrcnn mobilnetv1 export") +parser.add_argument("--device_id", type=int, default=0, help="Device id") +parser.add_argument("--batch_size", type=int, default=1, help="batch size") +parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.") +parser.add_argument("--file_name", type=str, default="maskrcnn_mobilenetv1", help="output file name.") +parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format") +parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", + help="device target") +args = parser.parse_args() + +context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id) + +if __name__ == '__main__': + config.test_batch_size = args.batch_size + net = Mask_Rcnn_Mobilenetv1(config) + load_checkpoint(args.ckpt_file, net=net) + + net.set_train(False) + + img_data = Tensor(np.zeros([args.batch_size, 3, config.img_height, config.img_width], np.float16)) + img_metas = Tensor(np.zeros([args.batch_size, 4], np.float16)) + gt_bboxes = Tensor(np.zeros([args.batch_size, config.num_gts, 4], np.float16)) + gt_labels = Tensor(np.zeros([args.batch_size, config.num_gts], np.int32)) + gt_num = Tensor(np.zeros([args.batch_size, config.num_gts], np.bool)) + gt_mask = Tensor(np.zeros([args.batch_size, 1, 1, 1], np.bool)) + + input_data = [img_data, img_metas, gt_bboxes, gt_labels, gt_num, gt_mask] + export(net, *input_data, file_name=args.file_name, file_format=args.file_format) diff --git a/model_zoo/official/cv/mobilenetv1/export.py b/model_zoo/official/cv/mobilenetv1/export.py new file mode 100644 index 0000000000..9af3d3cf0a --- /dev/null +++ b/model_zoo/official/cv/mobilenetv1/export.py @@ -0,0 +1,57 @@ +# 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. +# ============================================================================ + +import argparse +import numpy as np + +from mindspore import context, Tensor +from mindspore.train.serialization import export, load_checkpoint + +from src.mobilenet_v1 import mobilenet_v1 as mobilenet + +parser = argparse.ArgumentParser(description="mobilenetv1 export") +parser.add_argument("--device_id", type=int, default=0, help="Device id") +parser.add_argument("--batch_size", type=int, default=256, help="batch size") +parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.") +parser.add_argument("--dataset", type=str, default="imagenet2012", help="Dataset, either cifar10 or imagenet2012") +parser.add_argument("--file_name", type=str, default="mobilenetv1", help="output file name.") +parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format") +parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", + help="device target") +args = parser.parse_args() + +context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=args.device_id) + +if args.dataset == "cifar10": + from src.config import config1 as config +else: + from src.config import config2 as config + +if __name__ == "__main__": + config.batch_size = args.batch_size + + target = args.device_target + if target != "GPU": + context.set_context(device_id=args.device_id) + + network = mobilenet(class_num=config.class_num) + + param_dict = load_checkpoint(args.ckpt_file, net=network) + + network.set_train(False) + + input_data = Tensor(np.zeros([config.batch_size, 3, 224, 224]).astype(np.float32)) + + export(network, input_data, file_name=args.file_name, file_format=args.file_format) diff --git a/model_zoo/official/gnn/bgcf/export.py b/model_zoo/official/gnn/bgcf/export.py new file mode 100644 index 0000000000..40309725ea --- /dev/null +++ b/model_zoo/official/gnn/bgcf/export.py @@ -0,0 +1,65 @@ +# 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 ckpt to model""" +import argparse +import numpy as np + +from mindspore import context, Tensor +from mindspore.train.serialization import export, load_checkpoint + +from src.bgcf import BGCF +from src.callback import ForwardBGCF + +parser = argparse.ArgumentParser(description="bgcf export") +parser.add_argument("--device_id", type=int, default=0, help="Device id") +parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.") +parser.add_argument("--file_name", type=str, default="bgcf", help="output file name.") +parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format") +parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", + help="device target") +parser.add_argument("--input_dim", type=int, choices=[64, 128], default=64, help="embedding dimension") +parser.add_argument("--embedded_dimension", type=int, default=64, help="output embedding dimension") +parser.add_argument("--row_neighs", type=int, default=40, help="num of sampling neighbors in raw graph") +parser.add_argument("--gnew_neighs", type=int, default=20, help="num of sampling neighbors in sample graph") +parser.add_argument("--activation", type=str, default="tanh", choices=["relu", "tanh"], help="activation function") +args = parser.parse_args() + +context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id) + +if __name__ == "__main__": + num_user, num_item = 7068, 3570 + + network = BGCF([args.input_dim, num_user, num_item], + args.embedded_dimension, + args.activation, + [0.0, 0.0, 0.0], + num_user, + num_item, + args.input_dim) + + load_checkpoint(args.ckpt_file, net=network) + + forward_net = ForwardBGCF(network) + + users = Tensor(np.zeros([num_user,]).astype(np.int32)) + items = Tensor(np.zeros([num_item,]).astype(np.int32)) + neg_items = Tensor(np.zeros([num_item, 1]).astype(np.int32)) + u_test_neighs = Tensor(np.zeros([num_user, args.row_neighs]).astype(np.int32)) + u_test_gnew_neighs = Tensor(np.zeros([num_user, args.gnew_neighs]).astype(np.int32)) + i_test_neighs = Tensor(np.zeros([num_item, args.row_neighs]).astype(np.int32)) + i_test_gnew_neighs = Tensor(np.zeros([num_item, args.gnew_neighs]).astype(np.int32)) + + input_data = [users, items, neg_items, u_test_neighs, u_test_gnew_neighs, i_test_neighs, i_test_gnew_neighs] + export(forward_net, *input_data, file_name=args.file_name, file_format=args.file_format) diff --git a/model_zoo/official/recommend/deepfm/export.py b/model_zoo/official/recommend/deepfm/export.py new file mode 100644 index 0000000000..87c38aafc9 --- /dev/null +++ b/model_zoo/official/recommend/deepfm/export.py @@ -0,0 +1,50 @@ +# 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 ckpt to model""" +import argparse +import numpy as np + +from mindspore import context, Tensor +from mindspore.train.serialization import export, load_checkpoint + +from src.deepfm import ModelBuilder +from src.config import DataConfig, ModelConfig, TrainConfig + +parser = argparse.ArgumentParser(description="deepfm export") +parser.add_argument("--device_id", type=int, default=0, help="Device id") +parser.add_argument("--batch_size", type=int, default=16000, help="batch size") +parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.") +parser.add_argument("--file_name", type=str, default="deepfm", help="output file name.") +parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format") +parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", + help="device target") +args = parser.parse_args() + +context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id) + +if __name__ == "__main__": + data_config = DataConfig() + + model_builder = ModelBuilder(ModelConfig, TrainConfig) + _, network = model_builder.get_train_eval_net() + + load_checkpoint(args.ckpt_file, net=network) + + batch_ids = Tensor(np.zeros([data_config.batch_size, data_config.data_field_size]).astype(np.int32)) + batch_wts = Tensor(np.zeros([data_config.batch_size, data_config.data_field_size]).astype(np.float32)) + labels = Tensor(np.zeros([data_config.batch_size, 1]).astype(np.float32)) + + input_data = [batch_ids, batch_wts, labels] + export(network, *input_data, file_name=args.file_name, file_format=args.file_format)