def __init__(self,
                 block,
                 num_blocks,
                 num_classes=10,
                 reduction_ratio=1,
                 kernel_cbam=3,
                 use_cbam_block=False,
                 use_cbam_class=False):
        super(ResNet, self).__init__()
        self.in_planes = 64
        self.reduction_ratio = reduction_ratio
        self.kernel_cbam = kernel_cbam
        self.use_cbam_block = use_cbam_block
        self.use_cbam_class = use_cbam_class

        print(use_cbam_block, use_cbam_class)

        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
        self.linear = nn.Linear(512 * 52, num_classes)

        if self.use_cbam_class:
            self.cbam = CBAM(n_channels_in=512 * block.expansion,
                             reduction_ratio=reduction_ratio,
                             kernel_size=kernel_cbam)
Example #2
0
    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 downsample=None,
                 use_cbam=False):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2D(inplanes,
                               planes,
                               kernel_size=1,
                               bias_attr=False)
        self.bn1 = nn.BatchNorm2D(planes)
        self.conv2 = nn.Conv2D(planes,
                               planes,
                               kernel_size=3,
                               stride=stride,
                               padding=1,
                               bias_attr=False)
        self.bn2 = nn.BatchNorm2D(planes)
        self.conv3 = nn.Conv2D(planes,
                               planes * 4,
                               kernel_size=1,
                               bias_attr=False)
        self.bn3 = nn.BatchNorm2D(planes * 4)
        self.relu = nn.ReLU()
        self.downsample = downsample
        self.stride = stride

        if use_cbam:
            self.cbam = CBAM(planes * 4, 16)
        else:
            self.cbam = None
Example #3
0
 def __init__(self, nb_layers, input_dim, growth_rate):
     super(DenoiseRDBCBAM, self).__init__()
     self.ID = input_dim
     self.GR = growth_rate
     self.layer = self._make_layer(nb_layers, input_dim, growth_rate)
     self.conv1x1 = nn.Conv2d(in_channels = input_dim+nb_layers*growth_rate,\
                                 out_channels =input_dim,\
                                 kernel_size = 1,\
                                 stride=1,\
                                 padding=0  )
     self.cbamlayer = CBAM(gate_channels=input_dim, reduction_ratio=16)
Example #4
0
 def __init__(self, out_ch=64):
     super(WGAN_VGG_generator, self).__init__()
     self.conv_first = nn.Conv2d(1,
                                 out_ch,
                                 kernel_size=5,
                                 stride=5,
                                 padding=0)  # kernel_size=3,bias=False
     self.inception_module = inception_module(in_channels=1,
                                              pool_features=1)
     self.CBAM = CBAM(gate_channels=10)
     self.LeakyReLU = nn.LeakyReLU(0.1, inplace=True)  # nn.ReLU
     self.conv_1 = nn.Conv2d(1, out_ch, kernel_size=1, stride=1, padding=0)
     self.conv_last = nn.Conv2d(out_ch,
                                1,
                                kernel_size=1,
                                stride=1,
                                padding=0)
Example #5
0
    def __init__(self, in_planes, planes, stride=1, reduction_ratio = 1, kernel_cbam = 3, use_cbam = False):
        super(BasicBlock, self).__init__()
        self.use_cbam = use_cbam
        self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes)

        if self.use_cbam:
            self.cbam = CBAM(n_channels_in = self.expansion*planes, reduction_ratio = reduction_ratio, kernel_size = kernel_cbam)


        self.shortcut = nn.Sequential()
        if stride != 1 or in_planes != self.expansion*planes:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*planes)
            )
Example #6
0
    def __init__(
        self, inplanes, planes,
        stride=1, downsample=None,
        use_cbam=False
    ):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

        if use_cbam:
            self.cbam = CBAM(planes, 16)
        else:
            self.cbam = None
 def __init__(self, blocks=4, channel=128, scale=2):
     super(MyModel, self).__init__()
     self.deconv = Conv2DTranspose(filters=channel,
                                   kernel_size=[scale, scale],
                                   strides=[scale, scale],
                                   padding="valid",
                                   activation="relu")
     self.conv1 = Conv2D(filters=channel,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.conv2 = Conv2D(filters=channel,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.conv3 = Conv2D(filters=channel,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.conv4 = Conv2D(filters=channel,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.conv5 = Conv2D(filters=3,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.conv6 = Conv2D(filters=3,
                         kernel_size=[3, 3],
                         strides=[1, 1],
                         padding="same",
                         activation="relu")
     self.resblocks1 = [ResBlock(channel) for i in range(blocks)]
     self.resblocks2 = [ResBlock(channel) for i in range(blocks)]
     self.cbam = CBAM()
Example #8
0
    def build_module(self):
        # Assuming input shape is the pre-concatenated tensor shape
        # num_input_features should be dim 1 of the 4d tensor
        print('Dense Layer shape', self.input_shape)
        x = torch.zeros(self.input_shape)
        out = x
        #
        # self.layer_dict['se'] = SqueezeExciteLayer(input_shape=out.shape,
        #                                            reduction=self.se_reduction,
        #                                            use_bias=False)
        self.layer_dict['se'] = CBAM(out.shape[1], self.se_reduction)
        out = self.layer_dict['se'].forward(out)

        self.layer_dict['bn_1'] = nn.BatchNorm2d(out.shape[1])
        out = self.layer_dict['bn_1'].forward(out)
        out = F.leaky_relu(out)

        self.layer_dict['conv_1'] = nn.Conv2d(in_channels=out.shape[1],
                                              out_channels=self.bottleneck_factor * self.growth_rate,
                                              kernel_size=1, stride=1, bias=self.use_bias)
        out = self.layer_dict['conv_1'].forward(out)

        self.layer_dict['bn_2'] = nn.BatchNorm2d(out.shape[1])
        out = self.layer_dict['bn_2'].forward(out)
        out = F.leaky_relu(out)

        self.layer_dict['conv_2'] = nn.Conv2d(in_channels=out.shape[1],
                                              out_channels=self.growth_rate,
                                              kernel_size=3, stride=1, padding=self.dilation, bias=self.use_bias,
                                              dilation=self.dilation)
        out = self.layer_dict['conv_2'].forward(out)

        # TODO: Checkout dropout 2d
        if self.drop_rate > 0:
            print('Dropout with', self.drop_rate)
            self.layer_dict['dropout'] = nn.Dropout(p=self.drop_rate)
            out = self.layer_dict['dropout'](out)

        return out
Example #9
0
 def __init__(self, input_size=64, gate_channels=10):
     super(WGAN_VGG, self).__init__()
     self.generator = WGAN_VGG_generator()
     self.discriminator = WGAN_VGG_discriminator(input_size)
     self.cbam = CBAM(gate_channels)
     self.p_criterion = nn.MSELoss()
Example #10
0
def test_CBAM(input_tensor):
    c, h, w = input_tensor.shape[1:]
    cbam = CBAM(in_channels=c)
    return cbam(input_tensor)