-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
134 lines (113 loc) · 3.66 KB
/
engine.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
import torch
from sam.sam import SAM
import numpy as np
from tqdm import tqdm
import gc as gc
from utils import save_model
import wandb
def train_epoch(model,
train_dl,
criterion,
optimizer,
device
):
model.train()
loss_history = 0
for x_batch, y_batch in tqdm(train_dl, leave=False):
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
optimizer.zero_grad()
y_pred = model(x_batch)
loss = criterion(y_pred, y_batch)
if isinstance(optimizer, SAM):
loss.backward()
optimizer.first_step(zero_grad=True)
criterion(model(x_batch), y_batch).backward()
optimizer.second_step(zero_grad=True)
else:
loss.backward()
optimizer.step()
loss_history += loss.item()
return loss_history / len(train_dl)
@torch.no_grad()
def val_epoch(model,
val_dl,
criterion,
device
):
model.eval()
loss_history = 0
correct = 0
total = 0
with torch.no_grad():
for x_batch, y_batch in tqdm(val_dl, leave=False):
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
y_pred = model(x_batch)
_, predicted = torch.max(y_pred.data, 1)
total += y_batch.size(0)
correct += (predicted == y_batch).sum().item()
loss = criterion(y_pred, y_batch)
loss_history += loss.item()
return loss_history / len(val_dl), correct / total
def get_accuracy(model, val_dl, device):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for x_batch, y_batch in tqdm(val_dl, leave=False):
x_batch = x_batch.to(device)
y_batch = y_batch.to(device)
y_pred = model(x_batch)
_, predicted = torch.max(y_pred.data, 1)
total += y_batch.size(0)
correct += (predicted == y_batch).sum().item()
return correct / total
def train(model,
train_loader,
val_loader,
criterion,
optimizer,
lr_scheduler,
device,
epochs,
save_dir,
early_stopper=None,
unfreeze=None
):
results = {
"train_loss": [],
"val_loss": [],
"accuracy": []
}
best_val_loss = np.inf
for epoch in range(1, epochs + 1):
print(f"Epoch {epoch}:")
train_loss = train_epoch(model, train_loader, criterion, optimizer, device)
print(f"Train Loss: {train_loss:.4f}")
if lr_scheduler:
lr_scheduler.step()
results["train_loss"].append(train_loss)
val_loss, accuracy = val_epoch(model, val_loader, criterion, device)
results["accuracy"].append(accuracy)
print(f"Val Loss: {val_loss:.4f}")
print(f"Accuracy: {accuracy:.4f}")
print()
results["val_loss"].append(val_loss)
if val_loss < best_val_loss:
best_val_loss = val_loss
save_model(model, save_dir + "/best_model.pth")
wandb.log({"train_loss": train_loss, "val_loss": val_loss,"accuracy": accuracy})
save_model(model, save_dir + "/last_model.pth")
optimizer.zero_grad()
gc.collect()
torch.cuda.empty_cache()
if early_stopper:
if early_stopper.early_stop(val_loss):
print("Early stopping")
break
if unfreeze and epoch % 10 == 0:
stage = unfreeze[epoch // 10 - 1]
for param in model.features[stage].parameters():
param.requires_grad = True
return results