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.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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)
  31. if args.device_target == "Ascend":
  32. context.set_context(device_id=args.device_id)
  33. if __name__ == "__main__":
  34. if args.dataset == "citeseer":
  35. feature_size = [1, 3312, 3703]
  36. biases_size = [1, 3312, 3312]
  37. num_classes = 6
  38. else:
  39. feature_size = [1, 2708, 1433]
  40. biases_size = [1, 2708, 2708]
  41. num_classes = 7
  42. hid_units = GatConfig.hid_units
  43. n_heads = GatConfig.n_heads
  44. feature = np.random.uniform(0.0, 1.0, size=feature_size).astype(np.float32)
  45. biases = np.random.uniform(0.0, 1.0, size=biases_size).astype(np.float64)
  46. feature_size = feature.shape[2]
  47. num_nodes = feature.shape[1]
  48. gat_net = GAT(feature_size,
  49. num_classes,
  50. num_nodes,
  51. hid_units,
  52. n_heads,
  53. attn_drop=0.0,
  54. ftr_drop=0.0)
  55. gat_net.set_train(False)
  56. load_checkpoint(args.ckpt_file, net=gat_net)
  57. gat_net.add_flags_recursive(fp16=True)
  58. export(gat_net, Tensor(feature), Tensor(biases), file_name=args.file_name, file_format=args.file_format)