-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_CIFAR10.py
141 lines (112 loc) · 4.88 KB
/
train_CIFAR10.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
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm
import data_utils
import utils
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=5)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3)
self.conv3 = nn.Conv2d(128, 128, kernel_size=3)
self.fc1 = nn.Linear(128, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2(x), 2))
x = F.relu(F.max_pool2d(self.conv3(x), 2))
x = F.adaptive_avg_pool2d(x, 1)
x = x.view(-1, 128)
x = self.fc1(x)
return x
class UDALoss(nn.Module):
def __init__(self):
super(UDALoss, self).__init__()
def forward(self, model, x, x_h):
batchsize = x.shape[0]
with torch.no_grad():
pred_x = F.softmax(model(x), dim=1)
pred_x_h = F.log_softmax(model(x_h), dim=1)
lds = F.kl_div(pred_x_h, pred_x, None, None, reduction='sum') / batchsize
return lds
def train(args, model, device, data_iterators, optimizer):
model.train()
for i in tqdm(range(args.iters)):
# reset
if i % args.log_interval == 0:
ce_losses = utils.AverageMeter()
vat_losses = utils.AverageMeter()
prec1 = utils.AverageMeter()
x_l, y_l = next(data_iterators['labeled'])
x_ul, x_ul_da, _ = next(data_iterators['unlabeled'])
x_l, y_l = x_l.to(device), y_l.to(device)
x_ul = x_ul.to(device)
x_ul_da = x_ul_da.to(device)
optimizer.zero_grad()
uda_loss = UDALoss()
cross_entropy = nn.CrossEntropyLoss()
lds = uda_loss(model, x_ul, x_ul_da)
output = model(x_l)
classification_loss = cross_entropy(output, y_l)
loss = classification_loss + args.alpha * lds
loss.backward()
optimizer.step()
acc = utils.accuracy(output, y_l)
ce_losses.update(classification_loss.item(), x_l.shape[0])
prec1.update(acc.item(), x_l.shape[0])
if i % args.log_interval == 0:
print('\nIteration: {}\t'.format(i),
'CrossEntropyLoss {:.4f} ({:.4f})\t'.format(ce_losses.val, ce_losses.avg),
'Prec@1 {:.3f} ({:.3f})'.format(prec1.val, prec1.avg))
def test(model, device, data_iterators):
model.eval()
correct = 0
with torch.no_grad():
for x, y in tqdm(data_iterators['test']):
with torch.no_grad():
x, y = x.to(device), y.to(device)
outputs = model(x)
correct += torch.eq(outputs.max(dim=1)[1], y).detach().cpu().float().sum()
test_acc = correct / len(data_iterators['test'].dataset) * 100.
print('\nTest Accuracy: {:.4f}%\n'.format(test_acc))
def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--batch-size', type=int, default=32, metavar='N',
help='input batch size for training (default: 32)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--iters', type=int, default=10000, metavar='N',
help='number of iterations to train (default: 10000)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--alpha', type=float, default=1.0, metavar='ALPHA',
help='regularization coefficient (default: 0.01)')
parser.add_argument('--workers', type=int, default=8, metavar='W',
help='number of CPU')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=100, metavar='N',
help='how many batches to wait before logging training status')
args = parser.parse_args()
torch.manual_seed(args.seed)
np.random.seed(args.seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
data_iterators = data_utils.get_iters(
root_path='.',
l_batch_size=args.batch_size,
ul_batch_size=args.batch_size,
test_batch_size=args.test_batch_size,
workers=args.workers
)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
train(args, model, device, data_iterators, optimizer)
test(model, device, data_iterators)
if __name__ == '__main__':
main()