diff --git a/docs/zh/examples/laplace2d.md b/docs/zh/examples/laplace2d.md index f54388497b..e2fb10baa2 100644 --- a/docs/zh/examples/laplace2d.md +++ b/docs/zh/examples/laplace2d.md @@ -14,6 +14,18 @@ python laplace2d.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/laplace2d/laplace2d_pretrained.pdparams ``` +=== "模型导出命令" + + ``` sh + python laplace2d.py mode=export + ``` + +=== "模型推理命令" + + ``` sh + python laplace2d.py mode=infer + ``` + | 预训练模型 | 指标 | |:--| :--| | [laplace2d_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/laplace2d/laplace2d_pretrained.pdparams) | loss(MSE_Metric): 0.00002
MSE.u(MSE_Metric): 0.00002 | diff --git a/examples/laplace/conf/laplace2d.yaml b/examples/laplace/conf/laplace2d.yaml index 01a6b0c839..54440f5406 100644 --- a/examples/laplace/conf/laplace2d.yaml +++ b/examples/laplace/conf/laplace2d.yaml @@ -22,6 +22,7 @@ hydra: # general settings mode: train # running mode: train/eval seed: 42 +log_freq: 20 output_dir: ${hydra:run.dir} NPOINT_INTERIOR: 9801 NPOINT_BC: 400 @@ -50,3 +51,20 @@ TRAIN: EVAL: pretrained_model_path: null eval_with_no_grad: true + +INFER: + pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/laplace2d/laplace2d_pretrained.pdparams + export_path: ./inference/laplace2d + pdmodel_path: ${INFER.export_path}.pdmodel + pdpiparams_path: ${INFER.export_path}.pdiparams + device: gpu + engine: native + precision: fp32 + onnx_path: ${INFER.export_path}.onnx + ir_optim: true + min_subgraph_size: 10 + gpu_mem: 4000 + gpu_id: 0 + max_batch_size: 64 + num_cpu_threads: 4 + batch_size: 64 diff --git a/examples/laplace/laplace2d.py b/examples/laplace/laplace2d.py index 603c63bf4c..057888b8f4 100644 --- a/examples/laplace/laplace2d.py +++ b/examples/laplace/laplace2d.py @@ -205,14 +205,71 @@ def u_solution_func(out): solver.visualize() +def export(cfg: DictConfig): + # set model + model = ppsci.arch.MLP(**cfg.MODEL) + + # initialize solver + solver = ppsci.solver.Solver( + model, + pretrained_model_path=cfg.INFER.pretrained_model_path, + ) + # export model + from paddle.static import InputSpec + + input_spec = [ + {key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys}, + ] + solver.export(input_spec, cfg.INFER.export_path) + + +def inference(cfg: DictConfig): + from deploy.python_infer import pinn_predictor + + predictor = pinn_predictor.PINNPredictor(cfg) + + # set geometry + geom = { + "rect": ppsci.geometry.Rectangle( + cfg.DIAGONAL_COORD.xmin, cfg.DIAGONAL_COORD.xmax + ) + } + NPOINT_TOTAL = cfg.NPOINT_INTERIOR + cfg.NPOINT_BC + input_dict = geom["rect"].sample_interior(NPOINT_TOTAL, evenly=True) + + output_dict = predictor.predict( + {key: input_dict[key] for key in cfg.MODEL.input_keys}, cfg.INFER.batch_size + ) + + # mapping data to cfg.INFER.output_keys + output_dict = { + store_key: output_dict[infer_key] + for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys()) + } + + # save result + ppsci.visualize.save_vtu_from_dict( + "./laplace2d_pred.vtu", + {**input_dict, **output_dict}, + input_dict.keys(), + cfg.MODEL.output_keys, + ) + + @hydra.main(version_base=None, config_path="./conf", config_name="laplace2d.yaml") def main(cfg: DictConfig): if cfg.mode == "train": train(cfg) elif cfg.mode == "eval": evaluate(cfg) + elif cfg.mode == "export": + export(cfg) + elif cfg.mode == "infer": + inference(cfg) else: - raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'") + raise ValueError( + f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'" + ) if __name__ == "__main__":