CondenseNet V2:深度网络的稀疏特征重新激活
本文介绍CondenseNet V2模型的实现,该模型基于密集连接,针对DenseNet和CondenseNet特征复用问题,引入稀疏特征重激活,对冗余特征裁剪与更新,提升复用效率。文中给出基于Paddle的代码实现,包括各组件及预设模型,并测试了模型输出,还列出不同模型在ImageNet-1k上的精度表现。

引入
最近各种 Transformer 的视觉模型层出不穷,偶然看到一些新的 CNN 模型居然有一丝小兴奋这次就来大致实现一下 CPVR 2024 新鲜出炉的新模型 —— CondenseNet V2相关资料
论文:CondenseNet V2: Sparse Feature Reactivation for Deep Networks最新实现:jianghaojun/CondenseNetV2参考文章:CVPR2024 | 密集连接网络中的稀疏特征重激活论文概述
本文提出了一种基于密集连接的高效轻量级神经网络。针对 DenseNet 的特征复用冗余,CondenseNet 提出利用可学习分组卷积来裁剪掉冗余连接。然而,DenseNet 的和 CondenseNet 中特征一旦产生将不再发生任何更改,这就导致了部分特征的潜在价值被严重忽略。本文提出:与其直接删掉冗余,不妨给冗余特征一个“翻身”机会。因此我们提出一种可学习的稀疏特征重激活的方法,来有选择地更新冗余特征,从而增强特征的复用效率。CondenseNet V2 在 CondenseNet 的基础上引入了稀疏特征重激活,对冗余特征同时进行了裁剪和更新,有效提升了密集连接网络的特征复用效率,在图像分类和检测任务上取得的出色表现。更多详情请看上面的参考文章的内容代码实现
最新实现只提供了转换后的标准分组卷积的模型参数,大致的转换流程如下图:
!pip install ppim登录后复制In [2]
import paddleimport paddle.nn as nnimport paddle.vision.transforms as Tfrom ppim.models.common import kaiming_normal_, zeros_, ones_class SELayer(nn.Layer): def __init__(self, inplanes, reduction=16): super(SELayer, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2D(1) self.fc = nn.Sequential( nn.Linear(inplanes, inplanes // reduction, bias_attr=False), nn.ReLU(), nn.Linear(inplanes // reduction, inplanes, bias_attr=False), nn.Sigmoid() ) def forward(self, x): b, c, _, _ = x.shape y = self.avg_pool(x).reshape((b, c)) y = self.fc(y).reshape((b, c, 1, 1)) return x * y.expand_as(x)class HS(nn.Layer): def __init__(self): super(HS, self).__init__() self.relu6 = nn.ReLU6() def forward(self, inputs): return inputs * self.relu6(inputs + 3) / 6class Conv(nn.Sequential): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, activation='ReLU', bn_momentum=0.9): super(Conv, self).__init__() self.add_sublayer('norm', nn.BatchNorm2D( in_channels, momentum=bn_momentum)) if activation == 'ReLU': self.add_sublayer('activation', nn.ReLU()) elif activation == 'HS': self.add_sublayer('activation', HS()) else: raise NotImplementedError self.add_sublayer('conv', nn.Conv2D(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias_attr=False, groups=groups))def ShuffleLayer(x, groups): batchsize, num_channels, height, width = x.shape channels_per_group = num_channels // groups # reshape x = x.reshape((batchsize, groups, channels_per_group, height, width)) # transpose x = x.transpose((0, 2, 1, 3, 4)) # reshape x = x.reshape((batchsize, -1, height, width)) return xdef ShuffleLayerTrans(x, groups): batchsize, num_channels, height, width = x.shape channels_per_group = num_channels // groups # reshape x = x.reshape((batchsize, channels_per_group, groups, height, width)) # transpose x = x.transpose((0, 2, 1, 3, 4)) # reshape x = x.reshape((batchsize, -1, height, width)) return xclass CondenseLGC(nn.Layer): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, activation='ReLU'): super(CondenseLGC, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.groups = groups self.norm = nn.BatchNorm2D(self.in_channels) if activation == 'ReLU': self.activation = nn.ReLU() elif activation == 'HS': self.activation = HS() else: raise NotImplementedError self.conv = nn.Conv2D(self.in_channels, self.out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=self.groups, bias_attr=False) self.register_buffer('index', paddle.zeros( (self.in_channels,), dtype='int64')) def forward(self, x): x = paddle.index_select(x, self.index, axis=1) x = self.norm(x) x = self.activation(x) x = self.conv(x) x = ShuffleLayer(x, self.groups) return xclass CondenseSFR(nn.Layer): def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, groups=1, activation='ReLU'): super(CondenseSFR, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.groups = groups self.norm = nn.BatchNorm2D(self.in_channels) if activation == 'ReLU': self.activation = nn.ReLU() elif activation == 'HS': self.activation = HS() else: raise NotImplementedError self.conv = nn.Conv2D(self.in_channels, self.out_channels, kernel_size=kernel_size, padding=padding, groups=self.groups, bias_attr=False, stride=stride) self.register_buffer('index', paddle.zeros( (self.out_channels, self.out_channels))) def forward(self, x): x = self.norm(x) x = self.activation(x) x = ShuffleLayerTrans(x, self.groups) x = self.conv(x) # SIZE: N, C, H, W N, C, H, W = x.shape x = x.reshape((N, C, H * W)) x = x.transpose((0, 2, 1)) # SIZE: N, HW, C # x SIZE: N, HW, C; self.index SIZE: C, C; OUTPUT SIZE: N, HW, C x = paddle.matmul(x, self.index) x = x.transpose((0, 2, 1)) # SIZE: N, C, HW x = x.reshape((N, C, H, W)) # SIZE: N, C, HW return xclass _SFR_DenseLayer(nn.Layer): def __init__(self, in_channels, growth_rate, group_1x1, group_3x3, group_trans, bottleneck, activation, use_se=False): super(_SFR_DenseLayer, self).__init__() self.group_1x1 = group_1x1 self.group_3x3 = group_3x3 self.group_trans = group_trans self.use_se = use_se # 1x1 conv i --> b*k self.conv_1 = CondenseLGC(in_channels, bottleneck * growth_rate, kernel_size=1, groups=self.group_1x1, activation=activation) # 3x3 conv b*k --> k self.conv_2 = Conv(bottleneck * growth_rate, growth_rate, kernel_size=3, padding=1, groups=self.group_3x3, activation=activation) # 1x1 res conv k(8-16-32)--> i (k*l) self.sfr = CondenseSFR(growth_rate, in_channels, kernel_size=1, groups=self.group_trans, activation=activation) if self.use_se: self.se = SELayer(inplanes=growth_rate, reduction=1) def forward(self, x): x_ = x x = self.conv_1(x) x = self.conv_2(x) if self.use_se: x = self.se(x) sfr_feature = self.sfr(x) y = x_ + sfr_feature return paddle.concat([y, x], 1)class _SFR_DenseBlock(nn.Sequential): def __init__(self, num_layers, in_channels, growth_rate, group_1x1, group_3x3, group_trans, bottleneck, activation, use_se): super(_SFR_DenseBlock, self).__init__() for i in range(num_layers): layer = _SFR_DenseLayer( in_channels + i * growth_rate, growth_rate, group_1x1, group_3x3, group_trans, bottleneck, activation, use_se) self.add_sublayer('denselayer_%d' % (i + 1), layer)class _Transition(nn.Layer): def __init__(self): super(_Transition, self).__init__() self.pool = nn.AvgPool2D(kernel_size=2, stride=2) def forward(self, x): x = self.pool(x) return xclass CondenseNetV2(nn.Layer): def __init__(self, stages, growth, HS_start_block, SE_start_block, fc_channel, group_1x1, group_3x3, group_trans, bottleneck, last_se_reduction, class_dim=1000): super(CondenseNetV2, self).__init__() self.stages = stages self.growth = growth self.class_dim = class_dim self.last_se_reduction = last_se_reduction assert len(self.stages) == len(self.growth) self.progress = 0.0 self.init_stride = 2 self.pool_size = 7 self.features = nn.Sequential() # Initial nChannels should be 3 self.num_features = 2 * self.growth[0] # Dense-block 1 (224x224) self.features.add_sublayer('init_conv', nn.Conv2D(3, self.num_features, kernel_size=3, stride=self.init_stride, padding=1, bias_attr=False)) for i in range(len(self.stages)): activation = 'HS' if i >= HS_start_block else 'ReLU' use_se = True if i >= SE_start_block else False # Dense-block i self.add_block(i, group_1x1, group_3x3, group_trans, bottleneck, activation, use_se) self.fc = nn.Linear(self.num_features, fc_channel) self.fc_act = HS() # Classifier layer if class_dim > 0: self.classifier = nn.Linear(fc_channel, class_dim) self._initialize() def add_block(self, i, group_1x1, group_3x3, group_trans, bottleneck, activation, use_se): # Check if ith is the last one last = (i == len(self.stages) - 1) block = _SFR_DenseBlock( num_layers=self.stages[i], in_channels=self.num_features, growth_rate=self.growth[i], group_1x1=group_1x1, group_3x3=group_3x3, group_trans=group_trans, bottleneck=bottleneck, activation=activation, use_se=use_se, ) self.features.add_sublayer('denseblock_%d' % (i + 1), block) self.num_features += self.stages[i] * self.growth[i] if not last: trans = _Transition() self.features.add_sublayer('transition_%d' % (i + 1), trans) else: self.features.add_sublayer('norm_last', nn.BatchNorm2D(self.num_features)) self.features.add_sublayer('relu_last', nn.ReLU()) self.features.add_sublayer('pool_last', nn.AvgPool2D(self.pool_size)) # if useSE: self.features.add_sublayer('se_last', SELayer(self.num_features, reduction=self.last_se_reduction)) def forward(self, x): features = self.features(x) out = features.reshape((features.shape[0], -1)) out = self.fc(out) out = self.fc_act(out) if self.class_dim > 0: out = self.classifier(out) return out def _initialize(self): # initialize for m in self.sublayers(): if isinstance(m, nn.Conv2D): kaiming_normal_(m.weight) elif isinstance(m, nn.BatchNorm2D): ones_(m.weight) zeros_(m.bias)登录后复制/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:26: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations def convert_to_list(value, n, name, dtype=np.int):登录后复制
预设模型
In [3]def cdnv2_a(pretrained=False, **kwargs): model = CondenseNetV2( stages=[1, 1, 4, 6, 8], growth=[8, 8, 16, 32, 64], HS_start_block=2, SE_start_block=3, fc_channel=828, group_1x1=8, group_3x3=8, group_trans=8, bottleneck=4, last_se_reduction=16, **kwargs ) if pretrained: params = paddle.load('data/data80680/cdnv2_a.pdparams') model.set_dict(params) return modeldef cdnv2_b(pretrained=False, **kwargs): model = CondenseNetV2( stages=[2, 4, 6, 8, 6], growth=[6, 12, 24, 48, 96], HS_start_block=2, SE_start_block=3, fc_channel=1024, group_1x1=6, group_3x3=6, group_trans=6, bottleneck=4, last_se_reduction=16, **kwargs ) if pretrained: params = paddle.load('data/data80680/cdnv2_b.pdparams') model.set_dict(params) return modeldef cdnv2_c(pretrained=False, **kwargs): model = CondenseNetV2( stages=[4, 6, 8, 10, 8], growth=[8, 16, 32, 64, 128], HS_start_block=2, SE_start_block=3, fc_channel=1024, group_1x1=8, group_3x3=8, group_trans=8, bottleneck=4, last_se_reduction=16, **kwargs ) if pretrained: params = paddle.load('data/data80680/cdnv2_c.pdparams') model.set_dict(params) return model登录后复制模型测试
In [5]model = cdnv2_a()out = model(paddle.randn((1, 3, 224, 224)))print(out.shape)model.eval()out = model(paddle.randn((1, 3, 224, 224)))print(out.shape)登录后复制
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/nn/layer/norm.py:648: UserWarning: When training, we now always track global mean and variance. "When training, we now always track global mean and variance.")登录后复制
[1, 1000][1, 1000]登录后复制
精度表现
具体的模型精度表现如下(ImageNet-1k):
游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。
同类文章
逼AI当山顶洞人!Claude防话痨插件爆火,网友:受够了AI废话
新智元报道编辑:元宇【新智元导读】一个让AI像原始人一样说话的插件,在HN上一夜爆火,冲破2w星。它的核心只是一条简单粗暴的prompt:删掉冠词、客套和一切废话,号称能省下75%的输出token。
时间:2026-04-07 14:55
季度利润翻 8 倍,最赚钱的「卖铲人」财报背后,内存涨价狂潮如何收场?
AI 时代最赚钱的公司,可能从来不是做 AI 的那个。作者|张勇毅编辑|靖宇淘金热里最稳赚的人,从来不是淘金的,是卖铲子的。这句老话在 2026 年的科技行业又应验了一次。只不过这次卖铲子的不是英伟
时间:2026-04-07 14:49
Claude Code Harness+龙虾科研团来了!金字塔分层架构+多智能体
Claw AI Lab团队量子位 | 公众号 QbitAI你还在一个人做科研吗?科研最难的,从来不是问题本身,而是一个想法从文献到实验再到写作,只能靠自己一点点往前推。一个人方向偏了没人提醒,遇到歧
时间:2026-04-07 14:43
让离线强化学习从「局部描摹」变「全局布局」丨ICLR'26
面对复杂连续任务的长程规划,现有的生成式离线强化学习方法往往会暴露短板。它们生成的轨迹经常陷入局部合理但全局偏航的窘境。它们太关注眼前的每一步,却忘了最终的目的地。针对这一痛点,厦门大学和香港科技大
时间:2026-04-07 14:37
美国犹他州启动新试点项目:AI为患者开具精神类药物处方
IT之家 4 月 5 日消息,据外媒 PC Mag 当地时间 4 月 4 日报道,美国医疗机构 Legion Health 在犹他州获得监管批准,启动一项试点项目,允许 AI 系统为患者开具精神类药
时间:2026-04-07 14:30
- 日榜
- 周榜
- 月榜
相关攻略
2015-03-10 11:25
2015-03-10 11:05
2021-08-04 13:30
2015-03-10 11:22
2015-03-10 12:39
2022-05-16 18:57
2025-05-23 13:43
2025-05-23 14:01
热门教程
- 游戏攻略
- 安卓教程
- 苹果教程
- 电脑教程
反恐精英OL官网首页入口在哪 反恐精英OL官网首页入口
发布于 2026-04-07
红色沙漠传奇坐骑银牙获取位置 红色沙漠传奇坐骑银牙获取方法
发布于 2026-04-07
红色沙漠保险箱位置及解谜攻略 利贝坦修道院保险箱解谜
发布于 2026-04-07
西游Ⅲ官网首页入口在哪 西游Ⅲ官网首页入口
发布于 2026-04-07
杀戮尖塔2精神过载卡牌有什么用 精神过载卡牌图鉴效果
发布于 2026-04-07
动作冒险游戏《午夜之南》现已登陆PS5和Switch 2
发布于 2026-04-07
红色沙漠保险箱位置及解谜攻略 斯特伦大宅保险箱解谜
发布于 2026-04-07
红色沙漠斯特伦大宅8个听声按键保险箱解谜
发布于 2026-04-07
《腐烂国度3》开启A测
发布于 2026-04-07
从宿舍到全球!米哈游3位创始人捐赠母校上海交大 设立AI未来基石基金
发布于 2026-04-07
EA再次痛下杀手!17年后这款童年神游官宣停服
发布于 2026-04-07
14年前索尼PS广告太炸了
发布于 2026-04-07
《最后生还者》多人游戏倒在黎明前
发布于 2026-04-07
网传刘慈欣担任《鸣潮》世界观架构师引热议 库洛游戏辟谣:虚假编造
发布于 2026-04-07
小虞姬为"高价陪玩没人点"言论致歉:口无遮拦我的锅
发布于 2026-04-07
精忠报国!《帝国时代4》DLC岳飞传震撼来袭
发布于 2026-04-07
win10如何解决字体显示乱码_win10字体显示乱码完整指南一文搞懂
发布于 2026-04-06
WPS动态交互图表制作指南:让数据变化直观呈现
发布于 2026-04-07
PPT官方网站社区登录地址及用户交流中心入口
发布于 2026-04-07
WPS会员中心登录指南:个人官网入口直达
发布于 2026-04-07
2026最新教程:制作PPT动态交互图表详细步骤
发布于 2026-04-07
PPT交互式图表添加教程:5步让演示动起来(2026)
发布于 2026-04-07
PPT动态交互图表制作指南:3步搞定专业演示
发布于 2026-04-07
Excel交互动态图表制作教程:详细步骤指南
发布于 2026-04-07
PPT动态交互图表制作教程:5步插入可视化图表
发布于 2026-04-07
热门话题

