-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpretrain_surface_emnn.py
More file actions
267 lines (205 loc) · 7.82 KB
/
Copy pathpretrain_surface_emnn.py
File metadata and controls
267 lines (205 loc) · 7.82 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
import torch
import torch.nn.functional as F
from torch.optim import Adam
import argparse
import numpy as np
import trimesh
from scipy.spatial import cKDTree
from models.emnn_autoencoder import EMNN_AutoEncoder
from utils.partition import extract_partition_matrices, mesh_simplification_quadric_decimation
from glob import glob
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import yaml
from sklearn.metrics import roc_auc_score
from torch.utils.data import random_split
from tqdm import tqdm
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
from utils.hierarchical_graph import build_surface_graph
from utils.helpers import add_noise
from utils.dataset import SurfaceDataset, collate_surface_graphs
from torch.optim.lr_scheduler import CosineAnnealingLR
# ============================================================
# Train
# ============================================================
def train_epoch(model, loader, optimizer, device):
model.train()
total_loss = 0
pbar = tqdm(loader, desc="Train", leave=False)
for h, x, edge_index, face_index, edge_attr in pbar:
h = h.to(device)
x = x.to(device)
edge_index = edge_index.to(device)
face_index = face_index.to(device)
if edge_attr is not None:
edge_attr = edge_attr.to(device)
# ----------------------------
# Add noise
# ----------------------------
x_noisy, x_clean, noise_std = add_noise(x)
noise_std = float(noise_std)
target = x_clean - x_noisy
# ----------------------------
# Forward
# ----------------------------
pred_noise = model(
h, x_noisy, edge_index, face_index, edge_attr
)
# ----------------------------
# Denoising MSE loss
# ----------------------------
loss = ((pred_noise - target) ** 2).mean(dim=-1)
loss = loss.mean()
# ----------------------------
# Smoothness regularization
# ----------------------------
row, col = edge_index
smooth_loss = ((pred_noise[row] - pred_noise[col]) ** 2).mean()
loss = loss + 0.01 * smooth_loss
# ----------------------------
# Backprop
# ----------------------------
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
total_loss += loss.item()
return total_loss / len(loader)
# ============================================================
# Evaluate
# ============================================================
def evaluate_epoch(model, loader, device):
model.eval()
total_loss = 0
with torch.no_grad():
pbar = tqdm(loader, desc="Val", leave=False)
for h, x, edge_index, face_index, edge_attr in pbar:
h = h.to(device)
x = x.to(device)
edge_index = edge_index.to(device)
face_index = face_index.to(device)
if edge_attr is not None:
edge_attr = edge_attr.to(device)
# ----------------------------
# Add noise
# ----------------------------
x_noisy, x_clean, noise_std = add_noise(x)
noise_std = float(noise_std)
target = x_clean - x_noisy
# ----------------------------
# Forward
# ----------------------------
pred_noise = model(
h,
x_noisy,
edge_index,
face_index,
edge_attr
)
# ----------------------------
# Denoising MSE loss
# ----------------------------
loss = ((pred_noise - target) ** 2).mean(dim=-1)
loss = loss.mean()
total_loss += loss.item()
return total_loss / len(loader)
# ============================================================
# Train entry point
# ============================================================
def train(config):
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using device:", device)
surface_cfg = config["surface"]
data_cfg = config["data"]
# --------------------------------------------------------
# Dataset + Split
# --------------------------------------------------------
dataset = SurfaceDataset(
surface_dir=data_cfg["surface_dir"],
max_faces=surface_cfg["max_faces"],
)
dataset_size = len(dataset)
train_size = int(0.9 * dataset_size)
val_size = dataset_size - train_size
generator = torch.Generator().manual_seed(42)
train_dataset, val_dataset = random_split(
dataset,
[train_size, val_size],
generator=generator
)
train_loader = DataLoader(
train_dataset,
batch_size=data_cfg["batch_size"],
shuffle=True,
collate_fn=collate_surface_graphs
)
val_loader = DataLoader(
val_dataset,
batch_size=data_cfg["batch_size"],
shuffle=False,
collate_fn=collate_surface_graphs
)
# --------------------------------------------------------
# Model
# --------------------------------------------------------
model = EMNN_AutoEncoder(
in_node_nf=surface_cfg["input_dim"],
hidden_nf=surface_cfg["hidden_dim"],
latent_dim=surface_cfg["latent_dim"],
in_edge_nf=1,
n_layers=surface_cfg["n_layers"]
).to(device)
optimizer = Adam(model.parameters(), lr=surface_cfg["lr"])
scheduler = CosineAnnealingLR(optimizer, T_max=surface_cfg["epochs"], eta_min=1e-5)
# --------------------------------------------------------
# Logging
# --------------------------------------------------------
log_path = surface_cfg["log_path"]
os.makedirs(os.path.dirname(log_path), exist_ok=True)
with open(log_path, "w") as f:
f.write("========================================\n")
f.write("Surface Denoising Autoencoder Training Log\n")
f.write("========================================\n")
# --------------------------------------------------------
# Training Loop
# --------------------------------------------------------
best_val_loss = float("inf")
for epoch in range(surface_cfg["epochs"]):
train_loss = train_epoch(model, train_loader, optimizer, device)
val_loss = evaluate_epoch(model, val_loader, device)
scheduler.step()
log_str = (
f"Epoch {epoch:03d}\n"
f"Train Loss: {train_loss:.6f}\n"
f"Val Loss: {val_loss:.6f}\n"
f"LR: {scheduler.get_last_lr()[0]:.2e}\n"
f"----------------------------------------\n"
)
print(log_str)
with open(log_path, "a") as f:
f.write(log_str)
# --------------------------------------------------------
# Save best model (based on val loss)
# --------------------------------------------------------
if val_loss < best_val_loss:
best_val_loss = val_loss
torch.save({
"model_state_dict": model.encoder.state_dict(),
"config": config
}, surface_cfg["save_path"])
with open(log_path, "a") as f:
f.write(f"New best model saved (Val Loss={best_val_loss:.6f})\n")
print(f"Encoder saved to {surface_cfg['save_path']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="./config/model_config.yaml")
args = parser.parse_args()
# --------------------------------------------
# Load YAML config
# --------------------------------------------
with open(args.config, "r") as f:
config = yaml.safe_load(f)
train(config)