Commit e09d6a8b authored by dcuai's avatar dcuai
Browse files

Merge branch 'mmpretrain' into 'master'

updated to dtk24.04.1

See merge request !3
parents 0602bfe0 8ace0115
[submodule "mmclassification-mmcv"]
path = mmclassification-mmcv
url = http://developer.hpccube.com/codes/aicomponent/mmclassification-mmcv
[submodule "mmpretrain-mmcv"]
path = mmpretrain-mmcv
url = https://developer.hpccube.com/codes/OpenDAS/mmpretrain-mmcv
# VGG16
## 论文
`VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION`
- https://arxiv.org/abs/1409.1556
## 模型结构
VGG模型是2014年ILSVRC竞赛的第二名,第一名是GoogLeNet。但是VGG模型在多个迁移学习任务中的表现要优于GoogLeNet。而且,从图像中提取CNN特征,VGG模型是首选算法。
![20231124132639](./images/20231124132639.png)
## 算法原理
VGG16共有16个层,是一个相当深的卷积神经网络。VGG各种级别的结构都采用了5段卷积,每一段有一个或多个卷积层。
![20231124132925](./images/20231124132925.png)
## 环境配置
### Docker(方法一)
```bash
git clone --recursive http://developer.hpccube.com/codes/modelzoo/vgg16_mmcv.git
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.10.0-centos7.6-dtk-22.10.1-py37-latest
# <your IMAGE ID>用以上拉取的docker的镜像ID替换
docker run --shm-size 10g --network=host --name=vgg16 --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v $PWD/vgg16_mmcv:/home/vgg16_mmcv -it <your IMAGE ID> bash
cd vgg16_mmcv/mmclassification-mmcv
推荐使用docker方式运行,拉取提供的docker镜像
```shell
docker pull image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
```
基于拉取的镜像创建容器
```shell
# <your IMAGE ID or NAME>用以上拉取的docker的镜像ID或名称替换
docker run -it --name=vgg16-mmcv --network=host --ipc=host --shm-size=16g --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --privileged --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v /opt/hyhal:/opt/hyhal:ro <your IMAGE ID> bash
```
克隆并安装git仓库,安装相关依赖
```python
git clone --recursive http://developer.hpccube.com/codes/modelzoo/vgg16_mmcv.git
cd vgg16_mmcv/mmpretrain-mmcv
pip install -e .
pip install -r requirements.txt
```
### Dockerfile(方法二)
```plaintext
```bash
cd vgg16_mmcv/docker
docker build --no-cache -t vgg16_mmcv:latest .
docker run --rm --shm-size 10g --network=host --name=vgg16 --privileged --device=/dev/kfd --device=/dev/dri --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v $PWD/../../vgg16_mmcv:/home/vgg16_mmcv -it <your IMAGE ID> bash
# 若遇到Dockerfile启动的方式安装环境需要长时间等待,可注释掉里面的pip安装,启动容器后再安装python库:pip install -r requirements.txt
docker run -it --name=vgg16 --network=host --ipc=host --shm-size=16g --device=/dev/kfd --device=/dev/dri --device=/dev/mkfd --group-add video --privileged --cap-add=SYS_PTRACE --security-opt seccomp=unconfined -v /opt/hyhal:/opt/hyhal:ro <your IMAGE ID> bash
pip install -e .
# 若遇到Dockerfile启动的方式安装环境需要长时间等待,可注释掉里面的pip安装,启动容器后再安装python库:
# pip install -r requirements.txt
```
### Anaconda(方法三)
1、关于本项目DCU显卡所需的特殊深度学习库可从光合开发者社区下载安装: https://developer.hpccube.com/tool/
```plaintext
DTK驱动:dtk22.10.1
python:python3.7
torch:1.10.0
torchvision:0.10.0
mmcv:1.6.1
DTK驱动: DTK-24.04.1
python==3.10
torch==2.1.0
torchvision==0.16.0+das1.1.git7d45932.abi1.dtk2404.torch2.1
mmcv==2.0.1+das1.1.gite58da25.abi1.dtk2404.torch2.1.0
Tips:以上dtk驱动、python、torch等DCU相关工具版本需要严格一一对应
```
2、其它非特殊库参照requirements.txt安装
```plaintext
pip install -r requirements.txt
```
## 数据集
在本测试中可以使用ImageNet数据集,下载ImageNet数据集:http://113.200.138.88:18080/aidatasets/project-dependency/imagenet-2012
下载val数据:链接:https://pan.baidu.com/s/1oXsmsYahGVG3uOZ8e535LA?pwd=c3bc 提取码:c3bc 替换ImageNet数据集中的val目录
### ImageNet
在本项目中可以使用ImageNet数据集。ImageNet数据集官方下载地址:https://image-net.org。
或者从SCNet下载[ImageNet](http://113.200.138.88:18080/aidatasets/project-dependency/imagenet-2012)
- ImageNet数据集中的val部分[val](http://113.200.138.88:18080/aidatasets/project-dependency/shufflenet_v2_mmcv)
处理后的数据结构如下:
也可于SCNet快速下载[imagenet-2012](http://113.200.138.88:18080/aidatasets/project-dependency/imagenet-2012),下载其中的ILSVRC2012_img_train.tar和ILSVRC2012_img_val.tar,并按照以下方式解包
```bash
cd mmpretrain-mmcv/data/imagenet
mkdir train && cd train
tar -xvf ILSVRC2012_img_train.tar
```
解包后是1000个tar文件,每个tar对应了一个类别,分别解包至对应文件夹,可利用如下shell脚本。
```bash
for tarfile in *.tar; do
dirname="${tarfile%.tar}"
mkdir "$dirname"
tar -xvf "$tarfile" -C "$dirname"
done
```
目录结构如下
```
├── data
│ ├── imagenet
│   ├── meta
│   ├── val.txt
│   ├── train.txt
│   ...
│   ├── train
│   ├── val
data
└── imagenet
├── train
│   ├── n01440764
│   │   ├── n01440764_10026.JPEG
│   │   ├── n01440764_10027.JPEG
├──val
│   ├── n01440764
│   │   ├── ILSVRC2012_val_00000293.JPEG
```
### Tiny-ImageNet-200
由于ImageNet完整数据集较大,可以使用[tiny-imagenet-200](http://cs231n.stanford.edu/tiny-imagenet-200.zip)进行测试,可于SCNet快速下载[tiny-imagenet-200-scnet](http://113.200.138.88:18080/aidatasets/project-dependency/tiny-imagenet-200) ,此时需要对配置脚本进行一些修改:
- dataset配置文件(configs/\_\_base\_\_/datasets/xxx.py)中,需要对以下字段进行修改
```python
# dataset settings
dataset_type = 'CustomDataset' # 修改为CustomDataset
data_preprocessor = dict(
num_classes=200, # 修改类别为200
...
)
...
train_dataloader = dict(
batch_size=32,
num_workers=5,
dataset=dict(
type=dataset_type,
data_root='data/imagenet',
data_prefix='train', # 改为data_prefix='train',val_dataloader中同理
pipeline=train_pipeline),
sampler=dict(type='DefaultSampler', shuffle=True),
)
```
- model配置文件(configs/\_\_base\_\_/models/{MODEL_CONFIG}.py)中,同样需要将类别相关的值设置为200。
```python
# model settings
model = dict(
type='ImageClassifier',
...
head=dict(
type='LinearClsHead',
num_classes=200, # 将类别数改为200
...
))
```
本仓库的mmpretrain-mmcv中提供了使用tiny-imagenet-200进行训练的若干配置脚本,可参考进行设置。
## 训练
将训练数据解压到data目录下。
### 单机8卡
将训练数据集解压后放置于mmpretrain-mmcv/data/,对于tiny-imagenet,目录结构如下:
```
bash ./vgg16.sh
data
└── imagenet
├── test/
├── train/
├── val/
├── wnids.txt
└── words.txt
```
## result
![img](https://developer.hpccube.com/codes/modelzoo/vit_pytorch/-/raw/master/image/README/1695381570003.png)
### 单机8卡训练
- tiny-imagenet-200
```shell
bash tools/dist_train.sh vgg16-test.py 8
```
- imagenet
```shell
bash tools/dist_train.sh configs/vgg/vgg16_8xb32_in1k.py 8
```
如需其他卡数训练,将命令中的8改为所需卡数即可;
如遇端口占用问题,可在tools/dist_train.sh修改端口。
### 精度
测试数据使用的是ImageNet数据集,使用的加速卡是DCU Z100L。
| 卡数 | 精度 |
| :--: | :---------------------: |
| 8 | top1:0.7162;top5:0.9049 |
## 应用场景
### 算法类别
图像分类
### 热点行业
制造,能源,交通,网安
制造,能源,交通,网安,安防
## 源码仓库及问题反馈
- https://developer.hpccube.com/codes/modelzoo/vgg16_mmcv
https://developer.hpccube.com/codes/modelzoo/mobilenetv2_mmcv
## 参考资料
- https://github.com/open-mmlab/mmpretrain
https://github.com/open-mmlab/mmpretrainhttps://github.com/open-mmlab/mmpretrain
FROM image.sourcefind.cn:5000/dcu/admin/base/pytorch:1.10.0-centos7.6-dtk-22.10.1-py37-latest
FROM image.sourcefind.cn:5000/dcu/admin/base/pytorch:2.1.0-ubuntu20.04-dtk24.04.1-py3.10
ENV DEBIAN_FRONTEND=noninteractive
# 安装pip相关依赖
COPY requirements.txt requirements.txt
......
albumentations>=0.3.2 --no-binary qudida,albumentations
colorama
requests
einops
importlib-metadata
mat4py
matplotlib
modelindex
numpy<2
rich
scipy
matplotlib>=3.1.0
numpy
packaging
codecov
flake8
coverage
interrogate
isort==4.3.21
pytest
xdoctest >= 0.10.0
yapf
albumentations>=0.3.2 --no-binary qudida,albumentations # For Albumentations data transform
grad-cam >= 1.3.7,<1.5.0 # For CAM visualization
requests # For torchserve
scikit-learn # For t-SNE visualization and unit tests.
\ No newline at end of file
Subproject commit 0f6a312ab4b30c6e27efd93608268fe0fe3f7dcc
mmpretrain-mmcv @ 64c15d70
Subproject commit 64c15d709b3dfaa146df50951e9c1a14467bcf4e
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment