forked from thuml/CDAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_uspsmnist.py
160 lines (148 loc) · 7.21 KB
/
train_uspsmnist.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
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from data_list import ImageList
import os
from torch.autograd import Variable
import loss as loss_func
import numpy as np
import network
def train(args, model, ad_net, random_layer, train_loader, train_loader1, optimizer, optimizer_ad, epoch, start_epoch, method):
model.train()
len_source = len(train_loader)
len_target = len(train_loader1)
if len_source > len_target:
num_iter = len_source
else:
num_iter = len_target
for batch_idx in range(num_iter):
if batch_idx % len_source == 0:
iter_source = iter(train_loader)
if batch_idx % len_target == 0:
iter_target = iter(train_loader1)
data_source, label_source = next(iter_source)
data_source, label_source = data_source.cuda(), label_source.cuda()
data_target, label_target = next(iter_target)
data_target = data_target.cuda()
optimizer.zero_grad()
optimizer_ad.zero_grad()
feature, output = model(torch.cat((data_source, data_target), 0))
loss = nn.CrossEntropyLoss()(output.narrow(0, 0, data_source.size(0)), label_source)
softmax_output = nn.Softmax(dim=1)(output)
if epoch > start_epoch:
if method == 'CDAN-E':
entropy = loss_func.Entropy(softmax_output)
loss += loss_func.CDAN([feature, softmax_output], ad_net, entropy, network.calc_coeff(num_iter*(epoch-start_epoch)+batch_idx), random_layer)
elif method == 'CDAN':
loss += loss_func.CDAN([feature, softmax_output], ad_net, None, None, random_layer)
elif method == 'DANN':
loss += loss_func.DANN(feature, ad_net)
else:
raise ValueError('Method cannot be recognized.')
loss.backward()
optimizer.step()
if epoch > start_epoch:
optimizer_ad.step()
if (batch_idx+epoch*num_iter) % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx*args.batch_size, num_iter*args.batch_size,
100. * batch_idx / num_iter, loss.item()))
def test(args, model, test_loader):
model.eval()
test_loss = 0
correct = 0
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
feature, output = model(data)
test_loss += nn.CrossEntropyLoss()(output, target).item()
pred = output.data.cpu().max(1, keepdim=True)[1]
correct += pred.eq(target.data.cpu().view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def main():
# Training settings
parser = argparse.ArgumentParser(description='CDAN USPS MNIST')
parser.add_argument('method', type=str, default='CDAN-E', choices=['CDAN', 'CDAN-E', 'DANN'])
parser.add_argument('--task', default='USPS2MNIST', help='task to perform')
parser.add_argument('--batch_size', type=int, default=64,
help='input batch size for training (default: 64)')
parser.add_argument('--test_batch_size', type=int, default=1000,
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
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.5, metavar='M',
help='SGD momentum (default: 0.5)')
parser.add_argument('--gpu_id', type=str,
help='cuda device id')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log_interval', type=int, default=10,
help='how many batches to wait before logging training status')
parser.add_argument('--random', type=bool, default=False,
help='whether to use random')
args = parser.parse_args()
torch.manual_seed(args.seed)
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
if args.task == 'USPS2MNIST':
source_list = '../data/usps2mnist/usps_train.txt'
target_list = '../data/usps2mnist/mnist_train.txt'
test_list = '../data/usps2mnist/mnist_test.txt'
start_epoch = 1
decay_epoch = 6
elif args.task == 'MNIST2USPS':
source_list = '../data/usps2mnist/mnist_train.txt'
target_list = '../data/usps2mnist/usps_train.txt'
test_list = '../data/usps2mnist/usps_test.txt'
start_epoch = 1
decay_epoch = 5
else:
raise Exception('task cannot be recognized!')
train_loader = torch.utils.data.DataLoader(
ImageList(open(source_list).readlines(), transform=transforms.Compose([
transforms.Resize((28,28)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
]), mode='L'),
batch_size=args.batch_size, shuffle=True, num_workers=1, drop_last=True)
train_loader1 = torch.utils.data.DataLoader(
ImageList(open(target_list).readlines(), transform=transforms.Compose([
transforms.Resize((28,28)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
]), mode='L'),
batch_size=args.batch_size, shuffle=True, num_workers=1, drop_last=True)
test_loader = torch.utils.data.DataLoader(
ImageList(open(test_list).readlines(), transform=transforms.Compose([
transforms.Resize((28,28)),
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
]), mode='L'),
batch_size=args.test_batch_size, shuffle=True, num_workers=1)
model = network.LeNet()
model = model.cuda()
class_num = 10
if args.random:
random_layer = network.RandomLayer([model.output_num(), class_num], 500)
ad_net = network.AdversarialNetwork(500, 500)
random_layer.cuda()
else:
random_layer = None
ad_net = network.AdversarialNetwork(model.output_num() * class_num, 500)
ad_net = ad_net.cuda()
optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=0.0005, momentum=0.9)
optimizer_ad = optim.SGD(ad_net.parameters(), lr=args.lr, weight_decay=0.0005, momentum=0.9)
for epoch in range(1, args.epochs + 1):
if epoch % decay_epoch == 0:
for param_group in optimizer.param_groups:
param_group["lr"] = param_group["lr"] * 0.5
train(args, model, ad_net, random_layer, train_loader, train_loader1, optimizer, optimizer_ad, epoch, start_epoch, args.method)
test(args, model, test_loader)
if __name__ == '__main__':
main()