|
| 1 | +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Reference: https://github.com/openclimatefix/skillful_nowcasting |
| 17 | +""" |
| 18 | +from os import path as osp |
| 19 | + |
| 20 | +import hydra |
| 21 | +import matplotlib.pyplot as plt |
| 22 | +import numpy as np |
| 23 | +import paddle |
| 24 | +from omegaconf import DictConfig |
| 25 | + |
| 26 | +import ppsci |
| 27 | +from ppsci.utils import logger |
| 28 | + |
| 29 | + |
| 30 | +def visualize( |
| 31 | + cfg: DictConfig, |
| 32 | + x: paddle.Tensor, |
| 33 | + y: paddle.Tensor, |
| 34 | + y_hat: paddle.Tensor, |
| 35 | + batch_idx: int, |
| 36 | +) -> None: |
| 37 | + images = x[0] |
| 38 | + future_images = y[0] |
| 39 | + generated_images = y_hat[0] |
| 40 | + fig, axes = plt.subplots(2, 2) |
| 41 | + for i, ax in enumerate(axes.flat): |
| 42 | + alpha = images[i][0].numpy() |
| 43 | + alpha[alpha < 1] = 0 |
| 44 | + alpha[alpha > 1] = 1 |
| 45 | + ax.imshow(images[i].transpose([1, 2, 0]).numpy(), alpha=alpha, cmap="viridis") |
| 46 | + ax.axis("off") |
| 47 | + plt.subplots_adjust(hspace=0.1, wspace=0.1) |
| 48 | + plt.savefig(osp.join(cfg.output_dir, "Input_Image_Stack_Frame.png")) |
| 49 | + fig, axes = plt.subplots(3, 3) |
| 50 | + for i, ax in enumerate(axes.flat): |
| 51 | + alpha = future_images[i][0].numpy() |
| 52 | + alpha[alpha < 1] = 0 |
| 53 | + alpha[alpha > 1] = 1 |
| 54 | + ax.imshow( |
| 55 | + future_images[i].transpose([1, 2, 0]).numpy(), alpha=alpha, cmap="viridis" |
| 56 | + ) |
| 57 | + plt.subplots_adjust(hspace=0.1, wspace=0.1) |
| 58 | + plt.savefig(osp.join(cfg.output_dir, "Target_Image_Frame.png")) |
| 59 | + fig, axes = plt.subplots(3, 3) |
| 60 | + for i, ax in enumerate(axes.flat): |
| 61 | + alpha = generated_images[i][0].numpy() |
| 62 | + alpha[alpha < 1] = 0 |
| 63 | + alpha[alpha > 1] = 1 |
| 64 | + ax.imshow( |
| 65 | + generated_images[i].transpose([1, 2, 0]).numpy(), |
| 66 | + alpha=alpha, |
| 67 | + cmap="viridis", |
| 68 | + ) |
| 69 | + ax.axis("off") |
| 70 | + plt.subplots_adjust(hspace=0.1, wspace=0.1) |
| 71 | + plt.savefig(osp.join(cfg.output_dir, "Generated_Image_Frame.png")) |
| 72 | + |
| 73 | + |
| 74 | +def train(cfg: DictConfig): |
| 75 | + print("Not supported.") |
| 76 | + |
| 77 | + |
| 78 | +def evaluate(cfg: DictConfig): |
| 79 | + # set model |
| 80 | + model = ppsci.arch.DGMR(**cfg.MODEL) |
| 81 | + # load evaluate data |
| 82 | + dataset = ppsci.data.dataset.DGMRDataset(**cfg.DATASET) |
| 83 | + val_loader = paddle.io.DataLoader(dataset, batch_size=cfg.DATALOADER.batch_size) |
| 84 | + # initialize solver |
| 85 | + solver = ppsci.solver.Solver( |
| 86 | + model, |
| 87 | + pretrained_model_path=cfg.EVAL.pretrained_model_path, |
| 88 | + ) |
| 89 | + solver.model.eval() |
| 90 | + |
| 91 | + # evaluate pretrained model |
| 92 | + d_loss = [] |
| 93 | + g_loss = [] |
| 94 | + grid_loss = [] |
| 95 | + for batch_idx, batch in enumerate(val_loader): |
| 96 | + with paddle.no_grad(): |
| 97 | + out_dict = solver.model.validation_step(batch, batch_idx) |
| 98 | + # visualize |
| 99 | + images, future_images = batch |
| 100 | + images = images.astype(dtype="float32") |
| 101 | + future_images = future_images.astype(dtype="float32") |
| 102 | + generated_images = solver.model.generator(images) |
| 103 | + visualize(cfg, images, future_images, generated_images, batch_idx) |
| 104 | + d_loss.append(out_dict[0]) |
| 105 | + g_loss.append(out_dict[1]) |
| 106 | + grid_loss.append(out_dict[2]) |
| 107 | + logger.message(f"d_loss: {np.array(d_loss).mean()}") |
| 108 | + logger.message(f"g_loss: {np.array(g_loss).mean()}") |
| 109 | + logger.message(f"grid_loss: {np.array(grid_loss).mean()}") |
| 110 | + |
| 111 | + |
| 112 | +@hydra.main(version_base=None, config_path="./conf", config_name="dgmr.yaml") |
| 113 | +def main(cfg: DictConfig): |
| 114 | + if cfg.mode == "train": |
| 115 | + train(cfg) |
| 116 | + elif cfg.mode == "eval": |
| 117 | + evaluate(cfg) |
| 118 | + else: |
| 119 | + raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'") |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + main() |
0 commit comments