Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/zh/examples/laplace2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br>MSE.u(MSE_Metric): 0.00002 |
Expand Down
18 changes: 18 additions & 0 deletions examples/laplace/conf/laplace2d.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
59 changes: 58 additions & 1 deletion examples/laplace/laplace2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down