-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrain_script.py
More file actions
393 lines (327 loc) · 13.5 KB
/
train_script.py
File metadata and controls
393 lines (327 loc) · 13.5 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""
Train a model
"""
__date__ = "December 2024 - February 2025"
import argparse
import matplotlib
import matplotlib.pyplot as plt
import os
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
matplotlib.use("agg")
from torchmetrics.image import StructuralSimilarityIndexMeasure
from src.config_utils import Config
from src.data import FrameDataset
from src.model import PoseSplatter
from src.unet_3d import init_unet_primary_skip
from src.utils import get_cam_params
os.environ['TORCH_CUDA_ARCH_LIST'] = "8.6" # NOTE: move this!
LOSS_NAMES = ("iou", "ssim", "img")
LOSS_COLORS = ["goldenrod", "deepskyblue", "lightcoral", "darkorchid", "mediumseagreen"]
def get_iou_loss(predicted_mask, target_mask, eps=1e-6):
if predicted_mask.shape != target_mask.shape:
raise ValueError("Predicted and target masks must have the same shape.")
intersection = (predicted_mask * target_mask).sum(dim=(-2, -1))
union = (predicted_mask + target_mask - predicted_mask * target_mask).sum(dim=(-2, -1))
iou = (intersection + eps) / (union + eps)
return 1 - iou.mean()
def calculate_validation_loss(model, valid_loader, device, ssim_lambda, img_lambda, max_n_batches=None):
model.eval()
total_loss = 0.0
total_samples = 0
with torch.no_grad():
for batch_num, (mask, img, p_3d, angle, view_idx) in enumerate(valid_loader):
assert mask.shape[0] == 1, "batch size must be 1"
view_idx = int(view_idx[0])
rgb, alpha = model(
mask.to(device),
img.to(device),
p_3d.to(device),
float(angle[0]),
view_num=view_idx,
)
rgb = torch.permute(rgb[0], (2, 0, 1)) # [3, H, W]
alpha = alpha[0, ..., 0] # [H, W]
# Compute loss.
img_idx = model.observed_views.index(view_idx)
target_mask = mask[0,img_idx].to(device) # [H, W]
target_img = img[0,img_idx].to(device) # [3, H, W]
iou_loss = get_iou_loss(alpha, target_mask)
ssim_loss = ssim_lambda * (1.0 - ssim(target_img[None], rgb[None]))
img_loss = img_lambda * torch.abs(target_img - rgb).sum() / target_mask.sum()
total_loss += iou_loss.item() + ssim_loss.item() + img_loss.item()
batch_size = mask.shape[0]
total_samples += batch_size
if batch_num + 1 == max_n_batches:
break
model.train()
return total_loss / total_samples
def train_one_epoch(
model,
optimizer,
loader,
device,
ssim_lambda,
img_lambda,
pbar,
last_epoch_loss,
max_n_batches=None,
):
"""
Train the model for one epoch.
Args:
model (torch.nn.Module): The model to train.
optimizer (torch.optim.Optimizer): The optimizer to use.
loader (torch.utils.data.DataLoader): DataLoader for the training set.
device (str): Device to use for computation ('cuda' or 'cpu').
loss_names (tuple): Tuple of loss names for logging.
ssim_lambda (float): Weight for the SSIM loss.
img_lambda (float): Weight for the image loss.
Returns:
list: List of average losses for the epoch corresponding to loss_names.
"""
model.train() # Set the model to training mode
epoch_loss = [0.0 for _ in LOSS_NAMES]
total_samples = 0
for batch_num, (mask, img, p_3d, angle, view_idx) in enumerate(loader):
assert mask.shape[0] == 1, "batch size must be 1"
view_idx = int(view_idx[0])
# Zero gradients.
optimizer.zero_grad()
# Forward pass
rgb, alpha = model(
mask.to(device),
img.to(device),
p_3d.to(device),
float(angle[0]),
view_num=view_idx,
) # [1, 3, H, W], [1, H, W, 1]
rgb = torch.permute(rgb[0], (2, 0, 1)) # [3, H, W]
alpha = alpha[0, ..., 0] # [H, W]
# Compute loss.
img_idx = model.observed_views.index(view_idx)
target_mask = mask[0,img_idx].to(device) # [H, W]
target_img = img[0,img_idx].to(device) # [3, H, W]
iou_loss = get_iou_loss(alpha, target_mask)
ssim_loss = ssim_lambda * (1.0 - ssim(target_img[None], rgb[None]))
img_loss = img_lambda * torch.abs(target_img - rgb).sum() / target_mask.sum()
# Backward pass
total_loss = iou_loss + img_loss + ssim_loss
total_loss.backward()
optimizer.step()
# Accumulate losses.
batch_size = len(rgb)
epoch_loss[0] += iou_loss.item() * batch_size
epoch_loss[1] += ssim_loss.item() * batch_size
epoch_loss[2] += img_loss.item() * batch_size
total_samples += batch_size
loss = sum(epoch_loss) / total_samples
pbar.set_description(f"epoch loss: {last_epoch_loss:.5f} b {batch_num:04d}: {loss:.5f}")
if batch_num + 1 == max_n_batches:
break
# Average losses over the entire dataset
avg_losses = [loss / total_samples for loss in epoch_loss]
return avg_losses
@torch.no_grad()
def plot_predictions(model, loader, device, save_path="temp.pdf", num_examples=5):
"""
Plot model predictions alongside ground truth images and save the plot to a file.
Args:
model (torch.nn.Module): The model to use for generating predictions.
loader (torch.utils.data.DataLoader): DataLoader for the dataset.
device (str): Device to use for computation ('cuda' or 'cpu').
save_path (str): Path to save the output plot. Default is "temp.pdf".
num_examples (int): Number of examples to plot. Default is 5.
"""
model.eval() # Set the model to evaluation mode
_, axarr = plt.subplots(ncols=2, nrows=num_examples, figsize=(4, 2 * num_examples))
j = 0
for mask, img, p_3d, angle, view_idx in loader:
assert mask.shape[0] == 1, "batch size must be 1"
view_idx = int(view_idx[0])
img_idx = model.observed_views.index(view_idx)
rgb, _ = model(
mask.to(device),
img.to(device),
p_3d.to(device),
float(angle[0]),
view_num=view_idx,
)
rgb = torch.permute(rgb, (0, 3, 1, 2)) # [B, C, H, W]
axarr[j, 0].imshow(torch.permute(img[0,img_idx], (1, 2, 0)).cpu().numpy())
axarr[j, 0].axis("off")
axarr[j, 1].imshow(torch.permute(rgb[0], (1, 2, 0)).detach().cpu().numpy())
axarr[j, 1].axis("off")
j += 1
if j >= num_examples:
break
axarr[0, 0].set_title("Ground Truth")
axarr[0, 1].set_title("Prediction")
plt.tight_layout()
plt.savefig(save_path)
plt.close("all")
def plot_losses(losses, validation_losses=None, valid_every=None, save_path="loss.pdf"):
"""
Plot training and validation losses over epochs and save the plot to a file.
Args:
losses (list of lists): Training losses for each epoch. Each sublist contains losses for different metrics.
validation_losses (list of tuples): Validation losses with epochs as (epoch, loss). Default is None.
loss_names (tuple): Names of the loss components for plotting. Default is ("iou", "ssim", "img").
save_path (str): Path to save the output plot. Default is "loss.pdf".
"""
num_epochs = len(losses)
# Plot training losses
epochs = range(1, num_epochs + 1)
for i, loss_name in enumerate(LOSS_NAMES):
plt.semilogy(epochs, [loss[i] for loss in losses], c=LOSS_COLORS[i], label=loss_name)
plt.semilogy(epochs, [sum(loss) for loss in losses], c=LOSS_COLORS[-2], label="all")
# Plot validation losses as scatter points
if validation_losses and valid_every:
val_epochs = range(valid_every, num_epochs + 1, valid_every)
plt.plot(val_epochs, validation_losses, marker='o', color=LOSS_COLORS[-1], label="val")
ax = plt.gca()
ax.minorticks_on()
ax.grid(which='both')
plt.legend(loc="best")
plt.ylabel("Loss")
plt.xlabel("Epoch")
plt.title("Training and Validation Losses")
plt.tight_layout()
plt.savefig(save_path)
plt.close("all")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Train script for the model")
parser.add_argument("config", type=str, help="Path to the config JSON file")
parser.add_argument("--load", action="store_true", help="Flag to load a pre-trained model")
parser.add_argument("--ablation", action="store_true", help="Flag to train the ablation model")
parser.add_argument("--epochs", type=int, default=50, help="Epochs (int, default: 50)")
parser.add_argument("--max_batches", type=int, default=None, help="Max # batches (None or int, default: None")
args = parser.parse_args()
config = Config(args.config)
load = args.load # This will be True if --load is provided, False otherwise
n_epochs = args.epochs
print(f"Config file: {args.config}")
print(f"Load flag: {args.load}")
print(f"Ablation flag: {args.ablation}")
print(f"Epochs: {n_epochs}")
print(f"Max Batches: {args.max_batches}")
intrinsic, extrinsic, Ps = get_cam_params(
config.camera_fn,
ds=config.image_downsample,
up_fn=config.vertical_lines_fn,
auto_orient=True,
load_up_direction=not config.adaptive_camera,
)
C = len(Ps)
device = "cuda"
ssim = StructuralSimilarityIndexMeasure(data_range=1.0).to(device)
img_fn = os.path.join(config.image_directory, "images.h5")
volume_fn = os.path.join(config.volume_directory, "volumes.h5")
dset_args = (img_fn, volume_fn, config.center_rotation_fn, C, config.holdout_views)
num_workers = len(os.sched_getaffinity(0)) # available CPUs
print("num workers:", num_workers)
loader_kwargs = dict(batch_size=1, shuffle=True, num_workers=num_workers, prefetch_factor=2)
dset = FrameDataset(*dset_args, split="train")
loader = DataLoader(dset, **loader_kwargs)
valid_dset = FrameDataset(*dset_args, split="valid")
valid_loader = DataLoader(valid_dset, **loader_kwargs)
w = config.image_width // config.image_downsample
h = config.image_height // config.image_downsample
model = PoseSplatter(
intrinsics=intrinsic,
extrinsics=extrinsic,
W=w,
H=h,
ell=config.ell,
grid_size=config.grid_size,
volume_idx=config.volume_idx,
ablation=args.ablation,
volume_fill_color=config.volume_fill_color,
holdout_views=config.holdout_views,
adaptive_camera=config.adaptive_camera,
)
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=config.lr)
if load:
if args.ablation:
d = torch.load(config.model_fn[:-3] + "_ablation.pt")
else:
d = torch.load(config.model_fn)
epoch = d["epoch"]
losses = d["loss"]
validation_losses = d["validation_losses"]
model.load_state_dict(d["model_state_dict"])
optimizer.load_state_dict(d["optimizer_state_dict"])
print(f"Loaded checkpoint from epoch {epoch}.")
else:
if not args.ablation:
for unet in model.unets:
init_unet_primary_skip(unet)
init_unet_primary_skip(model.final_unet)
epoch = 0
losses = []
validation_losses = []
pbar = tqdm(range(n_epochs))
last_epoch_loss = 0.0
for _ in pbar:
epoch += 1
avg_losses = train_one_epoch(
model=model,
optimizer=optimizer,
loader=loader,
device=device,
ssim_lambda=config.ssim_lambda,
img_lambda=config.img_lambda,
pbar=pbar,
last_epoch_loss=last_epoch_loss,
max_n_batches=args.max_batches,
)
losses.append(avg_losses)
last_epoch_loss = sum(avg_losses)
if epoch % config.valid_every == 0:
validation_loss = calculate_validation_loss(
model,
valid_loader,
device,
ssim_lambda=config.ssim_lambda,
img_lambda=config.img_lambda,
max_n_batches=args.max_batches,
)
validation_losses.append(validation_loss)
if epoch % config.plot_every == 0:
if model.ablation:
prediction_fn = "reconstruction_ablation.pdf"
loss_fn = "loss_ablation.pdf"
else:
prediction_fn = "reconstruction.pdf"
loss_fn = "loss.pdf"
plot_predictions(
model,
loader,
device,
save_path=os.path.join(config.project_directory, prediction_fn),
num_examples=5,
)
plot_losses(
losses,
validation_losses=validation_losses,
valid_every=config.valid_every,
save_path=os.path.join(config.project_directory, loss_fn),
)
if epoch % config.save_every == 0:
if model.ablation:
checkpoint_fn = config.model_fn[:-3] + "_ablation.pt"
else:
checkpoint_fn = config.model_fn
torch.save(
{
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': losses,
'validation_losses': validation_losses,
'loss_names': LOSS_NAMES,
},
checkpoint_fn,
)