-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_parallel.py
More file actions
199 lines (163 loc) · 6.87 KB
/
train_parallel.py
File metadata and controls
199 lines (163 loc) · 6.87 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
import json
import os
import tomllib
import ltn
import torch
import torch.nn as nn
from torch.utils.data import ConcatDataset, DataLoader
from models.Logic_Tensor_Networks import Logic_Tensor_Networks
from utils.DataLoader import RelationshipDataset
# Load configuration
with open("config.toml", "rb") as config_file:
config = tomllib.load(config_file)
def train_combined():
"""Train all predicates together to maximize GPU utilization."""
# Read configuration
epochs = config["Train"]["epochs"]
batch_size = config["Train"]["batch_size"]
lr = config["Train"]["lr"]
# Enable CUDA optimizations
torch.backends.cudnn.benchmark = True
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# List of predicates to train
predicates = ["in", "on", "next to", "on top of", "near", "under"]
# Setup mixed precision training
scaler = torch.amp.GradScaler("cuda")
# Create dummy detector output for LTN initialization
num_obj = 10
detector_output = {
"centers": torch.randn(num_obj, 2, device=device),
"widths": torch.randn(num_obj, device=device),
"heights": torch.randn(num_obj, device=device),
"classes": torch.randint(0, 10, (num_obj,), device=device),
}
# Create LTN network
class_labels = list(range(100))
input_dim = 5
ltn_network = Logic_Tensor_Networks(
detector_output, input_dim, class_labels, device=device
)
# Dictionary to store predicate networks
pred_networks = {
"in": ltn_network.in_predicate,
"on": ltn_network.on_predicate,
"next to": ltn_network.next_to_predicate,
"on top of": ltn_network.on_top_of_predicate,
"near": ltn_network.near_predicate,
"under": ltn_network.under_predicate,
}
# Create loss functions and optimizers for each predicate
loss_fn = nn.BCEWithLogitsLoss()
optimizers = {
pred: torch.optim.AdamW(pred_networks[pred].parameters(), lr=lr)
for pred in predicates
}
# Load all datasets simultaneously
relationships_path = config["Trainer"]["relationships_path"]
image_meta_path = config["Trainer"]["image_meta_path"]
# Create dataset for each predicate
datasets = {}
for pred in predicates:
neg_predicates = [p for p in predicates if p != pred]
datasets[pred] = RelationshipDataset(
relationships_json_path=relationships_path,
image_meta_json_path=image_meta_path,
pos_predicate=pred,
# neg_predicates=neg_predicates,
)
# Create training and validation data loaders with optimizations
train_loaders = {}
val_loaders = {}
for pred in predicates:
train_size = int(0.8 * len(datasets[pred]))
val_size = len(datasets[pred]) - train_size
train_subset, val_subset = torch.utils.data.random_split(
datasets[pred], [train_size, val_size]
)
train_loaders[pred] = DataLoader(
train_subset,
batch_size=batch_size // len(predicates),
shuffle=True,
num_workers=2,
pin_memory=True,
)
val_loaders[pred] = DataLoader(
val_subset,
batch_size=batch_size // len(predicates),
shuffle=False,
num_workers=1,
pin_memory=True,
)
# Training loop
print(f"Training all predicates together on {device}")
training_logs = {pred: [] for pred in predicates}
for epoch in range(epochs):
for pred in predicates:
pred_networks[pred].train()
epoch_train_losses = {pred: 0.0 for pred in predicates}
train_batches = {pred: 0 for pred in predicates}
data_iters = {pred: iter(train_loaders[pred]) for pred in predicates}
active_predicates = set(predicates)
while active_predicates:
for pred in list(active_predicates):
try:
batch = next(data_iters[pred])
subj_features, obj_features, labels = batch
subj_features, obj_features, labels = (
subj_features.to(device, non_blocking=True),
obj_features.to(device, non_blocking=True),
labels.view(-1, 1).float().to(device, non_blocking=True),
)
subj_obj = ltn.Variable("subj", subj_features)
obj_obj = ltn.Variable("obj", obj_features)
optimizers[pred].zero_grad(set_to_none=True)
with torch.cuda.amp.autocast():
outputs = pred_networks[pred](subj_obj, obj_obj)
loss = loss_fn(outputs, labels)
scaler.scale(loss).backward()
scaler.step(optimizers[pred])
scaler.update()
epoch_train_losses[pred] += loss.item()
train_batches[pred] += 1
except StopIteration:
active_predicates.remove(pred)
for pred in predicates:
pred_networks[pred].eval()
epoch_val_loss, val_batches, correct, total = 0.0, 0, 0, 0
with torch.no_grad():
for batch in val_loaders[pred]:
subj_features, obj_features, labels = batch
subj_features, obj_features, labels = (
subj_features.to(device, non_blocking=True),
obj_features.to(device, non_blocking=True),
labels.view(-1, 1).float().to(device, non_blocking=True),
)
subj_obj = ltn.Variable("subj", subj_features)
obj_obj = ltn.Variable("obj", obj_features)
outputs = pred_networks[pred](subj_obj, obj_obj)
loss = loss_fn(outputs, labels)
epoch_val_loss += loss.item()
val_batches += 1
probs = torch.sigmoid(outputs)
preds = (probs >= 0.5).float()
correct += (preds == labels).sum().item()
total += labels.size(0)
training_logs[pred].append(
{
"epoch": epoch + 1,
"train_loss": epoch_train_losses[pred]
/ max(train_batches[pred], 1),
"val_loss": epoch_val_loss / max(val_batches, 1),
"val_accuracy": correct / max(total, 1),
}
)
os.makedirs("weights", exist_ok=True)
for pred in predicates:
torch.save(
pred_networks[pred].model.state_dict(),
f"weights/{pred.replace(' ', '_').lower()}_weights.pth",
)
with open(f"weights/{pred.replace(' ', '_').lower()}_log.json", "w") as f:
json.dump(training_logs[pred], f, indent=4)
if __name__ == "__main__":
train_combined()