-
Notifications
You must be signed in to change notification settings - Fork 18
/
models.py
1093 lines (874 loc) · 39.7 KB
/
models.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
"""
Models
"""
import tensorflow as tf
from absl import flags
from vrnn import VRNN
FLAGS = flags.FLAGS
models = {}
def register_model(name):
""" Add model to the list of models, e.g. add @register_model("name")
before a class definition """
assert name not in models, "duplicate model named " + name
def decorator(cls):
models[name] = cls
return cls
return decorator
def get_model(name, *args, **kwargs):
""" Based on the given name, call the correct model """
assert name in models.keys(), \
"Unknown model name " + name
return models[name](*args, **kwargs)
def list_models():
""" Returns list of all the available models """
return list(models.keys())
@tf.custom_gradient
def flip_gradient(x, grl_lambda):
""" Forward pass identity, backward pass negate gradient and multiply by """
grl_lambda = tf.cast(grl_lambda, dtype=tf.float32)
def grad(dy):
# the 0 is for grl_lambda, which doesn't have a gradient
return tf.negative(dy) * grl_lambda * tf.ones_like(x), 0
return x, grad
class FlipGradient(tf.keras.layers.Layer):
"""
Gradient reversal layer
global_step = tf.Variable storing the current step
schedule = a function taking the global_step and computing the grl_lambda,
e.g. `lambda step: 1.0` or some more complex function.
"""
def __init__(self, global_step, grl_schedule, **kwargs):
super().__init__(**kwargs)
self.global_step = global_step
self.grl_schedule = grl_schedule
def call(self, inputs, **kwargs):
""" Calculate grl_lambda first based on the current global step (a
variable) and then create the layer that does nothing except flip
the gradients """
grl_lambda = self.grl_schedule(self.global_step)
return flip_gradient(inputs, grl_lambda)
def DannGrlSchedule(num_steps):
""" GRL schedule from DANN paper """
num_steps = tf.cast(num_steps, tf.float32)
def schedule(step):
step = tf.cast(step, tf.float32)
return 2/(1+tf.exp(-10*(step/(num_steps+1))))-1
return schedule
class StopGradient(tf.keras.layers.Layer):
""" Stop gradient layer """
def call(self, inputs, **kwargs):
return tf.stop_gradient(inputs)
class ModelBase(tf.keras.Model):
""" Base model class (inheriting from Keras' Model class) """
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _get_trainable_variables_list(self, model_list):
""" Get all trainable variables if model is a list """
model_vars = []
for m in model_list:
model_vars += m.trainable_variables
return model_vars
def _get_trainable_variables(self, model):
""" Get trainable variables if model is a list or not """
if isinstance(model, list):
return self._get_trainable_variables_list(model)
return model.trainable_variables
@property
def trainable_variables_fe(self):
return self._get_trainable_variables(self.feature_extractor)
@property
def trainable_variables_task(self):
return self._get_trainable_variables(self.task_classifier)
@property
def trainable_variables_domain(self):
return self._get_trainable_variables(self.domain_classifier)
@property
def trainable_variables_task_fe(self):
return self.trainable_variables_fe \
+ self.trainable_variables_task
@property
def trainable_variables_task_fe_domain(self):
return self.trainable_variables_fe \
+ self.trainable_variables_task \
+ self.trainable_variables_domain
@property
def trainable_variables(self):
""" Returns all trainable variables in the model """
return self.trainable_variables_task_fe_domain
def set_learning_phase(self, training):
# Manually set the learning phase since we probably aren't using .fit()
# but layers like batch norm and dropout still need to know if
# training/testing
if training is True:
tf.keras.backend.set_learning_phase(1)
elif training is False:
tf.keras.backend.set_learning_phase(0)
# Allow easily overriding each part of the call() function, without having
# to override call() in its entirety
def call_feature_extractor(self, inputs, which_fe=None, which_tc=None,
which_dc=None, **kwargs):
if which_fe is not None:
assert isinstance(self.feature_extractor, list)
return self.feature_extractor[which_fe](inputs, **kwargs)
return self.feature_extractor(inputs, **kwargs)
def call_task_classifier(self, fe, which_fe=None, which_tc=None,
which_dc=None, **kwargs):
if which_tc is not None:
assert isinstance(self.task_classifier, list)
return self.task_classifier[which_tc](fe, **kwargs)
return self.task_classifier(fe, **kwargs)
def call_domain_classifier(self, fe, task, which_fe=None, which_tc=None,
which_dc=None, **kwargs):
if which_dc is not None:
assert isinstance(self.domain_classifier, list)
return self.domain_classifier[which_dc](fe, **kwargs)
return self.domain_classifier(fe, **kwargs)
def call(self, inputs, training=None, **kwargs):
self.set_learning_phase(training)
fe = self.call_feature_extractor(inputs, **kwargs)
task = self.call_task_classifier(fe, **kwargs)
domain = self.call_domain_classifier(fe, task, **kwargs)
return task, domain, fe
class ModelMakerBase:
"""
Make the feature extractor, task classifier, and domain classifier models
This is a class instead of just a make_xyz_model() returning the 3 parts
because in some cases (e.g. Heterogeneous DA) where we need multiple FE's
or (e.g. DannSmoothModel) where we need multiple DC's.
Also, this allows for sharing similar task/domain classifiers used in
multiple models.
"""
def __init__(self, **kwargs):
pass
def make_feature_extractor(self, **kwargs):
raise NotImplementedError("must implement for ModelMaker class")
def make_task_classifier(self, num_classes, **kwargs):
raise NotImplementedError("must implement for ModelMaker class")
def make_domain_classifier(self, num_domains, **kwargs):
raise NotImplementedError("must implement for ModelMaker class")
class CodatsModelMakerBase(ModelMakerBase):
""" Task and domain classifiers used for CoDATS and thus used for a number
of these models """
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
# Note: alternative is Dense(128, activation="tanh") like used by
# https://arxiv.org/pdf/1902.09820.pdf They say dropout of 0.7 but
# I'm not sure if that means 1-0.7 = 0.3 or 0.7 itself.
tf.keras.layers.Dense(500, use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(500, use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(num_domains),
])
# class AdjustmentLayer(tf.keras.layers.Layer):
# def __init__(self, layers, **kwargs):
# self.layers = layers
# super().__init__(**kwargs)
# # TODO add regularization / L2 norm or something
# def build(self, input_shapes):
# # Keep track of the weights
# weights = []
# # We want each layer but adjusted: multiply by variable and add variable
# for i, layer in enumerate(self.layers):
# for j, variable in enumerate(self.layer.trainable_variables):
# m = self.add_weight("layer"+str(i)+"_var"+str(j),
# shape=(self.n_h, self.n_prior_hidden), initializer="glorot_uniform")
# def call(self, inputs, **kwargs):
# return self.seq(inputs, **kwargs)
@register_model("fcn")
class FcnModelMaker(CodatsModelMakerBase):
"""
FCN (fully CNN) -- but domain classifier has additional dense layers
From: https://arxiv.org/pdf/1611.06455.pdf
Tested in: https://arxiv.org/pdf/1809.04356.pdf
Code from: https://github.com/hfawaz/dl-4-tsc/blob/master/classifiers/fcn.py
"""
def make_feature_extractor(self, previous_model=None, **kwargs):
# Make a new feature extractor if no previous feature extractor
if previous_model is None:
return tf.keras.Sequential([
tf.keras.layers.Conv1D(filters=128, kernel_size=8, padding="same",
use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Conv1D(filters=256, kernel_size=5, padding="same",
use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Conv1D(filters=128, kernel_size=3, padding="same",
use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.GlobalAveragePooling1D(),
])
# Only totally separate layer is the first Conv1D layer since the
# input shape may be different. The rest of the layers will be the
# layers from the other model.
return tf.keras.Sequential([
tf.keras.layers.Conv1D(filters=128, kernel_size=8, padding="same",
use_bias=False)
] + previous_model.layers[1:])
# However, if we do have a previous feature extractor, this one will
# be *changes* to the previous one, and regularized to be similar.
# return tf.keras.Sequential([
# tf.keras.layers.Conv1D(filters=128, kernel_size=8, padding="same",
# use_bias=False),
# # The rest of the layers will be the layers from the other model but
# # with some changes (multiply by some value and/or add by some
# # value). Note we skip the first layer since we replaced it above
# # with a new one entirely.
# AdjustmentLayer(layers=previous_model.layers[1:]),
# ])
class InceptionModule(tf.keras.layers.Layer):
""" Consists of the multiple kernel-size conv1d outputs concatenated
together """
def __init__(self, num_filters=32, activation="relu", **kwargs):
super().__init__(**kwargs)
self.num_filters = num_filters
# Step 1
self.bottleneck = self._conv1d(num_filters, kernel_size=1)
self.maxpool = tf.keras.layers.MaxPool1D(pool_size=3, strides=1,
padding="same")
# Step 2
#
# Note: if kernel_size=40 in the original code, and
# kernel_size_s = [self.kernel_size // (2 ** i) for i in range(3)]
# then we get 40, 20, 10 (note order doesn't matter since we concatenate
# them).
self.z1 = self._conv1d(num_filters, kernel_size=10)
self.z2 = self._conv1d(num_filters, kernel_size=20)
self.z3 = self._conv1d(num_filters, kernel_size=40)
self.z4 = self._conv1d(num_filters, kernel_size=1)
# Step 3 -- concatenate along feature dimension (axis=2 or axis=-1)
self.concat = tf.keras.layers.Concatenate(axis=-1)
self.bn = tf.keras.layers.BatchNormalization()
self.act = tf.keras.layers.Activation(activation)
# def get_config(self):
# config = super().get_config()
# config.update({
# "num_filters": self.num_filters,
# "activation": self.activation,
# })
# return config
def _conv1d(self, filters, kernel_size):
# Note: the blog post has some differences (presumably not matching the
# paper's code then) leaves of padding="same" (implying padding="valid"
# instead) and activation="relu" rather than activation="linear" in the
# paper's code (or here activation=None, the default).
#
# Or, maybe this is TF vs. Keras default differences.
return tf.keras.layers.Conv1D(filters=filters, kernel_size=kernel_size,
padding="same", use_bias=False)
def call(self, inputs, **kwargs):
# Step 1
Z_bottleneck = self.bottleneck(inputs, **kwargs)
Z_maxpool = self.maxpool(inputs, **kwargs)
# Step 2
Z1 = self.z1(Z_bottleneck, **kwargs)
Z2 = self.z2(Z_bottleneck, **kwargs)
Z3 = self.z3(Z_bottleneck, **kwargs)
Z4 = self.z4(Z_maxpool, **kwargs)
# Step 3
Z = self.concat([Z1, Z2, Z3, Z4])
Z = self.bn(Z, **kwargs)
return self.act(Z)
class InceptionShortcut(tf.keras.layers.Layer):
""" Shortcut for InceptionBlock -- required separate for a separate build()
since we don't know the right output dimension till running the network. """
def __init__(self, **kwargs):
super().__init__(**kwargs)
def build(self, input_shapes):
Z_residual_shape, Z_inception_shape = input_shapes
_, _, output_filters = Z_inception_shape
self.shortcut_conv1d = tf.keras.layers.Conv1D(filters=output_filters,
kernel_size=1, padding="same", use_bias=False)
self.shortcut_bn = tf.keras.layers.BatchNormalization()
self.shortcut_add = tf.keras.layers.Add()
def call(self, inputs, **kwargs):
Z_residual, Z_inception = inputs
# Create shortcut connection
Z_shortcut = self.shortcut_conv1d(Z_residual)
Z_shortcut = self.shortcut_bn(Z_shortcut)
# Add shortcut to Inception
return self.shortcut_add([Z_shortcut, Z_inception])
class InceptionBlock(tf.keras.layers.Layer):
""" Block consisting of 3 InceptionModules with shortcut at the end """
def __init__(self, num_modules=3, activation="relu", **kwargs):
super().__init__(**kwargs)
self.num_modules = num_modules
self.activation = activation
self.modules = [InceptionModule() for _ in range(num_modules)]
self.skip = InceptionShortcut()
self.act = tf.keras.layers.Activation(activation)
# def get_config(self):
# """ Required to save __init__ args when cloning
# See: https://www.tensorflow.org/guide/keras/custom_layers_and_models#you_can_optionally_enable_serialization_on_your_layers
# """
# config = super().get_config()
# config.update({
# "num_modules": self.num_modules,
# "activation": self.activation,
# })
# return config
def call(self, inputs, **kwargs):
Z = inputs
Z_residual = inputs
for i in range(self.num_modules):
Z = self.modules[i](Z, **kwargs)
Z = self.skip([Z_residual, Z], **kwargs)
return self.act(Z)
@register_model("inceptiontime")
class InceptionTimeModelMaker(CodatsModelMakerBase):
"""
InceptionTime -- but domain classifier has additional dense layers
Paper: https://arxiv.org/pdf/1909.04939.pdf
Keras code: https://towardsdatascience.com/deep-learning-for-time-series-classification-inceptiontime-245703f422db
Paper's code: https://github.com/hfawaz/InceptionTime
and in particular: https://github.com/hfawaz/InceptionTime/blob/master/classifiers/inception.py
Inherit from CoDATS model base since we'll use the same TC/DC maker functions.
InceptionTime is not designed for domain adaptation, just for time series
classification, so we'll use our CoDATS TC/DC choices.
"""
def make_feature_extractor(self, previous_model=None, **kwargs):
""" The entire InceptionTime feature extractor (just doesn't have last
dense layer, i.e. stops at GAP). Note: their code has num_modules=6, and
every third has a skip connection. Thus, that's the same as 2 blocks.
"""
if previous_model is None:
return tf.keras.Sequential([
InceptionBlock(),
InceptionBlock(),
tf.keras.layers.GlobalAveragePooling1D(),
])
else:
raise NotImplementedError(
"currently only FCN works with --share_most_weights")
def make_dense_bn_dropout(units, dropout):
return tf.keras.Sequential([
tf.keras.layers.Dense(units, use_bias=False), # BN has a bias term
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dropout(dropout),
])
def make_dense_ln_dropout(units, dropout):
return tf.keras.Sequential([
tf.keras.layers.Dense(units, use_bias=False), # BN has a bias term
tf.keras.layers.LayerNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.Dropout(dropout),
])
@register_model("mlp")
class MlpModelMaker(CodatsModelMakerBase):
"""
MLP -- but split task/domain classifier at last dense layer, and additional
dense layer for domain classifier
From: https://arxiv.org/pdf/1611.06455.pdf
Tested in: https://arxiv.org/pdf/1809.04356.pdf
Code from: https://github.com/hfawaz/dl-4-tsc/blob/master/classifiers/mlp.py
"""
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(500, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(500, activation="relu"),
tf.keras.layers.Dropout(0.2),
])
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(500, activation="relu"),
tf.keras.layers.Dropout(0.3),
tf.keras.layers.Dense(num_classes),
])
class ReflectSamePadding(tf.keras.layers.Layer):
"""
Output the same way that "same" padding would, but instead of zero padding
do reflection padding.
"""
def __init__(self, kernel_size, strides=1, **kwargs):
super().__init__(**kwargs)
self.kernel_size = kernel_size
self.strides = strides
def call(self, inputs, **kwargs):
time_steps = inputs.shape[1]
_, pad_before, pad_after = self.calc_padding(time_steps,
self.kernel_size, self.strides, "same")
# Note: for some reason works better when swapping before/after so that
# for odd paddings, we have the extra padding at the left rather than
# the right
return tf.pad(inputs, [[0, 0], [pad_after, pad_before], [0, 0]], "reflect")
def calc_padding(self, input_size, filter_size, stride, pad_type):
"""
See code (used to be in the API guide but since has vanished):
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/common_shape_fns.cc#L45
Note: copied from my tflite code
https://github.com/floft/vision-landing/blob/master/tflite_opencl.py
"""
assert pad_type == "valid" or pad_type == "same", \
"Only SAME and VALID padding types are implemented"
if pad_type == "valid":
output_size = int((input_size - filter_size + stride) / stride)
pad_before = 0
pad_after = 0
elif pad_type == "same":
output_size = int((input_size + stride - 1) / stride)
pad_needed = max(0, (output_size - 1)*stride + filter_size - input_size)
pad_before = pad_needed // 2
pad_after = pad_needed - pad_before
assert output_size >= 0, "output_size must be non-negative after padding"
return output_size, pad_before, pad_after
class ResnetBlock(tf.keras.layers.Layer):
""" Block consisting of other blocks but with residual connections """
def __init__(self, units, dropout, layers, layer_norm=False, **kwargs):
super().__init__(**kwargs)
if layer_norm:
self.blocks = [make_dense_ln_dropout(units, dropout) for _ in range(layers)]
else:
self.blocks = [make_dense_bn_dropout(units, dropout) for _ in range(layers)]
self.add = tf.keras.layers.Add()
def call(self, inputs, **kwargs):
""" Like Sequential but with a residual connection """
shortcut = inputs
net = inputs
for block in self.blocks:
net = block(net, **kwargs)
return self.add([shortcut, net], **kwargs)
class WangResnetBlock(tf.keras.layers.Layer):
"""
ResNet block for the "ResNet" model by Wang et al. (2017)
See make_resnet_model()
"""
def __init__(self, n_feature_maps, shortcut_resize=True,
kernel_sizes=[8, 5, 3], reflect_padding=False,
normalization=tf.keras.layers.BatchNormalization,
activation="relu", **kwargs):
super().__init__(**kwargs)
self.blocks = []
for kernel_size in kernel_sizes:
if reflect_padding:
self.blocks.append(tf.keras.Sequential([
ReflectSamePadding(kernel_size),
tf.keras.layers.Conv1D(filters=n_feature_maps,
kernel_size=kernel_size,
padding="valid", use_bias=False),
normalization(),
tf.keras.layers.Activation(activation),
]))
else:
self.blocks.append(tf.keras.Sequential([
tf.keras.layers.Conv1D(filters=n_feature_maps,
kernel_size=kernel_size,
padding="same", use_bias=False),
normalization(),
tf.keras.layers.Activation(activation),
]))
if shortcut_resize:
self.shortcut = tf.keras.Sequential([
tf.keras.layers.Conv1D(filters=n_feature_maps, kernel_size=1,
padding="same", use_bias=False),
normalization(),
])
else:
self.shortcut = tf.keras.Sequential([
normalization(),
])
self.add = tf.keras.layers.Add()
self.act = tf.keras.layers.Activation(activation)
def call(self, inputs, **kwargs):
net = inputs
for block in self.blocks:
net = block(net, **kwargs)
shortcut = self.shortcut(inputs, **kwargs)
add = self.add([net, shortcut], **kwargs)
return self.act(add, **kwargs)
@register_model("resnet")
class ResNetModelMaker(CodatsModelMakerBase):
"""
ResNet -- but domain classifier has additional dense layers
From: https://arxiv.org/pdf/1611.06455.pdf
Tested in: https://arxiv.org/pdf/1809.04356.pdf
Code from: https://github.com/hfawaz/dl-4-tsc/blob/master/classifiers/resnet.py
"""
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
WangResnetBlock(64),
WangResnetBlock(128),
WangResnetBlock(128, shortcut_resize=False),
tf.keras.layers.GlobalAveragePooling1D(),
])
@register_model("timenet")
class TimeNetModelMaker(ModelMakerBase):
"""
TimeNet https://arxiv.org/pdf/1706.08838.pdf
So, basically 3-layer GRU with 60 units followed by the rest in my "flat"
model above in make_vrada_model(). TimeNet doesn't seem to use dropout,
though HealthNet in https://arxiv.org/pdf/1904.00655.pdf does.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.fe_layers = 5
self.task_layers = 1
self.domain_layers = 2
self.resnet_layers = 2
self.units = 50
self.dropout = FLAGS.dropout
# General classifier used in both the task/domain classifiers
def _make_classifier(self, layers, num_outputs):
layers = [
make_dense_bn_dropout(self.units, self.dropout)
for _ in range(layers-1)
]
last = [tf.keras.layers.Dense(num_outputs)]
return tf.keras.Sequential(layers + last)
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.GRU(60, return_sequences=True),
tf.keras.layers.GRU(60, return_sequences=True),
tf.keras.layers.GRU(60),
tf.keras.layers.Flatten(),
] + [ # First can't be residual since x isn't of size units
make_dense_bn_dropout(self.units, self.dropout)
for _ in range(self.resnet_layers)
] + [
ResnetBlock(self.units, self.dropout, self.resnet_layers)
for _ in range(self.fe_layers-1)
])
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
self.make_classifier(self.task_layers, num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
self.make_classifier(self.domain_layers, num_domains),
])
@register_model("images_dann_mnist")
class DannMnistModelMaker(ModelMakerBase):
""" Figure 4(a) MNIST architecture -- Ganin et al. DANN JMLR 2016 paper """
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (5, 5), (1, 1), "valid", activation="relu"),
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "valid"),
tf.keras.layers.Conv2D(48, (5, 5), (1, 1), "valid", activation="relu"),
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "valid"),
tf.keras.layers.Flatten(),
])
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(100, "relu"),
tf.keras.layers.Dense(100, "relu"),
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(100, "relu"),
tf.keras.layers.Dense(num_domains),
])
@register_model("images_dann_svhn")
class DannSvhnModelMaker(ModelMakerBase):
""" Figure 4(b) SVHN architecture -- Ganin et al. DANN JMLR 2016 paper """
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.dropout = FLAGS.dropout
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Conv2D(64, (5, 5), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D((3, 3), (2, 2), "same"),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Conv2D(64, (5, 5), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.MaxPool2D((3, 3), (2, 2), "same"),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Conv2D(128, (5, 5), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Flatten(),
])
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(3072),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Dense(2048),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(1024),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Dense(1024),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Dropout(self.dropout),
tf.keras.layers.Dense(num_domains),
])
@register_model("images_dann_gtsrb")
class DannGtsrbModelMaker(ModelMakerBase):
""" Figure 4(c) SVHN architecture -- Ganin et al. DANN JMLR 2016 paper """
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Conv2D(96, (5, 5), (1, 1), "valid", activation="relu"),
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "valid"),
tf.keras.layers.Conv2D(144, (3, 3), (1, 1), "valid", activation="relu"),
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "valid"),
tf.keras.layers.Conv2D(256, (5, 5), (1, 1), "valid", activation="relu"),
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "valid"),
tf.keras.layers.Flatten(),
])
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(512, "relu"),
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Dense(1024, "relu"),
tf.keras.layers.Dense(1024, "relu"),
tf.keras.layers.Dense(num_domains),
])
class VadaModelMakerBase(ModelMakerBase):
""" Table 6 Small CNN -- Shu et al. VADA / DIRT-T ICLR 2018 paper
Note: they used small for digits, traffic signs, and WiFi and large for
CIFAR-10 and STL-10."""
def __init__(self, small, **kwargs):
super().__init__(**kwargs)
self.small = small
self.leak_alpha = 0.1
def _conv_blocks(self, depth):
return [
tf.keras.layers.Conv2D(depth, (3, 3), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(self.leak_alpha),
tf.keras.layers.Conv2D(depth, (3, 3), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(self.leak_alpha),
tf.keras.layers.Conv2D(depth, (3, 3), (1, 1), "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(self.leak_alpha),
]
def _pool_blocks(self):
return [
tf.keras.layers.MaxPool2D((2, 2), (2, 2), "same"),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.GaussianNoise(1),
]
def make_feature_extractor(self, **kwargs):
return tf.keras.Sequential(
self._conv_blocks(64 if self.small else 96)
+ self._pool_blocks()
+ self._conv_blocks(64 if self.small else 192)
+ self._pool_blocks())
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential(
self._conv_blocks(64 if self.small else 192)
+ [
tf.keras.layers.GlobalAvgPool2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(100),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.ReLU(),
tf.keras.layers.Dense(num_domains),
])
@register_model("images_vada_small")
class VadaSmallModelMaker(VadaModelMakerBase):
def __init__(self, **kwargs):
super().__init__(small=True, **kwargs)
@register_model("images_vada_large")
class VadaLargeModelMaker(VadaModelMakerBase):
def __init__(self, **kwargs):
super().__init__(small=False, **kwargs)
@register_model("images_resnet50")
class ResNet50ModelMaker(ModelMakerBase):
""" ResNet50 pre-trained on ImageNet -- for use with Office-31 datasets
Input should be 224x224x3 """
def make_feature_extractor(self, **kwargs):
return tf.keras.applications.ResNet50(
include_top=False, pooling="avg")
def make_task_classifier(self, num_classes, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(num_classes),
])
def make_domain_classifier(self, num_domains, **kwargs):
return tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(num_domains),
])
class CnnModelBase(ModelBase):
"""
Support a variety of CNN-based models, pick via command-line argument
Also supports having multiple FE's, TC's, or DC's. If not None, then the
corresponding variable is a list.
"""
def __init__(self, num_classes, num_domains, model_name,
num_feature_extractors=None,
num_task_classifiers=None,
num_domain_classifiers=None,
share_most_weights=False, **kwargs):
super().__init__(**kwargs)
self.num_classes = num_classes
self.num_domains = num_domains
model_maker = get_model(model_name)
self.feature_extractor = self._make_single_or_multiple(
model_maker.make_feature_extractor,
num_feature_extractors, share_most_weights)
self.task_classifier = self._make_single_or_multiple(
lambda **kwargs: model_maker.make_task_classifier(num_classes, **kwargs),
num_task_classifiers, share_most_weights)
self.domain_classifier = self._make_single_or_multiple(
lambda **kwargs: model_maker.make_domain_classifier(num_domains, **kwargs),
num_domain_classifiers, share_most_weights)
def _make_single_or_multiple(self, f, num, share_most_weights):
if num is not None:
if share_most_weights:
# Share most weights via passing in the previous model
# Note: only used for in feature extractor creation.
results = []
for _ in range(num):
previous_model = None
if len(results) > 0:
previous_model = results[-1]
results.append(f(previous_model=previous_model))
return results
else:
return [f() for _ in range(num)]
return f()
class BasicModel(CnnModelBase):
""" Model without adaptation (i.e. no DANN) """
pass
class DannModelBase:
""" DANN adds a gradient reversal layer before the domain classifier
Note: we don't inherit from CnnModelBase or any other specific model because
we want to support either CnnModelBase, RnnModelBase, etc. with multiple
inheritance.
"""
def __init__(self, num_classes, num_domains, global_step,
total_steps, **kwargs):
super().__init__(num_classes, num_domains, **kwargs)
grl_schedule = DannGrlSchedule(total_steps)
self.flip_gradient = FlipGradient(global_step, grl_schedule)
def call_domain_classifier(self, fe, task, **kwargs):
# Pass FE output through GRL then to DC
grl_output = self.flip_gradient(fe, **kwargs)
return super().call_domain_classifier(grl_output, task, **kwargs)
class DannModel(DannModelBase, CnnModelBase):
""" Model with adaptation (i.e. with DANN) """
pass
class HeterogeneousDannModel(DannModelBase, CnnModelBase):
""" Heterogeneous DANN model has multiple feature extractors """
def __init__(self, *args, num_feature_extractors=None, **kwargs):
# Require that we have num_feature_extractors
assert num_feature_extractors is not None
super().__init__(*args, num_feature_extractors=num_feature_extractors,
**kwargs)
class SleepModel(DannModelBase, CnnModelBase):
""" Sleep model is DANN but concatenating task classifier output (with stop
gradient) with feature extractor output when fed to the domain classifier """
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.concat = tf.keras.layers.Concatenate(axis=1)
self.stop_gradient = StopGradient()
def call_domain_classifier(self, fe, task, **kwargs):
# We could support this but it's awkward since we want to call the super's
# super's call_domain_classifier but not the super's version...
assert not isinstance(self.domain_classifier, list), \
"currently do not support SleepModel with multiple domain classifiers"
# Pass FE output through GRL and append stop-gradient-ed task output too