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

【PPSCI Export&Infer No. 29】 add export and inference #793

Merged
merged 2 commits into from
Mar 4, 2024

Conversation

NKNaN
Copy link
Contributor

@NKNaN NKNaN commented Mar 2, 2024

PR types

Others

PR changes

Others

Describe

#788

Copy link

paddle-bot bot commented Mar 2, 2024

Thanks for your contribution!

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢大佬提交代码。@lijialin03 周一辛苦看一下几个模型的推理结果是否符合预期

=== "模型导出命令"

``` sh
python topopt.py mode=export INFER.pretrained_model_name=Uniform
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否可以以注释的形式补充另外几个Poisson系列的模型呢?模型推理命令同

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@@ -14,8 +14,13 @@ hydra:
- EVAL.pretrained_model_path_dict
- EVAL.batch_size
- EVAL.num_val_step
- EXPORT.pretrained_model_name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPORT->INFER

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

INFER:
pretrained_model_name: null # a string, indicating which model you want to export. Support [Uniform, Poisson5, Poisson10, Poisson30].
pretrained_model_path_dict: {'Uniform': 'https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/uniform_pretrained.pdparams', 'Poisson5': 'https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson5_pretrained.pdparams', 'Poisson10': 'https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson10_pretrained.pdparams', 'Poisson30': 'https://paddle-org.bj.bcebos.com/paddlescience/models/topopt/poisson30_pretrained.pdparams'}
export_path: ./inference/topopt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果在pretrained_model_path_dict里的模型是互相独立的,可以把export_path改为./inference/topopt_${INFER.pretrained_model_name}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

sampler_num: 8 # a integer number, indicating the sampling rate of the sampling method, supported when `sampler_key` is Fixed or Poisson.
img_num: 4
res_img_figsize: null
save_res_path: ./inference/predicted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处的save_res_path也可以改为./inference/predicted_${INFER.pretrained_model_name}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

model,
eval_with_no_grad=True,
pretrained_model_path=cfg.INFER.pretrained_model_path_dict[
cfg.EXPORT.pretrained_model_name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPORT->INFER

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

solver.export(input_spec, cfg.INFER.export_path)


# model inference
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这行注释可以删除

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除

Comment on lines 389 to 391
def save_topopt_img(
input_dict, output_dict, ground_truth, res_path, figsize=None, npy=False
):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. save_topopt_img函数可以加一下typehint
  2. 为了更容易理解和更符合语义,建议这两个参数名改一下:npy->save_npyres_path->save_dir

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Comment on lines 408 to 413
print(output[i])
plt.title("Prediction")
plt.subplot(1, 4, 4)
plt.axis("off")
plt.imshow(np.round(ground_truth[i][0]), cmap="gray")
print(ground_truth[i])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两处print可以删除,否则会打印大量矩阵元素,如果是为了友好提示可以在for循环的最后一行使用logger.message信息打印保存成功的提示

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

忘记删掉了(✿◡‿◡),已修改

Comment on lines 416 to 419
plt.savefig(osp.join(res_path, "Prediction_" + str(i) + ".png"))
plt.close()
if npy:
with open(osp(res_path, "Prediction_" + str(i) + ".npy"), "wb") as f:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两处字符串拼接改为f-string:"Prediction_" + str(i) + ".png"改为f"Prediction_{i}.png"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

):

input = input_dict["input"]
output = output_dict["output"]
Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate Mar 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

保存前先创建文件夹,否则会报错

Suggested change
output = output_dict["output"]
output = output_dict["output"]
import os
os.makedirs(res_path, exist_ok=True)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

Comment on lines 20 to 29
import matplotlib.pyplot as plt
import numpy as np
import paddle
from omegaconf import DictConfig
from paddle import nn
from paddle.static import InputSpec
from topoptmodel import TopOptNN

import ppsci
from deploy.python_infer import pinn_predictor
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这几行import建议放在对应函数内部,否则会影响整体topopt的代码块行数定位

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@lijialin03
Copy link
Contributor

感谢大佬提交代码。@lijialin03 周一辛苦看一下几个模型的推理结果是否符合预期

已验证推理结果符合预期

Copy link
Collaborator

@HydrogenSulfate HydrogenSulfate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HydrogenSulfate HydrogenSulfate merged commit f0aacff into PaddlePaddle:develop Mar 4, 2024
3 of 4 checks passed
huohuohuohuohuo123 pushed a commit to huohuohuohuohuo123/PaddleScience that referenced this pull request Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor HappyOpenSource 快乐开源活动issue与PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants