-
Notifications
You must be signed in to change notification settings - Fork 185
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
Add export & inference for hPINNs #902
Changes from all commits
5692562
c4f6b42
b70f47b
1b09b59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,14 +409,79 @@ def evaluate(cfg: DictConfig): | |
solver.eval() | ||
|
||
|
||
def export(cfg: DictConfig): | ||
# set model | ||
model_re = ppsci.arch.MLP(**cfg.MODEL.re_net) | ||
model_im = ppsci.arch.MLP(**cfg.MODEL.im_net) | ||
model_eps = ppsci.arch.MLP(**cfg.MODEL.eps_net) | ||
|
||
# register transform | ||
model_re.register_input_transform(func_module.transform_in) | ||
model_im.register_input_transform(func_module.transform_in) | ||
model_eps.register_input_transform(func_module.transform_in) | ||
|
||
model_re.register_output_transform(func_module.transform_out_real_part) | ||
model_im.register_output_transform(func_module.transform_out_imaginary_part) | ||
model_eps.register_output_transform(func_module.transform_out_epsilon) | ||
|
||
# wrap to a model_list | ||
model_list = ppsci.arch.ModelList((model_re, model_im, model_eps)) | ||
|
||
# initialize solver | ||
solver = ppsci.solver.Solver( | ||
model_list, | ||
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 ["x", "y"]}, | ||
] | ||
solver.export(input_spec, cfg.INFER.export_path) | ||
|
||
|
||
def inference(cfg: DictConfig): | ||
from deploy.python_infer import pinn_predictor | ||
|
||
predictor = pinn_predictor.PINNPredictor(cfg) | ||
|
||
valid_dict = ppsci.utils.reader.load_mat_file( | ||
cfg.DATASET_PATH_VALID, ("x_val", "y_val", "bound") | ||
) | ||
input_dict = {"x": valid_dict["x_val"], "y": valid_dict["y_val"]} | ||
|
||
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.INFER.output_keys, output_dict.keys()) | ||
} | ||
|
||
ppsci.visualize.save_vtu_from_dict( | ||
"./hpinns_pred.vtu", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改为: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
inference的产物文件就不放到output_dir下面了吧,这个就保持 |
||
{**input_dict, **output_dict}, | ||
input_dict.keys(), | ||
cfg.INFER.output_keys, | ||
) | ||
|
||
|
||
@hydra.main(version_base=None, config_path="./conf", config_name="hpinns.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__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前导出后推理会报错:
这是由于导出时的input_keys和推理时的input_keys不匹配导致的。
这个模型中有input transform,即:("x","y")->transform->("x_cos_1","y_cons_1",...)->forward->("e_re",...),当前export函数中的input_keys是("x_cos_1","y_cons_1",...),而infer中是("x","y")。
因此,需要更改:
input_spec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已修改,感谢纠正🌹