-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_v2.py
685 lines (534 loc) · 26.8 KB
/
models_v2.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 7 14:37:09 2021
@author: vganapa1
"""
import tensorflow as tf
from tensorflow import keras
from helper_functions import InstanceNormalization
import numpy as np
from fractions import Fraction
def find_conv_output_dim(input_length, stride, kernel_size):
# finds output dimension for 'valid' padding
output_length = np.zeros([input_length,],dtype=np.int32)
output_length[0::stride]=1
output_length = output_length[:-kernel_size+1]
output_length = np.sum(output_length)
return output_length
def create_encode_net(image_x,
image_y,
image_x_r,
image_y_r,
num_leds,
num_feature_maps_vec,
batch_size,
num_blocks,
kernel_size,
stride_encode,
apply_norm, norm_type,
initializer,
dropout_prob,
intermediate_layers,
intermediate_kernel,
coords,
initial_repeats, # how many times I_m is repeated
encode_net_ind=0,
feature_maps_multiplier=2,
real_data=False,
):
num_feature_maps_vec = feature_maps_multiplier*np.array(num_feature_maps_vec)
input_Im = tf.keras.Input(shape=(image_x,image_y,1),
batch_size = batch_size, name='I_m')
input_Im_rescaled = tf.image.resize(input_Im, [image_x_r,image_y_r],
method='nearest',
preserve_aspect_ratio=False,
antialias=False, name='resize'
)
input_alpha = tf.keras.Input(shape=(num_leds,),
batch_size=batch_size,
name='alpha_sample')
if real_data:
input_coords_xm = tf.keras.Input(shape=(image_x,image_y),
batch_size = batch_size, name='coords_xm')
input_coords_ym = tf.keras.Input(shape=(image_x,image_y),
batch_size = batch_size, name='coords_ym')
input_coords_xm_rescaled = tf.expand_dims(input_coords_xm, -1)
input_coords_xm_rescaled = tf.image.resize(input_coords_xm_rescaled, [image_x_r,image_y_r],
method='nearest',
preserve_aspect_ratio=False,
antialias=False, name='resize'
)
input_coords_ym_rescaled = tf.expand_dims(input_coords_ym, -1)
input_coords_ym_rescaled = tf.image.resize(input_coords_ym_rescaled, [image_x_r,image_y_r],
method='nearest',
preserve_aspect_ratio=False,
antialias=False, name='resize'
)
coords = tf.concat((input_coords_xm_rescaled,input_coords_ym_rescaled), axis=-1)
# repeat input_Im
extra_repeats = feature_maps_multiplier - (initial_repeats+num_leds)%feature_maps_multiplier
input_Im_repeat = tf.repeat(input_Im_rescaled, initial_repeats+extra_repeats, axis=-1)
# repeat input_alpha
input_alpha_repeat = tf.expand_dims(input_alpha,axis=-2)
input_alpha_repeat = tf.expand_dims(input_alpha_repeat,axis=-2)
input_alpha_repeat = tf.repeat(input_alpha_repeat, image_x_r, axis=-3)
input_alpha_repeat = tf.repeat(input_alpha_repeat,image_y_r,axis=-2)
# combine input_Im and input_alpha
combined_input = tf.concat((input_Im_repeat,input_alpha_repeat), axis=-1)
if dropout_prob == 0:
apply_dropout = False
else:
apply_dropout = True
# want channel to be divisible by feature_maps_multiplier
repeats = Fraction(coords.shape[-1]/feature_maps_multiplier).denominator
coords_repeat = tf.repeat(coords,repeats,axis=-1)
output = tf.concat([combined_input,coords_repeat],axis=-1)
# Downsampling through the model
skips_val = []
skips_weight = []
skips_pixel_x = []
skips_pixel_y = []
skips_pixel_z = []
for i in range(num_blocks):
# intermediate layers, no residual connection
# conv with stride = 1 and padding = same
for l in range(intermediate_layers):
output = \
conv_block(output, # input
output.shape[-1], # output size channels
intermediate_kernel,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (1,1),
)
output_val, output_weight = tf.split(output, [output.shape[-1]//2, output.shape[-1]//2], axis=-1, num=None, name='split')
skips_val.append(output_val)
skips_weight.append(output_weight)
skips_pixel_x.append(output.shape[1])
skips_pixel_y.append(output.shape[2])
skips_pixel_z.append(output.shape[3])
output = \
conv_block(output, # input
num_feature_maps_vec[i], # output size channels
kernel_size,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (stride_encode, stride_encode),
)
output_val, output_weight = tf.split(output, [output.shape[-1]//2, output.shape[-1]//2], axis=-1, num=None, name='split')
skips_val.append(output_val)
skips_weight.append(output_weight)
skips_pixel_x.append(output.shape[1])
skips_pixel_y.append(output.shape[2])
skips_pixel_z.append(output.shape[3])
if real_data:
inputs = (input_Im, input_alpha, input_coords_xm, input_coords_ym)
else:
inputs = (input_Im, input_alpha)
model = keras.Model(inputs = inputs, outputs = (skips_val,skips_weight), name='encode_net_' + str(encode_net_ind))
model.summary()
return(model, skips_pixel_x, skips_pixel_y, skips_pixel_z)
def create_decode_net(skips_pixel_x,
skips_pixel_y,
skips_pixel_z,
num_leds,
num_zernike_coeff,
batch_size,
final_output_channels, # number of output channels
kernel_size,
stride_encode,
apply_norm, norm_type,
initializer,
dropout_prob,
intermediate_layers,
intermediate_kernel,
net_number = 0,
feature_maps_multiplier = 2,
use_first_skip = True,
real_data=False,
change_Ns=False,
vary_pupil=False
):
skips = []
for skip in range(len(skips_pixel_x)):
skip_input = tf.keras.Input(shape=(skips_pixel_x[skip],
skips_pixel_y[skip],
skips_pixel_z[skip]//feature_maps_multiplier),
batch_size = batch_size,
name=str(skip))
skips.append(skip_input)
if dropout_prob == 0:
apply_dropout = False
else:
apply_dropout = True
output = skips[-1]
skips_reverse = reversed(skips[:-1])
skips_pixel_z_reverse = reversed(skips_pixel_z[:-1])
# Upsampling and establishing the skip connections
for i, skip in enumerate(skips_reverse):
output_channels = next(skips_pixel_z_reverse)
# intermediate layers, don't change shape
# conv with stride = 1 and padding = same
for l in range(intermediate_layers):
output = \
conv_block(output, # input
output.shape[-1], # output size channels
intermediate_kernel,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (1,1),
)
output = conv_block(output, # input
output_channels, # output size channels
kernel_size,
apply_norm = apply_norm, norm_type=norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = True,
stride = (stride_encode,stride_encode),
)
skip_x = skip.shape[1]
skip_y = skip.shape[2]
# crop output
remove_pad_x = output.shape[1] - skip_x
remove_pad_y = output.shape[2] - skip_y
r_x = remove_pad_x%2
r_y = remove_pad_y%2
output = output[:,remove_pad_x//2+r_x:remove_pad_x//2+r_x+skip_x,remove_pad_y//2+r_y:remove_pad_y//2+r_y+skip_y,:]
if i==(len(skips_pixel_x)-2) and not(use_first_skip):
pass
else:
output = tf.keras.layers.Concatenate()([output, skip])
# final set of intermediate layers
for l in range(intermediate_layers):
output = \
conv_block(output, # input
output.shape[-1], # output size channels
intermediate_kernel,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (1,1),
)
# reconstruction output
output = \
conv_block(output, # input
final_output_channels*2, # output size channels
kernel_size,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (1,1),
)
output_mean, output_var = tf.split(output, [output.shape[-1]//2, output.shape[-1]//2],
axis=-1, num=None, name='split')
if real_data or vary_pupil:
output_small = \
conv_block(skips[-1], # input
1, # output size channels
kernel_size,
apply_norm = apply_norm, norm_type = norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
transpose = False,
stride = (1,1),
)
output_flattened = tf.keras.layers.Flatten()(output_small)
# zernike coeff output (effectively delta, as we always initialize zernike coeff with zeros)
zernike_coeff = dense_block(output_flattened, # input
num_zernike_coeff*2, # extra 2 for probability distribution output
apply_norm = apply_norm, norm_type=norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
bias_initializer = initializer,
use_bias = True,
)
zernike_coeff_mean, zernike_coeff_var = tf.split(zernike_coeff, [num_zernike_coeff,num_zernike_coeff],
axis=-1, num=None, name='split')
zernike_coeff_mean = zernike_coeff_mean/1e5
zernike_coeff_var = zernike_coeff_var-10
if change_Ns:
# Ns delta output
Ns_delta = dense_block(output_flattened, # input
num_leds*2*2, # Ns is num_leds x 2, extra 2 for probability distribution output
apply_norm = apply_norm, norm_type=norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
bias_initializer = initializer,
use_bias = True,
)
Ns_delta = Ns_delta/100
Ns_delta = tf.keras.layers.Reshape([num_leds,2*2])(Ns_delta)
Ns_delta_mean, Ns_delta_var = tf.split(Ns_delta, [2, 2],
axis=-1, num=None, name='split')
# cos theta delta output
cos_delta = dense_block(output_flattened, # input
num_leds*2, # extra 2 for probability distribution output
apply_norm = apply_norm, norm_type=norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
bias_initializer = initializer,
use_bias = True,
)
cos_delta = cos_delta/1e6
cos_delta_mean, cos_delta_var = tf.split(cos_delta, [num_leds,num_leds],
axis=-1, num=None, name='split')
# poisson noise multiplier delta
pnm_delta = dense_block(output_flattened, # input
2, # extra 2 for probability distribution output
apply_norm = apply_norm, norm_type=norm_type,
apply_dropout=apply_dropout, dropout_prob = dropout_prob,
initializer = initializer,
bias_initializer = initializer,
use_bias = True,
)
pnm_delta_mean, pnm_delta_var = tf.split(pnm_delta, [1,1],
axis=-1, num=None, name='split')
outputs = (output_mean, output_var, Ns_delta_mean, Ns_delta_var, zernike_coeff_mean, zernike_coeff_var,
cos_delta_mean, cos_delta_var, pnm_delta_mean, pnm_delta_var)
else:
outputs = (output_mean, output_var, zernike_coeff_mean, zernike_coeff_var,
)
else:
outputs = (output_mean, output_var)
model = keras.Model(inputs = (skips), outputs = outputs, name='decode_net_' + str(net_number))
model.summary()
return model
def process_flattened_input(flattened_input,
num_dense_layers,
intermediate_dim,
output_last_dim,
final_shape,
apply_norm,
norm_type,
apply_dropout,
dropout_prob,
initializer,
bias_initializer,
use_bias):
for layer_ind in range(num_dense_layers):
if layer_ind == num_dense_layers - 1:
output_dim = output_last_dim
else:
output_dim = intermediate_dim
flattened_input = dense_block(flattened_input, # input
output_dim,
apply_norm = apply_norm,
norm_type=norm_type,
apply_dropout=apply_dropout,
dropout_prob = dropout_prob,
initializer = initializer,
bias_initializer = bias_initializer,
use_bias=use_bias
)
flattened_input = tf.keras.layers.Reshape(final_shape)(flattened_input)
return flattened_input
def create_pi_net2(input_x,
input_y,
input_z,
num_leds,
num_patterns,
initializer,
num_dense_layers,
upsample,
dropout_prob,
apply_norm,
norm_type,
max_steps,
bias_initializer = 'glorot_uniform',
scale_factor_vec = None, # scale factor is subtractive
use_bias = False,
pi_iter = False):
'''
This function outputs both patterns and time_fraction.
'''
if dropout_prob == 0:
apply_dropout = False
else:
apply_dropout = True
# output of encode_net is the input to pi_net
inputs = tf.keras.Input(shape=(input_x,input_y,input_z), name='output_encode')
if pi_iter:
num_iter = tf.keras.Input(shape=(1,), name='num_iter')
flattened_input = tf.keras.layers.Flatten()(inputs)
if pi_iter:
flattened_input = tf.concat((flattened_input,num_iter*tf.ones_like(flattened_input)), axis=-1)
output = \
process_flattened_input(flattened_input,
num_dense_layers,
upsample,
num_leds*num_patterns*2,
[num_leds, num_patterns*2],
apply_norm,
norm_type,
apply_dropout,
dropout_prob,
initializer,
bias_initializer,
use_bias)
output_patterns, output_time = tf.split(output, [num_patterns, num_patterns], axis=-1, num=None, name='split')
flattened_output_time = tf.keras.layers.Flatten()(output_time)
output_time = \
process_flattened_input(flattened_output_time,
1, # num_dense_layers
upsample,
num_patterns,
[num_patterns,],
apply_norm,
norm_type,
apply_dropout,
dropout_prob,
initializer,
bias_initializer,
use_bias)
# rescaling layers
# output_patterns = keras.layers.experimental.preprocessing.Rescaling(scale = scale_factor)(output_patterns)
# output_time = keras.layers.experimental.preprocessing.Rescaling(scale = scale_factor)(output_time)
if pi_iter:
one_hot = tf.one_hot(tf.cast(num_iter, tf.int32), max_steps-1)
scale_factor = tf.expand_dims(tf.reduce_sum(scale_factor_vec*one_hot,axis=-1),axis=-2)
output_patterns = tf.add(output_patterns, -scale_factor)
if pi_iter:
model = keras.Model(inputs = (inputs, num_iter), outputs = (output_patterns), name='pi_net')
else:
model = keras.Model(inputs = (inputs), outputs = (output_patterns), name='pi_net')
model.summary()
return model
def dense_block(x, # input
output_last_dim,
apply_norm = False, norm_type='batchnorm',
apply_dropout=False, dropout_prob = 0,
initializer = 'glorot_uniform',
bias_initializer = 'zeros',
use_bias = False,
):
"""
Dropout => Dense => Maxout => Batchnorm
"""
if apply_dropout:
x = tf.keras.layers.Dropout(dropout_prob)(x)
x1 = tf.keras.layers.Dense(output_last_dim, activation=None, use_bias=use_bias, kernel_initializer=initializer,
bias_initializer=bias_initializer)(x)
x2 = tf.keras.layers.Dense(output_last_dim, activation=None, use_bias=use_bias, kernel_initializer=initializer,
bias_initializer=bias_initializer)(x)
## Maxout
x = tf.maximum(x1, x2)
if apply_norm:
if norm_type.lower() == 'batchnorm':
x = keras.layers.BatchNormalization()(x)
elif norm_type.lower() == 'instancenorm':
x = InstanceNormalization()(x)
return x
def periodic_padding(image, padding_tuple): # padding is added to beginnings and ends of x and y
'''
Create a periodic padding (wrap) around the image, to emulate periodic boundary conditions
https://github.com/tensorflow/tensorflow/issues/956
usage example:
image = tf.reshape(tf.range(30, dtype='float32'), shape=[5,6])
padded_image = periodic_padding(image, padding=2)
'''
padding_0,padding_1 = padding_tuple[0]
partial_image = image
if padding_0 != 0:
upper_pad = tf.repeat(image,int(np.ceil(padding_0/image.shape[1])),axis=1)[:,-padding_0:,:,:]
# upper_pad = image[:,-padding_0:,:,:]
partial_image = tf.concat([upper_pad, partial_image], axis=1)
if padding_1 != 0:
lower_pad = tf.repeat(image,int(np.ceil(padding_1/image.shape[1])),axis=1)[:,:padding_1,:,:]
# lower_pad = image[:,:padding_1,:,:]
partial_image = tf.concat([partial_image, lower_pad], axis=1)
padded_image = partial_image
padding_0,padding_1 = padding_tuple[1]
if padding_0 != 0:
left_pad = tf.repeat(partial_image,int(np.ceil(padding_0/image.shape[2])),axis=2)[:,:,-padding_0:,:]
# left_pad = partial_image[:,:,-padding_0:,:]
padded_image = tf.concat([left_pad, padded_image], axis=2)
if padding_1 != 0:
right_pad = tf.repeat(partial_image,int(np.ceil(padding_1/image.shape[2])),axis=2)[:,:,:padding_1,:]
# right_pad = partial_image[:,:,:padding_1,:]
padded_image = tf.concat([padded_image, right_pad], axis=2)
return padded_image
def conv_block(x, # input
output_last_dim, # output size channels
kernel_size,
apply_norm = False, norm_type='batchnorm',
apply_dropout=False, dropout_prob = 0,
initializer = 'glorot_uniform',
bias_initializer='zeros',
transpose = False,
stride = (2,2),
use_bias = True,
):
"""
Dropout => Conv2D => Maxout => Batchnorm
"""
stride_x, stride_y = stride
if apply_dropout:
x = tf.keras.layers.Dropout(dropout_prob)(x)
if transpose:
x1 = keras.layers.Conv2DTranspose(output_last_dim, (kernel_size, kernel_size),
strides=stride, padding='same',
dilation_rate=(1, 1),
use_bias=use_bias,
kernel_initializer=initializer,
bias_initializer=bias_initializer,
output_padding=None,
)(x)
x2 = keras.layers.Conv2DTranspose(output_last_dim, (kernel_size, kernel_size),
strides=stride, padding='same',
dilation_rate=(1, 1),
use_bias=use_bias,
kernel_initializer=initializer,
bias_initializer=bias_initializer,
output_padding=None,
)(x)
# # Add output_padding_x and output_padding_y
# r_x = output_padding_x%2
# r_y = output_padding_y%2
# x1 = periodic_padding(x1,((output_padding_x//2+r_x,output_padding_x//2),(output_padding_y//2+r_y,output_padding_y//2)))
# x2 = periodic_padding(x2, ((output_padding_x//2+r_x,output_padding_x//2),(output_padding_y//2+r_y,output_padding_y//2)))
# x1 = tf.pad(x1, ((0,0),(output_padding_x//2+r_x,output_padding_x//2),(output_padding_y//2+r_y,output_padding_y//2),(0,0)), \
# mode = "SYMMETRIC")
# x2 = tf.pad(x2, ((0,0),(output_padding_x//2+r_x,output_padding_x//2),(output_padding_y//2+r_y,output_padding_y//2),(0,0)), \
# mode = "SYMMETRIC")
else:
input_x = x.shape[-3]
input_y = x.shape[-2]
# Add padding such that the convolution doesn't change the input shape beyond stride effects
if input_x%stride_x:
pad_x = kernel_size - input_x%stride_x
else: # no remainder
pad_x = kernel_size - stride_x
if input_y%stride_y:
pad_y = kernel_size - input_y%stride_y
else: # no remainder
pad_y = kernel_size - stride_y
r_x = pad_x%2
r_y = pad_y%2
x = periodic_padding(x,((pad_x//2+r_x,pad_x//2),(pad_y//2+r_y,pad_y//2)))
# x = tf.pad(x, ((0,0),(pad_x//2+r_x,pad_x//2),(pad_y//2+r_y,pad_y//2),(0,0)), mode = "SYMMETRIC")
x1 = keras.layers.Conv2D(output_last_dim, (kernel_size, kernel_size), strides=stride, padding='valid',
kernel_initializer=initializer,
bias_initializer=bias_initializer,
use_bias=use_bias)(x)
x2 = keras.layers.Conv2D(output_last_dim, (kernel_size, kernel_size), strides=stride, padding='valid',
kernel_initializer=initializer,
bias_initializer=bias_initializer,
use_bias=use_bias)(x)
## Maxout
x = tf.maximum(x1, x2)
if apply_norm:
if norm_type.lower() == 'batchnorm':
x = keras.layers.BatchNormalization()(x)
elif norm_type.lower() == 'instancenorm':
x = InstanceNormalization()(x)
return(x)