PubAccount commited on
Commit
93f7cd5
·
verified ·
1 Parent(s): 08e8488

Update networks/decoder.py

Browse files
Files changed (1) hide show
  1. networks/decoder.py +21 -37
networks/decoder.py CHANGED
@@ -1,48 +1,37 @@
1
- from typing import Dict, List, Optional, Sequence, Union
2
-
3
  import torch
4
  import torch.nn as nn
5
- from torch import Tensor
6
- import torch.nn.functional as F
7
 
8
- from mmcv.cnn import ConvModule
9
  from .height_head import resize
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  class PPM(nn.ModuleList):
12
- """Pooling Pyramid Module used in PSPNet.
13
- Args:
14
- pool_scales (tuple[int]): Pooling scales used in Pooling Pyramid
15
- Module.
16
- in_channels (int): Input channels.
17
- channels (int): Channels after modules, before conv_seg.
18
- conv_cfg (dict|None): Config of conv layers.
19
- norm_cfg (dict|None): Config of norm layers.
20
- act_cfg (dict): Config of activation layers.
21
- align_corners (bool): align_corners argument of F.interpolate.
22
- """
23
-
24
- def __init__(self, pool_scales, in_channels, channels, conv_cfg, norm_cfg,
25
- act_cfg, align_corners, **kwargs):
26
  super(PPM, self).__init__()
27
  self.pool_scales = pool_scales
28
  self.align_corners = align_corners
29
  self.in_channels = in_channels
30
  self.channels = channels
31
- self.conv_cfg = conv_cfg
32
- self.norm_cfg = norm_cfg
33
- self.act_cfg = act_cfg
34
  for pool_scale in pool_scales:
35
  self.append(
36
  nn.Sequential(
37
  nn.AdaptiveAvgPool2d(pool_scale),
38
- ConvModule(
39
- self.in_channels,
40
- self.channels,
41
- 1,
42
- conv_cfg=self.conv_cfg,
43
- norm_cfg=self.norm_cfg,
44
- act_cfg=self.act_cfg,
45
- **kwargs)))
46
 
47
  def forward(self, x):
48
  """Forward function."""
@@ -64,10 +53,8 @@ class Decoder(nn.Module):
64
  num_deconv_filters=2,
65
  decover_filter=[128, 128],
66
  psp_channel=16,
67
- pool_scales=[1, 2, 3, 6],
68
- init_cfg=dict(
69
- type='TruncNormal', std=0.02, layer=['Conv2d', 'Linear']), ):
70
- super(Decoder, self).__init__(init_cfg=init_cfg)
71
 
72
  self.in_channel = in_channel
73
  self.num_deconv_filters = num_deconv_filters
@@ -114,9 +101,6 @@ class Decoder(nn.Module):
114
  pool_scales=pool_scales,
115
  in_channels=in_channel,
116
  channels=psp_channel,
117
- conv_cfg=dict(type='Conv2d'),
118
- norm_cfg=dict(type='SyncBN', requires_grad=True),
119
- act_cfg=dict(type='ReLU'),
120
  align_corners=False)
121
 
122
  def _make_deconv_layer(self, name, in_channel, out_channel):
 
 
 
1
  import torch
2
  import torch.nn as nn
 
 
3
 
 
4
  from .height_head import resize
5
 
6
+
7
+ class ConvModule(nn.Module):
8
+ """Conv + Norm + Activation with same submodule names as mmcv.ConvModule."""
9
+ def __init__(self, in_channels, out_channels, kernel_size, padding=0,
10
+ norm_layer=None, act_layer=None):
11
+ super().__init__()
12
+ self.conv = nn.Conv2d(in_channels, out_channels, kernel_size,
13
+ padding=padding, bias=(norm_layer is None))
14
+ self.bn = norm_layer(out_channels) if norm_layer is not None else nn.Identity()
15
+ self.activate = act_layer() if act_layer is not None else nn.Identity()
16
+
17
+ def forward(self, x):
18
+ return self.activate(self.bn(self.conv(x)))
19
+
20
  class PPM(nn.ModuleList):
21
+ def __init__(self, pool_scales, in_channels, channels, align_corners):
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  super(PPM, self).__init__()
23
  self.pool_scales = pool_scales
24
  self.align_corners = align_corners
25
  self.in_channels = in_channels
26
  self.channels = channels
 
 
 
27
  for pool_scale in pool_scales:
28
  self.append(
29
  nn.Sequential(
30
  nn.AdaptiveAvgPool2d(pool_scale),
31
+ ConvModule(in_channels, channels, 1,
32
+ norm_layer=nn.SyncBatchNorm,
33
+ act_layer=nn.ReLU),
34
+ ))
 
 
 
 
35
 
36
  def forward(self, x):
37
  """Forward function."""
 
53
  num_deconv_filters=2,
54
  decover_filter=[128, 128],
55
  psp_channel=16,
56
+ pool_scales=[1, 2, 3, 6]):
57
+ super(Decoder, self).__init__()
 
 
58
 
59
  self.in_channel = in_channel
60
  self.num_deconv_filters = num_deconv_filters
 
101
  pool_scales=pool_scales,
102
  in_channels=in_channel,
103
  channels=psp_channel,
 
 
 
104
  align_corners=False)
105
 
106
  def _make_deconv_layer(self, name, in_channel, out_channel):