-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
95 lines (76 loc) · 3.13 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
import torch
import time
import os
from tqdm import tqdm
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn as nn
import numpy as np
backend = "qnnpack"
def train_model(
model,
dataloaders,
dataset_sizes,
device,
model_name,
num_epochs=3,
):
since = time.time()
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.AdamW(model.parameters(),lr=0.001, fused=True)
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
working_dir = os.getcwd()
print(f"Model save path (working dir): {working_dir}")
best_accuracy = 0.0
for epoch in range(num_epochs):
print(f"Epoch {epoch}/{num_epochs - 1}")
print("-" * 10)
# Each epoch has a training and validation phase
for phase in ["train", "val"]:
if phase == "train":
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data
for inputs, labels in tqdm(dataloaders[phase]):
inputs, labels = inputs.to(device), labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == "train"):
outputs = model(inputs).squeeze(dim=1)
preds = (torch.sigmoid(outputs) > 0.5).float()
loss = criterion(outputs, labels.float())
# backward + optimize only if in training phase
if phase == "train":
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if phase == "train":
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_accuracy = running_corrects.to(torch.float32) / dataset_sizes[phase]
print(f"{phase} Loss: {epoch_loss:.4f} Acc: {epoch_accuracy:.4f}")
# If the current accuracy is better than the last best acc
# deep copy the model
if phase == "val" and epoch_accuracy > best_accuracy:
best_accuracy = epoch_accuracy
torch.save(
model.state_dict(), f"{working_dir}/{model_name}_best_acc.pth"
)
#torch.save(model, f"{working_dir}/full_{model_name}.pth")
print()
torch.save(model.state_dict(), f"{working_dir}/final_{model_name}.pth")
time_elapsed = time.time() - since
print(
f"Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s"
)
print(f"Best val accuracy: {best_accuracy:4f}")
# load best model weights
model.load_state_dict(torch.load( f"{working_dir}/final_{model_name}.pth"))
return model