Skip to content

Commit 3c3be84

Browse files
【PPSCI Export&Infer No.24】 biharmonic2d (#858)
* [SCI Export&Infer No.24] biharmonic2d * P[PSCI Export&Infer No.724] biharmonic2d fix
1 parent e1cdba5 commit 3c3be84

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

docs/zh/examples/biharmonic2d.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
python biharmonic2d.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/biharmonic2d/biharmonic2d_pretrained.pdparams
1515
```
1616

17+
=== "模型导出命令"
18+
19+
``` sh
20+
python biharmonic2d.py mode=export
21+
```
22+
23+
=== "模型推理命令"
24+
25+
``` sh
26+
python biharmonic2d.py mode=infer
27+
```
28+
1729
| 预训练模型 | 指标 |
1830
|:--| :--|
1931
| [biharmonic2d_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/biharmonic2d/biharmonic2d_pretrained.pdparams) | l2_error: 0.02774 |

examples/biharmonic2d/biharmonic2d.py

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def plotting(figname, output_dir, data, griddata_points, griddata_xi, boundary):
3939
for i, key in enumerate(data):
4040
plot_data = griddata(
4141
griddata_points,
42-
data[key].numpy().flatten(),
42+
data[key].flatten(),
4343
griddata_xi,
4444
method="cubic",
4545
)
@@ -343,7 +343,94 @@ def compute_outs(w, x, y):
343343
plotting(
344344
"eval_Mx_Mxy_My_Qx_Qy_w",
345345
cfg.output_dir,
346-
outs,
346+
{k: v.numpy() for k, v in outs.items()},
347+
griddata_points,
348+
griddata_xi,
349+
boundary,
350+
)
351+
352+
353+
def export(cfg: DictConfig):
354+
from paddle import nn
355+
from paddle.static import InputSpec
356+
357+
# set models
358+
disp_net = ppsci.arch.MLP(**cfg.MODEL)
359+
360+
# load pretrained model
361+
solver = ppsci.solver.Solver(
362+
model=disp_net, pretrained_model_path=cfg.INFER.pretrained_model_path
363+
)
364+
365+
class Wrapped_Model(nn.Layer):
366+
def __init__(self, model):
367+
super().__init__()
368+
self.model = model
369+
370+
def forward(self, x):
371+
model_out = self.model(x)
372+
outs = self.compute_outs(model_out["u"], x["x"], x["y"])
373+
return outs
374+
375+
def compute_outs(self, w, x, y):
376+
D = cfg.E * (cfg.HEIGHT**3) / (12.0 * (1.0 - cfg.NU**2))
377+
w_x2 = hessian(w, x)
378+
w_y2 = hessian(w, y)
379+
w_x_y = jacobian(jacobian(w, x), y)
380+
M_x = -(w_x2 + cfg.NU * w_y2) * D
381+
M_y = -(cfg.NU * w_x2 + w_y2) * D
382+
M_xy = (1 - cfg.NU) * w_x_y * D
383+
Q_x = -jacobian((w_x2 + w_y2), x) * D
384+
Q_y = -jacobian((w_x2 + w_y2), y) * D
385+
return {"Mx": M_x, "Mxy": M_xy, "My": M_y, "Qx": Q_x, "Qy": Q_y, "w": w}
386+
387+
solver.model = Wrapped_Model(solver.model)
388+
389+
# export models
390+
input_spec = [
391+
{key: InputSpec([None, 1], "float32", name=key) for key in disp_net.input_keys},
392+
]
393+
solver.export(input_spec, cfg.INFER.export_path)
394+
395+
396+
def inference(cfg: DictConfig):
397+
from deploy.python_infer import pinn_predictor
398+
399+
# set model predictor
400+
predictor = pinn_predictor.PINNPredictor(cfg)
401+
402+
# generate samples
403+
num_x = 201
404+
num_y = 301
405+
x_grad, y_grad = np.meshgrid(
406+
np.linspace(
407+
start=0, stop=cfg.LENGTH, num=num_x, endpoint=True, dtype=np.float32
408+
),
409+
np.linspace(
410+
start=0, stop=cfg.WIDTH, num=num_y, endpoint=True, dtype=np.float32
411+
),
412+
)
413+
x_faltten = x_grad.reshape(-1, 1)
414+
y_faltten = y_grad.reshape(-1, 1)
415+
416+
output_dict = predictor.predict(
417+
{"x": x_faltten, "y": y_faltten}, cfg.INFER.batch_size
418+
)
419+
420+
# mapping data to cfg.INFER.output_keys
421+
output_dict = {
422+
store_key: output_dict[infer_key]
423+
for store_key, infer_key in zip(cfg.INFER.output_keys, output_dict.keys())
424+
}
425+
426+
# plotting
427+
griddata_points = np.concatenate([x_faltten, y_faltten], axis=-1)
428+
griddata_xi = (x_grad, y_grad)
429+
boundary = [0, cfg.LENGTH, 0, cfg.WIDTH]
430+
plotting(
431+
"eval_Mx_Mxy_My_Qx_Qy_w",
432+
cfg.output_dir,
433+
output_dict,
347434
griddata_points,
348435
griddata_xi,
349436
boundary,
@@ -356,8 +443,14 @@ def main(cfg: DictConfig):
356443
train(cfg)
357444
elif cfg.mode == "eval":
358445
evaluate(cfg)
446+
elif cfg.mode == "export":
447+
export(cfg)
448+
elif cfg.mode == "infer":
449+
inference(cfg)
359450
else:
360-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
451+
raise ValueError(
452+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
453+
)
361454

362455

363456
if __name__ == "__main__":

examples/biharmonic2d/conf/biharmonic2d.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ hydra:
1111
- TRAIN.checkpoint_path
1212
- TRAIN.pretrained_model_path
1313
- EVAL.pretrained_model_path
14+
- INFER.pretrained_model_path
15+
- INFER.export_path
1416
- mode
1517
- output_dir
1618
- log_freq
@@ -72,3 +74,21 @@ EVAL:
7274
eval_with_no_grad: true
7375
batch_size:
7476
sup_validator: 128
77+
78+
INFER:
79+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/biharmonic2d/biharmonic2d_pretrained.pdparams
80+
export_path: ./inference/biharmonic2d
81+
pdmodel_path: ${INFER.export_path}.pdmodel
82+
pdpiparams_path: ${INFER.export_path}.pdiparams
83+
output_keys: ["Mx", "Mxy", "My", "Qx", "Qy", "w"]
84+
device: gpu
85+
engine: native
86+
precision: fp32
87+
onnx_path: ${INFER.export_path}.onnx
88+
ir_optim: true
89+
min_subgraph_size: 10
90+
gpu_mem: 4000
91+
gpu_id: 0
92+
max_batch_size: 128
93+
num_cpu_threads: 4
94+
batch_size: 128

0 commit comments

Comments
 (0)