Skip to content

Commit

Permalink
Merge pull request #3 from DUCH714/hackthon5th63_fix
Browse files Browse the repository at this point in the history
Hackthon5th63 fix
  • Loading branch information
DUCH714 committed Jan 17, 2024
2 parents 0739d7f + eb05d5f commit 2e4e9aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/zh/examples/phycrnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
```
| 预训练模型 | 指标 |
|:--| :--|
| [tempogan_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/phycrnet/phycrnet_burgers.pdparams) | a-RMSE: 3.20e-3 |
| [phycrnet_burgers_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/phycrnet/phycrnet_burgers.pdparams) | a-RMSE: 3.20e-3 |

## 1. 背景简介

Expand Down Expand Up @@ -145,9 +145,9 @@ examples/phycrnet/main.py:47:51
--8<--
```
而在评估过程中,我们使用`function.tranform_output_val`来进行评估,并生成累计均方根误差。
``` py linenums="134"
``` py linenums="142"
--8<--
examples/phycrnet/main.py:134:134
examples/phycrnet/main.py:142:142
--8<--
```
完成上述设置之后,只需要将上述实例化的对象按顺序传递给 `ppsci.solver.Solver`
Expand Down
1 change: 1 addition & 0 deletions examples/phycrnet/conf/burgers_equations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mode: train # running mode: train/eval
seed: 66
output_dir: ${hydra:run.dir}
DATA_PATH: ./data/burgers_1501x2x128x128.mat
case_name: 2D Burgers' equation

# set working condition
TIME_STEPS: 1001
Expand Down
35 changes: 22 additions & 13 deletions examples/phycrnet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,38 +336,36 @@ def get(self, epochs=1):
return input_dict_train, label_dict_train, input_dict_val, label_dict_val


def output_graph(model, input_dataset, fig_save_path):
def output_graph(model, input_dataset, fig_save_path, case_name):
output_dataset = model(input_dataset)
output = output_dataset["outputs"]
input = input_dataset["input"][0]

output = paddle.concat(tuple(output), axis=0)
output = paddle.concat((input.cuda(), output), axis=0)

# Padding x and y axis due to periodic boundary condition
output = paddle.concat((output[:, :, :, -1:], output, output[:, :, :, 0:2]), axis=3)
output = paddle.concat((output[:, :, -1:, :], output, output[:, :, 0:2, :]), axis=2)

truth = uv[0:2001, :, :, :]

truth = np.concatenate((truth[:, :, :, -1:], truth, truth[:, :, :, 0:2]), axis=3)
truth = np.concatenate((truth[:, :, -1:, :], truth, truth[:, :, 0:2, :]), axis=2)

# post-process
ten_true = []
ten_pred = []

for i in range(0, 100):
u_star, u_pred, v_star, v_pred = post_process(output, truth, num=20 * i)

ten_true.append([u_star, v_star])
ten_pred.append([u_pred, v_pred])

ten_true = paddle.stack(ten_true)
ten_pred = paddle.stack(ten_pred)
ten_true = np.stack(ten_true)
ten_pred = np.stack(ten_pred)

# compute the error
# a-RMSE
error = (
paddle.sum((ten_pred - ten_true) ** 2, axis=(1, 2, 3))
np.sum((ten_pred - ten_true) ** 2, axis=(1, 2, 3))
/ ten_true.shape[2]
/ ten_true.shape[3]
)
Expand All @@ -378,16 +376,26 @@ def output_graph(model, input_dataset, fig_save_path):
M = M.T / np.arange(N)
M[:, 0] = 0
M[0, :] = 0

M = paddle.to_tensor(M)
aRMSE = paddle.sqrt(M.T @ error)
t = np.linspace(0, 4, N)
plt.plot(t, aRMSE)
plt.plot(t, aRMSE, color="r")
plt.yscale("log")
plt.ylim((0, 1e-2))
plt.xlabel("t")
plt.ylabel("a-RMSE")
plt.ylim((1e-4, 10))
plt.xlim((0, 4))
plt.savefig(fig_save_path + "error.jpg")
plt.legend(
[
"PhyCRNet",
],
loc="upper left",
)
plt.title(case_name)
plt.savefig(fig_save_path + "/error.jpg")

fig, ax = plt.subplots(3, 4, figsize=(18, 12))
_, ax = plt.subplots(3, 4, figsize=(18, 12))
ax[0, 0].contourf(ten_true[25, 0])
ax[0, 0].set_title("t=1")
ax[0, 0].set_ylabel("truth")
Expand All @@ -407,5 +415,6 @@ def output_graph(model, input_dataset, fig_save_path):
ax[0, 3].set_title("t=4")
ax[1, 3].contourf(ten_pred[99, 0])
ax[2, 3].contourf(ten_true[99, 0] - ten_pred[99, 0])
plt.savefig(fig_save_path + "Burgers.jpg")
plt.title(case_name)
plt.savefig(fig_save_path + "/Burgers.jpg")
plt.close()
2 changes: 1 addition & 1 deletion examples/phycrnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def _transform_out(_in, _out):
).get()
ppsci.utils.load_pretrain(model, cfg.EVAL.pretrained_model_path)
model.register_output_transform(None)
functions.output_graph(model, input_dict_val, cfg.output_dir)
functions.output_graph(model, input_dict_val, cfg.output_dir, cfg.case_name)


@hydra.main(
Expand Down

0 comments on commit 2e4e9aa

Please sign in to comment.