-
Notifications
You must be signed in to change notification settings - Fork 6
/
msdistgan_mnist.py
executable file
·178 lines (156 loc) · 6.69 KB
/
msdistgan_mnist.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
'''
************************************************************************
Implementation of SS/MS-DistGAN model by the authors of the paper:
"Self-supervised GAN: Analysis and Improvement with Multi-class Minimax
Game", NeurIPS 2019.
************************************************************************
'''
import os, sys
import numpy as np
import argparse
from msdistgan import MSDistGAN
from modules.dataset import Dataset
from modules.eval import compute_fid_score
if __name__ == '__main__':
'''
********************************************************************
* Command-line arguments
********************************************************************
'''
parser = argparse.ArgumentParser()
parser.add_argument('--gpu_id', type=int, default=0, help='The ID of the specified GPU')
parser.add_argument('--is_train', type=int, default=1, help='train: 1, test: 0')
parser.add_argument('--out_dir', type=str, default='./output/', help='The output path of the program')
parser.add_argument('--data_source', type=str, default='./data/mnist/', help='The dataset path to store (if data is unavailable, it is downloaded automatically)')
parser.add_argument('--ss_task', type=int, default=2, help='0: no SS, 1: SS task, 2: MS task (self-supervised task)')
parser.add_argument('--n_steps', type=int, default=100000, help='The number of training iterations')
parser.add_argument('--noise_dim', type=int, default=100, help='The dimension of latent noise')
parser.add_argument('--batch_size', type=int, default=64, help='Mini-batch size')
opt = parser.parse_args()
'''
********************************************************************
* Database and outputs
********************************************************************
'''
db_name = 'mnist'
out_dir = opt.out_dir
data_source = opt.data_source
is_train = opt.is_train
'''
********************************************************************
* Network architectures and objective losses
********************************************************************
'''
'''
To apply SS and SS into Dist-GAN model: Refer to Dist-GAN paper
for more details: https://arxiv.org/abs/1803.08887
'''
model = 'distgan'
'''
network architecture supports: 'dcgan'
'''
nnet_type = 'dcgan'
'''
objective loss type supports: 'log'
'''
loss_type = 'log'
'''
0: no use ss/ms task (original dist-gan)
1: the original self-supervised task (SS task)
2: the multi-class minimax self-supervised task (MS task)
To select suggested @lambda_d and @lambda_g for SS or MS tasks
'''
ss_task = opt.ss_task
'''
Selected pamraeters for SS and MS tasks
@lambda_d: SS/MS for discriminator learning
@lambda_g: SS/MS for generator learning
'''
if nnet_type == 'dcgan':
if ss_task == 1:
lambda_d = 1.0
lambda_g = 0.0
elif ss_task == 2:
lambda_d = 1.0
lambda_g = 0.1
else:
lambda_d = 0.0
lambda_g = 0.0
else:
print('[msdistgan_mnist.py -- __main__] nnet_type = %s is not supported.' % (nnet_type))
exit()
'''
********************************************************************
* Training, network architectures and model parameters
********************************************************************
'''
n_steps = opt.n_steps # the number of iterations
noise_dim = opt.noise_dim # the noise dimension
'''
The dimension of feature size for original dist-gan model.
If you're using our pre-defined datasets, keep it!
dcgan: 4096
Otherwise, adapt to new feature_dim for your new dataset.
'''
feature_dim = 4096.
'''
The unit dimensions for network architectures.
@df_dim: feature map unit for discriminator.
@gf_dim: feature map unit for generator.
@ef_dim: feature map unit for encoder.
@lr: learning rate
@beta1, beta2 parameters for Adam optimizer
'''
df_dim = 64
gf_dim = 64
ef_dim = 64
lr = 2e-4
beta1 = 0.5
beta2 = 0.9
batch_size = opt.batch_size # bach size for each iteration
'''
The fixed parameters for original Dist-GAN model. Refer to Dist-GAN
paper for more details: https://arxiv.org/abs/1803.08887
'''
lambda_p = 0.5 # gradient-penalty term
lambda_r = 1.0 # data-latent distance term
lambda_w = np.sqrt(noise_dim * 1.0/feature_dim)
'''
********************************************************************
* Training and testing
********************************************************************
'''
ext_name = 'sstask_%d_ld_%.02f_lg_%.02f_batch_%d_niters_%d' \
% (ss_task, lambda_d, lambda_g , batch_size, n_steps)
#output dir
out_dir = os.path.join(out_dir, db_name + '_' + model + '_' \
+ nnet_type + '_' \
+ loss_type + '_' \
+ ext_name, db_name)
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# setup dataset
dataset = Dataset(name=db_name, source=data_source, \
batch_size=batch_size)
# setup gan model and train
msdistgan = MSDistGAN(model=model, \
is_train = is_train, \
ss_task = ss_task, \
loss_type = loss_type, \
lambda_p = lambda_p, \
lambda_r = lambda_r, \
lambda_w = lambda_w, \
lambda_d = lambda_d, \
lambda_g = lambda_g, \
noise_dim = noise_dim, \
lr = lr, \
beta1 = beta1, \
beta2 = beta2, \
nnet_type = nnet_type, \
df_dim = df_dim, \
gf_dim = gf_dim, \
ef_dim = ef_dim, \
dataset=dataset, \
n_steps = n_steps, \
out_dir=out_dir)
msdistgan.train()