卷积块CBM(conv+batchnorm+mish)

卷 积 块 C B M ( c o n v + b a t c h n o r m + m i s h ) 卷积块CBM(conv+batchnorm+mish) CBM(conv+batchnorm+mish)

Conv:提取特征
BN:1.防止梯度消失 2.防止过拟合 3.促进收敛
Mish:更优秀的激活函数,相比于其他,可以更有效的防止梯度消失

在这里插入图片描述

class Mish(nn.Module):
    def __init__(self):
        super(Mish, self).__init__()

    def forward(self, x):
        return x * torch.tanh(F.softplus(x))# F.softplus(x) == torch.log(1+torch.exp(x))


class CBM(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride=1):
        super(CBM, self).__init__()

        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, kernel_size//2, bias=False)
        self.bn = nn.BatchNorm2d(out_channels)
        self.activation = Mish()

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.activation(x)
        return x

测试

rgb = torch.randn(1, 3, 32, 32) # (batchsize,channel,w,h)
#print(rgb)
print(rgb.shape)

在这里插入图片描述

test_downsample_conv = BasicConv(3, 1,3,stride=2)
x = test_downsample_conv(rgb)
#print(x)
print(x.shape)

在这里插入图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题是技术问题,可以回答。以下是一个示例代码: ```python import torch import torch.nn as nn class ConvLSTMCell(nn.Module): def __init__(self, input_size, hidden_size): super(ConvLSTMCell, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.Gates = nn.Conv2d(input_size + hidden_size, 4 * hidden_size, 3, padding = 1) def forward(self, input, hidden, cell): combined = torch.cat((input, hidden), dim=1) gates = self.Gates(combined) forget_gate, input_gate, output_gate, cell_gate = gates.chunk(4, dim=1) forget_gate = torch.sigmoid(forget_gate) input_gate = torch.sigmoid(input_gate) cell_gate = torch.tanh(cell_gate) output_gate = torch.sigmoid(output_gate) cell = (forget_gate * cell) + (input_gate * cell_gate) hidden = output_gate * torch.tanh(cell) return hidden, cell class ConvLSTMLayer(nn.Module): def __init__(self, input_size, hidden_size): super(ConvLSTMLayer, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.ConvLSTMCell = ConvLSTMCell(input_size, hidden_size) def forward(self, input, Hidden_State=None, Cell_State=None): if Hidden_State is None: Hidden_State = torch.zeros(input.size()[0], self.hidden_size, input.size()[2], input.size()[3]).cuda() if Cell_State is None: Cell_State = torch.zeros(input.size()[0], self.hidden_size, input.size()[2], input.size()[3]).cuda() Hidden_State_List = [] for timestep in range(input.size()[1]): Hidden_State, Cell_State = self.ConvLSTMCell(input[:,timestep,:,:,:], Hidden_State, Cell_State) Hidden_State_List.append(Hidden_State) return torch.stack(Hidden_State_List, dim=1), Hidden_State, Cell_State class ConvBlock(nn.Module): def __init__(self, in_channels, out_channels): super(ConvBlock, self).__init__() self.conv = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True) ) def forward(self, x): return self.conv(x) class SegmentationModel(nn.Module): def __init__(self): super(SegmentationModel, self).__init__() self.convlstm_layer = ConvLSTMLayer(512, 512) self.conv_block1 = ConvBlock(512, 256) self.conv_block2 = ConvBlock(256, 128) self.classification = nn.Sequential( nn.Conv2d(128, 2, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(2), nn.Softmax(dim=1) ) def forward(self, x): x, _, _ = self.convlstm_layer(x) x = self.conv_block1(x) x = self.conv_block2(x) x = self.classification(x) return x ``` 这是一个简单的分割模型,由ConvLSTMLayer和两个ConvBlock组成,输入5帧大小为512 x W x H的数据,输出大小为2 x W x H的分割结果。其中ConvLSTMLayer是一个5帧ConvLSTM层,用于学习时序信息。ConvBlock是一个包含卷积BatchNorm卷积操作,用于提取特征。分类层使用1x1卷积进行2分类,并使用Softmax进行归一化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值