-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
200 lines (178 loc) · 7.97 KB
/
main.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
"""
Basic Usage:
python main.py --model <model_name> --model_config <path_to_json> --logdir <result_dir> ...
Please see config.py for other command line usage.
"""
import warnings
import torch
from torch import optim
from torchvision.datasets import CIFAR10
from torchvision import transforms
from utils import seed_experiment, to_device, cross_entropy_loss, compute_accuracy
from config import get_config_parser
import json
from mlp import MLP
from resnet18 import ResNet18
from mlpmixer import MLPMixer
from tqdm import tqdm
from torch.utils.data import DataLoader
import time
import os
def train(epoch, model, dataloader, optimizer, args):
model.train()
total_iters = 0
epoch_accuracy=0
epoch_loss=0
start_time = time.time()
for idx, batch in enumerate(dataloader):
batch = to_device(batch, args.device)
optimizer.zero_grad()
imgs, labels = batch
logits = model(imgs)
loss = cross_entropy_loss(logits, labels)
acc = compute_accuracy(logits, labels)
loss.backward()
optimizer.step()
epoch_accuracy += acc.item() / len(dataloader)
epoch_loss += loss.item() / len(dataloader)
total_iters += 1
if idx % args.print_every == 0:
tqdm.write(f"[TRAIN] Epoch: {epoch}, Iter: {idx}, Loss: {loss.item():.5f}")
tqdm.write(f"== [TRAIN] Epoch: {epoch}, Accuracy: {epoch_accuracy:.3f} ==>")
return epoch_loss, epoch_accuracy, time.time() - start_time
def evaluate(epoch, model, dataloader, args, mode="val"):
model.eval()
epoch_accuracy=0
epoch_loss=0
total_iters = 0
start_time = time.time()
with torch.no_grad():
for idx, batch in enumerate(dataloader):
batch = to_device(batch, args.device)
imgs, labels = batch
logits = model(imgs)
loss = cross_entropy_loss(logits, labels)
acc = compute_accuracy(logits, labels)
epoch_accuracy += acc.item() / len(dataloader)
epoch_loss += loss.item() / len(dataloader)
total_iters += 1
if idx % args.print_every == 0:
tqdm.write(
f"[{mode.upper()}] Epoch: {epoch}, Iter: {idx}, Loss: {loss.item():.5f}"
)
tqdm.write(
f"=== [{mode.upper()}] Epoch: {epoch}, Iter: {idx}, Accuracy: {epoch_accuracy:.3f} ===>"
)
return epoch_loss, epoch_accuracy, time.time() - start_time
if __name__ == "__main__":
parser = get_config_parser()
args = parser.parse_args()
# Check for the device
if (args.device == "cuda") and not torch.cuda.is_available():
warnings.warn(
"CUDA is not available, make that your environment is "
"running on GPU (e.g. in the Notebook Settings in Google Colab). "
'Forcing device="cpu".'
)
args.device = "cpu"
if args.device == "cpu":
warnings.warn(
"You are about to run on CPU, and might run out of memory "
"shortly. You can try setting batch_size=1 to reduce memory usage."
)
# Seed the experiment, for repeatability
seed_experiment(args.seed)
test_transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize([0.49139968, 0.48215841, 0.44653091], [0.24703223, 0.24348513, 0.26158784])
])
# For training, we add some augmentation. Networks are too powerful and would overfit.
train_transform = transforms.Compose([transforms.RandomHorizontalFlip(),
transforms.RandomResizedCrop((32,32),scale=(0.8,1.0),ratio=(0.9,1.1)),
transforms.ToTensor(),
transforms.Normalize([0.49139968, 0.48215841, 0.44653091], [0.24703223, 0.24348513, 0.26158784])
])
# Loading the training dataset. We need to split it into a training and validation part
# We need to do a little trick because the validation set should not use the augmentation.
train_dataset = CIFAR10(root='./data', train=True, transform=train_transform, download=True)
val_dataset = CIFAR10(root='./data', train=True, transform=test_transform, download=True)
train_set, _ = torch.utils.data.random_split(train_dataset, [45000, 5000])
_, val_set = torch.utils.data.random_split(val_dataset, [45000, 5000])
# Loading the test set
test_set = CIFAR10(root='./data', train=False, transform=test_transform, download=True)
# Load model
print(f'Build model {args.model.upper()}...')
if args.model_config is not None:
print(f'Loading model config from {args.model_config}')
with open(args.model_config) as f:
model_config = json.load(f)
else:
raise ValueError('Please provide a model config json')
print(f'########## {args.model.upper()} CONFIG ################')
for key, val in model_config.items():
print(f'{key}:\t{val}')
print('############################################')
model_cls = {'mlp': MLP, 'resnet18': ResNet18, 'mlpmixer': MLPMixer}[args.model]
model = model_cls(**model_config)
model.to(args.device)
# Optimizer
if args.optimizer == "adamw":
optimizer = optim.AdamW(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
elif args.optimizer == "adam":
optimizer = optim.Adam(model.parameters(), lr=args.lr)
elif args.optimizer == "sgd":
optimizer = optim.SGD(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
elif args.optimizer == "momentum":
optimizer = optim.SGD(
model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
)
print(
f"Initialized {args.model.upper()} model with {sum(p.numel() for p in model.parameters())} "
f"total parameters, of which {sum(p.numel() for p in model.parameters() if p.requires_grad)} are learnable."
)
train_losses, valid_losses = [], []
train_accs, valid_accs = [], []
train_times, valid_times = [], []
# We define a set of data loaders that we can use for various purposes later.
train_dataloader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True, drop_last=True, pin_memory=True, num_workers=4)
valid_dataloader = DataLoader(val_set, batch_size=args.batch_size, shuffle=False, drop_last=False, num_workers=4)
test_dataloader = DataLoader(test_set, batch_size=args.batch_size, shuffle=False, drop_last=False, num_workers=4)
for epoch in range(args.epochs):
tqdm.write(f"====== Epoch {epoch} ======>")
loss, acc, wall_time = train(epoch, model, train_dataloader, optimizer,args)
train_losses.append(loss)
train_accs.append(acc)
train_times.append(wall_time)
loss, acc, wall_time = evaluate(epoch, model, valid_dataloader,args)
valid_losses.append(loss)
valid_accs.append(acc)
valid_times.append(wall_time)
test_loss, test_acc, test_time = evaluate(
epoch, model, test_dataloader, args, mode="test"
)
print(f"===== Best validation Accuracy: {max(valid_accs):.3f} =====>")
# Save log if logdir provided
if args.logdir is not None:
print(f'Writing training logs to {args.logdir}...')
os.makedirs(args.logdir, exist_ok=True)
with open(os.path.join(args.logdir, 'results.json'), 'w') as f:
f.write(json.dumps(
{
"train_losses": train_losses,
"valid_losses": valid_losses,
"train_accs": train_accs,
"valid_accs": valid_accs,
"test_loss": test_loss,
"test_acc": test_acc
},
indent=4,
))
# Visualize
if args.visualize and args.model in ['resnet18', 'mlpmixer']:
model.visualize(args.logdir)