当前位置: 首页 > news >正文

每日Attention学习24——Strip Convolution Block

模块出处

[TIP 21] [link] CoANet: Connectivity Attention Network for Road Extraction From Satellite Imagery


模块名称

Strip Convolution Block (SCB)


模块作用

多方向条形特征提取


模块结构

在这里插入图片描述


模块特点
  • 类PSP设计,采用四个并行分支提取不同维度的信息
  • 相比于经典的横向/纵向条形卷积,引入了两种斜方向的卷积来更好的学习斜向线条

模块代码
import torch
import torch.nn as nn
import torch.nn.functional as F


class SCB(nn.Module):
    def __init__(self, in_channels, n_filters):
        super(SCB, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, in_channels // 4, 1)
        self.bn1 = nn.BatchNorm2d(in_channels // 4)
        self.relu1 = nn.ReLU()
        self.deconv1 = nn.Conv2d(
            in_channels // 4, in_channels // 8, (1, 9), padding=(0, 4)
        )
        self.deconv2 = nn.Conv2d(
            in_channels // 4, in_channels // 8, (9, 1), padding=(4, 0)
        )
        self.deconv3 = nn.Conv2d(
            in_channels // 4, in_channels // 8, (9, 1), padding=(4, 0)
        )
        self.deconv4 = nn.Conv2d(
            in_channels // 4, in_channels // 8, (1, 9), padding=(0, 4)
        )
        self.bn2 = nn.BatchNorm2d(in_channels // 4 + in_channels // 4)
        self.relu2 = nn.ReLU()
        self.conv3 = nn.Conv2d(
            in_channels // 4 + in_channels // 4, n_filters, 1)
        self.bn3 = nn.BatchNorm2d(n_filters)
        self.relu3 = nn.ReLU()

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)
        x1 = self.deconv1(x)
        x2 = self.deconv2(x)
        x3 = self.inv_h_transform(self.deconv3(self.h_transform(x)))
        x4 = self.inv_v_transform(self.deconv4(self.v_transform(x)))
        x = torch.cat((x1, x2, x3, x4), 1)
        x = self.bn2(x)
        x = self.relu2(x)
        x = self.conv3(x)
        x = self.bn3(x)
        x = self.relu3(x)
        return x

    def h_transform(self, x):
        shape = x.size()
        x = torch.nn.functional.pad(x, (0, shape[-1]))
        x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
        x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
        return x

    def inv_h_transform(self, x):
        shape = x.size()
        x = x.reshape(shape[0], shape[1], -1).contiguous()
        x = torch.nn.functional.pad(x, (0, shape[-2]))
        x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
        x = x[..., 0: shape[-2]]
        return x

    def v_transform(self, x):
        x = x.permute(0, 1, 3, 2)
        shape = x.size()
        x = torch.nn.functional.pad(x, (0, shape[-1]))
        x = x.reshape(shape[0], shape[1], -1)[..., :-shape[-1]]
        x = x.reshape(shape[0], shape[1], shape[2], 2*shape[3]-1)
        return x.permute(0, 1, 3, 2)

    def inv_v_transform(self, x):
        x = x.permute(0, 1, 3, 2)
        shape = x.size()
        x = x.reshape(shape[0], shape[1], -1)
        x = torch.nn.functional.pad(x, (0, shape[-2]))
        x = x.reshape(shape[0], shape[1], shape[-2], 2*shape[-2])
        x = x[..., 0: shape[-2]]
        return x.permute(0, 1, 3, 2)


if __name__ == '__main__':
    x = torch.randn([1, 64, 44, 44])
    scb = SCB(in_channels=64, n_filters=64)
    out = scb(x)
    print(out.shape)  # [1, 64, 44, 44]

相关文章:

  • 嵌入式开发工程师笔试面试指南-Linux系统移植
  • 计算机组成与接口10
  • 深入探索C语言中的sscanf和sprintf函数
  • 【C++笔记】C++11智能指针的使用及其原理
  • 2025年SCI1区TOP:真菌生长优化算法FGO,深度解析+性能实测
  • java23种设计模式-观察者模式
  • Spring MVC框架六:Ajax技术
  • 用 DeepSeek 打样!KubeSphere LuBan 用 3 天/3 分钟“干掉”大模型部署焦虑
  • 第五六七章
  • Element Plus: el-card的内容滚动问题
  • ES 客户端 API 二次封装思想
  • C++大整数类的设计与实现
  • 点云配准技术的演进与前沿探索:从传统算法到深度学习融合(2)
  • 【Grok3】强化学习(Reinforcement Learning, RL)复习文档
  • Python 编程题 第二节:组合数字、乘法口诀表、水仙花数、反向输出四位数、判断三角形
  • 蓝桥杯 五子棋对弈
  • AI人工智能机器学习之神经网络
  • 人工智能丨大语言模型不再高不可攀!DeepSeek开源FlashMLA,开启AI新纪元
  • 验证码介绍及生成与验证(HTML + JavaScript实现)
  • 组件注册方式、传递数据
  • “五一”假期逛上海车展请提前购票,展会现场不售当日票
  • 厚重与潮流交织,淮安展现“运河之都”全新城市想象
  • 举牌超200轮!中铁建7.76亿元竞得北京通州梨园宅地
  • 辽宁辽阳火灾事故饭店经营者已被控制,善后处置全面展开
  • 王毅会见泰国外长玛里:坚决有力打击电诈等跨境犯罪
  • 长三角铁路“五一”假期运输今启动:预计发送旅客量增6%,5月1日当天有望创新高