想给卷积网络添加注意力机制吗?是否已经厌倦了使用SE-NET?本项目使用Paddle2.0复现了含有注意力机制的卷积网络CBAM和BAM,并在动物分类数据集上进行了训练和验证。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

本项目首次使用paddle2.0复现了含有注意力机制的网络CBAM和BAM,并在动物数据集上进行了训练和验证。
动物数据集的划分是按8:2的的划分方法进行训练集与验证集划分的。
CBAM网络的核心思想是提出了CBAM模块。该模块对输入先经过通道注意力模块,和输入相乘后再经过空间注意力模块,和输入再次相乘后得到调整参数的注意力特征图。如图1所示。

图1 CBAM模块细节示意图
BAM网络的核心思想是提出了BAM模块。BAM可以认为是并行版的CBAM。如图2所示。

图2 BAM模块细节示意图
具体实现可以fork后见代码细节。
论文原文:CBAM: Convolutional Block Attention Module
参考代码:
PyTorch的实现
本项目使用10分类的动物数据集进行训练和测试.
该十分类动物数据集,包含dog,horse,elephant,butterfly,chicken,cat,cow,sheep,spider和squirrel。每一分类的图片数量为2k-5k。
| 文件名或文件夹名 | 功能 |
|---|---|
| cbam.py | CBAM模块定义文件 |
| bam.py | BAM模块定义文件 |
| cbam_resnet.py | CBAM和BAM网络定义文件 |
| animal_dataset.py | 数据集定义文件 |
| config.py | 配置文件 |
| train_val_split.py | 训练验证划分文件 |
| train.py | 模型训练 |
| eval.py | 模型验证 |
!unzip -q data/data70196/animals.zip -d work/dataset
import osimport randomfrom matplotlib import pyplot as pltfrom PIL import Image
imgs = []
paths = os.listdir('work/dataset')for path in paths:
img_path = os.path.join('work/dataset', path) if os.path.isdir(img_path):
img_paths = os.listdir(img_path)
img = Image.open(os.path.join(img_path, random.choice(img_paths)))
imgs.append((img, path))
f, ax = plt.subplots(3, 3, figsize=(12,12))for i, img in enumerate(imgs[:9]):
ax[i//3, i%3].imshow(img[0])
ax[i//3, i%3].axis('off')
ax[i//3, i%3].set_title('label: %s' % img[1])
plt.show()<Figure size 864x864 with 9 Axes>
!python code/train_val_split.py
finished train val split!
!python code/train.py --net 'cbam_resnet'
!python code/eval.py --net 'cbam_resnet'
W0218 15:48:02.818117 23045 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1
W0218 15:48:02.824904 23045 device_context.cc:372] device: 0, cuDNN Version: 7.6.
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 103/103 [==============================] - loss: 0.3478 - acc: 0.8544 - 232ms/step
Eval samples: 3276
{'loss': [0.347824], 'acc': 0.8543956043956044}!python code/train.py --net 'bam_resnet'
W0218 15:48:47.528769 23145 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1 W0218 15:48:47.532490 23145 device_context.cc:372] device: 0, cuDNN Version: 7.6. The loss value printed in the log is the current step, and the metric is the average value of previous step. Epoch 1/50
!python code/eval.py --net 'bam_resnet'
W0218 19:49:38.340137 5185 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1
W0218 19:49:38.343930 5185 device_context.cc:372] device: 0, cuDNN Version: 7.6.
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 103/103 [==============================] - loss: 0.2684 - acc: 0.8504 - 199ms/step
Eval samples: 3276
{'loss': [0.2684111], 'acc': 0.8504273504273504}
图3. 使用CBAM和BAM的训练验证图示
!python code/train.py --net 'resnet'
!python code/eval.py --net 'resnet'
W0213 21:34:50.038996 12684 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0213 21:34:50.043457 12684 device_context.cc:372] device: 0, cuDNN Version: 7.6.
Eval begin...
The loss value printed in the log is the current batch, and the metric is the average value of previous step.
step 103/103 [==============================] - loss: 1.4232 - acc: 0.5888 - 191ms/step
Eval samples: 3276
{'loss': [1.4232028], 'acc': 0.5888278388278388}
图4. 使用ResNet的训练验证图示

图5. 使用CBAM、BAM和ResNet的验证比较图示
以上就是基于Paddle2.0的注意力卷积网络CBAM和BAM的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号