-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_plane.py
292 lines (250 loc) · 8.68 KB
/
single_plane.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
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
#!~/venv/bin/python
import os
import sys
import time
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
import weave
import random
import wandb
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader
import numpy as np
import json
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
#
sys.path.append("./src")
sys.path.append("./src/net_cfg.json")
sys.path.append("./src/cls.py")
from src import fns as fns
from src import cls as cls
# ==================================#
# ======== LOAD CONFIG FILE ========#
# ==================================#
with open("./src/net_cfg.json", "r") as cfg:
config = json.load(cfg)
plane = 0
train_frac = config["dataset"]["train_fraction"]
val_frac = config["dataset"]["val_fraction"]
generator_seed = config["dataset"]["generator_seed"]
# dataloader configuration ----------
batch_size = config["dataloader"]["batch_size"]
num_workers = config["dataloader"]["num_workers"]
shuffle = config["dataloader"]["shuffle"]
# training configuration ------------
optimizer = config["training"]["optimizer"]
scheduler = config["training"]["scheduler"]
criterion = config["training"]["criterion"]
lr = config["training"]["learning_rate"]
momentum = config["training"]["momentum"] # only for SGD
weight_decay = config["training"]["weight_decay"] # only for Adam and RAdam
gamma = config["training"]["gamma"] # only for StepLR and ExponentialLR
step_size = config["training"]["step_size"] # only for StepLR
end_factor = config["training"]["end_factor"] # only for LinearLR
num_epochs = config["training"]["num_epochs"]
patience = config["training"]["patience"]
sgn = "pos"
# ==================================#
# ======== SET UP DIRECTORIES ======#
# ==================================#
data_dir = "/mnt/lustre/helios-shared/GAMS/dune/pdk-root"
print(f"Data dir: {data_dir}")
# ==================================#
# ============ W&B SETUP ===========#
# ==================================#
datetime = time.strftime("%d-%m_%H-%M")
wandb_run_name = f"{datetime}_resnet18_plane{plane}"
print(f"WB run name: {wandb_run_name} ")
run = wandb.init(
project="pdecay-masters-single-w-decay-modes",
name=wandb_run_name,
config={
"learning_rate": lr,
"architecture": config["model"]["architecture"],
"epochs": num_epochs,
"optimizer": optimizer,
"scheduler": scheduler,
"additional_info": config["model"]["remarks"],
"patience": patience,
"batch_size": batch_size,
"momentum": momentum,
"weight_decay": weight_decay,
"gamma": gamma,
"step_size": step_size,
"end_factor": end_factor,
"PLANE": plane,
"sgn": sgn,
},
)
weave_run = weave.init("pdecay-masters-single-w-decay-modes")
table = wandb.Table(columns=["ground truth", "prediction"])
val_table = wandb.Table(columns=["ground truth", "prediction"])
train_df = pd.DataFrame(columns=["ground_truth", "output"])
val_df = pd.DataFrame(columns=["ground_truth", "output"])
# ==================================#
# model = cls.ModifiedResNet()
## SMALLER MODELS: =================
# -> mobilenets
# model = cls.ModifiedMobileNetV3()
# model = cls.ModifiedMobileNetV2()
# -> efficientnets
# ==================================
#torch set number of threads to number of cores
torch.set_num_threads(4)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = cls.ModifiedEfficientNet(dropout=0.1, device=device)
# Distribute model across all available GPUs
if torch.cuda.device_count() > 1:
print(f"Using {torch.cuda.device_count()} GPUs.")
model = nn.DataParallel(model)
model.to(device)
# for name, param in model.named_parameters():
# print(f"{name} is on {param.device}")
# Use optimizer and scheduler from config file
# Optimizer:
if optimizer == "Adam":
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay)
elif optimizer == "SGD":
optimizer = optim.SGD(
model.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay
)
elif optimizer == "Adadelta":
optimizer = optim.Adadelta(
model.parameters(), lr=lr, weight_decay=weight_decay, rho=0.9
)
elif optimizer == "RAdam":
optimizer = optim.RAdam(model.parameters(), lr=lr, weight_decay=weight_decay)
# Scheduler:
if scheduler == "StepLR":
scheduler = lr_scheduler.StepLR(
optimizer, step_size=step_size, gamma=gamma, verbose=True
)
elif scheduler == "LinearLR":
scheduler = lr_scheduler.LinearLR(
optimizer, end_factor=end_factor, total_iters=num_epochs, verbose=True
)
elif scheduler == "ExponentialLR":
scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=gamma, verbose=True)
elif scheduler == "ReduceLROnPlateau":
scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, "min", 0.1)
elif scheduler == "CosineAnnealingLR":
scheduler = lr_scheduler.CosineAnnealingLR(
optimizer, T_max=num_epochs, verbose=True, eta_min=1e-8
)
# Use criterion from config file
if criterion == "BCEWithLogitsLoss":
criterion = nn.BCEWithLogitsLoss()
# ==================================#
# ======== DATA PREPARATION ========#
# ==================================#
print("================")
print("Creating dataset.")
print("================")
print("Plane {}".format(plane))
signal_dir = os.path.join(
data_dir, "pdk-npz"
) # evt dirs of structure: metadata in csv format & plane dirs -> npz file
background_dir = os.path.join(data_dir, "atmonu-npz")
signal_decay_dirs = [
os.path.join("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/pdk_decays", dir)
for dir in os.listdir("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/pdk_decays")
if os.path.isdir(
os.path.join("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/pdk_decays", dir)
)
]
background_decay_dirs = [
os.path.join("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/atmonu_decays", dir)
for dir in os.listdir("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/atmonu_decays")
if os.path.isdir(
os.path.join("/mnt/lustre/helios-shared/GAMS/dune/pdk-root/atmonu_decays", dir)
)
]
decay_dirs = signal_decay_dirs + background_decay_dirs
events = (evt for decay_dir in decay_dirs for evt in Path(decay_dir).iterdir() if evt.is_dir())
dataset = cls.SparseMatrixDataset(event_paths=events, plane_idx=plane)
# print(dataset[:5])
print("==================")
print("Splitting dataset.")
print("==================")
train, val, test = fns.strat_meta_split(
dataset=dataset,
labels=dataset.labels,
train_frac=train_frac,
val_frac=val_frac,
generator_seed=735,
)
print("Dataset split.")
dataloaders = {
"train": DataLoader(
train, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, drop_last=True
),
"test": DataLoader(
test, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, drop_last=True
),
"val": DataLoader(
val, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers, drop_last=True
),
}
print (f'lenghts. Dataset: {len(dataset)}, Train: {len(train)}, Val: {len(val)}, Test: {len(test)}')
print(f'Dataloaders: Train: {len(dataloaders["train"])}, Val: {len(dataloaders["val"])}, Test: {len(dataloaders["test"])}')
# ==================================#
# =========== TRAINING =============#
# ==================================#
start_time = time.time()
print("====================================")
print(
"Training model. Started at {}".format(
time.strftime("%H:%M:%S", time.localtime(start_time))
)
)
print("====================================")
(
trained_model,
train_loss,
val_loss,
train_acc,
val_acc,
precision_vals,
recall_vals,
f1_vals,
best_ep,
) = fns.train_model(
model=model,
train_loader=dataloaders["train"],
val_loader=dataloaders["val"],
optimizer=optimizer,
scheduler=scheduler,
criterion=criterion,
num_epochs=num_epochs,
device=device,
patience=patience,
table=table,
val_table=val_table,
df_train=train_df,
df_val=val_df,
plane=plane,
seed=generator_seed,
)
print("Training complete. Took {} minutes.".format((time.time() - start_time) / 60))
run.log({f"train_table_{sgn}_plane{plane}": table})
run.log({f"val_table_{sgn}_plane{plane}": val_table})
np.save(
f"/mnt/lustre/helios-home/gartmann/venv/diagnostics/resnet18-plane{plane}-train-loss.npy",
train_loss,
)
np.save(
f"/mnt/lustre/helios-home/gartmann/venv/diagnostics/resnet18-plane{plane}-train-acc.npy",
train_acc,
)
np.save(
f"/mnt/lustre/helios-home/gartmann/venv/diagnostics/resnet18-plane{plane}-val-loss.npy",
val_loss,
)
np.save(
f"/mnt/lustre/helios-home/gartmann/venv/diagnostics/resnet18-plane{plane}-val-acc.npy",
val_acc,
)
run.finish()