-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
srgan_train.py
1757 lines (1539 loc) · 70.4 KB
/
srgan_train.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.2'
# jupytext_version: 1.2.0
# kernelspec:
# display_name: deepbedmap
# language: python
# name: deepbedmap
# ---
# %% [markdown]
# # **Enhanced Super-Resolution Generative Adversarial Network training**
#
# Here in this jupyter notebook, we will train an adapted Enhanced Super-Resolution Generative Adversarial Network (ESRGAN),
# to create a high-resolution (250m) Antarctic bed Digital Elevation Model(DEM) from a low-resolution (1000m) BEDMAP2 DEM.
# In addition to that, we use additional correlated inputs that can also tell us something about the bed topography.
#
# <img src="https://yuml.me/diagram/scruffy;dir:LR/class/[Antarctic Snow Accumulation (1000m)]->[Generator model],[MEASURES Ice Flow Velocity (450m)]->[Generator model],[REMA (100m)]->[Generator model],[BEDMAP2 (1000m)]->[Generator model],[Generator model]->[High res bed DEM (250m)],[High res bed DEM (250m)]->[Discriminator model],[Groundtruth Image (250m)]->[Discriminator model],[Discriminator model]->[True/False]" alt="4 input ESRGAN model"/>
# %% [markdown]
# # 0. Setup libraries
# %%
import functools
import glob
import os
import random
import shutil
import sys
import typing
import comet_ml
import IPython.display
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pygmt as gmt
import quilt
import rasterio
import tqdm
import xarray as xr
import chainer
import chainer.functions as F
import chainer.links as L
import cupy
import livelossplot
import optuna
import ssim.functions
from features.environment import _load_ipynb_modules, _download_model_weights_from_comet
try: # check if CUDA_VISIBLE_DEVICES environment variable is set
os.environ["CUDA_VISIBLE_DEVICES"]
except KeyError: # if not set, then set it to the first GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
print("Python :", sys.version.split("\n")[0])
chainer.print_runtime_info()
# %%
# Chainer configurations https://docs.chainer.org/en/latest/reference/configuration.html
# Use CuDNN deterministic mode
chainer.global_config.cudnn_deterministic = True
# Set seed values
seed = 42
random.seed = seed
np.random.seed(seed=seed)
if cupy.is_available():
for c in range(cupy.cuda.runtime.getDeviceCount()):
with cupy.cuda.Device(c):
cupy.random.seed(seed=42)
# %% [markdown]
# # 1. Load data
# - Download pre-packaged data from [Quilt](https://github.com/quiltdata/quilt)
# - Convert arrays for Chainer, from Numpy (CPU) to CuPy (GPU) format (if available)
# %%
def load_data_into_memory(
refresh_cache: bool = True,
quilt_hash: str = "580960bc97696f7ca89dba61fb6225a2ff631d49876fefef8dc05a033f13e14f",
) -> (chainer.datasets.dict_dataset.DictDataset, str):
"""
Downloads the prepackaged tiled data from quilt based on a hash,
and loads it into CPU or GPU memory depending on what is available.
"""
if refresh_cache:
quilt.install(
package="weiji14/deepbedmap/model/train", hash=quilt_hash, force=True
)
pkg = quilt.load(pkginfo="weiji14/deepbedmap/model/train", hash=quilt_hash)
W1_data = pkg.W1_data() # miscellaneous data REMA
W2_data = pkg.W2_data() # miscellaneous data MEASURES Ice Flow
W3_data = pkg.W3_data() # miscellaneous data Arthern Accumulation
X_data = pkg.X_data() # low resolution BEDMAP2
Y_data = pkg.Y_data() # high resolution groundtruth
# print(W1_data.shape, W2_data.shape, W3_data.shape, X_data.shape, Y_data.shape)
# Detect if there is a CUDA GPU first
if cupy.is_available():
print("Using GPU")
W1_data = chainer.backend.cuda.to_gpu(array=W1_data, device=None)
W2_data = chainer.backend.cuda.to_gpu(array=W2_data, device=None)
W3_data = chainer.backend.cuda.to_gpu(array=W3_data, device=None)
X_data = chainer.backend.cuda.to_gpu(array=X_data, device=None)
Y_data = chainer.backend.cuda.to_gpu(array=Y_data, device=None)
else:
print("Using CPU only")
return (
chainer.datasets.DictDataset(
X=X_data, W1=W1_data, W2=W2_data, W3=W3_data, Y=Y_data
),
quilt_hash,
)
# %% [markdown]
# ## 1.1 Split dataset into training (train) and development (dev) sets
# %%
def get_train_dev_iterators(
dataset: chainer.datasets.dict_dataset.DictDataset,
first_size: int, # size of training set
batch_size: int = 128,
seed: int = 42,
) -> (
chainer.iterators.serial_iterator.SerialIterator,
int,
chainer.iterators.serial_iterator.SerialIterator,
int,
):
"""
Create Chainer Dataset Iterators after splitting dataset into
training and development (validation) sets.
"""
# Train/Dev split of the dataset
train_set, dev_set = chainer.datasets.split_dataset_random(
dataset=dataset, first_size=first_size, seed=seed
)
# Create Chainer Dataset Iterators out of the split datasets
train_iter = chainer.iterators.SerialIterator(
dataset=train_set, batch_size=batch_size, repeat=True, shuffle=True
)
dev_iter = chainer.iterators.SerialIterator(
dataset=dev_set, batch_size=batch_size, repeat=True, shuffle=False
)
print(
f"Training dataset: {len(train_set)} tiles,",
f"Development dataset: {len(dev_set)} tiles",
)
return train_iter, len(train_set), dev_iter, len(dev_set)
# %% [markdown]
# # 2. Architect model
#
# Enhanced Super Resolution Generative Adversarial Network (ESRGAN) model based on [Wang et al. 2018](https://arxiv.org/abs/1809.00219v2).
# Refer to original Pytorch implementation at https://github.com/xinntao/ESRGAN.
# See also previous (non-enhanced) SRGAN model architecture by [Ledig et al. 2017](https://arxiv.org/abs/1609.04802).
# %% [markdown]
# ## 2.1 Generator Network Architecture
#
# ![ESRGAN architecture - Generator Network composed of many Dense Convolutional Blocks](https://github.com/xinntao/ESRGAN/raw/master/figures/architecture.jpg)
#
# 3 main components: 1) Input Block, 2) Residual Blocks, 3) Upsampling Blocks
# %% [markdown]
# ### 2.1.1 Input block, specially customized for DeepBedMap to take in 3 different inputs
#
# Details of the first convolutional layer for each input:
#
# - Input tiles are 11000m by 11000m.
# - Convolution filter kernels are 3000m by 3000m.
# - Strides are 1000m by 1000m.
#
# Example: for a 100m spatial resolution tile:
#
# - Input tile is 110pixels by 110pixels
# - Convolution filter kernels are 30pixels by 30pixels
# - Strides are 10pixels by 10pixels
#
# Note that the first convolutional layers uses **valid** padding, see https://github.com/weiji14/deepbedmap/pull/65.
# %%
class DeepbedmapInputBlock(chainer.Chain):
"""
Custom input block for DeepBedMap.
Takes in BEDMAP2 (X), REMA Ice Surface Elevation (W1),
MEaSUREs Ice Surface Velocity x and y components (W2) and Snow Accumulation (W3).
Passes them through custom-sized convolutions, with the results being concatenated.
Each filter kernel is 3km by 3km in size, with a 1km stride and no padding.
So for a 1km resolution image, (i.e. 1km pixel size):
kernel size is (3, 3), stride is (1, 1), and pad is (0, 0)
X (?,1,11,11) --Conv2D-- (?,32,9,9) \
W1 (?,1,110,110) --Conv2D-- (?,32,9,9) --Concat-- (?,128,9,9)
W2 (?,2,22,22) --Conv2D-- (?,32,9,9) /
W3 (?,1,11,11) --Conv2D-- (?,32,9,9) /
"""
def __init__(self, out_channels=32):
super().__init__()
init_weights = chainer.initializers.HeNormal(scale=0.1, fan_option="fan_in")
with self.init_scope():
self.conv_on_X = L.Convolution2D(
in_channels=1,
out_channels=out_channels,
ksize=(3, 3),
stride=(1, 1),
pad=(0, 0), # 'valid' padding
initialW=init_weights,
)
self.conv_on_W1 = L.Convolution2D(
in_channels=1,
out_channels=out_channels,
ksize=(30, 30),
stride=(10, 10),
pad=(0, 0), # 'valid' padding
initialW=init_weights,
)
self.conv_on_W2 = L.Convolution2D(
in_channels=2,
out_channels=out_channels,
ksize=(6, 6),
stride=(2, 2),
pad=(0, 0), # 'valid' padding
initialW=init_weights,
)
self.conv_on_W3 = L.Convolution2D(
in_channels=1,
out_channels=out_channels,
ksize=(3, 3),
stride=(1, 1),
pad=(0, 0), # 'valid' padding
initialW=init_weights,
)
def forward(self, x, w1, w2, w3):
"""
Forward computation, i.e. evaluate based on inputs X, W1, W2 and W3
"""
x_ = self.conv_on_X(x)
w1_ = self.conv_on_W1(w1)
w2_ = self.conv_on_W2(w2)
w3_ = self.conv_on_W3(w3)
a = F.concat(xs=(x_, w1_, w2_, w3_))
return a
# %% [markdown]
# ### 2.1.2 Residual Block
#
# ![The Residual in Residual Dense Block in detail](https://raw.githubusercontent.com/xinntao/ESRGAN/master/figures/RRDB.png)
# %%
class ResidualDenseBlock(chainer.Chain):
"""
Residual Dense Block made up of 5 Convolutional2D-LeakyReLU layers.
Final output has a residual scaling factor.
"""
def __init__(
self,
in_out_channels: int = 64,
inter_channels: int = 32,
residual_scaling: float = 0.1,
):
super().__init__()
self.residual_scaling = residual_scaling
init_weights = chainer.initializers.HeNormal(scale=0.1, fan_option="fan_in")
with self.init_scope():
self.conv_layer1 = L.Convolution2D(
in_channels=in_out_channels,
out_channels=inter_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.conv_layer2 = L.Convolution2D(
in_channels=None,
out_channels=inter_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.conv_layer3 = L.Convolution2D(
in_channels=None,
out_channels=inter_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.conv_layer4 = L.Convolution2D(
in_channels=None,
out_channels=inter_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.conv_layer5 = L.Convolution2D(
in_channels=None,
out_channels=in_out_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
def forward(self, x):
"""
Forward computation, i.e. evaluate based on input x
"""
a0 = x
a1 = self.conv_layer1(a0)
a1 = F.leaky_relu(x=a1, slope=0.2)
a1_cat = F.concat(xs=(a0, a1), axis=1)
a2 = self.conv_layer2(a1_cat)
a2 = F.leaky_relu(x=a2, slope=0.2)
a2_cat = F.concat(xs=(a0, a1, a2), axis=1)
a3 = self.conv_layer3(a2_cat)
a3 = F.leaky_relu(x=a3, slope=0.2)
a3_cat = F.concat(xs=(a0, a1, a2, a3), axis=1)
a4 = self.conv_layer4(a3_cat)
a4 = F.leaky_relu(x=a4, slope=0.2)
a4_cat = F.concat(xs=(a0, a1, a2, a3, a4), axis=1)
a5 = self.conv_layer5(a4_cat)
# Final concatenation, with residual scaling of 0.2
a6 = F.add(a5 * self.residual_scaling, a0)
return a6
# %%
class ResInResDenseBlock(chainer.Chain):
"""
Residual in Residual Dense block made of 3 Residual Dense Blocks
------------ ---------- ------------
| || || |
-----DenseBlock--DenseBlock--DenseBlock-(+)--
| |
--------------------------------------
"""
def __init__(
self, denseblock_class=ResidualDenseBlock, residual_scaling: float = 0.1
):
super().__init__()
self.residual_scaling = residual_scaling
with self.init_scope():
self.residual_dense_block1 = denseblock_class(
residual_scaling=residual_scaling
)
self.residual_dense_block2 = denseblock_class(
residual_scaling=residual_scaling
)
self.residual_dense_block3 = denseblock_class(
residual_scaling=residual_scaling
)
def forward(self, x):
"""
Forward computation, i.e. evaluate based on input x
"""
a1 = self.residual_dense_block1(x)
a2 = self.residual_dense_block2(a1)
a3 = self.residual_dense_block3(a2)
# Final concatenation, with residual scaling of 0.2
a4 = F.add(a3 * self.residual_scaling, x)
return a4
# %% [markdown]
# ### 2.1.3 Build the Generator Network, with upsampling layers!
#
# ![4 inputs feeding into the Generator Network, producing a high resolution prediction output](https://yuml.me/dfd301a2.png)
#
# <!--
# [W3_input(ACCUMULATION)|1x11x11]-k3n32s1>[W3_inter|32x9x9],[W3_inter]->[Concat|128x9x9]
# [W2_input(MEASURES)|2x22x22]-k6n32s2>[W2_inter|32x9x9],[W2_inter]->[Concat|128x9x9]
# [W1_input(REMA)|1x110x110]-k30n32s10>[W1_inter|32x9x9],[W1_inter]->[Concat|128x9x9]
# [X_input(BEDMAP2)|1x11x11]-k3n32s1>[X_inter|32x9x9],[X_inter]->[Concat|128x9x9]
# [Concat|128x9x9]->[Generator-Network|Many-Residual-Blocks],[Generator-Network]->[Y_hat(High-Resolution_DEM)|1x36x36]
# -->
# %%
class GeneratorModel(chainer.Chain):
"""
The generator network which is a deconvolutional neural network.
Converts a low resolution input into a super resolution output.
Glues the input block with several residual blocks and upsampling layers
Parameters:
num_residual_blocks -- how many Residual-in-Residual Dense Blocks to use
residual_scaling -- scale factor for residuals before adding to parent branch
out_channels -- integer representing number of output channels/filters/kernels
Example:
A convolved input_shape of (9,9,1) passing through b residual blocks with
a scaling of 4 and out_channels 1 will result in an image of shape (36,36,1)
>>> generator_model = GeneratorModel()
>>> y_pred = generator_model.forward(
... x=np.random.rand(1, 1, 11, 11).astype("float32"),
... w1=np.random.rand(1, 1, 110, 110).astype("float32"),
... w2=np.random.rand(1, 2, 22, 22).astype("float32"),
... w3=np.random.rand(1, 1, 11, 11).astype("float32"),
... )
>>> y_pred.shape
(1, 1, 36, 36)
>>> generator_model.count_params()
8907749
"""
def __init__(
self,
inblock_class=DeepbedmapInputBlock,
resblock_class=ResInResDenseBlock,
num_residual_blocks: int = 12,
residual_scaling: float = 0.1,
out_channels: int = 1,
):
super().__init__()
self.num_residual_blocks = num_residual_blocks
self.residual_scaling = residual_scaling
init_weights = chainer.initializers.HeNormal(scale=0.1, fan_option="fan_in")
with self.init_scope():
# Initial Input and Residual Blocks
self.input_block = inblock_class()
self.pre_residual_conv_layer = L.Convolution2D(
in_channels=None,
out_channels=64,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.residual_network = resblock_class(
residual_scaling=residual_scaling
).repeat(n_repeat=num_residual_blocks)
self.post_residual_conv_layer = L.Convolution2D(
in_channels=None,
out_channels=64,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
# Upsampling Layers
self.post_upsample_conv_layer_1 = L.Convolution2D(
in_channels=None,
out_channels=64,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
self.post_upsample_conv_layer_2 = L.Convolution2D(
in_channels=None,
out_channels=64,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
initialW=init_weights,
)
# Final post-upsample convolution layers
self.final_conv_layer1 = L.DeformableConvolution2D(
in_channels=None,
out_channels=64,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
offset_initialW=init_weights,
deform_initialW=init_weights,
)
self.final_conv_layer2 = L.DeformableConvolution2D(
in_channels=None,
out_channels=out_channels,
ksize=(3, 3),
stride=(1, 1),
pad=1, # 'same' padding
offset_initialW=init_weights,
deform_initialW=init_weights,
)
def forward(
self, x: cupy.ndarray, w1: cupy.ndarray, w2: cupy.ndarray, w3: cupy.ndarray
):
"""
Forward computation, i.e. evaluate based on input tensors
Each input should be either a numpy or cupy array.
"""
# 0 part
# Resize inputs to right scale using convolution
# with hardcoded kernel_sizes, strides and padding lengths
# Also concatenate all inputs
a0 = self.input_block(x=x, w1=w1, w2=w2, w3=w3)
# 1st part
# Pre-residual k3n64s1
a1 = self.pre_residual_conv_layer(a0)
a1 = F.leaky_relu(x=a1, slope=0.2)
# 2nd part
# Residual blocks k3n64s1
a2 = self.residual_network(a1)
# 3rd part
# Post-residual blocks k3n64s1
a3 = self.post_residual_conv_layer(a2)
a3 = F.add(a1, a3)
# 4th part
# Upsampling (hardcoded to be 4x, actually 2x run twice)
# Uses Nearest Neighbour Interpolation followed by Convolution2D k3n64s1
a4_1 = F.resize_images(
x=a3, output_shape=(2 * a3.shape[-2], 2 * a3.shape[-1]), mode="nearest"
)
a4_1 = self.post_upsample_conv_layer_1(a4_1)
a4_1 = F.leaky_relu(x=a4_1, slope=0.2)
a4_2 = F.resize_images(
x=a4_1,
output_shape=(2 * a4_1.shape[-2], 2 * a4_1.shape[-1]),
mode="nearest",
)
a4_2 = self.post_upsample_conv_layer_2(a4_2)
a4_2 = F.leaky_relu(x=a4_2, slope=0.2)
# 5th part
# Generate high resolution output k3n64s1 and k3n1s1
a5_1 = self.final_conv_layer1(a4_2)
a5_1 = F.leaky_relu(x=a5_1, slope=0.2)
a5_2 = self.final_conv_layer2(a5_1)
return a5_2
# %% [markdown]
# ## 2.2 Discriminator Network Architecture
#
# Discriminator implementation following that of [ESRGAN](https://arxiv.org/abs/1809.00219).
# [VGG-style](https://arxiv.org/abs/1409.1556)
# Consists of 10 Conv2D-BatchNorm-LeakyReLU blocks, followed by 2 Fully Connected Layers of size 100 and 1, with **no** final sigmoid activation.
# Note also how the BatchNormalization layers **are still preserved**.
# Original Pytorch implementation can be found [here](https://github.com/xinntao/BasicSR/blame/902b4ae1f4beec7359de6e62ed0aebfc335d8dfd/codes/models/modules/architecture.py#L86-L129).
#
# ![Discriminator Network](https://yuml.me/diagram/scruffy/class/[High-Resolution_DEM|32x32x1]->[Discriminator-Network],[Discriminator-Network]->[False/True|0/1])
# %%
class DiscriminatorModel(chainer.Chain):
"""
The discriminator network which is a convolutional neural network.
Takes ONE high resolution input image and predicts whether it is
real or fake on a scale of 0 to 1, where 0 is fake and 1 is real.
Consists of several Conv2D-BatchNorm-LeakyReLU blocks, followed by
a fully connected linear layer with LeakyReLU activation and a final
fully connected linear layer with Sigmoid activation.
>>> discriminator_model = DiscriminatorModel()
>>> y_pred = discriminator_model.forward(
... x=np.random.rand(2, 1, 36, 36).astype("float32")
... )
>>> y_pred.shape
(2, 1)
>>> discriminator_model.count_params()
10370761
"""
def __init__(self):
super().__init__()
init_weights = chainer.initializers.HeNormal(scale=0.1, fan_option="fan_in")
with self.init_scope():
self.conv_layer0 = L.Convolution2D(
in_channels=None,
out_channels=64,
ksize=3,
stride=1,
pad=1, # 'same' padding
nobias=False, # only first Conv2D layer uses bias
initialW=init_weights,
)
self.conv_layer1 = L.Convolution2D(None, 64, 4, 2, 1, True, init_weights)
self.conv_layer2 = L.Convolution2D(None, 128, 3, 1, 1, True, init_weights)
self.conv_layer3 = L.Convolution2D(None, 128, 4, 2, 1, True, init_weights)
self.conv_layer4 = L.Convolution2D(None, 128, 3, 1, 1, True, init_weights)
self.conv_layer5 = L.Convolution2D(None, 256, 4, 2, 1, True, init_weights)
self.conv_layer6 = L.Convolution2D(None, 256, 3, 1, 1, True, init_weights)
self.conv_layer7 = L.Convolution2D(None, 512, 4, 2, 1, True, init_weights)
self.conv_layer8 = L.Convolution2D(None, 512, 3, 1, 1, True, init_weights)
self.conv_layer9 = L.Convolution2D(None, 512, 4, 2, 1, True, init_weights)
self.batch_norm1 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm2 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm3 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm4 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm5 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm6 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm7 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm8 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.batch_norm9 = L.BatchNormalization(axis=(0, 2, 3), eps=1e-5)
self.linear_1 = L.Linear(in_size=None, out_size=100, initialW=init_weights)
self.linear_2 = L.Linear(in_size=None, out_size=1, initialW=init_weights)
def forward(self, x: cupy.ndarray):
"""
Forward computation, i.e. evaluate based on input tensor
Each input should be either a numpy or cupy array.
"""
# 1st part
# Convolutonal Block without Batch Normalization k3n64s1
a0 = self.conv_layer0(x=x)
a0 = F.leaky_relu(x=a0, slope=0.2)
# 2nd part
# Convolutional Blocks with Batch Normalization k3n{64*f}s{1or2}
a1 = self.conv_layer1(x=a0)
a1 = self.batch_norm1(x=a1)
a1 = F.leaky_relu(x=a1, slope=0.2)
a2 = self.conv_layer2(x=a1)
a2 = self.batch_norm2(x=a2)
a2 = F.leaky_relu(x=a2, slope=0.2)
a3 = self.conv_layer3(x=a2)
a3 = self.batch_norm3(x=a3)
a3 = F.leaky_relu(x=a3, slope=0.2)
a4 = self.conv_layer4(x=a3)
a4 = self.batch_norm4(x=a4)
a4 = F.leaky_relu(x=a4, slope=0.2)
a5 = self.conv_layer5(x=a4)
a5 = self.batch_norm5(x=a5)
a5 = F.leaky_relu(x=a5, slope=0.2)
a6 = self.conv_layer6(x=a5)
a6 = self.batch_norm6(x=a6)
a6 = F.leaky_relu(x=a6, slope=0.2)
a7 = self.conv_layer7(x=a6)
a7 = self.batch_norm7(x=a7)
a7 = F.leaky_relu(x=a7, slope=0.2)
a8 = self.conv_layer8(x=a7)
a8 = self.batch_norm8(x=a8)
a8 = F.leaky_relu(x=a8, slope=0.2)
a9 = self.conv_layer9(x=a8)
a9 = self.batch_norm9(x=a9)
a9 = F.leaky_relu(x=a9, slope=0.2)
# 3rd part
# Flatten, Dense (Fully Connected) Layers and Output
a10 = F.reshape(x=a9, shape=(len(a9), -1)) # flatten while keeping batch_size
a10 = self.linear_1(x=a10)
a10 = F.leaky_relu(x=a10, slope=0.2)
a11 = self.linear_2(x=a10)
# a11 = F.sigmoid(x=a11) # no sigmoid activation, as it is in the loss function
return a11
# %% [markdown]
# ## 2.3 Define Loss function and Metrics for the Generator and Discriminator Networks
#
# Now we define the Perceptual Loss function for our Generator and Discriminator neural network models, where:
#
# $$Perceptual Loss = Content Loss + Adversarial Loss + Topographic Loss + Structural Loss$$
#
# ![Perceptual Loss in an adapted Enhanced Super Resolution Generative Adversarial Network](https://yuml.me/19155033.png)
#
# <!--
# [LowRes-Inputs]-Generator>[SuperResolution_DEM]
# [SuperResolution_DEM]-.->[note:Content-Loss|MeanAbsoluteError{bg:yellow}]
# [LowRes-Inputs]-.->[note:Topographic-Loss|MeanAbsoluteError{bg:yellow}]
# [SuperResolution_DEM]-.->[note:Structural-Loss|SSIM{bg:yellow}]
# [SuperResolution_DEM]-.->[note:Topographic-Loss]
# [HighRes-Groundtruth_DEM]-.->[note:Content-Loss]
# [HighRes-Groundtruth_DEM]-.->[note:Structural-Loss]
# [SuperResolution_DEM]-Discriminator>[False_or_True_Prediction]
# [HighRes-Groundtruth_DEM]-Discriminator>[False_or_True_Prediction]
# [False_or_True_Prediction]<->[False_or_True_Label]
# [False_or_True_Prediction]-.->[note:Adversarial-Loss|BinaryCrossEntropy{bg:yellow}]
# [False_or_True_Label]-.->[note:Adversarial-Loss]
# [note:Content-Loss]-.->[note:Perceptual-Loss{bg:gold}]
# [note:Adversarial-Loss]-.->[note:Perceptual-Loss{bg:gold}]
# [note:Topographic-Loss]-.->[note:Perceptual-Loss{bg:gold}]
# [note:Structural-Loss]-.->[note:Perceptual-Loss{bg:gold}]
# -->
# %% [markdown]
# ### Content Loss
#
# The original SRGAN paper by [Ledig et al. 2017](https://arxiv.org/abs/1609.04802v5) calculates *Content Loss* based on the ReLU activation layers of the pre-trained 19 layer VGG network.
# The implementation below is less advanced, simply using an L1 loss, i.e., a pixel-wise [Mean Absolute Error (MAE) loss](https://docs.chainer.org/en/latest/reference/generated/chainer.functions.mean_absolute_error.html) as the *Content Loss*.
# Specifically, the *Content Loss* is calculated as the MAE difference between the output of the generator model (i.e. the predicted Super Resolution Image) and that of the groundtruth image (i.e. the true High Resolution Image).
#
# $$ e_i = ||G(x_{i}) - y_i||_{1} $$
#
# $$ Loss_{Content} = Mean Absolute Error = \dfrac{1}{n} \sum\limits_{i=1}^n e_i $$
#
# where $G(x_{i})$ is the Generator Network's predicted value, and $y_i$ is the groundtruth value, respectively at pixel $i$.
# $e_i$ thus represents the absolute error (L1 loss) (denoted by $||\dots||_{1}$) between the predicted and groundtruth value.
# We then sum all the pixel-wise errors $e_i,\dots,e_n$ and divide by the number of pixels $n$ to get the Arithmetic Mean $\dfrac{1}{n} \sum\limits_{i=1}^n$ of our error which is our *Content Loss*.
# %% [markdown]
# ### Adversarial Loss
#
# The *Adversarial Loss* or *Generative Loss* (confusing I know) is the same as in the original SRGAN paper.
# It is defined based on the probabilities of the discriminator believing that the reconstructed Super Resolution Image is a natural High Resolution Image.
# The implementation below uses the [Binary CrossEntropy loss](https://keras.io/losses/#binary_crossentropy).
# Specifically, this *Adversarial Loss* is calculated between the output of the discriminator model (a value between 0 and 1) and that of the groundtruth label (a boolean value of either 0 or 1).
#
# $$ Loss_{Adversarial} = Binary Cross Entropy Loss = -\dfrac{1}{n} \sum\limits_{i=1}^n ( y_i ln(\sigma(x_i)) + (1-y_i) ln(1 - \sigma(x_i) ) $$
#
# where $\sigma$ is the [Sigmoid](https://en.wikipedia.org/wiki/Sigmoid_function) activation function, $\sigma = \dfrac{1}{1+e^{-x}} = \dfrac{e^x}{e^x+1}$, $y_i$ is the groundtruth label (1 for real, 0 for fake) and $x_i$ is the prediction (before sigmoid activation is applied), all respectively at pixel $i$.
#
# $\sigma(x)$ is basically the sigmoid activated output from a Standard Discriminator neural network, which some people also denote as $D(.)$.
# Technically, some people also write $D(x) = \sigma(C(x))$, where $C(x)$ is the raw, non-transformed output from the Discriminator neural network (i.e. no sigmoid activation applied) on the input data $x$.
# For simplicity, we now denote $C(x)$ simply as $x$ in the following equations, i.e. using $\sigma(x)$ to replace $\sigma(C(x))$.
#
# Again, the [Binary Cross Entropy Loss](https://en.wikipedia.org/wiki/Cross_entropy#Cross-entropy_error_function_and_logistic_regression) calculated on one pixel is defined as follows:
#
# $$ -( y ln(\sigma(x)) + (1-y) ln(1 - \sigma(x) )$$
#
# With the full expansion as such:
#
# $$ -\bigg[ y ln\big(\dfrac{e^x}{e^x+1}\big) + (1-y) ln\big(1 - \dfrac{e^x}{e^x+1}\big) \bigg] $$
#
# The above equation is mathematically equivalent to the one below, and can be derived using [Logarithm rules](https://en.wikipedia.org/wiki/Logarithm#Product,_quotient,_power,_and_root) such as the Power Rule and Product Rule, and using the fact that $ln(e)=1$ and $ln(1)=0$:
#
# $$ -[ xy - ln(1+e^x) ] $$
#
# However, this reformed equation is numerically unstable (see discussion [here](https://www.reddit.com/r/MachineLearning/comments/4euzmk/logsumexp_for_logistic_regression/)), and is good for values of $x<0$.
# For values of $x>=0$, there is an alternative representation which we can derive:
#
# $$ -[ xy - ln(1+e^x) - x + x ] $$
# $$ -[ x(y-1) - ln(1 + e^x) + ln(e^x) ] $$
# $$ -\bigg[ x(y-1) - ln\big(\dfrac{e^x}{1+e^x}\big) \bigg] $$
# $$ -\bigg[ x(y-1) - ln\big(\dfrac{1}{1+e^{-x}}\big) \bigg] $$
# $$ - [ x(y-1) - ln(1) + ln(1+e^{-x}) ] $$
# $$ - [ x(y-1) + ln(1+e^{-x}) $$
#
# In order to have a numerically stable function that works for both $x<0$ and $x>=0$, we can write it like so as in Caffe's implementation:
#
# $$ -[ x(y - 1_{x>=0} - ln(1+e^{x-2x\cdot1_{x>=0}}) ] $$
#
# Alternatively, Chainer does it like so:
#
# $$ -[ x(y - 1_{x>=0} - ln(1+e^{-|x|}) ] $$
#
# Or in Python code (the Chainer implemention from [here](https://github.com/chainer/chainer/blob/v6.0.0b1/chainer/functions/loss/sigmoid_cross_entropy.py#L41-L44)), bearing in mind that the natural logarithm $ln$ is `np.log` in Numpy:
#
# ```python
# sigmoidbinarycrossentropyloss = -(x * (y - (x >= 0)) - np.log1p(np.exp(-np.abs(x))))
# ```
#
# See also how [Pytorch](https://pytorch.org/docs/stable/nn.html?highlight=bcewithlogitsloss#torch.nn.BCEWithLogitsLoss) and [Tensorflow](https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits) implements this in a numerically stable manner.
# %% [markdown]
# ### Topographic Loss
#
# In addition to the L1 Content Loss, we further define a *Topographic Loss*.
# Specifically, we want each of the averaged value in each 4x4 grid of the predicted DeepBedMap image to correspond to a 1x1 pixel on the BEDMAP2 image.
#
# Due to BEDMAP2 having a 4x lower resolution than the predicted DeepBedMap DEM (1000m compared to 250m), we first apply a 4x4 [Mean/Average Pooling](https://docs.chainer.org/en/latest/reference/generated/chainer.functions.average_pooling_2d.html) operation on the DeepBedMap image, turning it into a 1x1 pixel grid that matches the shape of the BEDMAP2 image.
#
# $$ \bar{\hat{y}}_j = Mean = \dfrac{1}{n} \sum\limits_{i=1}^n y_i $$
#
# where $\bar{\hat{y}}_j$ is the mean/average of all predicted pixel values $y_i$ across the 16 high resolution (DeepBedMap) pixels $i$ within a 4x4 grid corresponding to the spatial location of one low resolution (BEDMAP2) pixel at position $j$.
# Then, we calculate the [MAE](https://docs.chainer.org/en/latest/reference/generated/chainer.functions.mean_absolute_error.html) difference between the output of the generator model (i.e. the predicted Super-Resolution DeepBedMap bed DEM Image) and that of the BEDMAP2 (i.e. the original Low Resolution Image we are super-resolving).
#
# $$ e_j = ||\bar{\hat{y}}_j - x_j||_{1} $$
#
# where $\bar{\hat{y}}_j$ is the mean of the 4x4 pixels we just calculated above, and $x_j$ is the spatially corresponding BEDMAP2 pixel, respectively at BEDMAP2 pixel $j$.
# $e_j$ thus represents the absolute error (L1 loss) (denoted by $||\dots||_{1}$) between the (averaged) super-resolution and low-resolution values.
# We then sum all the pixel-wise errors $e_j,\dots,e_m$ and divide by the number of low resolution pixels $m$ to get the Arithmetic Mean $\dfrac{1}{m} \sum\limits_{i=1}^m$ of our error which is our *Topographic Loss*.
#
# $$ Loss_{Topographic} = Mean Absolute Error = \dfrac{1}{m} \sum\limits_{j=1}^m e_j $$
# %% [markdown]
# ### Structural Loss
#
# Complementing the L1 Content Loss further, we define a *Structural (Similarity) Loss*.
# This is computed using the [Structural Similarity (SSIM)](https://en.wikipedia.org/wiki/Structural_similarity) Index,
# on a moving window (here set to 9x9) between the super resolution predicted image and high resolution groundtruth image.
# The comparison takes into account luminance, contrast and structural information and is calculated over a single window patch like so:
#
# $$SSIM(x,y) = \dfrac{(2\mu_x\mu_y + c_1)(2\sigma_{xy} + c_2)}{(\mu_x^2 + \mu_y^2 + c_1)(\sigma_x^2 + \sigma_y^2 + c_2)} $$
#
# where $\mu_x$ and $\mu_y$ are the average (mean) of predicted image $x$ and groundtruth image $y$ respectively,
# $\sigma_{xy}$ is the covariance of $x$ and $y$,
# $\sigma_x^2$ and $\sigma_y^2$ are the variance of $x$ and $y$ respectively, and
# $c_1$ and $c_2$ are two variables set to $0.01^2$ and $0.03^2$ to stabilize division with a weak denominator.
# Our Structural Loss can then be formulated as follows:
#
# $$ Loss_{Structural} = 1 - \dfrac{1}{p} \sum\limits_{i=1}^p SSIM(x, y)_p $$
#
# where we do $1$ minus the mean of all structural similarity values $SSIM(x, y)$ calculated over every patch $p$ obtained via a sliding window over our predicted image $x$ and groundtruth image $y$.
# %%
def calculate_generator_loss(
y_pred: chainer.variable.Variable,
y_true: cupy.ndarray,
fake_labels: cupy.ndarray,
real_labels: cupy.ndarray,
fake_minus_real_target: cupy.ndarray,
real_minus_fake_target: cupy.ndarray,
x_topo: cupy.ndarray,
content_loss_weighting: float = 1e-2, # e.g. ~35 * 1e-2 = 0.35
adversarial_loss_weighting: float = 2e-2, # e.g. ~10 * 2e-2 = 0.20
topographic_loss_weighting: float = 2e-3, # e.g. ~35 * 2e-3 = 0.07
structural_loss_weighting: float = 5.25e-0, # e.g. ~0.75 * 5.25e-0 = 3.9375
) -> chainer.variable.Variable:
"""
This function calculates the weighted sum between
"Content Loss", "Adversarial Loss", "Topographic Loss", and "Structural Loss"
which forms the basis for training the Generator Network.
>>> calculate_generator_loss(
... y_pred=chainer.variable.Variable(data=np.ones(shape=(2, 1, 12, 12))),
... y_true=np.full(shape=(2, 1, 12, 12), fill_value=10.0),
... fake_labels=np.array([[-1.2], [0.5]]),
... real_labels=np.array([[0.5], [-0.8]]),
... fake_minus_real_target=np.array([[1], [1]]).astype(np.int32),
... real_minus_fake_target=np.array([[0], [0]]).astype(np.int32),
... x_topo=np.full(shape=(2, 1, 3, 3), fill_value=9.0),
... )
variable(4.35108415)
"""
# Content Loss (L1, Mean Absolute Error) between predicted and groundtruth 2D images
content_loss = F.mean_absolute_error(x0=y_pred, x1=y_true)
# Adversarial Loss between 1D labels
adversarial_loss = calculate_discriminator_loss(
real_labels_pred=real_labels,
fake_labels_pred=fake_labels,
real_minus_fake_target=real_minus_fake_target, # Zeros (0) instead of ones (1)
fake_minus_real_target=fake_minus_real_target, # Ones (1) instead of zeros (0)
)
# Topographic Loss (L1, Mean Absolute Error) between predicted and low res 2D images
topographic_loss = F.mean_absolute_error(
x0=F.average_pooling_2d(x=y_pred, ksize=(4, 4)), x1=x_topo
)
# Structural Similarity Loss between predicted and groundtruth 2D images
structural_loss = 1 - ssim_loss_func(y_pred=y_pred, y_true=y_true)
# Get generator loss
weighted_content_loss = content_loss_weighting * content_loss
weighted_adversarial_loss = adversarial_loss_weighting * adversarial_loss
weighted_topographic_loss = topographic_loss_weighting * topographic_loss
weighted_structural_loss = structural_loss_weighting * structural_loss
g_loss = (
weighted_content_loss
+ weighted_adversarial_loss
+ weighted_topographic_loss
+ weighted_structural_loss
)
return g_loss
# %%
def psnr(
y_pred: cupy.ndarray, y_true: cupy.ndarray, data_range=2 ** 32
) -> cupy.ndarray:
"""
Peak Signal-Noise Ratio (PSNR) metric, calculated batchwise.
See https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio#Definition
Can take in either numpy (CPU) or cupy (GPU) arrays as input.
Implementation is same as skimage.measure.compare_psnr with data_range=2**32
>>> psnr(
... y_pred=np.ones(shape=(2, 1, 3, 3)),
... y_true=np.full(shape=(2, 1, 3, 3), fill_value=2),
... )
192.65919722494797
"""
xp = chainer.backend.get_array_module(y_true)
# Calculate Mean Squred Error along predetermined axes
mse = xp.mean(xp.square(xp.subtract(y_pred, y_true)), axis=None)
# Calculate Peak Signal-Noise Ratio, setting MAX_I as 2^32, i.e. max for int32
return xp.multiply(20, xp.log10(data_range / xp.sqrt(mse)))
# %%
def ssim_loss_func(
y_pred: chainer.variable.Variable,
y_true: cupy.ndarray,
window_size: int = 9,
stride: int = 1,
) -> chainer.variable.Variable:
"""
Structural Similarity (SSIM) loss/metric, calculated with default window size of 9.
See https://en.wikipedia.org/wiki/Structural_similarity
Can take in either numpy (CPU) or cupy (GPU) arrays as input.
>>> ssim_loss_func(
... y_pred=chainer.variable.Variable(data=np.ones(shape=(2, 1, 9, 9))),
... y_true=np.full(shape=(2, 1, 9, 9), fill_value=2.0),
... )
variable(0.800004)
"""
if not y_pred.shape == y_true.shape:
raise ValueError("Input images must have the same dimensions.")
ssim_value = ssim.functions.ssim_loss(
y=y_pred, t=y_true, window_size=window_size, stride=stride
)
return ssim_value
# %%
def calculate_discriminator_loss(
real_labels_pred: chainer.variable.Variable,
fake_labels_pred: chainer.variable.Variable,
real_minus_fake_target: cupy.ndarray,
fake_minus_real_target: cupy.ndarray,
) -> chainer.variable.Variable:
"""
This function purely calculates the "Adversarial Loss"
in a Relativistic Average Generative Adversarial Network (RaGAN).
It forms the basis for training the Discriminator Network,
but it is also used as part of the Generator Network's loss function.
See paper by Jolicoeur-Martineau, 2018 at https://arxiv.org/abs/1807.00734
for the mathematical details of the RaGAN loss function.
Original Sigmoid_Cross_Entropy formula:
-(y * np.log(sigmoid(x)) + (1 - y) * np.log(1 - sigmoid(x)))
Numerically stable formula:
-(x * (y - (x >= 0)) - np.log1p(np.exp(-np.abs(x))))
where y = the target difference between real and fake labels (i.e. 1 - 0 = 1)
x = the calculated difference between real_labels_pred and fake_labels_pred
>>> calculate_discriminator_loss(
... real_labels_pred=chainer.variable.Variable(data=np.array([[1.1], [-0.5]])),
... fake_labels_pred=chainer.variable.Variable(data=np.array([[-0.3], [1.0]])),
... real_minus_fake_target=np.array([[1], [1]]),
... fake_minus_real_target=np.array([[0], [0]]),
... )
variable(1.56670504)
"""
# Calculate arithmetic mean of real/fake predicted labels
real_labels_pred_avg = F.mean(real_labels_pred)
fake_labels_pred_avg = F.mean(fake_labels_pred)
# Binary Cross-Entropy Loss with Sigmoid
real_versus_fake_loss = F.sigmoid_cross_entropy(
x=(real_labels_pred - fake_labels_pred_avg), t=real_minus_fake_target