|
- # Copyright 2021 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
-
- import mindspore
- from mindspore import context, Tensor
- from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
-
- from src.fots import FOTS_Infer
-
- parser = argparse.ArgumentParser(description='fots 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("--testing_shape_h", type=int, default=720, help="test hight shape")
- parser.add_argument("--testing_shape_w", type=int, default=720, help="test width shape")
- parser.add_argument("--ckpt_file", default='outputs/2021-11-03_time_16_00_13/ckpt_0/0-52_773.ckpt', type=str, help="Checkpoint file path.")
- parser.add_argument("--file_name", type=str, default="fots", help="output file name.")
- parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='MINDIR', 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)
- if args.device_target == "Ascend":
- context.set_context(device_id=args.device_id)
-
- def load_networks_params(ckpt_path, network):
-
- param_dict = load_checkpoint(ckpt_path)
- param_dict_new = {}
- for key, values in param_dict.items():
- if key.startswith('moments.'):
- continue
- # conv1层
- elif key.startswith('fots_network.resnet.conv1.weight'):
- param_dict_new['conv1.0.weight'] = values
- elif key.startswith('fots_network.resnet.bn1.gamma'):
- param_dict_new['conv1.1.gamma'] = values
- elif key.startswith('fots_network.resnet.bn1.beta'):
- param_dict_new['conv1.1.beta'] = values
- elif key.startswith('fots_network.resnet.bn1.moving_mean'):
- param_dict_new['conv1.1.moving_mean'] = values
- elif key.startswith('fots_network.resnet.bn1.moving_variance'):
- param_dict_new['conv1.1.moving_variance'] = values
-
- # encoder层
- elif key.startswith('fots_network.resnet.'):
- param_dict_new[key[20:].replace('layer', 'encoder')] = values
-
- # 其他层
- elif key.startswith('fots_network.'):
- param_dict_new[key[13:]] = values
- else:
- param_dict_new[key] = values
- load_param_into_net(network, param_dict_new)
-
-
- if __name__ == "__main__":
-
- args.file_name = args.file_name
-
- network = FOTS_Infer()
- network.set_train(False)
-
- param_dict = load_checkpoint(args.ckpt_file)
- # load_networks_params(args.ckpt_file, network)
- load_param_into_net(network, param_dict)
-
-
- input_data = Tensor(np.zeros([args.batch_size, 3, args.testing_shape_h, args.testing_shape_w]), mindspore.float32)
-
- export(network, input_data, file_name=args.file_name, file_format=args.file_format)
- print('==========success export===============')
|