-
Notifications
You must be signed in to change notification settings - Fork 184
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
【SCU】【PPSCI Export&Infer No.28】phylstm #1031
Open
wwwuyan
wants to merge
1
commit into
PaddlePaddle:develop
Choose a base branch
from
wwwuyan:phylstm
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -308,14 +308,144 @@ def evaluate(cfg: DictConfig): | |||||
solver.eval() | ||||||
|
||||||
|
||||||
def export(cfg: DictConfig): | ||||||
mat = scipy.io.loadmat(cfg.DATA_FILE_PATH) | ||||||
u_data = mat["target_X_tf"] | ||||||
u_data = u_data.reshape([u_data.shape[0], u_data.shape[1], 1]) | ||||||
u_all = u_data | ||||||
eta_star = u_all[0:10] | ||||||
eta = eta_star | ||||||
# set model | ||||||
model = ppsci.arch.DeepPhyLSTM( | ||||||
cfg.MODEL.input_size, | ||||||
eta.shape[2], | ||||||
cfg.MODEL.hidden_size, | ||||||
cfg.MODEL.model_type, | ||||||
) | ||||||
# initialize solver | ||||||
solver = ppsci.solver.Solver( | ||||||
model, | ||||||
pretrained_model_path=cfg.INFER.pretrained_model_path, | ||||||
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.
Suggested change
|
||||||
) | ||||||
# export model | ||||||
from paddle.static import InputSpec | ||||||
|
||||||
input_spec = [ | ||||||
{key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys}, | ||||||
] | ||||||
Comment on lines
+333
to
+335
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. 这里的形状可以运行evaluate时,在DeepPhyLSTM.forward函数里打印输入的shape得到 |
||||||
solver.export(input_spec, cfg.INFER.export_path) | ||||||
|
||||||
|
||||||
def inference(cfg: DictConfig): | ||||||
from deploy.python_infer import pinn_predictor | ||||||
|
||||||
predictor = pinn_predictor.PINNPredictor(cfg) | ||||||
|
||||||
mat = scipy.io.loadmat(cfg.DATA_FILE_PATH) | ||||||
ag_data = mat["input_tf"] # ag, ad, av | ||||||
u_data = mat["target_X_tf"] | ||||||
ut_data = mat["target_Xd_tf"] | ||||||
utt_data = mat["target_Xdd_tf"] | ||||||
ag_data = ag_data.reshape([ag_data.shape[0], ag_data.shape[1], 1]) | ||||||
u_data = u_data.reshape([u_data.shape[0], u_data.shape[1], 1]) | ||||||
ut_data = ut_data.reshape([ut_data.shape[0], ut_data.shape[1], 1]) | ||||||
utt_data = utt_data.reshape([utt_data.shape[0], utt_data.shape[1], 1]) | ||||||
|
||||||
t = mat["time"] | ||||||
dt = t[0, 1] - t[0, 0] | ||||||
|
||||||
ag_all = ag_data | ||||||
u_all = u_data | ||||||
u_t_all = ut_data | ||||||
u_tt_all = utt_data | ||||||
|
||||||
# finite difference | ||||||
N = u_data.shape[1] | ||||||
phi1 = np.concatenate( | ||||||
[ | ||||||
np.array([-3 / 2, 2, -1 / 2]), | ||||||
np.zeros([N - 3]), | ||||||
] | ||||||
) | ||||||
temp1 = np.concatenate([-1 / 2 * np.identity(N - 2), np.zeros([N - 2, 2])], axis=1) | ||||||
temp2 = np.concatenate([np.zeros([N - 2, 2]), 1 / 2 * np.identity(N - 2)], axis=1) | ||||||
phi2 = temp1 + temp2 | ||||||
phi3 = np.concatenate( | ||||||
[ | ||||||
np.zeros([N - 3]), | ||||||
np.array([1 / 2, -2, 3 / 2]), | ||||||
] | ||||||
) | ||||||
phi_t0 = ( | ||||||
1 | ||||||
/ dt | ||||||
* np.concatenate( | ||||||
[ | ||||||
np.reshape(phi1, [1, phi1.shape[0]]), | ||||||
phi2, | ||||||
np.reshape(phi3, [1, phi3.shape[0]]), | ||||||
], | ||||||
axis=0, | ||||||
) | ||||||
) | ||||||
phi_t0 = np.reshape(phi_t0, [1, N, N]) | ||||||
|
||||||
ag_star = ag_all[0:10] | ||||||
eta_star = u_all[0:10] | ||||||
eta_t_star = u_t_all[0:10] | ||||||
eta_tt_star = u_tt_all[0:10] | ||||||
ag_c_star = ag_all[0:50] | ||||||
lift_star = -ag_c_star | ||||||
|
||||||
eta = eta_star | ||||||
ag = ag_star | ||||||
lift = lift_star | ||||||
eta_t = eta_t_star | ||||||
eta_tt = eta_tt_star | ||||||
ag_c = ag_c_star | ||||||
g = -eta_tt - ag | ||||||
phi_t = np.repeat(phi_t0, ag_c_star.shape[0], axis=0) | ||||||
|
||||||
input_dict = { | ||||||
"eta": eta, | ||||||
"eta_t": eta_t, | ||||||
"g": g, | ||||||
"ag": ag, | ||||||
"ag_c": ag_c, | ||||||
"lift": lift, | ||||||
"phi_t": phi_t, | ||||||
} | ||||||
|
||||||
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( | ||||||
"./phylstm2_pred.vtu", | ||||||
{**input_dict, **output_dict}, | ||||||
input_dict.keys(), | ||||||
cfg.MODEL.output_keys, | ||||||
) | ||||||
|
||||||
|
||||||
@hydra.main(version_base=None, config_path="./conf", config_name="phylstm2.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__": | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
红框里的代码也需要加上