Skip to content

Commit aab73e3

Browse files
authored
【PPSCI Export&Infer No.34】 (#1129)
* pretrain.py添加export和inference * pretrain.py添加export与infere * 上次pr修改,风速微调模型及降水量模型添加export与inference,markdown文件添加命令 * precip.yaml文件mode修正 * pretrain.yamlmode修正 * fourcastnet.md文件修正 * 修正0408的review
1 parent d1c07b7 commit aab73e3

File tree

7 files changed

+440
-6
lines changed

7 files changed

+440
-6
lines changed

docs/zh/examples/fourcastnet.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@
2626
python train_precip.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/precip.pdparams WIND_MODEL_PATH=https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/finetune.pdparams
2727
```
2828

29+
=== "模型导出命令"
30+
31+
``` sh
32+
# 风速预训练模型导出
33+
python train_pretrain.py mode=export
34+
# 风速微调模型导出
35+
python train_finetune.py mode=export
36+
# 降水量模型导出
37+
python train_precip.py mode=export
38+
```
39+
40+
=== "模型推理命令"
41+
42+
``` sh
43+
# 下载风速预测小样本数据
44+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience%2Fdatasets%2FFourcastNet%2F2018-04-04_n6_precip.npy -P ./datasets/era5/test/
45+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience%2Fdatasets%2FFourcastNet%2F2018-04-04_n6.npy -P ./datasets/era5/test/
46+
# 风速预训练模型推理
47+
python train_pretrain.py mode=infer
48+
# 风速微调模型推理
49+
python train_finetune.py mode=infer
50+
# 下载降水量预测小样本数据
51+
wget -nc https://paddle-org.bj.bcebos.com/paddlescience%2Fdatasets%2FFourcastNet%2F2018-09-08_n32.npy -P ./datasets/era5/test/
52+
# 降水量模型推理
53+
python train_precip.py mode=infer
54+
```
55+
56+
2957
| 模型 | 变量名称 | ACC/RMSE(6h) | ACC/RMSE(30h) | ACC/RMSE(60h) | ACC/RMSE(120h) | ACC/RMSE(192h) |
3058
| :-- | :-- | :-- | :-- | :-- | :-- | :-- |
3159
| [风速模型](https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/finetune.pdparams) | U10 | 0.991/0.567 | 0.963/1.130 | 0.891/1.930 | 0.645/3.438 | 0.371/4.915 |

examples/fourcastnet/conf/fourcastnet_finetune.yaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ hydra:
2121
subdir: ./
2222

2323
# general settings
24-
mode: train # running mode: train/eval
24+
mode: train # running mode: train/eval/export/infer
2525
seed: 1024
2626
output_dir: ${hydra:run.dir}
2727
log_freq: 20
@@ -48,10 +48,13 @@ VALID_FILE_PATH: ./datasets/era5/test
4848
# set test data path
4949
TEST_FILE_PATH: ./datasets/era5/out_of_sample/2018.h5
5050

51+
INFER_FILE_PATH: ./datasets/era5/test/2018-09-08_n32.npy
52+
5153
# model settings
5254
MODEL:
5355
afno:
5456
input_keys: ["input"]
57+
output_keys: ["output"]
5558

5659
# training settings
5760
TRAIN:
@@ -75,3 +78,22 @@ EVAL:
7578
compute_metric_by_batch: true
7679
eval_with_no_grad: true
7780
batch_size: 1
81+
82+
INFER:
83+
pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/finetune.pdparams"
84+
export_path: ./inference/fourcastnet_finetune
85+
pdmodel_path: ${INFER.export_path}.pdmodel
86+
pdiparams_path: ${INFER.export_path}.pdiparams
87+
onnx_path: ${INFER.export_path}.onnx
88+
batch_size: ${TRAIN.batch_size}
89+
output_keys: ${MODEL.afno.output_keys}
90+
device: gpu
91+
engine: native
92+
precision: fp32
93+
ir_optim: true
94+
min_subgraph_size: 5
95+
gpu_mem: 2000
96+
gpu_id: 0
97+
max_batch_size: 1024
98+
num_cpu_threads: 10
99+
num_timestamps: 32

examples/fourcastnet/conf/fourcastnet_precip.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ hydra:
2121
subdir: ./
2222

2323
# general settings
24-
mode: train # running mode: train/eval
24+
mode: train # running mode: train/eval/export/infer
2525
seed: 1024
2626
output_dir: ${hydra:run.dir}
2727
log_freq: 20
@@ -56,6 +56,10 @@ TEST_FILE_PATH: ./datasets/era5/precip/out_of_sample/2018.h5
5656
# set wind model path
5757
WIND_MODEL_PATH: outputs_fourcastnet_finetune/checkpoints/latest
5858

59+
# set inference data path
60+
WIND_INFER_PATH: ./datasets/era5/test/2018-04-04_n6.npy
61+
INFER_FILE_PATH: ./datasets/era5/test/2018-04-04_n6_precip.npy
62+
5963
# model settings
6064
MODEL:
6165
afno:
@@ -86,3 +90,23 @@ EVAL:
8690
compute_metric_by_batch: true
8791
eval_with_no_grad: true
8892
batch_size: 1
93+
94+
INFER:
95+
pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/precip.pdparams"
96+
WIND_MODEL_PATH: "https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/finetune.pdparams"
97+
export_path: ./inference/fourcastnet_precip
98+
pdmodel_path: ${INFER.export_path}.pdmodel
99+
pdiparams_path: ${INFER.export_path}.pdiparams
100+
onnx_path: ${INFER.export_path}.onnx
101+
batch_size: ${TRAIN.batch_size}
102+
output_keys: ${MODEL.afno.output_keys}
103+
device: gpu
104+
engine: native
105+
precision: fp32
106+
ir_optim: true
107+
min_subgraph_size: 5
108+
gpu_mem: 2000
109+
gpu_id: 0
110+
max_batch_size: 1024
111+
num_cpu_threads: 10
112+
num_timestamps: 6

examples/fourcastnet/conf/fourcastnet_pretrain.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ hydra:
2121
subdir: ./
2222

2323
# general settings
24-
mode: train # running mode: train/eval
24+
mode: train # running mode: train/eval/export/infer
2525
seed: 1024
2626
output_dir: ${hydra:run.dir}
2727
log_freq: 20
@@ -46,6 +46,9 @@ DATA_TIME_MEAN_PATH: ./datasets/era5/stat/time_means.npy
4646
# set evaluate data path
4747
VALID_FILE_PATH: ./datasets/era5/test
4848

49+
# set inference data path
50+
INFER_FILE_PATH: ./datasets/era5/test/2018-09-08_n32.npy
51+
4952
# model settings
5053
MODEL:
5154
afno:
@@ -72,3 +75,23 @@ EVAL:
7275
compute_metric_by_batch: true
7376
eval_with_no_grad: true
7477
batch_size: 8
78+
79+
80+
INFER:
81+
pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/fourcastnet/pretrain.pdparams"
82+
export_path: ./inference/fourcastnet_pretrain
83+
pdmodel_path: ${INFER.export_path}.pdmodel
84+
pdiparams_path: ${INFER.export_path}.pdiparams
85+
onnx_path: ${INFER.export_path}.onnx
86+
batch_size: ${TRAIN.batch_size}
87+
output_keys: ${MODEL.afno.output_keys}
88+
device: gpu
89+
engine: native
90+
precision: fp32
91+
ir_optim: true
92+
min_subgraph_size: 5
93+
gpu_mem: 2000
94+
gpu_id: 0
95+
max_batch_size: 1024
96+
num_cpu_threads: 10
97+
num_timestamps: 32

examples/fourcastnet/train_finetune.py

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import functools
16+
import os
1617
from os import path as osp
1718
from typing import Tuple
1819

@@ -333,6 +334,110 @@ def output_wind_func(d, var_name, data_mean, data_std):
333334
solver.visualize()
334335

335336

337+
def export(cfg: DictConfig):
338+
# set model
339+
model = ppsci.arch.AFNONet(**cfg.MODEL.afno)
340+
341+
# initialize solver
342+
solver = ppsci.solver.Solver(
343+
model,
344+
pretrained_model_path=cfg.INFER.pretrained_model_path,
345+
)
346+
# export model
347+
from paddle.static import InputSpec
348+
349+
input_spec = [
350+
{
351+
key: InputSpec([None, 20, cfg.IMG_H, cfg.IMG_W], "float32", name=key)
352+
for key in model.input_keys
353+
},
354+
]
355+
solver.export(input_spec, cfg.INFER.export_path)
356+
357+
358+
def inference(cfg: DictConfig):
359+
from deploy.python_infer import pinn_predictor
360+
361+
predictor = pinn_predictor.PINNPredictor(cfg)
362+
363+
data_mean, data_std = fourcast_utils.get_mean_std(
364+
cfg.DATA_MEAN_PATH, cfg.DATA_STD_PATH, cfg.VARS_CHANNEL
365+
)
366+
367+
data = np.load(cfg.INFER_FILE_PATH)
368+
input_0 = (data[:, 0] - data_mean) / data_std
369+
all_data = input_0
370+
371+
for t in range(cfg.INFER.num_timestamps):
372+
data_t = data[:, t + 1]
373+
data_t = (data_t - data_mean) / data_std
374+
all_data = np.concatenate((all_data, data_t), axis=0)
375+
376+
input_dict = {cfg.MODEL.afno.input_keys[0]: all_data}
377+
378+
vis_output = predictor.predict(input_dict, cfg.INFER.batch_size)
379+
380+
vis_dict = {
381+
store_key: vis_output[infer_key]
382+
for store_key, infer_key in zip(cfg.MODEL.afno.output_keys, vis_output.keys())
383+
}
384+
385+
def output_wind_func(output, data_mean, data_std):
386+
output = (output * data_std) + data_mean
387+
wind_data = (output[0] ** 2 + output[1] ** 2) ** 0.5
388+
return wind_data
389+
390+
wind_pred = []
391+
pred_dict = {}
392+
for i in range(cfg.INFER.num_timestamps):
393+
hour = (i + 1) * 6
394+
wind_ = [
395+
output_wind_func(
396+
vis_dict[cfg.MODEL.afno.output_keys[0]][i], data_mean, data_std
397+
)
398+
]
399+
wind_pred.append(wind_)
400+
pred_dict[f"output_{hour}h"] = np.asarray(wind_)
401+
output_dict = {cfg.MODEL.afno.output_keys[0]: np.array(wind_pred)}
402+
403+
wind_pred = []
404+
target_dict = {}
405+
for i in range(cfg.INFER.num_timestamps):
406+
hour = (i + 1) * 6
407+
wind_ = [(data[0][i][0] ** 2 + data[0][i][1] ** 2) ** 0.5]
408+
target_dict[f"target_{hour}h"] = np.asarray(wind_)
409+
410+
vis_dict = {**pred_dict, **target_dict}
411+
412+
plot_expr_dict = {}
413+
for hour in range(6, 6 + cfg.INFER.num_timestamps * 6, 6):
414+
plot_expr_dict.update(
415+
{
416+
f"target_{hour}h": lambda d, hour=hour: d[f"target_{hour}h"],
417+
f"output_{hour}h": lambda d, hour=hour: d[f"output_{hour}h"],
418+
}
419+
)
420+
421+
visualizer_weather = ppsci.visualize.VisualizerWeather(
422+
vis_dict,
423+
plot_expr_dict,
424+
xticks=np.linspace(0, cfg.IMG_W - 1, 13),
425+
xticklabels=[str(i) for i in range(360, -1, -30)],
426+
yticks=np.linspace(0, cfg.IMG_H - 1, 7),
427+
yticklabels=[str(i) for i in range(90, -91, -30)],
428+
vmin=0,
429+
vmax=25,
430+
colorbar_label="m\s",
431+
batch_size=1,
432+
num_timestamps=cfg.INFER.num_timestamps,
433+
prefix="wind",
434+
)
435+
visualizer_weather.save(cfg.INFER.export_path, vis_dict)
436+
save_path = osp.join(cfg.INFER.export_path, "predict.npy")
437+
os.makedirs(cfg.INFER.export_path, exist_ok=True)
438+
np.save(save_path, output_dict[cfg.MODEL.afno.output_keys[0]])
439+
440+
336441
@hydra.main(
337442
version_base=None, config_path="./conf", config_name="fourcastnet_finetune.yaml"
338443
)
@@ -341,8 +446,14 @@ def main(cfg: DictConfig):
341446
train(cfg)
342447
elif cfg.mode == "eval":
343448
evaluate(cfg)
449+
elif cfg.mode == "export":
450+
export(cfg)
451+
elif cfg.mode == "infer":
452+
inference(cfg)
344453
else:
345-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
454+
raise ValueError(
455+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
456+
)
346457

347458

348459
if __name__ == "__main__":

0 commit comments

Comments
 (0)