-
Notifications
You must be signed in to change notification settings - Fork 27
/
train.py
227 lines (192 loc) · 7.06 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
# Some part borrowed from official tutorial https://github.com/pytorch/examples/blob/master/imagenet/main.py
from __future__ import print_function
from __future__ import absolute_import
import os
import numpy as np
import argparse
import importlib
import time
import logging
import warnings
from collections import OrderedDict
import torch
import torch.nn as nn
from torch.utils.data.dataset import Dataset
from torchvision import datasets, transforms
from torch.utils.tensorboard import SummaryWriter
from models import SupResNet, SSLResNet
import data
import trainers
from losses import SupConLoss
from utils import *
def main():
parser = argparse.ArgumentParser(description="SSD evaluation")
parser.add_argument(
"--results-dir",
type=str,
default="/data/data_vvikash/fall20/SSD/trained_models/",
) # change this
parser.add_argument("--exp-name", type=str, default="temp")
parser.add_argument(
"--training-mode", type=str, choices=("SimCLR", "SupCon", "SupCE")
)
# model
parser.add_argument("--arch", type=str, default="resnet50")
parser.add_argument("--num-classes", type=int, default=10)
# training
parser.add_argument("--dataset", type=str, default="cifar10")
parser.add_argument("--data-dir", type=str, default="/data/data_vvikash/datasets/")
parser.add_argument("--normalize", action="store_true", default=False)
parser.add_argument("--batch-size", type=int, default=512)
parser.add_argument("--size", type=int, default=32)
parser.add_argument("--epochs", type=int, default=500)
parser.add_argument("--lr", type=float, default=0.5)
parser.add_argument("--momentum", type=float, default=0.9)
parser.add_argument("--weight-decay", type=float, default=1e-4)
parser.add_argument("--warmup", action="store_true")
# ssl
parser.add_argument(
"--method", type=str, default="SupCon", choices=["SupCon", "SimCLR", "SupCE"]
)
parser.add_argument("--temperature", type=float, default=0.5)
# misc
parser.add_argument("--print-freq", type=int, default=100)
parser.add_argument("--save-freq", type=int, default=50)
parser.add_argument("--ckpt", type=str, help="checkpoint path")
parser.add_argument("--seed", type=int, default=12345)
args = parser.parse_args()
device = "cuda:0"
if args.batch_size > 256 and not args.warmup:
warnings.warn("Use warmup training for larger batch-sizes > 256")
if not os.path.isdir(args.results_dir):
os.mkdir(args.results_dir)
# create resutls dir (for logs, checkpoints, etc.)
result_main_dir = os.path.join(args.results_dir, args.exp_name)
if os.path.exists(result_main_dir):
n = len(next(os.walk(result_main_dir))[-2]) # prev experiments with same name
result_sub_dir = result_sub_dir = os.path.join(
result_main_dir,
"{}--dataset-{}-arch-{}-lr-{}_epochs-{}".format(
n + 1, args.dataset, args.arch, args.lr, args.epochs
),
)
else:
os.mkdir(result_main_dir)
result_sub_dir = result_sub_dir = os.path.join(
result_main_dir,
"1--dataset-{}-arch-{}-lr-{}_epochs-{}".format(
args.dataset, args.arch, args.lr, args.epochs
),
)
create_subdirs(result_sub_dir)
# add logger
logging.basicConfig(level=logging.INFO, format="%(message)s")
logger = logging.getLogger()
logger.addHandler(
logging.FileHandler(os.path.join(result_sub_dir, "setup.log"), "a")
)
logger.info(args)
# seed cuda
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
np.random.seed(args.seed)
# Create model
if args.training_mode in ["SimCLR", "SupCon"]:
model = SSLResNet(arch=args.arch).to(device)
elif args.training_mode == "SupCE":
model = SupResNet(arch=args.arch, num_classes=args.num_classes).to(device)
else:
raise ValueError("training mode not supported")
# load feature extractor on gpu
model.encoder = torch.nn.DataParallel(model.encoder).to(device)
# Dataloader
train_loader, test_loader, _ = data.__dict__[args.dataset](
args.data_dir,
mode="ssl" if args.training_mode in ["SimCLR", "SupCon"] else "org",
normalize=args.normalize,
size=args.size,
batch_size=args.batch_size,
)
criterion = (
SupConLoss(temperature=args.temperature).cuda()
if args.training_mode in ["SimCLR", "SupCon"]
else nn.CrossEntropyLoss().cuda()
)
optimizer = torch.optim.SGD(
model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
# select training and validation methods
trainer = (
trainers.ssl
if args.training_mode in ["SimCLR", "SupCon"]
else trainers.supervised
)
val = knn if args.training_mode in ["SimCLR", "SupCon"] else baseeval
# warmup
if args.warmup:
wamrup_epochs = 10
print(f"Warmup training for {wamrup_epochs} epochs")
warmup_lr_scheduler = torch.optim.lr_scheduler.CyclicLR(
optimizer,
base_lr=0.01,
max_lr=args.lr,
step_size_up=wamrup_epochs * len(train_loader),
)
for epoch in range(wamrup_epochs):
trainer(
model,
device,
train_loader,
criterion,
optimizer,
warmup_lr_scheduler,
epoch,
args,
)
best_prec1 = 0
for p in optimizer.param_groups:
p["lr"] = args.lr
p["initial_lr"] = args.lr
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, args.epochs * len(train_loader), 1e-4
)
for epoch in range(0, args.epochs):
trainer(
model, device, train_loader, criterion, optimizer, lr_scheduler, epoch, args
)
prec1, _ = val(model, device, test_loader, criterion, args, epoch)
# remember best accuracy and save checkpoint
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
d = {
"epoch": epoch + 1,
"arch": args.arch,
"state_dict": model.state_dict(),
"best_prec1": best_prec1,
"optimizer": optimizer.state_dict(),
}
save_checkpoint(
d,
is_best,
os.path.join(result_sub_dir, "checkpoint"),
)
if not (epoch + 1) % args.save_freq:
save_checkpoint(
d,
is_best,
os.path.join(result_sub_dir, "checkpoint"),
filename=f"checkpoint_{epoch+1}.pth.tar",
)
logger.info(
f"Epoch {epoch}, validation accuracy {prec1}, best_prec {best_prec1}"
)
# clone results to latest subdir (sync after every epoch)
clone_results_to_latest_subdir(
result_sub_dir, os.path.join(result_main_dir, "latest_exp")
)
if __name__ == "__main__":
main()