-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
99 lines (91 loc) · 4.9 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
from __future__ import print_function
import argparse
import torch
from CETL import Cetl
import os
# Training settings
parser = argparse.ArgumentParser(description='PyTorch CETL Implementation')
parser.add_argument('--all_use', type=str, default='no', metavar='N',
help='use all training data? in usps adaptation')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--checkpoint_dir', type=str, default='checkpoint', metavar='N',
help='source only or not')
parser.add_argument('--eval_only', action='store_true', default=False,
help='evaluation only option')
parser.add_argument('--lr', type=float, default=0.0002, metavar='LR',
help='learning rate (default: 0.0002)')
parser.add_argument('--max_epoch', type=int, default=200, metavar='N',
help='how many epochs')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--num_k', type=int, default=4, metavar='N',
help='hyper paremeter for generator update')
parser.add_argument('--one_step', action='store_true', default=False,
help='one step training with gradient reversal layer')
parser.add_argument('--optimizer', type=str, default='adam', metavar='N', help='which optimizer')
parser.add_argument('--resume_epoch', type=int, default=100, metavar='N',
help='epoch to resume')
parser.add_argument('--save_epoch', type=int, default=10, metavar='N',
help='when to restore the model')
parser.add_argument('--save_model', action='store_true', default=False,
help='save_model or not')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--source', type=str, default='svhn', metavar='N',
help='source dataset')
parser.add_argument('--target', type=str, default='mnist', metavar='N', help='target dataset')
parser.add_argument('--use_abs_diff', action='store_true', default=False,
help='use absolute difference value as a measurement')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
#print(args)
def main():
# if not args.one_step:
solver = Cetl(args, source=args.source, target=args.target, learning_rate=args.lr, batch_size=args.batch_size,
optimizer=args.optimizer, num_k=args.num_k, all_use=args.all_use,
checkpoint_dir=args.checkpoint_dir,
save_epoch=args.save_epoch)
record_num = 0
if args.source == 'usps' or args.target == 'usps':
record_train = 'record/%s_%s_k_%s_alluse_%s_onestep_%s_%s.txt' % (
args.source, args.target, args.num_k, args.all_use, args.one_step, record_num)
record_test = 'record/%s_%s_k_%s_alluse_%s_onestep_%s_%s_test.txt' % (
args.source, args.target, args.num_k, args.all_use, args.one_step, record_num)
while os.path.exists(record_train):
record_num += 1
record_train = 'record/%s_%s_k_%s_alluse_%s_onestep_%s_%s.txt' % (
args.source, args.target, args.num_k, args.all_use, args.one_step, record_num)
record_test = 'record/%s_%s_k_%s_alluse_%s_onestep_%s_%s_test.txt' % (
args.source, args.target, args.num_k, args.all_use, args.one_step, record_num)
else:
record_train = 'record/%s_%s_k_%s_onestep_%s_%s.txt' % (
args.source, args.target, args.num_k, args.one_step, record_num)
record_test = 'record/%s_%s_k_%s_onestep_%s_%s_test.txt' % (
args.source, args.target, args.num_k, args.one_step, record_num)
while os.path.exists(record_train):
record_num += 1
record_train = 'record/%s_%s_k_%s_onestep_%s_%s.txt' % (
args.source, args.target, args.num_k, args.one_step, record_num)
record_test = 'record/%s_%s_k_%s_onestep_%s_%s_test.txt' % (
args.source, args.target, args.num_k, args.one_step, record_num)
if not os.path.exists(args.checkpoint_dir):
os.mkdir(args.checkpoint_dir)
if not os.path.exists('record'):
os.mkdir('record')
if args.eval_only:
solver.test(0)
else:
count = 0
for t in range(args.max_epoch):
num = solver.train(t, record_file=record_train)
count += num
if t % 1 == 0:
solver.test(t, record_file=record_test, save_model=args.save_model)
if count >= 20000:
break
if __name__ == '__main__':
main()