-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneration_builder_with_matchingclassifier.py
323 lines (266 loc) · 19.5 KB
/
generation_builder_with_matchingclassifier.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import utils.interpolations as interpolations
import numpy as np
import tqdm
from utils.storage import save_statistics, build_experiment_folder
from tensorflow.contrib import slim
from dagan_networks_wgan_with_matchingclassifier import *
from utils.sampling_with_matchingclassifier import sample_generator, sample_two_dimensions_generator
import time
def isNaN(num):
return num != num
class ExperimentBuilder(object):
def __init__(self, args, data):
tf.reset_default_graph()
self.continue_from_epoch = args.continue_from_epoch
self.experiment_name = args.experiment_title
self.saved_models_filepath, self.log_path, self.save_image_path = build_experiment_folder(self.experiment_name)
self.num_gpus = args.num_of_gpus
self.batch_size = args.batch_size
# self.support_number = args.support_number
self.selected_classes = args.selected_classes
gen_depth_per_layer = args.generator_inner_layers
discr_depth_per_layer = args.discriminator_inner_layers
self.z_dim = args.z_dim
self.num_generations = args.num_generations
self.dropout_rate_value = args.dropout_rate_value
self.data = data
self.reverse_channels = False
# generator_layers = [64, 64, 128, 128, 256, 256]
# gen_inner_layers = [gen_depth_per_layer, gen_depth_per_layer, gen_depth_per_layer, gen_depth_per_layer,
# gen_depth_per_layer, gen_depth_per_layer]
# generator_layer_padding = ["SAME", "SAME", "SAME", "SAME", "SAME", "SAME"]
generator_layers = [64, 64, 128, 128]
gen_inner_layers = [gen_depth_per_layer, gen_depth_per_layer,
gen_depth_per_layer, gen_depth_per_layer]
generator_layer_padding = ["SAME", "SAME", "SAME", "SAME"]
discriminator_layers = [64, 64, 128, 128]
discr_inner_layers = [discr_depth_per_layer, discr_depth_per_layer, discr_depth_per_layer,
discr_depth_per_layer]
image_height = data.image_height
image_width = data.image_width
image_channel = data.image_channel
self.classes = tf.placeholder(tf.int32)
self.selected_classes = tf.placeholder(tf.int32)
self.support_number = tf.placeholder(tf.int32)
#### [self.input_x_i, self.input_y_i, self.input_global_y_i] --> [images, few shot label, global label]
## batch: [self.input_x_i, self.input_y_i, self.input_global_y_i]
## support: self.input_x_j, self.input_y_j, self.input_global_y_j]
## the input of discriminator: [self.input_x_j_selected, self.input_global_y_j_selected]
self.input_x_i = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size, image_height, image_width,
image_channel], 'batch')
self.input_y_i = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size, self.data.selected_classes],
'y_inputs_bacth')
self.input_global_y_i = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size, self.data.training_classes],
'y_inputs_bacth_global')
self.input_x_j = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size,
self.data.selected_classes * self.data.support_number,
image_height, image_width,
image_channel], 'support')
self.input_y_j = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size,
self.data.selected_classes * self.data.support_number,
self.data.selected_classes], 'y_inputs_support')
self.input_global_y_j = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size,
self.data.selected_classes * self.data.support_number,
self.data.training_classes], 'y_inputs_support_global')
self.input_x_j_selected = tf.placeholder(tf.float32, [self.num_gpus, self.batch_size, image_height, image_width,
image_channel], 'support_discriminator')
self.input_global_y_j_selected = tf.placeholder(tf.float32,
[self.num_gpus, self.batch_size, self.data.training_classes],
'y_inputs_support_discriminator')
# self.z_input = tf.placeholder(tf.float32, [self.batch_size*self.data.selected_classes, self.z_dim], 'z-input')
# self.z_input_2 = tf.placeholder(tf.float32, [self.batch_size*self.data.selected_classes, self.z_dim], 'z-input_2')
self.z_input = tf.placeholder(tf.float32, [self.batch_size, self.z_dim], 'z-input')
self.z_input_2 = tf.placeholder(tf.float32, [self.batch_size, self.z_dim], 'z-input_2')
self.training_phase = tf.placeholder(tf.bool, name='training-flag')
self.z1z2_training = tf.placeholder(tf.bool, name='z1z2_training-flag')
self.random_rotate = tf.placeholder(tf.bool, name='rotation-flag')
self.dropout_rate = tf.placeholder(tf.float32, name='dropout-prob')
self.is_z2 = args.is_z2
self.is_z2_vae = args.is_z2_vae
self.matching = args.matching
self.fce = args.fce
self.full_context_unroll_k = args.full_context_unroll_k
self.average_per_class_embeddings = args.average_per_class_embeddings
self.restore_path = args.restore_path
self.is_z2 = args.is_z2
self.is_z2_vae = args.is_z2_vae
self.loss_G = args.loss_G
self.loss_D = args.loss_D
self.loss_CLA = args.loss_CLA
self.loss_FSL = args.loss_FSL
self.loss_KL = args.loss_KL
self.loss_recons_B = args.loss_recons_B
self.loss_matching_G = args.loss_matching_G
self.loss_matching_D = args.loss_matching_D
self.loss_sim = args.loss_sim
self.strategy = args.strategy
#### training/validation/testin
time_1 = time.time()
dagan = DAGAN(batch_size=self.batch_size, input_x_i=self.input_x_i, input_x_j=self.input_x_j,
input_y_i=self.input_y_i, input_y_j=self.input_y_j, input_global_y_i=self.input_global_y_i,
input_global_y_j=self.input_global_y_j,
input_x_j_selected=self.input_x_j_selected,
input_global_y_j_selected=self.input_global_y_j_selected, \
selected_classes=self.data.selected_classes, support_num=self.data.support_number,
classes=self.data.training_classes,
dropout_rate=self.dropout_rate, generator_layer_sizes=generator_layers,
generator_layer_padding=generator_layer_padding, num_channels=data.image_channel,
is_training=self.training_phase, augment=self.random_rotate,
discriminator_layer_sizes=discriminator_layers,
discr_inner_conv=discr_inner_layers, is_z2=self.is_z2, is_z2_vae=self.is_z2_vae,
gen_inner_conv=gen_inner_layers, num_gpus=self.num_gpus, z_dim=self.z_dim, z_inputs=self.z_input,
z_inputs_2=self.z_input_2,
use_wide_connections=args.use_wide_connections, fce=self.fce, matching=self.matching,
full_context_unroll_k=self.full_context_unroll_k,
average_per_class_embeddings=self.average_per_class_embeddings,
loss_G=self.loss_G, loss_D=self.loss_D, loss_KL=self.loss_KL, loss_recons_B=self.loss_recons_B,
loss_matching_G=self.loss_matching_G, loss_matching_D=self.loss_matching_D,
loss_CLA=self.loss_CLA, loss_FSL=self.loss_FSL, loss_sim=self.loss_sim,
z1z2_training=self.z1z2_training)
self.same_images = dagan.sample_same_images()
# self.summary, self.losses, self.accuracy, self.graph_ops = classifier.init_train()
self.total_train_batches = int(data.training_data_size / (self.batch_size * self.num_gpus))
self.total_val_batches = int(data.validation_data_size / (self.batch_size * self.num_gpus))
self.total_test_batches = int(data.testing_data_size / (self.batch_size * self.num_gpus))
self.total_gen_batches = int(data.testing_data_size / (self.batch_size * self.num_gpus))
self.init = tf.global_variables_initializer()
time_2 = time.time()
# print('time for constructing graph:',time_2 - time_1)
self.tensorboard_update_interval = int(self.total_train_batches / 1 / self.num_gpus)
self.total_epochs = 800
# if self.continue_from_epoch == -1:
# save_statistics(self.log_path, ['epoch', 'total_d_train_loss_mean', 'total_d_val_loss_mean',
# 'total_d_train_loss_std', 'total_d_val_loss_std',
# 'total_g_train_loss_mean', 'total_g_val_loss_mean',
# 'total_g_train_loss_std', 'total_g_val_loss_std'], create=True)
def run_experiment(self):
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
# with tf.Session() as sess:
time_4 = time.time()
sess.run(self.init)
print('time for initializing global parameters:', time.time() - time_4)
# self.train_writer = tf.summary.FileWriter("{}/train_logs/".format(self.log_path),
# graph=tf.get_default_graph())
# self.validation_writer = tf.summary.FileWriter("{}/validation_logs/".format(self.log_path),
# graph=tf.get_default_graph())
log_name = "z2vae{}_z2{}_g{}_d{}_kl{}_cla{}_fzl{}_reconsB{}_matchingG{}_matchingD{}_sim{}_Net_batchsize{}_zdim{}".format(
self.is_z2_vae, self.is_z2, self.loss_G, self.loss_D, self.loss_KL, self.loss_CLA, self.loss_FSL,
self.loss_recons_B, self.loss_matching_G, self.loss_matching_D, self.loss_sim, self.batch_size,
self.z_dim)
self.train_writer = tf.summary.FileWriter("{}/train_logs/{}".format(self.log_path, log_name),
graph=sess.graph)
self.validation_writer = tf.summary.FileWriter("{}/validation_logs/{}".format(self.log_path, log_name),
graph=sess.graph)
self.train_saver = tf.train.Saver()
self.val_saver = tf.train.Saver()
# variable_names = [v.name for v in tf.trainable_variables()]
# print(variable_names)
start_from_epoch = 0
if self.continue_from_epoch != -1:
start_from_epoch = self.continue_from_epoch
# checkpoint = "{}train_saved_model_{}_{}.ckpt".format(self.saved_models_filepath, self.experiment_name, self.continue_from_epoch)
checkpoint = self.restore_path
variables_to_restore = []
for var in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
# print(var)
variables_to_restore.append(var)
tf.logging.info('Fine-tuning from %s' % checkpoint)
fine_tune = slim.assign_from_checkpoint_fn(
checkpoint,
variables_to_restore,
ignore_missing_vars=True)
fine_tune(sess)
self.iter_done = 0
self.disc_iter = 1
self.gen_iter = 1
best_d_val_loss = np.inf
### z preprocess
self.spherical_interpolation = False
if self.spherical_interpolation:
self.z_vectors = interpolations.create_mine_grid(rows=1, cols=self.num_generations, dim=self.z_dim,
space=3, anchors=None, spherical=True, gaussian=True)
self.z_vectors_2 = interpolations.create_mine_grid(rows=1, cols=self.num_generations, dim=self.z_dim,
space=3, anchors=None, spherical=True, gaussian=True)
self.z_2d_vectors = interpolations.create_mine_grid(rows=self.num_generations,
cols=self.num_generations,
dim=100, space=3, anchors=None,
spherical=True, gaussian=True)
self.z_2d_vectors_2 = interpolations.create_mine_grid(rows=self.num_generations,
cols=self.num_generations,
dim=100, space=3, anchors=None,
spherical=True, gaussian=True)
else:
self.z_vectors = np.random.normal(size=(self.num_generations, self.z_dim))
self.z_vectors_2 = np.random.normal(size=(self.num_generations, self.z_dim))
self.z_2d_vectors = np.random.normal(size=(self.num_generations, self.z_dim))
self.z_2d_vectors_2 = np.random.normal(size=(self.num_generations, self.z_dim))
self.support_vector = [x for x in range(10)]
### training with train set with parameter update, validation without training, epoch
image_name = "z2vae{}_z2{}_g{}_d{}_kl{}_cla{}_fzl{}_reconsB{}_matchingG{}_matchingD{}_sim{}_Net_batchsize{}_zdim{}".format(
self.is_z2_vae, self.is_z2, self.loss_G, self.loss_D, self.loss_KL, self.loss_CLA, self.loss_FSL,
self.loss_recons_B, self.loss_matching_G, self.loss_matching_D, self.loss_sim, self.batch_size,
self.z_dim)
if self.continue_from_epoch >= 0:
# print('starting sampling')
similarities_list = []
f_encode_z_list = []
matching_feature_list = []
with tqdm.tqdm(total=self.total_gen_batches) as pbar_samp:
for i in range(self.total_gen_batches):
x_test_i_selected_classes, x_test_j, y_test_i_selected_classes, y_test_j, y_global_test_i_selected_classes, y_global_test_j = self.data.get_test_batch()
# np.random.seed(i)
# self.z_vectors = np.random.normal(size=(self.num_generations, self.z_dim))
# self.z_vectors_2 = np.random.normal(size=(self.num_generations, self.z_dim))
if i >= 0:
for j in range(1):
before_sample = time.time()
x_test_i = x_test_i_selected_classes[:, :, j, :, :, :]
y_test_i = y_test_i_selected_classes[:, :, j, :]
y_global_test_i = y_global_test_i_selected_classes[:, :, j, :]
support_index = int(np.random.choice(self.data.support_number, size=1))
x_test_j_selected = x_test_j[:, :, support_index, :, :, :]
y_test_j_selected = y_test_j[:, :, support_index, :]
y_global_test_j_selected = y_global_test_j[:, :, support_index, :]
_, _, _ = sample_generator(num_generations=self.num_generations,
sess=sess,
same_images=self.same_images,
input_a=self.input_x_i,
input_b=self.input_x_j,
input_y_i=self.input_y_i,
input_y_j=self.input_y_j,
input_global_y_i=self.input_global_y_i,
input_global_y_j=self.input_global_y_j,
classes=self.classes,
classes_selected=self.selected_classes,
number_support=self.support_number,
z_input=self.z_input,
z_input_2=self.z_input_2,
# selected_global_x_j = self.input_x_j_selected,
# selected_global_y_j=self.input_global_y_j_selected,
# conditional_inputs=x_test_i,
# y_input_i = y_test_i,
conditional_inputs=x_test_j_selected,
y_input_i=y_test_j_selected,
support_input=x_test_j,
y_input_j=y_test_j,
y_global_input_i=y_global_test_i,
y_global_input_j=y_global_test_j,
classes_number=self.data.training_classes,
selected_classes=self.data.selected_classes,
support_number=self.data.support_number,
# input_global_x_j_selected = x_test_j_selected,
# input_global_y_j_selected = y_global_test_j_selected,
z_vectors=self.z_vectors,
z_vectors_2=self.z_vectors_2,
data=self.data,
batch_size=self.batch_size,
file_name="{}/{}_{}_{}.png".format(self.save_image_path,
image_name,
self.continue_from_epoch,
i),
dropout_rate=self.dropout_rate,
dropout_rate_value=self.dropout_rate_value,
training_phase=self.training_phase,
z1z2_training=self.z1z2_training,
is_training=False,
training_z1z2=False)