-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_train.py
162 lines (141 loc) · 4.68 KB
/
main_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
import argparse
import os
import time
from copy import deepcopy
import arg_parser
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.optim
import torch.utils.data
from torch.utils.data.sampler import SubsetRandomSampler
from trainer import train, validate
import utils
from utils import *
best_sa = 0
def main():
global args, best_sa
args = arg_parser.parse_args()
torch.cuda.set_device(int(args.gpu))
os.makedirs(args.save_dir, exist_ok=True)
if args.seed:
setup_seed(args.seed)
(
model,
train_set,
valid_set,
test_set
) = utils.setup_model_dataset(args)
model.cuda()
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
num_workers=0,
pin_memory=True,
shuffle=True,
)
val_loader = torch.utils.data.DataLoader(
valid_set,
batch_size=args.batch_size,
num_workers=0,
pin_memory=True,
shuffle=True,
)
print(f"number of train dataset {len(train_loader.dataset)}")
print(f"number of val dataset {len(val_loader.dataset)}")
criterion = nn.CrossEntropyLoss()
decreasing_lr = list(map(int, args.decreasing_lr.split(",")))
optimizer = torch.optim.SGD(
model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
if args.imagenet_arch:
lambda0 = (
lambda cur_iter: (cur_iter + 1) / args.warmup
if cur_iter < args.warmup
else (
0.5
* (
1.0
+ np.cos(
np.pi * ((cur_iter - args.warmup) / (args.epochs - args.warmup))
)
)
)
)
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda0)
else:
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=decreasing_lr, gamma=0.1
) # 0.1 is fixed
if args.resume:
print("resume from checkpoint {}".format(args.checkpoint))
checkpoint = torch.load(
args.checkpoint, map_location=torch.device("cuda:" + str(args.gpu))
)
best_sa = checkpoint["best_sa"]
print(best_sa)
start_epoch = checkpoint["epoch"]
all_result = checkpoint["result"]
model.load_state_dict(checkpoint["state_dict"], strict=False)
optimizer.load_state_dict(checkpoint["optimizer"])
scheduler.load_state_dict(checkpoint["scheduler"])
initalization = checkpoint["init_weight"]
print("loading from epoch: ", start_epoch, "best_sa=", best_sa)
else:
all_result = {}
all_result["train_ta"] = []
all_result["test_ta"] = []
all_result["val_ta"] = []
start_epoch = 0
state = 0
for epoch in range(start_epoch, args.epochs):
start_time = time.time()
print(
"Epoch #{}, Learning rate: {}".format(
epoch, optimizer.state_dict()["param_groups"][0]["lr"]
)
)
acc = train(train_loader, model, criterion, optimizer, epoch, args)
# evaluate on validation set
tacc = validate(val_loader, model, criterion, args)
scheduler.step()
all_result["train_ta"].append(acc)
all_result["val_ta"].append(tacc)
is_best_sa = tacc > best_sa
best_sa = max(tacc, best_sa)
save_checkpoint(
{
"result": all_result,
"epoch": epoch + 1,
"state_dict": model.state_dict(),
"best_sa": best_sa,
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
},
is_SA_best=is_best_sa,
pruning=state,
save_path=args.save_dir,
)
print("one epoch duration:{}".format(time.time() - start_time))
# plot training curve
plt.plot(all_result["train_ta"], label="train_acc")
plt.plot(all_result["val_ta"], label="val_acc")
plt.legend()
plt.savefig(os.path.join(args.save_dir, str(state) + "net_train.png"))
plt.close()
# report result
print("Performance on the test data set")
test_tacc = validate(val_loader, model, criterion, args)
if len(all_result["val_ta"]) != 0:
val_pick_best_epoch = np.argmax(np.array(all_result["val_ta"]))
print(
"* best SA = {}, Epoch = {}".format(
all_result["val_ta"][val_pick_best_epoch], val_pick_best_epoch + 1
)
)
if __name__ == "__main__":
main()