forked from d-michail/firecastnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_fcn.py
executable file
·215 lines (187 loc) · 5.91 KB
/
inference_fcn.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import logging
import xarray as xr
import numpy as np
from tqdm import tqdm
import json
import torch
from tqdm import tqdm
import xarray as xr
from tqdm import tqdm
import numpy as np
import json
import argparse
from seasfire.firecastnet_lit import FireCastNetLit
logger = logging.getLogger(__name__)
def main(args):
level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(level=level)
# load mean and std
mean_std_dict_filename = f"cube_mean_std_dict_{args.target_shift}.json"
logger.info("Opening mean-std statistics = {}".format(mean_std_dict_filename))
mean_std_dict = None
with open(mean_std_dict_filename, "r") as f:
mean_std_dict = json.load(f)
input_vars = [
"mslp",
"tp",
"vpd",
"sst",
"t2m_mean",
"ssrd",
"swvl1",
"lst_day",
"ndvi",
"pop_dens",
]
lsm_var = "lsm"
static_vars = [lsm_var]
log_preprocess_input_vars = ["tp", "pop_dens"]
target_var = "gwis_ba"
logger.info("Opening local cube zarr file: {}".format(args.cube_path))
cube = xr.open_zarr(args.cube_path, consolidated=False)
for var_name in log_preprocess_input_vars:
logger.info("Log-transforming input var: {}".format(var_name))
cube[var_name] = xr.DataArray(
np.log(1.0 + cube[var_name].values),
coords=cube[var_name].coords,
dims=cube[var_name].dims,
attrs=cube[var_name].attrs,
)
for static_v in static_vars:
logger.info(
"Expanding time dimension on static variable = {}.".format(static_v)
)
cube[static_v] = cube[static_v].expand_dims(dim={"time": cube.time}, axis=0)
# normalize input variables
for var in input_vars:
var_mean = mean_std_dict[f"{var}_mean"]
var_std = mean_std_dict[f"{var}_std"]
cube[var] = (cube[var] - var_mean) / var_std
# keep only needed vars
ds = cube[input_vars + static_vars]
ds = ds.fillna(-1)
# shift time inputs forward in time
logger.info(f"Shifting inputs by {args.target_shift}.")
for var in input_vars:
if args.target_shift > 0:
ds[var] = ds[var].shift(time=args.target_shift, fill_value=0)
# load model from checkpoint
logger.info(f"Loading model from ckpt = {args.ckpt_path}")
model = FireCastNetLit.load_from_checkpoint(args.ckpt_path)
model.eval()
model.dglTo(model.device)
# prepare predictions for storage
predictions = np.zeros_like(
cube[target_var]
)
logger.info(f"Will create samples for [{args.start_time}, {args.end_time}]")
ds_selected = ds.sel(time=slice(args.start_time, args.end_time))
ds_selected_time_indexes = ds.get_index("time").get_indexer(ds_selected["time"])
for t_index in tqdm(ds_selected_time_indexes, desc="Processing samples"):
if t_index < args.timeseries - 1:
continue
sample = ds.isel(time=slice(t_index - args.timeseries + 1, t_index + 1))
sample_tensor = torch.tensor(sample.to_array().values, dtype=torch.float32).to(
model.device
)
sample_tensor = sample_tensor.unsqueeze(0)
with torch.no_grad():
prediction = model.predict_step(sample_tensor)
prediction = prediction.cpu().numpy()
predictions[t_index] = prediction.squeeze()
all_nan = np.isnan(prediction).all()
if all_nan:
logger.warn("All prediction values are NaN")
da = xr.DataArray(
predictions,
dims=("time", "latitude", "longitude"),
coords={
"time": ds["time"],
"latitude": ds["latitude"],
"longitude": ds["longitude"],
},
)
da = da.where(cube[lsm_var] > 0.1, np.nan)
output_var_name = f"{args.output_var_prefix}_{args.target_shift}"
logger.info(f"Creating new zarr store at {args.output_path}")
ds_output = xr.Dataset({output_var_name: da})
ds_output.to_zarr(args.output_path, mode="w")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Inference FireCastNet")
parser.add_argument(
"--cube-path",
metavar="KEY",
type=str,
action="store",
dest="cube_path",
default="cube.zarr",
help="Cube path",
)
parser.add_argument(
"--ckpt-path",
metavar="KEY",
type=str,
action="store",
dest="ckpt_path",
default="best.ckpt",
help="Checkpoint path",
)
parser.add_argument(
"--target-shift",
metavar="KEY",
type=int,
action="store",
dest="target_shift",
default=1,
help="Target shift",
)
parser.add_argument(
"--timeseries",
metavar="KEY",
type=int,
action="store",
dest="timeseries",
default=24,
help="Timeseries length",
)
parser.add_argument(
"--start-time",
metavar="KEY",
type=str,
action="store",
dest="start_time",
default="2019-01-01",
help="Start time",
)
parser.add_argument(
"--end-time",
metavar="KEY",
type=str,
action="store",
dest="end_time",
default="2020-01-01",
help="End time",
)
parser.add_argument(
"--output-var-prefix",
metavar="KEY",
type=str,
action="store",
dest="output_var_prefix",
default="predictions_cls_ba",
help="Prediction variable prefix",
)
parser.add_argument(
"--output-path",
metavar="KEY",
type=str,
action="store",
dest="output_path",
default="predictions.zarr",
help="Output path",
)
parser.add_argument("--debug", dest="debug", action="store_true")
parser.add_argument("--no-debug", dest="debug", action="store_false")
args = parser.parse_args()
main(args)