-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
220 lines (191 loc) · 6.63 KB
/
run.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
216
217
218
219
220
import argparse
import pathlib
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import tqdm
from cloudcasting.constants import (
DATA_INTERVAL_SPACING_MINUTES,
IMAGE_SIZE_TUPLE,
NUM_CHANNELS,
)
from cloudcasting.dataset import SatelliteDataset, ValidationSatelliteDataset
from cloudcasting.utils import numpy_validation_collate_fn
from matplotlib import pyplot as plt
from torch.utils.data import DataLoader
import ocf_tsimagemixer.imagemixer
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
def train(
batch_size: int,
device: str,
forecast_steps: int,
history_steps: int,
num_epochs: int,
output_directory: pathlib.Path,
training_data_path: str,
num_workers: int = 0,
) -> None:
# Batch size must be greater than 1
if batch_size < 2:
print("Batch size must be 2 or greater")
return
# Load the training dataset
dataset = SatelliteDataset(
zarr_path=training_data_path,
start_time="2022-01-31",
end_time=None,
history_mins=(history_steps - 1) * DATA_INTERVAL_SPACING_MINUTES,
forecast_mins=forecast_steps * DATA_INTERVAL_SPACING_MINUTES,
sample_freq_mins=DATA_INTERVAL_SPACING_MINUTES,
nan_to_num=True,
)
# Construct a DataLoader
gen = torch.Generator()
gen.manual_seed(0)
train_dataloader = DataLoader(
dataset=dataset,
batch_size=batch_size,
num_workers=num_workers,
worker_init_fn=seed_worker,
generator=gen,
)
# Create the model
model = ocf_tsimagemixer.ImageMixer(
batch_size,
history_steps,
forecast_steps,
NUM_CHANNELS,
IMAGE_SIZE_TUPLE[0],
IMAGE_SIZE_TUPLE[1],
)
model = model.to(device)
# Loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001)
# Training loop
best_loss = 999
best_model = None
save_model = False
for epoch in range(num_epochs):
# Set model to training mode
model.train()
for X, y in tqdm.tqdm(train_dataloader):
# All batches must be the same size
if X.shape[0] != batch_size:
print(f"Skipping batch with size {X.shape[0]}")
continue
# Zero the parameter gradients
optimizer.zero_grad()
# Forward pass
outputs = model(X.to(device))
# Calculate the loss
loss = criterion(outputs, y.to(device))
# Backward pass and optimize
loss.backward()
optimizer.step()
if loss.item() < best_loss:
best_loss = loss.item()
best_model = model
save_model = True
print(
f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}, Best loss {best_loss:.4f}"
)
if save_model:
torch.save(
best_model.state_dict(),
output_directory / f"best-model-epoch-{epoch}-loss-{best_loss:.3g}.state-dict.pt",
)
save_model = False
def validation_plot(y: np.array, y_hat: np.array, output_path: pathlib.Path) -> None:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(y[0][0][0], cmap="gray")
ax2.imshow(y_hat[0][0][0], cmap="gray")
fig.savefig(output_path)
def validate(
batch_size: int,
device: str,
forecast_steps: int,
history_steps: int,
output_directory: pathlib.Path,
state_dict_path: str,
validation_data_path: str,
num_workers: int = 0,
):
# Create the model
model = ocf_tsimagemixer.ImageMixer(
batch_size,
history_steps,
forecast_steps,
NUM_CHANNELS,
IMAGE_SIZE_TUPLE[0],
IMAGE_SIZE_TUPLE[1],
)
model.load_state_dict(torch.load(state_dict_path, map_location=device, weights_only=True))
model = model.to(device)
model.eval()
# Set up the validation dataset
valid_dataset = ValidationSatelliteDataset(
zarr_path=validation_data_path,
history_mins=(model.history_steps - 1) * DATA_INTERVAL_SPACING_MINUTES,
forecast_mins=model.forecast_steps * DATA_INTERVAL_SPACING_MINUTES,
sample_freq_mins=DATA_INTERVAL_SPACING_MINUTES,
nan_to_num=True,
)
valid_dataloader = DataLoader(
dataset=valid_dataset,
batch_size=batch_size,
num_workers=num_workers,
shuffle=False,
collate_fn=numpy_validation_collate_fn,
drop_last=False,
)
for idx, (X, y) in enumerate(tqdm.tqdm(valid_dataloader)):
if idx % 100 == 0:
y_hat = model(torch.from_numpy(X).to(device))
validation_plot(y, y_hat.detach().cpu(), output_directory / f"cloud-{idx}.png")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
cmd_group = parser.add_mutually_exclusive_group(required=True)
cmd_group.add_argument("--train", action="store_true", help="Run training")
cmd_group.add_argument("--validate", action="store_true", help="Run validation")
parser.add_argument("--batch-size", type=int, help="Batch size", default=2)
parser.add_argument("--data-path", type=str, help="Path to the input data")
parser.add_argument("--num-history-steps", type=int, help="History steps", default=24)
parser.add_argument("--num-epochs", type=int, help="Number of epochs", default=10)
parser.add_argument("--model-state-dict", type=str, help="Path to model state dict")
parser.add_argument("--output-directory", type=str, help="Path to save outputs to")
args = parser.parse_args()
# Get the appropriate PyTorch device
device = (
"mps"
if torch.backends.mps.is_available()
else "cuda" if torch.cuda.is_available() else "cpu"
)
# Ensure output directory exists
output_directory=pathlib.Path(args.output_directory)
output_directory.mkdir(parents=True, exist_ok=True)
if args.train:
train(
batch_size=args.batch_size,
device=device,
forecast_steps=1,
history_steps=args.num_history_steps,
num_epochs=args.num_epochs,
output_directory=output_directory,
training_data_path=args.data_path,
)
if args.validate:
validate(
batch_size=args.batch_size,
device=device,
forecast_steps=1,
history_steps=args.num_history_steps,
output_directory=output_directory,
state_dict_path=args.model_state_dict,
validation_data_path=args.data_path,
)