Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【快乐开源】为 PaddleScience 案例添加 export 和 inference 功能 #788

Open
HydrogenSulfate opened this issue Feb 26, 2024 · 20 comments
Assignees
Labels
HappyOpenSource 快乐开源活动issue与PR

Comments

@HydrogenSulfate
Copy link
Collaborator

HydrogenSulfate commented Feb 26, 2024

This project will be mentored by @HydrogenSulfate and @xusuyong

以下是待添加模型导出和python推理功能的案例列表:

序号 文档文件 案例文件 认领人/状态/PR号
1 deephpms.md deephpms/burgers.pydeephpms/korteweg_de_vries.py
deephpms/kuramoto_sivashinsky.py
deephpms/navier_stokes.py
deephpms/schrodinger.py
 @GoldenStain
2 deeponet.md operator_learning/deeponet.py  @liujun121533
@HydrogenSulfate #901
@essos-bot
3 euler_beam.md euler_beam/euler_beam.py  @GreatV #796
4 laplace2d.md laplace/laplace2d.py  @GreatV #797
5 lorenz.md lorenz/train_transformer.py  @GreatV #801
6 rossler.md rossler/train_transformer.py  @GreatV #803
7 volterra_ide.md ide/volterra_ide.py  @GreatV #807
8 amgnet.md amgnet/amgnet_airfoil.pyamgnet/amgnet_cylinder.py  
9 bubble.md bubble/bubble.py  @wufei2 #887
10 cfdgcn.md cfdgcn/cfdgcn.py  
11 cylinder2d_unsteady.md cylinder/2d_unsteady/cylinder2d_unsteady_Re100.py  @wufei2 #883
12 cylinder2d_unsteady_transformer_physx.md cylinder/2d_unsteady/transformer_physx/train_transformer.py  @wufei2 #883
13 darcy2d.md darcy/darcy2d.py  @wufei2 #900
14 deepcfd.md deepcfd/deepcfd.py  @GoldenStain #994
15 ldc2d_steady.md ldc/ldc2d_steady_Re10.py  @wufei2 #875
16 ldc2d_unsteady.md ldc/ldc2d_unsteady_Re10.py  @wufei2 #875
17 labelfree_DNN_surrogate.md pipe/poiseuille_flow.py  
18 nsfnet.md aneurysm/aneurysm_flow.py  
19 phycrnet.md phycrnet/main.py  
20 shock_wave.md shock_wave/shock_wave.py  @smallpoxscattered #890
21 tempoGAN.md tempoGAN/tempoGAN.py  @smallpoxscattered #884
22 nsfnet4.md nsfnet/VP_NSFNet4.py  @smallpoxscattered #864
23 viv.md fsi/viv.py  @smallpoxscattered #832
24 biharmonic2d.md biharmonic2d/biharmonic2d.py  @smallpoxscattered #858
25 bracket.md bracket/bracket.py  @1want2sleep #878
26 control_arm.md control_arm/forward_analysis.py  @smallpoxscattered #893
27 epnn.md control_arm/inverse_parameter.py  @smallpoxscattered #893
28 phylstm.md phylstm/phylstm2.pyphylstm/phylstm3.py  
29 topopt.md topopt/topopt.py  @NKNaN #793
30 heat_exchanger.md heat_exchanger/heat_exchanger.py  @wufei2 #892
31 heat_pinn.md heat_pinn/heat_pinn.py  @1want2sleep #926
32 phygeonet.md phygeonet/heat_equation.py
phygeonet/heat_equation_with_bc.py
 
33 hpinns.md hpinns/holography.py  
34 fourcastnet.md fourcastnet/train_pretrain.py
fourcastnet/train_precip.py
fourcastnet/train_finetune.py
 
35 nowcastnet.md nowcastnet/nowcastnet.py @smallpoxscattered #895

认领方式

请大家以 comment 的形式认领任务,如:

【报名】:1、3、12-13

多个任务之间需要使用中文顿号分隔,报名多个连续任务可用横线表示,如 2-5
PR 提交格式:在 PR 的标题中以 【PPSCI Export&Infer No.】 开头,注明任务编号

看板信息

任务方向 任务数量 提交作品 / 任务认领 提交率 完成 完成率
快乐开源 35 25 / 26 71.43% 25 71.43%

统计信息

排名不分先后 @HydrogenSulfate (1) @GreatV (5) @wufei2 (7) @GoldenStain (1) @smallpoxscattered (8) @1want2sleep (2) @NKNaN (1)

1. 背景

PaddleScience 套件为现有的 30+ 模型支持了一键训练、一键测试功能,但通过模型导出和python推理功能才能使得每个案例可以快速部署在各种设备上。

2. 收益

学习模型导出和python推理的基本流程,为案例代码添加模型导出和python推理功能

3. 开发流程

3.1 安装 PaddleScience

  1. 安装 Paddle:https://paddlescience-docs.readthedocs.io/zh/latest/zh/install_setup/#13-paddlepaddle
  2. 安装 PaddleScience(git 源码安装):https://paddlescience-docs.readthedocs.io/zh/latest/zh/install_setup/#__tabbed_1_1

3.2 代码开发

以为 aneurysm 案例添加模型导出和 python 推理函数为例

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)
  1. 参考train/eval函数,以同样的方式实例化一个网络模型model,如果

  2. 实例化一个solver,传入model、预训练模型路径变量cfg.INFER.pretrained_model_path(该路径设置为案例文档开头“模型评估命令”的EVAL.pretrained_model_path后的url值即可,如下所示)
    image

  3. 根据modelforwrad函数接受的输入格式,构造同样格式的input_spec。aneurysm 的输入格式为{"x": Tensor, "y": Tensor, "z": Tensor},因此构造的input_spec为:{"x": InputSpec([None, 1], "float32", name="x"), "y": InputSpec([None, 1], "float32", name="y"), "z": InputSpec([None, 1], "float32", name="z")}

  4. 调用solver.export导出模型至cfg.INFER.export_path路径下,导出成功后会打印:Inference model has been exported to: ./inference/aneurysm, including *.pdmodel, *.pdiparams and *.pdiparams.info files.
    至此模型导出函数export就完成了。接下来介绍如何撰写 python 推理代码inference函数

def inference(cfg: DictConfig):
    from deploy.python_infer import pinn_predictor

    predictor = pinn_predictor.PINNPredictor(cfg)
    eval_data_dict = reader.load_csv_file(
        cfg.EVAL_CSV_PATH,
        ("x", "y", "z", "u", "v", "w", "p"),
        {
            "x": "Points:0",
            "y": "Points:1",
            "z": "Points:2",
            "u": "U:0",
            "v": "U:1",
            "w": "U:2",
            "p": "p",
        },
    )
    input_dict = {
        "x": (eval_data_dict["x"] - cfg.CENTER[0]) * cfg.SCALE,
        "y": (eval_data_dict["y"] - cfg.CENTER[1]) * cfg.SCALE,
        "z": (eval_data_dict["z"] - cfg.CENTER[2]) * cfg.SCALE,
    }
    output_dict = predictor.predict(input_dict, 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())
    }

    ppsci.visualize.save_vtu_from_dict(
        "./aneurysm_pred.vtu",
        {**input_dict, **output_dict},
        input_dict.keys(),
        cfg.MODEL.output_keys,
    )
  1. 导入本模型对应的predictor,因为 aneurysm 的输入都是形状为[batch_size, channels]的Tensor,因此使用导入pinn_predictor即可;如果输入形状语义或者组织结构与之不同,则需要参照deploy/python_infer/pinn_predictor.pyPINNPredictor,写一个对应的XXXPredictor,再导入使用
  2. cfg为参数,实例化predictor
  3. 参考eval函数,构造输入数据input_dict
  4. 调用predictor.predict接受输入数据并推理得到输出output_dict
  5. 由于 python 推理的输出名字与原model的输出名字不同,因此需要将输出output_dict的键重新映射回正确的键上
  6. 参照eval函数,用保存/打印等方式记录推理输出output_dict
    运行结果如下所示
    image

如果在模型导出/推理时需要额外的超参数,则可以将这些超参数添加到案例对应 YAML 文件的 INFER 字段下,参考:https://github.com/PaddlePaddle/PaddleScience/pull/786/files#diff-1c33a8b95e9d0c72395763d5c906936652654533714e23211e2e2bb204d379f3R50-R55

3.3 文档添加 export&inference 运行命令

按照 3. 编写文档教程,安装文档渲染插件并渲染文档,用网页打开文档预览链接。

aneurysm.md文档为例,添加红框内两处文本即可
image

添加后文档渲染结果如下所示

image
image

3.4 整理代码并提交PR

参考 PaddleScience文档-贡献指南-整理代码并提交

4. 参考资料

@NKNaN
Copy link
Contributor

NKNaN commented Feb 28, 2024

【报名】:29

@GreatV
Copy link
Contributor

GreatV commented Mar 4, 2024

【报名】:3

@GreatV
Copy link
Contributor

GreatV commented Mar 5, 2024

【报名】:4

@GreatV
Copy link
Contributor

GreatV commented Mar 7, 2024

【报名】:5

@GreatV
Copy link
Contributor

GreatV commented Mar 11, 2024

【报名】:6

@GreatV
Copy link
Contributor

GreatV commented Mar 15, 2024

【报名】:7

@smallpoxscattered
Copy link
Contributor

【报名】:23

@1want2sleep
Copy link
Contributor

【报名】:25

@smallpoxscattered
Copy link
Contributor

【报名】:24

@smallpoxscattered
Copy link
Contributor

【报名】:22

@wufei2
Copy link
Contributor

wufei2 commented May 2, 2024

【报名】:15-16

@1want2sleep
Copy link
Contributor

【报名】:31

@wufei2
Copy link
Contributor

wufei2 commented May 8, 2024

【报名】:11-12

@smallpoxscattered
Copy link
Contributor

【报名】:21

@wufei2
Copy link
Contributor

wufei2 commented May 11, 2024

【报名】:9

@smallpoxscattered
Copy link
Contributor

【报名】:20

@wufei2
Copy link
Contributor

wufei2 commented May 12, 2024

【报名】:30

@smallpoxscattered
Copy link
Contributor

smallpoxscattered commented May 12, 2024

【报名】:26-27

@wufei2
Copy link
Contributor

wufei2 commented May 15, 2024

【报名】:13

@GoldenStain
Copy link
Contributor

【报名】:1、14

@PaddlePaddle PaddlePaddle deleted a comment from essos-bot Sep 26, 2024
@PaddlePaddle PaddlePaddle deleted a comment from Fripping Sep 26, 2024
@PaddlePaddle PaddlePaddle deleted a comment from Fripping Sep 26, 2024
@PaddlePaddle PaddlePaddle deleted a comment from BHmingyang Sep 26, 2024
@PaddlePaddle PaddlePaddle deleted a comment from Caogration Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HappyOpenSource 快乐开源活动issue与PR
Projects
Status: In Progress
Development

No branches or pull requests

9 participants