You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

export.py 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """export checkpoint file into air, mindir and onnx models"""
  16. import argparse
  17. import numpy as np
  18. from mindspore import Tensor, context, load_checkpoint, export
  19. from src.gat import GAT
  20. from src.config import GatConfig
  21. parser = argparse.ArgumentParser(description="GAT export")
  22. parser.add_argument("--device_id", type=int, default=0, help="Device id")
  23. parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.")
  24. parser.add_argument("--dataset", type=str, default="cora", choices=["cora", "citeseer"], help="Dataset.")
  25. parser.add_argument("--file_name", type=str, default="gat", help="output file name.")
  26. parser.add_argument("--file_format", type=str, choices=["AIR", "ONNX", "MINDIR"], default="AIR", help="file format")
  27. parser.add_argument("--device_target", type=str, default="Ascend",
  28. choices=["Ascend", "GPU", "CPU"], help="device target (default: Ascend)")
  29. args = parser.parse_args()
  30. context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id)
  31. if __name__ == "__main__":
  32. if args.dataset == "citeseer":
  33. feature_size = [1, 3312, 3703]
  34. biases_size = [1, 3312, 3312]
  35. num_classes = 6
  36. else:
  37. feature_size = [1, 2708, 1433]
  38. biases_size = [1, 2708, 2708]
  39. num_classes = 7
  40. hid_units = GatConfig.hid_units
  41. n_heads = GatConfig.n_heads
  42. feature = np.random.uniform(0.0, 1.0, size=feature_size).astype(np.float32)
  43. biases = np.random.uniform(0.0, 1.0, size=biases_size).astype(np.float64)
  44. feature_size = feature.shape[2]
  45. num_nodes = feature.shape[1]
  46. gat_net = GAT(feature_size,
  47. num_classes,
  48. num_nodes,
  49. hid_units,
  50. n_heads,
  51. attn_drop=0.0,
  52. ftr_drop=0.0)
  53. gat_net.set_train(False)
  54. load_checkpoint(args.ckpt_file, net=gat_net)
  55. gat_net.add_flags_recursive(fp16=True)
  56. export(gat_net, Tensor(feature), Tensor(biases), file_name=args.file_name, file_format=args.file_format)