Skip to content
Snippets Groups Projects
Select Git revision
  • c895f6f703ad7dd2f99e751d9884b0aa5d0eea25
  • openEuler-1.0-LTS default protected
  • openEuler-22.09
  • OLK-5.10
  • openEuler-22.03-LTS
  • openEuler-22.03-LTS-Ascend
  • master
  • openEuler-22.03-LTS-LoongArch-NW
  • openEuler-22.09-HCK
  • openEuler-20.03-LTS-SP3
  • openEuler-21.09
  • openEuler-21.03
  • openEuler-20.09
  • 4.19.90-2210.5.0
  • 5.10.0-123.0.0
  • 5.10.0-60.63.0
  • 5.10.0-60.62.0
  • 4.19.90-2210.4.0
  • 5.10.0-121.0.0
  • 5.10.0-60.61.0
  • 4.19.90-2210.3.0
  • 5.10.0-60.60.0
  • 5.10.0-120.0.0
  • 5.10.0-60.59.0
  • 5.10.0-119.0.0
  • 4.19.90-2210.2.0
  • 4.19.90-2210.1.0
  • 5.10.0-118.0.0
  • 5.10.0-106.19.0
  • 5.10.0-60.58.0
  • 4.19.90-2209.6.0
  • 5.10.0-106.18.0
  • 5.10.0-106.17.0
33 results

Kbuild

Blame
  • export.py 2.54 KiB
    # Copyright 2020-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.
    # ============================================================================
    
    """export checkpoint file into air, onnx, mindir models
       Suggest run as python export.py --file_name [file_name] --ckpt_files [ckpt path] --file_format [file format]
    """
    import argparse
    import numpy as np
    import mindspore as ms
    from mindspore import context, Tensor
    from mindspore.train.serialization import export, load_checkpoint, load_param_into_net
    
    parser = argparse.ArgumentParser(description='post process for 310 inference')
    parser.add_argument("--backbone", type=str, required=True, default="densenet161", help="model backbone")
    parser.add_argument("--ckpt_path", type=str, required=True, help="checkpoint file path")
    parser.add_argument("--file_name", type=str, default="densenet161", help="file name")
    parser.add_argument("--file_format", type=str, default="MINDIR", choices=["MINDIR", "AIR"], help="file format")
    parser.add_argument("--device_target", type=str, default="Ascend", choices=["Ascend", "GPU"], help="device target")
    parser.add_argument("--device_id", type=int, default=0, help="device target")
    args = parser.parse_args()
    
    context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target)
    
    def model_export():
        '''main export func'''
        if args.device_target == "Ascend":
            context.set_context(device_id=args.device_id)
    
        if args.backbone == "densenet121":
            from src.densenet121 import MainModel
        elif args.backbone == "densenet161":
            from src.densenet161 import MainModel
        elif args.backbone == "densenet169":
            from src.densenet169 import MainModel
        elif args.backbone == "densenet201":
            from src.densenet201 import MainModel
    
        net = MainModel()
    
        param_dict = load_checkpoint(args.ckpt_path)
        load_param_into_net(net, param_dict)
    
        input_arr = Tensor(np.zeros([1, 3, 224, 224]), ms.float32)
        export(net, input_arr, file_name=args.file_name, file_format=args.file_format)
    
    if __name__ == '__main__':
        model_export()