-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_v2.py
665 lines (554 loc) · 34.4 KB
/
train_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
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
from scipy.sparse.linalg import eigsh
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer
from src import graphconv
from src import gcw
from src import processtools as pt
from src import healpyfunctional as hpf
import healpy as hp
from pygsp.graphs import SphereHealpix
from pygsp import filters
from tqdm import tqdm
print(tf.config.get_visible_devices())
class GCNHealpy_uNetlike(Model, gcw.GCW):
"""
Graph convolutional NN models for the healpy pixelization scheme.
Precalculates the polynomial approximation of the graph laplacian for graph convolutional layers.
"""
def __init__(self,
nside,
indices,
channels=1,
use_polyK=False,
verbose=True):
"""
:param nside: nside of the input maps
:param indices: indices of the input maps
:param channels: number of input channels
:param use_polyK: Bool. Optional. If True, will precalculate P(L) and use P(L) in graph convolution
layers. Might lead to performance gains.
"""
super(GCNHealpy_uNetlike, self).__init__(name='')
self.nside = nside
self.indices = indices
self.channels = channels
self.use_polyK = use_polyK
self.verbose = verbose
self.polydict = {}
self.Ldict = {}
def l2(self, weight_decay):
return tf.keras.regularizers.L2(l2=weight_decay)
def model(self, weight_decay, sdrate, include_top=True, num_classes=3):
"""
:param weight_decay: l2 regularization penalty to apply on the convolution kernels
:param sdrate: spatial dropout rate to apply after the convolution layers
:param include_top: if true, will include the globalavereagepooling and densely connected layers
:param num_classes: number of outputs of the final densely connected layer.
"""
inputs = tf.keras.layers.Input(shape=(len(self.indices), self.channels), name="input_maps")
x1 = self.Conv(nside=self.nside, indices=self.indices, n_neighbors=8, poly_type='chebyshev',
K=4, Fout=16, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(inputs)
x1 = self.Conv(nside=self.nside, indices=self.indices, n_neighbors=8, poly_type='chebyshev',
K=4, Fout=16, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x1)
x1 = hpf.HealpyPseudoConv(p=1, Fout=16, activation='relu', initializer='he_normal',
kernel_regularizer=self.l2(weight_decay), nside=self.nside,
indices=self.indices)(x1)
x1 = tf.keras.layers.SpatialDropout1D(sdrate)(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x2 = self.Conv(nside=self.nside, indices=self.indices, n_neighbors=20, poly_type='chebyshev',
K=8, Fout=32, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(inputs)
x2 = hpf.HealpyPseudoConv(p=1, Fout=32, activation='relu', initializer='he_normal',
kernel_regularizer=self.l2(weight_decay), nside=self.nside,
indices=self.indices)(x2)
x2 = tf.keras.layers.SpatialDropout1D(sdrate)(x2)
x2 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x2)
x3 = self.Conv(nside=self.nside, indices=self.indices, n_neighbors=20, poly_type='chebyshev',
K=12, Fout=16, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(inputs)
x3 = hpf.HealpyPseudoConv(p=1, Fout=16, activation='relu', initializer='he_normal',
kernel_regularizer=self.l2(weight_decay), nside=self.nside,
indices=self.indices)(x3)
x3 = tf.keras.layers.SpatialDropout1D(sdrate)(x3)
x3 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x3)
nside_out1 = hpf.HealpyPseudoConv(p=1, Fout=16, activation='relu',
initializer='he_normal',
kernel_regularizer=self.l2(weight_decay),
nside=self.nside, indices=self.indices).nside_out
indices_out1 = hpf.HealpyPseudoConv(p=1, Fout=16, activation='relu',
initializer='he_normal',
kernel_regularizer=self.l2(weight_decay),
nside=self.nside, indices=self.indices).indices_out
x = tf.keras.layers.Concatenate(axis=-1)([x1,x2,x3]) #output of 'conv+P 1', nside = 64, F=64
x1 = self.Conv(nside=nside_out1, indices=indices_out1, n_neighbors=20, poly_type='chebyshev',
K=4, Fout=32, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x)
x1 = self.Conv(nside=nside_out1, indices=indices_out1, n_neighbors=8, poly_type='chebyshev',
K=8, Fout=32, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x1)
x1 = hpf.HealpyPseudoConv(p=1, Fout=32, activation='relu',
initializer='he_normal', kernel_regularizer=self.l2(weight_decay),
nside=nside_out1, indices=indices_out1)(x1)
x1 = tf.keras.layers.SpatialDropout1D(sdrate)(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x2 = self.Conv(nside=nside_out1, indices=indices_out1, n_neighbors=20, poly_type='chebyshev',
K=12, Fout=32, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x)
x2 = hpf.HealpyPseudoConv(p=1, Fout=32, activation='relu',
initializer='he_normal', kernel_regularizer=self.l2(weight_decay),
nside=nside_out1, indices=indices_out1)(x2)
x2 = tf.keras.layers.SpatialDropout1D(sdrate)(x2)
x2 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x2)
x3 = tf.keras.layers.Concatenate(axis=-1)([x1,x2])
xres = hpf.HealpyPool(nside=nside_out1, indices = indices_out1, p=1, pool_type='AVG')(x)
xres = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xres)
nside_out2 = hpf.HealpyPool(nside=nside_out1, indices = indices_out1,
p=1, pool_type='AVG').nside_out
indices_out2 = hpf.HealpyPool(nside=nside_out1, indices = indices_out1,
p=1, pool_type='AVG').indices_out
x = tf.keras.layers.Add()([x3,xres]) #output of 'conv+P 2', nside=32, F=128
x1 = self.Conv(nside=nside_out2, indices=indices_out2, n_neighbors=8, poly_type='chebyshev',
K=8, Fout=128, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x)
x = self.Conv(nside=nside_out2, indices=indices_out2, n_neighbors=20, poly_type='chebyshev',
K=12, Fout=128, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x1)
x1 = self.Conv(nside=nside_out2, indices=indices_out2, n_neighbors=8, poly_type='chebyshev',
K=4, Fout=128, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x)
x1 = self.Conv(nside=nside_out2, indices=indices_out2, n_neighbors=20, poly_type='chebyshev',
K=8, Fout=128, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x1)
x = tf.keras.layers.Add()([x,x1]) #output of 'conv 3', nside = 32, F=128
xup = hpf.HealpyPseudoConv_Transpose(nside=nside_out2, indices=indices_out2,
p=1, Fout=64,
kernel_initializer='he_normal')(x)
xup = tf.keras.layers.ReLU()(xup)
xup = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup)
nside_up1 = hpf.HealpyPseudoConv_Transpose(nside=nside_out2, indices=indices_out2,
p=1, Fout=64,
kernel_initializer='he_normal').nside_out
indices_up1 = hpf.HealpyPseudoConv_Transpose(nside=nside_out2, indices=indices_out2,
p=1, Fout=64,
kernel_initializer='he_normal').indices_out
xup = self.Conv(nside=nside_up1, indices=indices_up1, n_neighbors=20, poly_type='chebyshev',
K=8, Fout=64, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(xup)
xup = hpf.HealpyPseudoConv(p=1, Fout=128, activation='relu',
initializer='he_normal', kernel_regularizer=self.l2(weight_decay),
nside=nside_out1, indices=indices_out1)(xup)
xup = tf.keras.layers.SpatialDropout1D(sdrate)(xup)
xup = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup) #output of 'u/d 1', nside=32, F=128
for i in range(2):
x1 = self.SeparableConv(nside=nside_out2,
indices=indices_out2,
n_neighbors=8,
poly_type='chebyshev',
K=6,
Fout=128,
depth_multiplier=2,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x1 = self.SeparableConv(nside=nside_out2,
indices=indices_out2,
n_neighbors=20,
poly_type='chebyshev',
K=10,
Fout=128,
depth_multiplier=2,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x1)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.SpatialDropout1D(sdrate)(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x = tf.keras.layers.Add()([x, x1]) #output of 'sepconv 1', nside=32, F=128
x = tf.keras.layers.Add()([x, xup])
x = self.DepthwiseConv(nside=nside_out2, indices=indices_out2, n_neighbors=20, poly_type='chebyshev',
K=8, depth_multiplier=2, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(x)
x = hpf.HealpyPool(nside=nside_out2, indices=indices_out2, p=1, pool_type='AVG')(x)
x = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x)
x = tf.keras.layers.SpatialDropout1D(sdrate)(x) #output of 'dconv + P 1', nside=16, F=256
nside_out3 = hpf.HealpyPool(nside=nside_out2, indices=indices_out2, p=1, pool_type='AVG').nside_out
indices_out3 = hpf.HealpyPool(nside=nside_out2, indices=indices_out2, p=1, pool_type='AVG').indices_out
xup2 = hpf.HealpyPseudoConv_Transpose(nside=nside_out3, indices=indices_out3,
p=1, Fout=128,
kernel_initializer='he_normal')(x)
xup2 = tf.keras.layers.ReLU()(xup2)
xup2 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup2)
nside_up2 = hpf.HealpyPseudoConv_Transpose(nside=nside_out3, indices=indices_out3,
p=1, Fout=128,
kernel_initializer='he_normal').nside_out
indices_up2 = hpf.HealpyPseudoConv_Transpose(nside=nside_out3, indices=indices_out3,
p=1, Fout=128,
kernel_initializer='he_normal').indices_out
xupmasked = hpf.HealpyMask(unmasked_indices=indices_up2)(xup)
xup2 = tf.keras.layers.Add()([xupmasked, xup2])
xup2 = self.Conv(nside=nside_up2, indices=indices_up2, n_neighbors=20, poly_type='chebyshev',
K=8, Fout=128, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(xup2)
xup2 = hpf.HealpyPseudoConv(p=1, Fout=256, activation='relu',
initializer='he_normal', kernel_regularizer=self.l2(weight_decay),
nside=nside_up2, indices=indices_up2)(xup2)
xup2 = tf.keras.layers.SpatialDropout1D(sdrate)(xup2)
xup2 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup2) #output of 'u/d 2', nside=16, F=256
for i in range(2):
x1 = self.SeparableConv(nside=nside_out3,
indices=indices_out3,
n_neighbors=20,
poly_type='chebyshev',
K=4,
Fout=256,
depth_multiplier=1,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x1 = self.SeparableConv(nside=nside_out3,
indices=indices_out3,
n_neighbors=8,
poly_type='chebyshev',
K=8,
Fout=256,
depth_multiplier=1,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x1)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.SpatialDropout1D(sdrate)(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x = tf.keras.layers.Add()([x, x1]) #output of 'sepconv 2', nside=16, F=256
x = tf.keras.layers.Add()([x, xup2])
x = self.Conv(nside=nside_out3, indices=indices_out3, n_neighbors=8, poly_type='chebyshev',
K=8, Fout=512, activation='relu', use_bn=True,
kernel_regularizer=self.l2(weight_decay))(x)
x = hpf.HealpyPool(nside=nside_out3, indices=indices_out3, p=1, pool_type='AVG')(x)
x = tf.keras.layers.SpatialDropout1D(sdrate)(x)
x = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x) #output of 'conv+P 3', nside=8, F=512
nside_out4 = hpf.HealpyPool(nside=nside_out3, indices=indices_out3, p=1, pool_type='AVG').nside_out
indices_out4 = hpf.HealpyPool(nside=nside_out3, indices=indices_out3, p=1, pool_type='AVG').indices_out
xup3 = hpf.HealpyPseudoConv_Transpose(nside=nside_out4, indices=indices_out4,
p=1, Fout=256,
kernel_initializer='he_normal')(x)
xup3 = tf.keras.layers.ReLU()(xup3)
xup3 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup3)
nside_up3 = hpf.HealpyPseudoConv_Transpose(nside=nside_out4, indices=indices_out4,
p=1, Fout=256,
kernel_initializer='he_normal').nside_out
indices_up3 = hpf.HealpyPseudoConv_Transpose(nside=nside_out4, indices=indices_out4,
p=1, Fout=256,
kernel_initializer='he_normal').indices_out
xup2masked = hpf.HealpyMask(unmasked_indices=indices_up3)(xup2)
xup3 = tf.keras.layers.Add()([xup2, xup3])
xup3 = self.Conv(nside=nside_up3, indices=indices_up3, n_neighbors=20, poly_type='chebyshev',
K=8, Fout=256, activation='relu', use_bn=True,
kernel_initializer='he_normal', kernel_regularizer=self.l2(weight_decay))(xup3)
xup3 = hpf.HealpyPseudoConv(p=1, Fout=512, activation='relu',
initializer='he_normal', kernel_regularizer=self.l2(weight_decay),
nside=nside_up3, indices=indices_up3)(xup3)
xup3 = tf.keras.layers.SpatialDropout1D(sdrate)(xup3)
xup3 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(xup3) #output of 'u/d 3', nside=8, F=512
for i in range(2):
x1 = self.SeparableConv(nside=nside_out4,
indices=indices_out4,
n_neighbors=8,
poly_type='chebyshev',
K=8,
Fout=512,
depth_multiplier=1,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x1 = self.SeparableConv(nside=nside_out4,
indices=indices_out4,
n_neighbors=8,
poly_type='chebyshev',
K=8,
Fout=512,
depth_multiplier=1,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x1)
x1 = tf.keras.layers.ReLU()(x1)
x1 = tf.keras.layers.SpatialDropout1D(sdrate)(x1)
x1 = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x1)
x = tf.keras.layers.Add()([x, x1]) #output of 'sep conv 3', nside=8, F=512
x = tf.keras.layers.Add()([x, xup3])
x = self.SeparableConv(nside=nside_out4,
indices=indices_out4,
n_neighbors=8,
poly_type='chebyshev',
K=8,
Fout=512,
depth_multiplier=1,
pointwise_initializer='he_normal',
depthwise_initializer='he_normal',
pointwise_regularizer=self.l2(weight_decay),
depthwise_regularizer=self.l2(weight_decay))(x)
x = tf.keras.layers.ReLU()(x)
x = tf.keras.layers.BatchNormalization(axis=-1, momentum=0.99,
epsilon=0.001, center=False,
scale=False)(x)
if include_top == True:
outputs = tf.keras.layers.GlobalAveragePooling1D()(x)
outputs = tf.keras.layers.Dense(num_classes, activation='softmax')(outputs)
return Model(inputs = inputs, outputs = outputs)
#load data
print('Loading data...')
a_lm_triv = np.load('data/realizations_L_infty_lmax_250_num_1000.npy').astype(np.complex128)
a_lm_torus1400 = np.load('data/realizations_L_1400_lmax_250_num_1000.npy').astype(np.complex128)
a_lm_torus2800 = np.load('data/realizations_L_2800_lmax_250_num_1000.npy').astype(np.complex128)
print('Data loading complete.')
#input indices and masking:
print('Preparing the mask and calculating relevant map indices')
nside = 128
npix = hp.nside2npix(nside=nside)
indices = np.arange(npix)
mask=hp.read_map('data/masks/COM_Mask_CMB-common-Mask-Int_2048_R3.fits')
print('Mask preparation done.')
#unmasked pixels:
unmasked_pix = pt.get_indices(mask=mask, nside=nside, target_nside=nside)
#aggresive masking:
worst_case_pix = pt.get_indices(mask=mask, nside=nside, target_nside=8)
#adaptive masking:
adaptive_case_pix = pt.get_indices(mask=mask, nside=nside, target_nside=nside//2)
print('Relevant map indices are calculated.')
print('Defining TensorFlow distribution strategy.')
strategy = tf.distribute.MirroredStrategy(cross_device_ops=tf.distribute.NcclAllReduce(num_packs=2))
#tf.keras.backend.clear_session()
#defining batch sizes
print('Creating TensorFlow datasets.')
BATCH_SIZE_PER_REPLICA = 5
GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync
#create datasets:
train_data, test_data, x_eval, y_eval, x_alm_train, y_train = pt.create_dataset(a_lm_torus1400,
a_lm_torus2800,
a_lm_triv,
relevant_pix=adaptive_case_pix,
global_batch_size=GLOBAL_BATCH_SIZE,
trainperc=0.8,
evalperc=0.05,
strategy=strategy)
print('Dataset creation completed.')
tf.keras.backend.clear_session()
print('Creating the model.')
with strategy.scope():
GCN_v2 = GCNHealpy_uNetlike(nside=nside,
indices=adaptive_case_pix,
channels=1,
use_polyK=False)
model = GCN_v2.model(weight_decay=1e-4,
sdrate=0.05,
include_top=True,
num_classes=3)
print('Model creation complete.')
model.summary(110)
import os
import csv
checkpoint_path = "runs_2/training_3_class/SGDopt_xception_v3_L_precalc_adaptive_mask/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
csv_logger = tf.keras.callbacks.CSVLogger('training_log.csv')
callbacklist = []
callbacks = tf.keras.callbacks.CallbackList(
callbacklist, add_history=True, model=model)
def lr_decay(lr_init, epoch, num_batches, decay=0.998):
steps = epoch * BUFFER_SIZE//GLOBAL_BATCH_SIZE + num_batches
if epoch < 20:
return lr_init
else:
return lr_init* (decay)**(-20*BUFFER_SIZE//GLOBAL_BATCH_SIZE)*(decay)**(steps)
with strategy.scope():
# Set reduction to `none` so we can do the reduction afterwards and divide by
# global batch size.
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(reduction=tf.keras.losses.Reduction.NONE)
def compute_loss(labels, predictions):
per_example_loss = loss_object(labels, predictions)
return tf.nn.compute_average_loss(per_example_loss, global_batch_size=GLOBAL_BATCH_SIZE)
test_loss = tf.keras.metrics.Mean(name='test_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3, #1e-3 with mom = 0.8 and decay = 0.998 is very stable
momentum = 0.8) #but seems to stagnate (or run out of data)
checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model)
def train_step(inputs):
samples, labels = inputs
with tf.GradientTape() as tape:
predictions = model(samples, training=True)
loss = compute_loss(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_accuracy.update_state(labels, predictions)
return loss
def test_step(inputs):
samples, labels = inputs
predictions = model(samples, training=False)
t_loss = loss_object(labels, predictions)
test_loss.update_state(t_loss)
test_accuracy.update_state(labels, predictions)
# `run` replicates the provided computation and runs it
# with the distributed input.
@tf.function()
def distributed_train_step(dataset_inputs):
per_replica_losses = strategy.run(train_step, args=(dataset_inputs,))
return strategy.reduce(tf.distribute.ReduceOp.SUM, per_replica_losses,
axis=None)
@tf.function()
def distributed_test_step(dataset_inputs):
return strategy.run(test_step, args=(dataset_inputs,))
train_loss_xception_v2 = []
train_accuracy_xception_v2 = []
test_loss_xception_v2 = []
test_accuracy_xception_v2 = []
logs = {}
BUFFER_SIZE = len(x_alm_train)
EPOCHS = 100
AUG_EPOCH = 50 #at the end of 50th, 100th etc epochs, we will rotate the training data randomly
#for aug_epoch in range(AUG_EPOCHS):
# print(f"Augmentation epoch {aug_epoch+1}/{AUG_EPOCHS}")
# if aug_epoch >= 1:
# print('Creating new training dataset...')
# train_data = pt.rotate_train_data(alm=x_alm_train, y_train=y_train, relevant_pix=adaptive_case_pix,
# global_batch_size=GLOBAL_BATCH_SIZE, strategy=strategy)
# print('Done.')
print("------------------------------------------------------")
print(f"Starting Training, Epochs:{EPOCHS}, Augmentation Epochs:{EPOCHS//AUG_EPOCH}")
print("------------------------------------------------------")
for epoch in range(EPOCHS):
if epoch > 0:
if (epoch)%AUG_EPOCH == 0:
print(f"Augmentation Epoch {(epoch+1)//AUG_EPOCH}/{EPOCHS//AUG_EPOCH} ")
print('Rotating training dataset...')
train_data = pt.rotate_train_data(alm=x_alm_train, y_train=y_train,
relevant_pix=adaptive_case_pix,
global_batch_size=GLOBAL_BATCH_SIZE,
strategy=strategy)
print('Rotation complete.')
# TRAIN LOOP
print(f"Starting with Epoch {epoch + 1}/{EPOCHS}", flush=True)
total_loss = 0.0
num_batches = 0
with tqdm(train_data, total=BUFFER_SIZE//GLOBAL_BATCH_SIZE) as pbar:
for x in pbar:
optimizer.learning_rate = lr_decay(5e-4, epoch, num_batches, 0.9995)
pbar.set_description(f"Epoch {epoch +1}/{EPOCHS}", refresh=True)
total_loss += distributed_train_step(x)
num_batches += 1
pbar.set_postfix({'train_loss': total_loss.numpy()/num_batches,
'learning_rate': optimizer.learning_rate.numpy()}, refresh=True)
train_loss = total_loss / num_batches
# TEST LOOP
for x in test_data:
distributed_test_step(x)
template = ("Epoch {}/{}, Training Loss: {:.5g}, Training Accuracy: {:.5g}, Test Loss: {:.5g}, "
"Test Accuracy: {:.5g}")
print (template.format(epoch+1, EPOCHS,train_loss.numpy(),
train_accuracy.result().numpy(), test_loss.result().numpy(),
test_accuracy.result().numpy()))
train_loss_xception_v2.append(train_loss)
train_accuracy_xception_v2.append(train_accuracy.result())
test_loss_xception_v2.append(test_loss.result())
test_accuracy_xception_v2.append(test_accuracy.result())
logs[f"training_loss-epoch:{epoch+1}"] = train_loss.numpy()
logs[f"training_accuracy-epoch:{epoch+1}"] = train_accuracy.result().numpy()
logs[f"test_loss-epoch:{epoch+1}"] = test_loss.result().numpy()
logs[f"test_accuracy-epoch:{epoch+1}"] = test_accuracy.result().numpy()
if (epoch+1) % 2 == 0:
checkpoint.save(checkpoint_prefix)
checkpoint_template = ("training_3_class/SGDopt_xception_v3_L_precalc_adaptive_mask/cp-{:04d}.ckpt")
print('Creating checkpoint...')
print('Checkpoint saved. Filename:')
print(checkpoint_template.format(epoch+1))
test_loss.reset_states()
train_accuracy.reset_states()
test_accuracy.reset_states()
training_logs = csv.writer(open("training_logs.csv", "w"))
for key, val in logs.items():
training_logs.writerow([key, val])
print("Training complete. Saving weights.")
model.save_weights("runs_2/training_3_class/SGDopt_xception_v3_L_precalc_adaptive_mask/weights.h5")
print("Creating plots.")
epochs = np.arange(1,EPOCHS+1)
#epochs = np.arange(1, len(train_loss_xception_v2))
fig, axes = plt.subplots(2, figsize=(10, 10), sharex=True)
#fig.title('Metrics')
fig.subplots_adjust(hspace=0)
axes[0].set_ylabel("Loss", fontsize=14)
#axes[0].set_xlabel("Epoch", fontsize=14)
axes[0].plot(epochs, train_loss_xception_v2, label = 'Training')
axes[0].plot(epochs, test_loss_xception_v2, '--', label = 'Validation')
axes[0].grid(visible=True, axis='both')
axes[0].set_yscale('log')
axes[0].legend()
axes[0].set_title('Training Metrics (Heavy Masking)')
axes[1].set_ylabel("Accuracy", fontsize=14)
axes[1].set_xlabel("Epoch", fontsize=14)
axes[1].plot(epochs, train_accuracy_xception_v2, label = 'Training')
axes[1].plot(epochs, test_accuracy_xception_v2, '--', label = 'Validation')
axes[1].grid(visible=True, axis='both')
#axes[1].set_yscale('log')
axes[1].legend()
plt.show()
fig.savefig('runs_2/test_metrics_on_separable_convs_v1.pdf')
print("Training metric plots saved to 'runs_2/test_metrics_on_separable_convs_v1.pdf'.")
import sys
sys.exit()