-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
133 lines (115 loc) · 4.33 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
from __future__ import print_function
import os
import random
import shutil
from tqdm import tqdm
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from PIL import Image
from config import args_wideresnet, args_preactresnet18
from utils import load_model, AverageMeter, accuracy
# Use CUDA
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
use_cuda = torch.cuda.is_available()
seed = 11037
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.deterministic = True
class MyDataset(torch.utils.data.Dataset):
def __init__(self, transform):
images = np.load('data.npy')
labels = np.load('label.npy')
assert labels.min() >= 0
assert images.dtype == np.uint8
assert images.shape[0] <= 50000
assert images.shape[1:] == (32, 32, 3)
self.images = [Image.fromarray(x) for x in images]
self.labels = labels / labels.sum(axis=1, keepdims=True) # normalize
self.labels = self.labels.astype(np.float32)
self.transform = transform
def __getitem__(self, index):
image, label = self.images[index], self.labels[index]
image = self.transform(image)
return image, label
def __len__(self):
return len(self.labels)
def cross_entropy(outputs, smooth_labels):
loss = torch.nn.KLDivLoss(reduction='batchmean')
return loss(F.log_softmax(outputs, dim=1), smooth_labels)
def main():
for arch in ['preactresnet18', 'wideresnet']:
if arch == 'wideresnet':
args = args_wideresnet
else:
args = args_preactresnet18
assert args['epochs'] <= 200
if args['batch_size'] > 256:
# force the batch_size to 256, and scaling the lr
args['optimizer_hyperparameters']['lr'] *= 256/args['batch_size']
args['batch_size'] = 256
# Data
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = MyDataset(transform=transform_train)
trainloader = data.DataLoader(trainset, batch_size=args['batch_size'], shuffle=True, num_workers=4)
# Model
model = load_model(arch)
best_acc = 0 # best test accuracy
optimizer = optim.__dict__[args['optimizer_name']](model.parameters(),
**args['optimizer_hyperparameters'])
if args['scheduler_name'] != None:
scheduler = torch.optim.lr_scheduler.__dict__[args['scheduler_name']](optimizer,
**args['scheduler_hyperparameters'])
model = model.cuda()
# Train and val
for epoch in tqdm(range(args['epochs'])):
train_loss, train_acc = train(trainloader, model, optimizer)
print(args)
print('acc: {}'.format(train_acc))
# save model
best_acc = max(train_acc, best_acc)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'acc': train_acc,
'best_acc': best_acc,
'optimizer' : optimizer.state_dict(),
}, arch=arch)
if args['scheduler_name'] != None:
scheduler.step()
print('Best acc:')
print(best_acc)
def train(trainloader, model, optimizer):
losses = AverageMeter()
accs = AverageMeter()
model.eval()
# switch to train mode
model.train()
for (inputs, soft_labels) in trainloader:
inputs, soft_labels = inputs.cuda(), soft_labels.cuda()
targets = soft_labels.argmax(dim=1)
outputs = model(inputs)
loss = cross_entropy(outputs, soft_labels)
acc = accuracy(outputs, targets)
optimizer.zero_grad()
loss.backward()
optimizer.step()
losses.update(loss.item(), inputs.size(0))
accs.update(acc[0].item(), inputs.size(0))
return losses.avg, accs.avg
def save_checkpoint(state, arch):
filepath = os.path.join(arch + '.pth.tar')
torch.save(state, filepath)
if __name__ == '__main__':
main()