-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMAU_run.py
152 lines (130 loc) · 6.15 KB
/
MAU_run.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
import os
import argparse
import numpy as np
from core.data_provider import datasets_factory
from core.models.model_factory import Model
import core.trainer as trainer
import pynvml
pynvml.nvmlInit()
# -----------------------------------------------------------------------------
parser = argparse.ArgumentParser(description='MAU')
parser.add_argument('--dataset', type=str, default='mnist')
parser.add_argument('--is_train', type=str, default='False', required=True)
args_main = parser.parse_args()
args_main.tied = True
if args_main.is_train == 'True':
from configs.mnist_train_configs import configs
else:
from configs.mnist_configs import configs
parser = configs()
parser.add_argument('--device', type=str, default='cuda')
args = parser.parse_args()
args.tied = True
def schedule_sampling(eta, itr, channel, batch_size):
zeros = np.zeros((batch_size,
args.total_length - args.input_length - 1,
args.img_height // args.patch_size,
args.img_width // args.patch_size,
args.patch_size ** 2 * channel))
if not args.scheduled_sampling:
return 0.0, zeros
if itr < args.sampling_stop_iter:
eta -= args.sampling_changing_rate
else:
eta = 0.0
print('eta: ', eta)
random_flip = np.random.random_sample(
(batch_size, args.total_length - args.input_length - 1))
true_token = (random_flip < eta)
ones = np.ones((args.img_height // args.patch_size,
args.img_width // args.patch_size,
args.patch_size ** 2 * channel))
zeros = np.zeros((args.img_height // args.patch_size,
args.img_width // args.patch_size,
args.patch_size ** 2 * channel))
real_input_flag = []
for i in range(batch_size):
for j in range(args.total_length - args.input_length - 1):
if true_token[i, j]:
real_input_flag.append(ones)
else:
real_input_flag.append(zeros)
real_input_flag = np.array(real_input_flag)
real_input_flag = np.reshape(real_input_flag,
(batch_size,
args.total_length - args.input_length - 1,
args.img_height // args.patch_size,
args.img_width // args.patch_size,
args.patch_size ** 2 * channel))
return eta, real_input_flag
def train_wrapper(model):
begin = 0
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
meminfo_begin = pynvml.nvmlDeviceGetMemoryInfo(handle)
if args.pretrained_model:
model.load(args.pretrained_model)
begin = int(args.pretrained_model.split('-')[-1])
train_input_handle = datasets_factory.data_provider(configs=args,
data_train_path=args.data_train_path,
dataset=args.dataset,
data_test_path=args.data_val_path,
batch_size=args.batch_size,
is_training=True,
is_shuffle=True)
val_input_handle = datasets_factory.data_provider(configs=args,
data_train_path=args.data_train_path,
dataset=args.dataset,
data_test_path=args.data_val_path,
batch_size=args.batch_size,
is_training=False,
is_shuffle=False)
eta = args.sampling_start_value
eta -= (begin * args.sampling_changing_rate)
itr = begin
# real_input_flag = {}
for epoch in range(0, args.max_epoches):
if itr > args.max_iterations:
break
for ims in train_input_handle:
if itr > args.max_iterations:
break
batch_size = ims.shape[0]
eta, real_input_flag = schedule_sampling(eta, itr, args.img_channel, batch_size)
if itr % args.test_interval == 0:
print('Validate:')
trainer.test(model, val_input_handle, args, itr)
trainer.train(model, ims, real_input_flag, args, itr)
if itr % args.snapshot_interval == 0 and itr > begin:
model.save(itr)
itr += 1
meminfo_end = pynvml.nvmlDeviceGetMemoryInfo(handle)
print("GPU memory:%dM" % ((meminfo_end.used - meminfo_begin.used) / (1024 ** 2)))
def test_wrapper(model):
model.load(args.pretrained_model)
test_input_handle = datasets_factory.data_provider(configs=args,
data_train_path=args.data_train_path,
dataset=args.dataset,
data_test_path=args.data_test_path,
batch_size=args.batch_size,
is_training=False,
is_shuffle=False)
itr = 1
for i in range(itr):
trainer.test(model, test_input_handle, args, itr)
if __name__ == '__main__':
print('Initializing models')
if args.is_training == 'True':
args.is_training = True
else:
args.is_training = False
model = Model(args)
if args.is_training:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
if not os.path.exists(args.gen_frm_dir):
os.makedirs(args.gen_frm_dir)
train_wrapper(model)
else:
if not os.path.exists(args.gen_frm_dir):
os.makedirs(args.gen_frm_dir)
test_wrapper(model)