本项目用Paddle2.0复现RAN,在10类动物数据集(8:2划分)上实验,对比RAN-56与ResNet50。RAN-56验证准确率85.26%,远高于ResNet50的58.88%,体现注意力机制优势。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

项目背景
RAN(Residual Attention Network)是2017年CVPR上的一篇论文Residual Attention Network for Image Classification中提出的基于注意力机制的卷积网络模型。本项目即对其进行复现。
项目简介
本项目首次使用paddle2.0复现了含有注意力机制的网络RAN,并在动物数据集上进行了训练和验证。
动物数据集的划分是按8:2的的划分方法进行训练集与验证集划分的。
模型简介
RNA网络的核心思想是提出了注意力模块。该模块含有残差结构,可以像残差网络一样通过堆叠注意力模块来提高网络的性能。有别于后续出现的含有注意力机制的卷积网络,RAN不是即插即用的模块,而是一种独立的完备的网络架构。RAN的网络结构如图1所示。

图1 残差注意力模块细节示意图
具体实现可以fork后见代码细节。
论文原文:Residual Attention Network for Image Classification
参考代码:
PyTorch的实现
数据集介绍
本项目使用10分类的动物数据集进行训练和测试.
该十分类动物数据集,包含dog,horse,elephant,butterfly,chicken,cat,cow,sheep,spider和squirrel。每一分类的图片数量为2k-5k。
文件结构
| 文件名或文件夹名 | 功能 |
|---|---|
| ran.py | ran网络定义文件 |
| attention_module.py | 注意力模块定义文件 |
| basic_layers.py | 残差模块定义文件 |
| 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()划分训练集和验证集
!python code/train_val_split.py
finished train val split!
使用RAN-56网络进行动物分类的训练并验证
!python code/train.py --net 'ran'
验证
!python code/eval.py --net 'ran'
W0224 11:30:25.761442 11241240 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1
W0224 11:30:25.766026 11241240 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.7512 - acc: 0.8526 - 184ms/step
Eval samples: 3276
{'loss': [0.75124124395], 'acc': 0.8525641025641025}图示训练验证过程

图2. 使用RAN-56的训练验证图示
使用ResNet50网络进行动物分类的训练并验证
训练
!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}图示训练验证过程

图3. 使用ResNet-50的训练验证图示
比较

图4. 使用RAN-56和ResNet-50的验证比较图示










