DilatedEncoder

文章介绍了DilatedEncoder的实现,它包含DilatedBottleneck层,使用了不同膨胀率的卷积来扩展感受野。该结构可能用于目标检测模型,如yolof,通过结合不同膨胀率的编码器层增强特征提取能力。
摘要由CSDN通过智能技术生成

DilatedEncoder:来自yolof
代码来自https://github.com/yjh0410此博主

import torch
import torch.nn as nn
# from ..basic.conv import Conv


def get_activation1(name="lrelu", inplace=True):
    if name == "silu":
        module = nn.SiLU(inplace=inplace)
    elif name == "relu":
        module = nn.ReLU(inplace=inplace)
    elif name == "lrelu":
        module = nn.LeakyReLU(0.1, inplace=inplace)
    elif name is None:
        module = nn.Identity()
    else:
        raise AttributeError("Unsupported act type: {}".format(name))
    return module


# Basic conv1 layer
class Conv1(nn.Module):
    def __init__(self, c1, c2, k=1, p=0, s=1, d=1, g=1, act='lrelu', depthwise=False, bias=False):
        super(Conv1, self).__init__()
        if depthwise:
            assert c1 == c2
            self.convs = nn.Sequential(
                nn.Conv2d(c1, c2, k, stride=s, padding=p, dilation=d, groups=c1, bias=bias),
                nn.BatchNorm2d(c2),
                get_activation1(name=act),
                nn.Conv2d(c2, c2, kernel_size=1, bias=bias),
                nn.BatchNorm2d(c2),
                get_activation1(name=act)
            )
        else:
            self.convs = nn.Sequential(
                nn.Conv2d(c1, c2, k, stride=s, padding=p, dilation=d, groups=g, bias=bias),
                nn.BatchNorm2d(c2),
                get_activation1(name=act)
            )

    def forward(self, x):
        return self.convs(x)


# Dilated Encoder
class DilatedBottleneck(nn.Module):
    def __init__(self, c, d=1, e=0.5, act='lrelu'):
        super(DilatedBottleneck, self).__init__()
        c_ = int(c * e)
        self.branch = nn.Sequential(
            Conv1(c, c_, k=1, act=act),
            Conv1(c_, c_, k=3, p=d, d=d, act=act),
            Conv1(c_, c, k=1, act=act)
        )

    def forward(self, x):
        return x + self.branch(x)


class DilatedEncoder(nn.Module):
    """ DilateEncoder """
    def __init__(self, c1, c2, act='lrelu', dilation_list=[2, 4, 6, 8]):
        super(DilatedEncoder, self).__init__()
        self.projector = nn.Sequential(
            Conv1(c1, c2, k=1, act=None),
            Conv1(c2, c2, k=3, p=1, act=None)
        )
        encoders = []
        for d in dilation_list:
            encoders.append(DilatedBottleneck(c=c2, d=d, act=act))
        self.encoders = nn.Sequential(*encoders)

    def forward(self, x):
        x = self.projector(x)
        x = self.encoders(x)

        return x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值