inference_gpu.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import os
  2. import numpy as np
  3. import onnx
  4. import onnxruntime as ort
  5. # The directory of your input and output data
  6. input_data_dir = 'input_data'
  7. output_data_dir = 'output_data'
  8. model_24 = onnx.load('pangu_weather_24.onnx')
  9. # Set the behavier of onnxruntime
  10. options = ort.SessionOptions()
  11. options.enable_cpu_mem_arena=False
  12. options.enable_mem_pattern = False
  13. options.enable_mem_reuse = False
  14. # Increase the number for faster inference and more memory consumption
  15. options.intra_op_num_threads = 1
  16. # Set the behavier of cuda provider
  17. cuda_provider_options = {'arena_extend_strategy':'kSameAsRequested',}
  18. # Initialize onnxruntime session for Pangu-Weather Models
  19. ort_session_24 = ort.InferenceSession('pangu_weather_24.onnx', sess_options=options, providers=[('CUDAExecutionProvider', cuda_provider_options)])
  20. # Load the upper-air numpy arrays
  21. input = np.load(os.path.join(input_data_dir, 'input_upper.npy')).astype(np.float32)
  22. # Load the surface numpy arrays
  23. input_surface = np.load(os.path.join(input_data_dir, 'input_surface.npy')).astype(np.float32)
  24. # Run the inference session
  25. output, output_surface = ort_session_24.run(None, {'input':input, 'input_surface':input_surface})
  26. # Save the results
  27. np.save(os.path.join(output_data_dir, 'output_upper'), output)
  28. np.save(os.path.join(output_data_dir, 'output_surface'), output_surface)