Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
1934 lines (1587 loc) · 46.6 KB
/
Copy pathmodels.py
File metadata and controls
1934 lines (1587 loc) · 46.6 KB
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
"""Model architectures for the Mars segmentation comparison app.
This file contains inference-only definitions for the five checkpoint families
whose tensor names and shapes are documented in checkpoint_inspection_report.txt.
No dataset loading, training, downloads, or output-folder creation occurs here.
All model constructors default to pretrained=False so EfficientNet weights are
not downloaded when loading an existing checkpoint.
"""
from __future__ import annotations
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights
# -----------------------------------------------------------------------------
# Five-class U-Net and U-Net + CBAM
# -----------------------------------------------------------------------------
class DoubleConv(nn.Module):
"""Two 3x3 convolution + batch norm + ReLU layers."""
def __init__(self, in_channels: int, out_channels: int):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.block(x)
class UNet5(nn.Module):
"""Standard U-Net matching unet.pth (5 output channels)."""
def __init__(self, num_classes: int = 5):
super().__init__()
self.enc1 = DoubleConv(3, 64)
self.enc2 = DoubleConv(64, 128)
self.enc3 = DoubleConv(128, 256)
self.enc4 = DoubleConv(256, 512)
self.bottleneck = DoubleConv(512, 1024)
self.pool = nn.MaxPool2d(2)
self.up1 = nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2)
self.dec1 = DoubleConv(1024, 512)
self.up2 = nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2)
self.dec2 = DoubleConv(512, 256)
self.up3 = nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2)
self.dec3 = DoubleConv(256, 128)
self.up4 = nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)
self.dec4 = DoubleConv(128, 64)
self.final = nn.Conv2d(64, num_classes, kernel_size=1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
s1 = self.enc1(x)
s2 = self.enc2(self.pool(s1))
s3 = self.enc3(self.pool(s2))
s4 = self.enc4(self.pool(s3))
b = self.bottleneck(self.pool(s4))
x = self.dec1(torch.cat((self.up1(b), s4), dim=1))
x = self.dec2(torch.cat((self.up2(x), s3), dim=1))
x = self.dec3(torch.cat((self.up3(x), s2), dim=1))
x = self.dec4(torch.cat((self.up4(x), s1), dim=1))
return self.final(x)
class ChannelAttention(nn.Module):
def __init__(self, in_planes: int, ratio: int = 16):
super().__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.fc = nn.Sequential(
nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False),
nn.ReLU(inplace=True),
nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False),
)
self.sigmoid = nn.Sigmoid()
def forward(self, x: torch.Tensor) -> torch.Tensor:
avg_out = self.fc(self.avg_pool(x))
max_out = self.fc(self.max_pool(x))
return self.sigmoid(avg_out + max_out)
class SpatialAttention(nn.Module):
def __init__(self, kernel_size: int = 7):
super().__init__()
self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=kernel_size // 2, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x: torch.Tensor) -> torch.Tensor:
avg = torch.mean(x, dim=1, keepdim=True)
max_, _ = torch.max(x, dim=1, keepdim=True)
return self.sigmoid(self.conv1(torch.cat([avg, max_], dim=1)))
class ConvBlockCBAM(nn.Module):
def __init__(self, in_c: int, out_c: int):
super().__init__()
self.conv1 = nn.Conv2d(in_c, out_c, 3, padding=1)
self.bn1 = nn.BatchNorm2d(out_c)
self.conv2 = nn.Conv2d(out_c, out_c, 3, padding=1)
self.bn2 = nn.BatchNorm2d(out_c)
self.relu = nn.ReLU(inplace=True)
self.ca = ChannelAttention(out_c)
self.sa = SpatialAttention()
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.relu(self.bn1(self.conv1(x)))
x = self.relu(self.bn2(self.conv2(x)))
x = self.ca(x) * x
x = self.sa(x) * x
return x
class EncoderBlock(nn.Module):
def __init__(self, in_c: int, out_c: int):
super().__init__()
self.conv = ConvBlockCBAM(in_c, out_c)
self.pool = nn.MaxPool2d(2)
def forward(self, x: torch.Tensor):
features = self.conv(x)
return features, self.pool(features)
class CBAMDecoderBlock(nn.Module):
def __init__(self, in_c: int, out_c: int):
super().__init__()
self.up = nn.ConvTranspose2d(in_c, out_c, 2, stride=2)
self.conv = ConvBlockCBAM(out_c * 2, out_c)
def forward(self, x: torch.Tensor, skip: torch.Tensor) -> torch.Tensor:
x = self.up(x)
if x.shape[-2:] != skip.shape[-2:]:
x = F.interpolate(x, size=skip.shape[-2:], mode="bilinear", align_corners=False)
return self.conv(torch.cat([x, skip], dim=1))
class CBAMUNet5(nn.Module):
"""CBAM U-Net matching unet_cbam.pth (5 output channels)."""
def __init__(self, num_classes: int = 5):
super().__init__()
self.enc1 = EncoderBlock(3, 64)
self.enc2 = EncoderBlock(64, 128)
self.enc3 = EncoderBlock(128, 256)
self.enc4 = EncoderBlock(256, 512)
self.bottleneck = ConvBlockCBAM(512, 1024)
self.dec1 = CBAMDecoderBlock(1024, 512)
self.dec2 = CBAMDecoderBlock(512, 256)
self.dec3 = CBAMDecoderBlock(256, 128)
self.dec4 = CBAMDecoderBlock(128, 64)
self.final = nn.Conv2d(64, num_classes, 1)
def forward(self, x: torch.Tensor) -> torch.Tensor:
s1, p1 = self.enc1(x)
s2, p2 = self.enc2(p1)
s3, p3 = self.enc3(p2)
s4, p4 = self.enc4(p3)
b = self.bottleneck(p4)
x = self.dec1(b, s4)
x = self.dec2(x, s3)
x = self.dec3(x, s2)
x = self.dec4(x, s1)
return self.final(x)
# -----------------------------------------------------------------------------
# CCHNet_code model family
# -----------------------------------------------------------------------------
class FPConvBNReLU(nn.Module):
def __init__(
self,
in_ch,
out_ch,
k=3,
p=1,
):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(
in_ch,
out_ch,
k,
padding=p,
bias=False,
),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
)
def forward(self, x):
return self.block(x)
class FPAGCB(nn.Module):
"""
Adaptive Gated Convolutional Block.
The block creates a learned gate over convolutional features
and uses a residual connection to preserve useful information.
"""
def __init__(
self,
channels,
reduction=8,
):
super().__init__()
hidden = max(
channels // reduction,
8
)
self.conv1 = nn.Conv2d(
channels,
channels,
3,
padding=1,
bias=False,
)
self.bn1 = nn.BatchNorm2d(
channels
)
self.conv2 = nn.Conv2d(
channels,
channels,
3,
padding=1,
bias=False,
)
self.bn2 = nn.BatchNorm2d(
channels
)
self.gate = nn.Sequential(
nn.Conv2d(
channels,
hidden,
1,
bias=False,
),
nn.ReLU(inplace=True),
nn.Conv2d(
hidden,
channels,
1,
bias=True,
),
nn.Sigmoid(),
)
self.out_act = nn.ReLU(
inplace=True
)
def forward(self, x):
y = self.conv1(x)
y = self.bn1(y)
y = F.relu(y, inplace=True)
y = self.conv2(y)
y = self.bn2(y)
gate = self.gate(y)
y = y * gate
return self.out_act(
x + y
)
class FPCSAIB(nn.Module):
"""
Channel-wise and Spatial Attention Integration Block.
"""
def __init__(
self,
channels,
reduction=8,
):
super().__init__()
hidden = max(
channels // reduction,
8
)
self.mlp = nn.Sequential(
nn.Conv2d(
channels,
hidden,
1,
bias=False,
),
nn.ReLU(inplace=True),
nn.Conv2d(
hidden,
channels,
1,
bias=False,
),
)
self.spatial = nn.Conv2d(
2,
1,
kernel_size=7,
padding=3,
bias=False,
)
def forward(self, x):
avg = F.adaptive_avg_pool2d(
x, 1
)
mx = F.adaptive_max_pool2d(
x, 1
)
channel_attn = torch.sigmoid(
self.mlp(avg) +
self.mlp(mx)
)
x = x * channel_attn
avg_sp = torch.mean(
x,
dim=1,
keepdim=True,
)
max_sp, _ = torch.max(
x,
dim=1,
keepdim=True,
)
spatial_input = torch.cat(
[avg_sp, max_sp],
dim=1,
)
spatial_attn = torch.sigmoid(
self.spatial(
spatial_input
)
)
return x * spatial_attn
class FPFARB(nn.Module):
"""
Fusion Attention Refinement Block.
It fuses decoder and skip features, then refines the fused
representation using FPCSAIB and a residual path.
"""
def __init__(
self,
in_ch,
out_ch,
):
super().__init__()
self.fuse = nn.Sequential(
nn.Conv2d(
in_ch,
out_ch,
1,
bias=False,
),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(
out_ch,
out_ch,
3,
padding=1,
bias=False,
),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
)
self.attn = FPCSAIB(
out_ch
)
self.residual = (
nn.Conv2d(
in_ch,
out_ch,
1,
bias=False,
)
if in_ch != out_ch
else nn.Identity()
)
def forward(self, x):
identity = self.residual(x)
y = self.fuse(x)
y = self.attn(y)
return F.relu(
y + identity,
inplace=True,
)
class FPEfficientNetEncoder(nn.Module):
def __init__(
self,
pretrained=True,
):
super().__init__()
weights = (
EfficientNet_B0_Weights.DEFAULT
if pretrained
else None
)
# If pretrained weights cannot be downloaded, fall back to
# randomly initialized EfficientNet instead of crashing.
try:
backbone = efficientnet_b0(weights=weights)
except Exception as exc:
print("WARNING: Could not load pretrained EfficientNet-B0 weights.")
print("Reason:", exc)
print("Falling back to randomly initialized EfficientNet-B0.")
backbone = efficientnet_b0(weights=None)
self.features = backbone.features
def forward(self, x):
skips = []
for i, layer in enumerate(
self.features
):
x = layer(x)
# For 512x512:
# index 0 -> 256x256, 32 ch
# index 2 -> 128x128, 24 ch
# index 3 -> 64x64, 40 ch
# index 4 -> 32x32, 80 ch
# final -> 16x16, 1280 ch
if i in (0, 2, 3, 4):
skips.append(x)
return skips, x
class FPDecoderBlock(nn.Module):
def __init__(
self,
in_ch,
skip_ch,
out_ch,
):
super().__init__()
self.farb = FPFARB(
in_ch + skip_ch,
out_ch,
)
def forward(self, x, skip):
x = F.interpolate(
x,
size=skip.shape[-2:],
mode="bilinear",
align_corners=False,
)
x = torch.cat(
[x, skip],
dim=1,
)
return self.farb(x)
class YNet(nn.Module):
def __init__(
self,
num_classes=12,
pretrained=True,
):
super().__init__()
self.encoder = FPEfficientNetEncoder(
pretrained=pretrained
)
# Encoder feature enhancement.
self.agcb0 = FPAGCB(32)
self.csai0 = FPCSAIB(32)
self.agcb1 = FPAGCB(24)
self.csai1 = FPCSAIB(24)
self.agcb2 = FPAGCB(40)
self.csai2 = FPCSAIB(40)
self.agcb3 = FPAGCB(80)
self.csai3 = FPCSAIB(80)
# Bottleneck.
self.bottleneck_reduce = FPConvBNReLU(
1280,
256,
k=1,
p=0,
)
self.bottleneck_agcb = FPAGCB(
256
)
self.bottleneck_csai = FPCSAIB(
256
)
self.bottleneck_farb = FPFARB(
256,
256,
)
# Decoder.
self.dec1 = FPDecoderBlock(
256, 80, 192
)
self.dec2 = FPDecoderBlock(
192, 40, 128
)
self.dec3 = FPDecoderBlock(
128, 24, 64
)
self.dec4 = FPDecoderBlock(
64, 32, 32
)
self.dec5 = nn.Sequential(
FPConvBNReLU(32, 32),
FPConvBNReLU(32, 16),
)
# Y-Net branch 1: terrain segmentation.
self.terrain_head = nn.Conv2d(
16,
num_classes,
1,
)
# Y-Net branch 2: boundary refinement.
self.boundary_head = nn.Conv2d(
16,
1,
1,
)
def forward(self, x):
skips, bottleneck = self.encoder(x)
s0 = self.csai0(
self.agcb0(skips[0])
)
s1 = self.csai1(
self.agcb1(skips[1])
)
s2 = self.csai2(
self.agcb2(skips[2])
)
s3 = self.csai3(
self.agcb3(skips[3])
)
x = self.bottleneck_reduce(
bottleneck
)
x = self.bottleneck_agcb(x)
x = self.bottleneck_csai(x)
x = self.bottleneck_farb(x)
x = self.dec1(x, s3)
x = self.dec2(x, s2)
x = self.dec3(x, s1)
x = self.dec4(x, s0)
x = F.interpolate(
x,
scale_factor=2,
mode="bilinear",
align_corners=False,
)
x = self.dec5(x)
terrain_logits = self.terrain_head(x)
boundary_logits = self.boundary_head(x)
return terrain_logits, boundary_logits
# -----------------------------------------------------------------------------
# CCHNet_binarization model family
# -----------------------------------------------------------------------------
BINARY_EPS = 1e-8
BINARY_EFFICIENTNET_INDICES = (5, 6, 7, 8)
BINARY_DECODER = False
BINARY_ATTENTION = False
BIN_BOTTLENECK_CH = 128
BIN_DEC1_CH = 192
BIN_DEC2_CH = 128
BIN_DEC3_CH = 64
BIN_DEC4_CH = 32
BIN_FINAL_CH = 24
class BinarySignSTE(torch.autograd.Function):
"""
Straight-through estimator for sign activation.
Forward:
sign(x)
Backward:
gradient is passed through inside |x| <= 1 and clipped
outside that range.
"""
@staticmethod
def forward(
ctx,
x
):
ctx.save_for_backward(x)
return torch.where(
x >= 0,
torch.ones_like(x),
-torch.ones_like(x)
)
@staticmethod
def backward(
ctx,
grad_output
):
(x,) = ctx.saved_tensors
grad = grad_output.clone()
mask = (
x.abs() <= 1.0
).to(grad.dtype)
return grad * mask
def binary_sign(x):
return BinarySignSTE.apply(x)
class BinaryConv2d(nn.Module):
"""
Binary-weight convolution.
The underlying real-valued weight remains trainable.
During forward propagation it is binarized with STE.
A per-output-channel scale factor is used:
alpha = mean(abs(weight))
W_binary = sign(W) * alpha
This is much more stable than using raw +/-1 weights.
"""
def __init__(
self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=False
):
super().__init__()
if isinstance(
kernel_size,
int
):
kh = kw = kernel_size
else:
kh, kw = kernel_size
if isinstance(
stride,
int
):
sh = sw = stride
else:
sh, sw = stride
if isinstance(
padding,
int
):
ph = pw = padding
else:
ph, pw = padding
if isinstance(
dilation,
int
):
dh = dw = dilation
else:
dh, dw = dilation
self.in_channels = (
in_channels
)
self.out_channels = (
out_channels
)
self.kernel_size = (
kh,
kw
)
self.stride = (
sh,
sw
)
self.padding = (
ph,
pw
)
self.dilation = (
dh,
dw
)
self.groups = groups
self.weight = nn.Parameter(
torch.empty(
out_channels,
in_channels // groups,
kh,
kw
)
)
if bias:
self.bias = nn.Parameter(
torch.zeros(
out_channels
)
)
else:
self.bias = None
nn.init.kaiming_normal_(
self.weight,
mode="fan_out",
nonlinearity="relu"
)
def forward(self, x):
scale = (
self.weight
.abs()
.mean(
dim=(1, 2, 3),
keepdim=True
)
)
binary_weight = (
binary_sign(
self.weight
) *
scale.clamp_min(
BINARY_EPS
)
)
return F.conv2d(
x,
binary_weight,
self.bias,
self.stride,
self.padding,
self.dilation,
self.groups
)
class BinaryConvBNReLU(nn.Module):
def __init__(
self,
in_ch,
out_ch,
k=3,
p=1,
groups=1
):
super().__init__()
self.conv = BinaryConv2d(
in_ch,
out_ch,
k,
padding=p,
groups=groups,
bias=False
)
self.bn = nn.BatchNorm2d(
out_ch
)
self.act = nn.ReLU(
inplace=True
)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
return x
class BinConvBNReLU(nn.Module):
def __init__(
self,
in_ch,
out_ch,
k=3,
p=1
):
super().__init__()
self.block = nn.Sequential(
nn.Conv2d(
in_ch,
out_ch,
k,
padding=p,
bias=False
),
nn.BatchNorm2d(
out_ch
),
nn.ReLU(
inplace=True
)
)
def forward(self, x):
return self.block(x)
class BinAGCB(nn.Module):
"""
Adaptive Gated Convolutional Block.
Binary 3x3 feature convolutions are used when
BINARY_ATTENTION=True.
"""
def __init__(
self,
channels,
reduction=8,
binary=True
):
super().__init__()
hidden = max(
channels // reduction,
8
)
Conv = (
BinaryConv2d
if binary
else nn.Conv2d
)
self.conv1 = Conv(
channels,
channels,
3,
padding=1,
bias=False
)
self.bn1 = nn.BatchNorm2d(
channels
)
self.conv2 = Conv(
channels,
channels,
3,
padding=1,
bias=False
)
self.bn2 = nn.BatchNorm2d(
channels
)
# 1x1 gates remain FP because they control the feature
# selection and are relatively small.
self.gate = nn.Sequential(
nn.Conv2d(
channels,
hidden,
1,
bias=False
),
nn.ReLU(
inplace=True
),
nn.Conv2d(
hidden,
channels,
1,
bias=True
),
nn.Sigmoid()
)
self.out_act = nn.ReLU(
inplace=True
)
def forward(self, x):
y = self.conv1(x)
y = self.bn1(y)
y = F.relu(
y,
inplace=True
)
y = self.conv2(y)
y = self.bn2(y)
gate = self.gate(y)
y = y * gate
return self.out_act(
x + y