-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
391 lines (310 loc) · 16.9 KB
/
train.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
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
#Train our models and save them to disk
#Usage: python train.py --config config/config.json --dataloader torchvision
import os
import time
import numpy as np
import json
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torch.amp import GradScaler, autocast
from tqdm import tqdm
from psutil import cpu_count
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.utils.tensorboard import SummaryWriter
from mobilenet import MobileNetSmall3D,MobileNetLarge3D
from movinet import MoViNetA2
from mobilevit import MobileViT
from helpers import calculate_accuracy_bce, average_for_plotting, calculate_accuracy
#classification 0 is zone 1, classification 1 is zone 2, etc.
device = 'cuda' if torch.cuda.is_available() else 'cpu'
dtype = torch.bfloat16 if device == 'cuda' else torch.float32
def state_dict_converter(state_dict):
for key in list(state_dict.keys()):
if key.startswith("_orig_mod."):
new_key = key.replace("_orig_mod.", "")
state_dict[new_key] = state_dict[key]
del state_dict[key]
return state_dict
def create_dataloader(dataloader,batch_size,mean,std,train_annotations_file,val_annotations_file,video_paths):
#create dataloader
if dataloader == "torchvision":
from torch.utils.data import DataLoader
from dataloader import PicklebotDataset, custom_collate
#from torchvision import transforms may want to put this back in one day
#establish our normalization using transforms,
#note that we are doing this in our dataloader as opposed to in the training loop like with dali
#transform = transforms.Normalize(mean,std)
#dataset
train_dataset = PicklebotDataset(train_annotations_file,video_paths,dtype=dtype,backend='opencv') #may want to add transform=transform back
train_loader = DataLoader(train_dataset, batch_size=batch_size,shuffle=False,collate_fn=custom_collate,num_workers=16,pin_memory=True)
val_dataset = PicklebotDataset(val_annotations_file,video_paths,dtype=dtype,backend='opencv') #may want to add transform=transform back
val_loader = DataLoader(val_dataset, batch_size=batch_size,shuffle=False,collate_fn=custom_collate,num_workers=16,pin_memory=True)
elif dataloader == "dali":
from nvidia.dali.plugin.pytorch import DALIClassificationIterator, LastBatchPolicy
from helpers import dali_video_pipeline
#information for the dali pipeline
sequence_length = 130 #longest videos in our dataset
initial_prefetch_size = 20
#video paths
train_video_paths = '/home/henry/Documents/PythonProjects/picklebotdataset/train'
val_video_paths = '/home/henry/Documents/PythonProjects/picklebotdataset/val'
num_train_videos = len(os.listdir(train_video_paths + '/' + 'balls')) + len(os.listdir(train_video_paths + '/' + 'strikes'))
num_val_videos = len(os.listdir(val_video_paths + '/' + 'balls')) + len(os.listdir(val_video_paths + '/' + 'strikes'))
#multiply mean and val by 255 to convert to 0-255 range
mean = (torch.tensor(mean)*255)[None,None,None,:]
std = (torch.tensor(std)*255)[None,None,None,:]
print("Building DALI pipelines...")
#build our pipelines
train_pipe = dali_video_pipeline(batch_size=batch_size, num_threads=cpu_count()//2, device_id=0, file_root=train_video_paths,
sequence_length=sequence_length,initial_prefetch_size=initial_prefetch_size,mean=mean*255,std=std*255)
val_pipe = dali_video_pipeline(batch_size=batch_size, num_threads=cpu_count()//2, device_id=0, file_root=val_video_paths,
sequence_length=sequence_length,initial_prefetch_size=initial_prefetch_size,mean=mean,std=std)
train_pipe.build()
val_pipe.build()
train_loader = DALIClassificationIterator(train_pipe, auto_reset=True,last_batch_policy=LastBatchPolicy.PARTIAL, size=num_train_videos)
val_loader = DALIClassificationIterator(val_pipe, auto_reset=True,last_batch_policy=LastBatchPolicy.PARTIAL, size=num_val_videos)
elif dataloader == "rocal":
from amd.rocal.plugin.pytorch import ROCALClassificationIterator
from helpers import rocal_video_pipeline
#information for the dali pipeline
sequence_length = 130 #longest videos in our dataset
initial_prefetch_size = 20
#video paths
train_video_paths = '/home/henry/Documents/PythonProjects/picklebotdataset/train'
val_video_paths = '/home/henry/Documents/PythonProjects/picklebotdataset/val'
num_train_videos = len(os.listdir(train_video_paths + '/' + 'balls')) + len(os.listdir(train_video_paths + '/' + 'strikes'))
num_val_videos = len(os.listdir(val_video_paths + '/' + 'balls')) + len(os.listdir(val_video_paths + '/' + 'strikes'))
#multiply mean and val by 255 to convert to 0-255 range
mean = (torch.tensor(mean)*255)[None,None,None,:]
std = (torch.tensor(std)*255)[None,None,None,:]
print("Building rocAL pipelines...")
#build our pipelines
train_pipe = rocal_video_pipeline(batch_size=batch_size, num_threads=cpu_count()//2, device_id=0, file_root=train_video_paths,
sequence_length=sequence_length,initial_prefetch_size=initial_prefetch_size,mean=mean*255,std=std*255)
val_pipe = rocal_video_pipeline(batch_size=batch_size, num_threads=cpu_count()//2, device_id=0, file_root=val_video_paths,
sequence_length=sequence_length,initial_prefetch_size=initial_prefetch_size,mean=mean,std=std)
train_pipe.build()
val_pipe.build()
train_loader = ROCALClassificationIterator(train_pipe, auto_reset=True, size=num_train_videos)
val_loader = ROCALClassificationIterator(val_pipe, auto_reset=True, size=num_val_videos)
return train_loader, val_loader
def load_config(config_path):
with open(config_path) as config_file:
config = json.load(config_file)
return config
def extract_features_labels(output,dataloader):
if dataloader == "torchvision":
features = output[0].to(device,non_blocking=True).permute(0,-1,1,2,3).to(torch.bfloat16) / 255 #doing these operations on the gpu makes loading 2x faster, investigating if we're better off doing this on the cpu to keep the gpu working on the model
labels = output[1].unsqueeze(1).to(device,non_blocking=True)
elif dataloader == "dali":
features = output[0]["data"].float().to(device,non_blocking=True)
features = features/255 #normalize to 0-1
features = features.permute(0,-1,1,2,3) #move channels to front
labels = output[0]["label"].to(device,non_blocking=True)
labels = labels.float()
return features,labels
@torch.no_grad()
def estimate_loss(model,val_loader,criterion,dataloader,use_autocast):
print("Evaluating...")
model.eval()
if str(criterion) == "CrossEntropyLoss()":
accuracy_calc = calculate_accuracy
elif str(criterion) == "BCEWithLogitsLoss()":
accuracy_calc = calculate_accuracy_bce
val_correct = 0
val_samples = 0
val_loss = 0
for output in tqdm(val_loader):
features,labels = extract_features_labels(output,dataloader)
if use_autocast:
with autocast(dtype=dtype,device_type=device):
outputs = model(features)
if str(criterion) == "CrossEntropyLoss()":
labels = labels.to(torch.long).squeeze(1)
val_correct += accuracy_calc(outputs,labels)
val_samples += labels.size(0)
val_loss += criterion(outputs,labels).item()
else:
outputs = model(features)
if str(criterion) == "CrossEntropyLoss()":
labels = labels.to(torch.long).squeeze(1)
val_correct += accuracy_calc(outputs,labels)
val_samples += labels.size(0)
val_loss += criterion(outputs,labels).item()
val_loss /= len(val_loader)
val_accuracy = val_correct/val_samples
return val_loss, val_accuracy
def train(config, dataloader="torchvision"):
#hyperparameters
torch.manual_seed(1234)
if torch.cuda.is_available():
torch.cuda.manual_seed(1234)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
model_name = config["model_name"]
learning_rate = config["learning_rate"]
batch_size = config["batch_size"]
max_iters = config["max_iters"]
eval_interval = config["eval_interval"]
weight_decay = config["weight_decay"]
std = tuple(config["std"]) #(0.2104, 0.1986, 0.1829)
mean = tuple(config["mean"]) #(0.3939, 0.3817, 0.3314)
use_autocast = config["use_autocast"]
compile = config["compile"]
criterion = config["criterion"]
checkpoint = config["checkpoint"]
train_annotations_file = config["train_annotations_file"]
val_annotations_file = config["val_annotations_file"]
video_paths = config["video_paths"]
num_classes = config["num_classes"]
print(f"Training model: {model_name} Using device: {device} with dtype: {dtype}")
#create model
valid_models = {"MoViNetA2":MoViNetA2,"MobileNetLarge3D":MobileNetLarge3D,"MobileNetSmall3D":MobileNetSmall3D,"MobileViT":MobileViT}
if model_name in valid_models:
if model_name == "MobileViT":
dims = config["dims"]
channels = config["channels"]
model = valid_models[model_name](dims=dims,channels=channels,num_classes=num_classes).to(device,non_blocking=True)
accuracy_calc = calculate_accuracy
else:
model = valid_models[model_name](num_classes=13).to(device,non_blocking=True)
accuracy_calc = calculate_accuracy
else:
raise ValueError(f"Invalid model name: {model_name}")
model.initialize_weights()
if torch.cuda.device_count() > 1:
print(f"Using {torch.cuda.device_count()} GPUs")
model = nn.DataParallel(model)
#create optimizer
optimizer = optim.AdamW(model.parameters(),lr=learning_rate,weight_decay=weight_decay,fused=True)
#create scheduler
eta_min = learning_rate/10
scheduler = CosineAnnealingLR(optimizer,T_max=max_iters,eta_min=eta_min)
#create loss function
valid_losses = {"CE":nn.CrossEntropyLoss(),"BCE":nn.BCEWithLogitsLoss()}
if criterion in valid_losses:
criterion = valid_losses[criterion]
else:
raise ValueError(f"Invalid criterion: {criterion}")
#create scaler for mixed precision training
if use_autocast:
scaler = GradScaler('cuda')
#create tensorboard writer
run_name = f"{model_name}_{criterion}"
writer = SummaryWriter(f"runs/{run_name}")
if checkpoint is not None:
print("Loading checkpoint...")
checkpoint = torch.load(checkpoint)
model.load_state_dict(state_dict_converter(checkpoint))
start_epoch = config["checkpoint"]
print(f"Loaded checkpoint at epoch {start_epoch}")
#create dataloader
train_loader, val_loader = create_dataloader(dataloader,batch_size,mean,std,train_annotations_file,val_annotations_file,video_paths)
#compile the model
if compile:
print("Compiling the model... (takes a ~minute)")
model = torch.compile(model) # requires PyTorch 2 and a modern gpu (seems like mostly V/A/H 100s work best, but it absolutely speeds up my 7900xtx)
print("Compilation complete!")
#train the model
#training loop
start_time = time.time()
print(f"Training... started: {time.ctime(start_time)}")
train_losses = torch.tensor([])
train_percent = torch.tensor([])
val_losses = []
val_percent = []
try:
for iter in range(max_iters):
model.train()
train_correct = 0
train_samples = 0
batch_loss_list = []
batch_percent_list = []
for batch_idx, output in tqdm(enumerate(train_loader)):
features,labels = extract_features_labels(output,dataloader)
optimizer.zero_grad(set_to_none=True)
if use_autocast:
with autocast('cuda',enabled=True,dtype=dtype):
outputs = model(features)
if str(criterion) == "CrossEntropyLoss()":
labels = labels.to(torch.long).squeeze(1)
loss = criterion(outputs,labels)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
else:
outputs = model(features)
if str(criterion) == "CrossEntropyLoss()":
labels = labels.to(torch.long).squeeze(1)
loss = criterion(outputs,labels)
loss.backward()
optimizer.step()
train_correct += accuracy_calc(outputs,labels)
train_samples += labels.size(0)
#append to lists
batch_loss_list.append(loss.item())
batch_percent_list.append(train_correct/train_samples)
#write to tensorboard
writer.add_scalar("training loss",loss.item(),(iter+1)*batch_idx)
writer.add_scalar("training accuracy",train_correct/train_samples,(iter+1)*batch_idx)
scheduler.step() #update learning rate
train_losses = torch.cat((train_losses,average_for_plotting(batch_loss_list).unsqueeze(1)))
train_percent = torch.cat((train_percent,average_for_plotting(batch_percent_list).unsqueeze(1)))
elapsed_time = time.time() - start_time
remaining_iters = max_iters - iter
avg_time_per_iter = elapsed_time / (iter + 1)
estimated_remaining_time = remaining_iters * avg_time_per_iter
if iter % eval_interval == 0 or iter == max_iters - 1:
val_loss, val_accuracy = estimate_loss(model,val_loader,criterion,use_autocast=use_autocast,dataloader=dataloader)
val_losses.append(val_loss)
val_percent.append(val_accuracy)
print(f"Step {iter}: Train Loss: {train_losses[-1].mean().item():.4f}, Val Loss: {val_losses[-1]:.4f}")
print(f"Step {iter}: Train Accuracy: {(train_percent[-1].mean().item())*100:.2f}%, Val Accuracy: {val_percent[-1]*100:.2f}%")
writer.add_scalar('val loss', val_losses[-1], iter)
writer.add_scalar('val accuracy', val_percent[-1], iter)
torch.save(model.state_dict(), f'checkpoints/{model_name}{iter}.pth')
tqdm.write(f"Iter [{iter+1}/{max_iters}] - Elapsed Time: {elapsed_time:.2f}s - Remaining Time: [{estimated_remaining_time:.2f}]")
if iter == max_iters - 1:
print("Training completed:")
print(f"Final Train Loss: {train_losses[-1].mean().item():.4f}")
print(f"Final Val Loss: {val_losses[-1]:.4f}")
print(f"Final Train Accuracy: {(train_percent[-1].mean().item())*100:.2f}%")
print(f"Final Val Accuracy: {val_percent[-1]*100:.2f}%")
except KeyboardInterrupt:
print(f"Keyboard interrupt,\nFinal Train Loss: {train_losses[-1].mean().item():.4f}")
print(f"Final Val Loss: {val_losses[-1]:.4f}")
print(f"Final Train Accuracy: {(train_percent[-1].mean().item())*100:.2f}%")
print(f"Final Val Accuracy: {val_percent[-1]*100:.2f}%")
finally:
torch.save(model.state_dict(), f'checkpoints/{run_name}_finished.pth')
with open(f'statistics/{run_name}_finished_train_losses.npy', 'wb') as f:
np.save(f, train_losses.cpu().numpy())
with open(f'statistics/{run_name}_finished_val_losses.npy', 'wb') as f:
np.save(f, np.array(val_losses))
with open(f'statistics/{run_name}_finished_train_percent.npy', 'wb') as f:
np.save(f, train_percent.cpu().numpy())
with open(f'statistics/{run_name}_finished_val_percent.npy', 'wb') as f:
np.save(f, (val_percent[-1].cpu().numpy()))
print(f"Model and statistics saved!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Train a model with the specified config")
parser.add_argument("--config","-C", type=str, required=True, help="Path to config file")
parser.add_argument("--dataloader", "-D", type=str, required=False, help="Choose a dataloader from torchvision, dali, or rocal")
args = parser.parse_args()
config = load_config(args.config)
if args.dataloader is not None:
dataloader = args.dataloader
else:
dataloader = "torchvision"
def profile():
train(config,dataloader)
import cProfile
profiler = cProfile.Profile()
profiler.runcall(profile)
import pstats
from pstats import SortKey
stats = pstats.Stats(profiler)
stats.sort_stats(SortKey.TIME) # Sort by time
stats.dump_stats('train_stats.prof')