forked from NVIDIA/modulus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
103 lines (88 loc) · 3.31 KB
/
validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# SPDX-FileCopyrightText: Copyright (c) 2023 - 2024 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import matplotlib.pyplot as plt
from torch import FloatTensor
from modulus.launch.logging import LaunchLogger
class GridValidator:
"""Grid Validator
The validator compares model output and target, inverts normalisation and plots a sample
Parameters
----------
loss_fun : MSELoss
loss function for assessing validation error
norm : Dict, optional
mean and standard deviation for each channel to normalise input and target
font_size : float, optional
font size used in figures
"""
def __init__(
self,
loss_fun,
norm: dict = {"permeability": (0.0, 1.0), "darcy": (0.0, 1.0)},
font_size: float = 28.0,
):
self.norm = norm
self.criterion = loss_fun
self.font_size = font_size
self.headers = ("invar", "truth", "prediction", "relative error")
def compare(
self,
invar: FloatTensor,
target: FloatTensor,
prediction: FloatTensor,
step: int,
logger: LaunchLogger,
) -> float:
"""compares model output, target and plots everything
Parameters
----------
invar : FloatTensor
input to model
target : FloatTensor
ground truth
prediction : FloatTensor
model output
step : int
iteration counter
logger : LaunchLogger
logger to which figure is passed
Returns
-------
float
validation error
"""
loss = self.criterion(prediction, target)
norm = self.norm
# pick first sample from batch
invar = invar * norm["permeability"][1] + norm["permeability"][0]
target = target * norm["darcy"][1] + norm["darcy"][0]
prediction = prediction * norm["darcy"][1] + norm["darcy"][0]
invar = invar.cpu().numpy()[0, -1, :, :]
target = target.cpu().numpy()[0, 0, :, :]
prediction = prediction.detach().cpu().numpy()[0, 0, :, :]
plt.close("all")
plt.rcParams.update({"font.size": self.font_size})
fig, ax = plt.subplots(1, 4, figsize=(15 * 4, 15), sharey=True)
im = []
im.append(ax[0].imshow(invar))
im.append(ax[1].imshow(target))
im.append(ax[2].imshow(prediction))
im.append(ax[3].imshow((prediction - target) / norm["darcy"][1]))
for ii in range(len(im)):
fig.colorbar(im[ii], ax=ax[ii], location="bottom", fraction=0.046, pad=0.04)
ax[ii].set_title(self.headers[ii])
logger.log_figure(figure=fig, artifact_file=f"validation_step_{step:03d}.png")
return loss