PaddleX---Faster RCNN目标检测

RCNN介绍

RCNN(Regions with CNN features)是将CNN方法应用到目标检测问题上,由年轻有为的RBG大神提出,借助CNN良好的特征提取和分类性能,通过RegionProposal方法实现目标检测问题的转化。

算法分为三步:

1)候选区域选择,根据提取的目标图像进行归一化,作为CNN的标准输入。

2)CNN特征提取,标准的CNN过程就是根据输入进行卷积/池化等操作,得到固定维度的输出

3)分类与边界回归,包含两个子步骤,一是对上一步的输出向量进行分类(需要根据特征训练分类器),二是通过边界回归得到精准的目标区域。

Fast-RCNN

Faster RCNN是一个目标检测模型,将特征提取、候选框提取、bounding box回归、分类集成在了一个网络中,使得检测速度和精度有所提升。

Fast-RCNN主要贡献就是在于对RCNN进行加速,在以下方面得到改进:

1)借鉴SPP思路,提出了简化版的ROI池化层,同时加入了候选框映射功能,使得网络能够反向传播,解决了SPP的整体网络训练问题。

2)多任务的loss层

21)softmaxloss代替了SVM,证明了softmax比SVM有更好的效果。

22)smoothloss取代了boudingbox回归

将分类和边框回归进行合并,通过多任务loss层进一步整合深度网络,统一了训练过程,提高了算法的准确性。

3)全连接层通过SVD加速

4)结合上面的改进,模型训练时可对所有层进行更新,除了速度提升外(训练速度是SPP的3倍,测试速度10倍),得到了更好的检测效果(VOCO7数据集mAP为70,mAP,是目标检测中衡量识别精度的指标,多个类别目标检测中,每个类别都可以根据召回率和准确率绘制一条曲线,AP就是该曲线下的面积,mAP意思是对每一类的AP再求平均。)

这里使用了paddlepaddle中的PaddleX进行Faster RCNN,所以先安装PaddelX

安装paddlex,地址是https://mirror.baidu.com/pypi/simple,地址下面是有下划线的
! pip install paddlex -i https://mirror.baidu.com/pypi/simple
Looking in indexes: https://mirror.baidu.com/pypi/simple
Collecting paddlex
  Downloading https://mirror.baidu.com/pypi/packages/d6/e3/da77d8ef8c08d190199e02ba6296ead5f92ea556f0785c3bd27dce7cc28f/paddlex-0.1.4-py3-none-any.whl (190kB)
     |████████████████████████████████| 194kB 10.9MB/s eta 0:00:01
Collecting pycocotools (from paddlex)
  Downloading https://mirror.baidu.com/pypi/packages/96/84/9a07b1095fd8555ba3f3d519517c8743c2554a245f9476e5e39869f948d2/pycocotools-2.0.0.tar.gz (1.5MB)
     |████████████████████████████████| 1.5MB 20.0MB/s eta 0:00:01
Collecting colorama (from paddlex)
  Downloading https://mirror.baidu.com/pypi/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (4.36.1)
Collecting paddleslim==1.0.1 (from paddlex)
  Downloading https://mirror.baidu.com/pypi/packages/69/3c/880afac020e3393da5a55b4e0b504d2b644a7ebe91092d953185f09660d1/paddleslim-1.0.1-py2.py3-none-any.whl (103kB)
     |████████████████████████████████| 112kB 12.5MB/s eta 0:00:01
Requirement already satisfied: pyyaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (5.1.2)
Requirement already satisfied: visualdl==1.3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (1.3.0)
Collecting paddlehub>=1.6.2 (from paddlex)
  Downloading https://mirror.baidu.com/pypi/packages/6e/07/d4839d63853c01d2f9d040ff079e63e007c9e4084e74f447baf46b426811/paddlehub-1.6.2-py3-none-any.whl (207kB)
     |████████████████████████████████| 215kB 22.6MB/s eta 0:00:01
Requirement already satisfied: Flask in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (1.1.1)
Requirement already satisfied: numpy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (1.16.4)
Requirement already satisfied: protobuf>=3.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (3.10.0)
Requirement already satisfied: Pillow in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (6.2.0)
Requirement already satisfied: scipy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (1.3.0)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (4.1.1.26)
Requirement already satisfied: cma==2.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.7.0)
Requirement already satisfied: gunicorn>=19.10.0; sys_platform != "win32" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (20.0.4)
Requirement already satisfied: chardet==3.0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.0.4)
Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (4.1.0)
Requirement already satisfied: tensorboard>=1.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.1.0)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.21.0)
Requirement already satisfied: six>=1.10.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.12.0)
Requirement already satisfied: tb-paddle in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.3.6)
Requirement already satisfied: yapf==0.26.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.26.0)
Requirement already satisfied: pandas; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.23.4)
Requirement already satisfied: sentencepiece in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.1.85)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.22.0)
Requirement already satisfied: nltk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.4.5)
Requirement already satisfied: flake8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.7.9)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask->visualdl==1.3.0->paddlex) (7.0)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask->visualdl==1.3.0->paddlex) (1.1.0)
Requirement already satisfied: Werkzeug>=0.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask->visualdl==1.3.0->paddlex) (0.16.0)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Flask->visualdl==1.3.0->paddlex) (2.10.1)
Requirement already satisfied: setuptools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from protobuf>=3.1.0->visualdl==1.3.0->paddlex) (41.4.0)
Requirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.10.0)
Requirement already satisfied: grpcio>=1.24.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.26.0)
Requirement already satisfied: absl-py>=0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.8.1)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.33.6)
Requirement already satisfied: markdown>=2.6.8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (3.1.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.4.1)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (0.23)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.4.10)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.3.4)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.3.0)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (2.0.1)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (0.10.0)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (16.7.9)
Requirement already satisfied: moviepy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tb-paddle->paddlehub>=1.6.2->paddlex) (1.0.1)
Requirement already satisfied: python-dateutil>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub>=1.6.2->paddlex) (2.8.0)
Requirement already satisfied: pytz>=2011k in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub>=1.6.2->paddlex) (2019.3)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (1.25.6)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (2019.9.11)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (0.6.1)
Requirement already satisfied: pyflakes<2.2.0,>=2.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (2.1.1)
Requirement already satisfied: pycodestyle<2.6.0,>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (2.5.0)
Requirement already satisfied: entrypoints<0.4.0,>=0.3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (0.3)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->Flask->visualdl==1.3.0->paddlex) (1.1.1)
Requirement already satisfied: rsa<4.1,>=3.1.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (4.0)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (4.0.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.2.7)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.3.0)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->pre-commit->paddlehub>=1.6.2->paddlex) (0.6.0)
Requirement already satisfied: imageio<3.0,>=2.5; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (2.6.1)
Requirement already satisfied: decorator<5.0,>=4.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (4.4.0)
Requirement already satisfied: proglog<=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (0.1.9)
Requirement already satisfied: imageio-ffmpeg>=0.2.0; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (0.3.0)
Requirement already satisfied: pyasn1>=0.1.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (3.1.0)
Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->pre-commit->paddlehub>=1.6.2->paddlex) (7.2.0)
Building wheels for collected packages: pycocotools
  Building wheel for pycocotools (setup.py) ... done
  Created wheel for pycocotools: filename=pycocotools-2.0.0-cp37-cp37m-linux_x86_64.whl size=286717 sha256=89fe973cc42c0ec310a7ca73ed58ae6a98ac35ce38ce7a8fd0f4ff20dc5ff9f5
  Stored in directory: /home/aistudio/.cache/pip/wheels/87/53/98/53ebc0e2e042812e7626cb4e1e9e5418a7e77c187d1719620f
Successfully built pycocotools
Installing collected packages: pycocotools, colorama, paddleslim, paddlehub, paddlex
  Found existing installation: paddlehub 1.5.0
    Uninstalling paddlehub-1.5.0:
      Successfully uninstalled paddlehub-1.5.0
Successfully installed colorama-0.4.3 paddlehub-1.6.2 paddleslim-1.0.1 paddlex-0.1.4 pycocotools-2.0.0

2.准备昆虫目标检测数据集

#下载数据集并进行解压
! wget https://bj.bcebos.com/paddlex/datasets/insect_det.tar.gz
! tar xzvf insect_det.tar.gz
--2020-05-05 21:30:46--  https://bj.bcebos.com/paddlex/datasets/insect_det.tar.gz
Resolving bj.bcebos.com (bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to bj.bcebos.com (bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17304082 (17M) [application/octet-stream]
Saving to: ‘insect_det.tar.gz’

insect_det.tar.gz   100%[===================>]  16.50M  7.87MB/s    in 2.1s    

2020-05-05 21:30:49 (7.87 MB/s) - ‘insect_det.tar.gz’ saved [17304082/17304082]

insect_det/
insect_det/labels.txt
insect_det/JPEGImages/
insect_det/JPEGImages/0026.jpg
insect_det/JPEGImages/0135.jpg
insect_det/JPEGImages/0035.jpg
insect_det/JPEGImages/0174.jpg
insect_det/JPEGImages/0208.jpg
insect_det/JPEGImages/0120.jpg
insect_det/JPEGImages/0173.jpg
insect_det/JPEGImages/0132.jpg
insect_det/JPEGImages/0087.jpg
insect_det/JPEGImages/0039.jpg
insect_det/JPEGImages/0031.jpg
insect_det/JPEGImages/0115.jpg
insect_det/JPEGImages/0153.jpg
insect_det/JPEGImages/0003.jpg
insect_det/JPEGImages/0082.jpg
insect_det/JPEGImages/0177.jpg
insect_det/JPEGImages/0101.jpg
insect_det/JPEGImages/0133.jpg
insect_det/JPEGImages/0104.jpg
insect_det/JPEGImages/0109.jpg
insect_det/JPEGImages/0088.jpg
insect_det/JPEGImages/0056.jpg
insect_det/JPEGImages/0196.jpg
insect_det/JPEGImages/0140.jpg
insect_det/JPEGImages/0188.jpg
insect_det/JPEGImages/0078.jpg
insect_det/JPEGImages/0126.jpg
insect_det/JPEGImages/0127.jpg
insect_det/JPEGImages/0216.jpg
insect_det/JPEGImages/0110.jpg
insect_det/JPEGImages/0067.jpg
insect_det/JPEGImages/0014.jpg
insect_det/JPEGImages/0117.jpg
insect_det/JPEGImages/0212.jpg
insect_det/JPEGImages/0161.jpg
insect_det/JPEGImages/0138.jpg
insect_det/JPEGImages/0130.jpg
insect_det/JPEGImages/0175.jpg
insect_det/JPEGImages/0009.jpg
insect_det/JPEGImages/0095.jpg
insect_det/JPEGImages/0013.jpg
insect_det/JPEGImages/0090.jpg
insect_det/JPEGImages/0197.jpg
insect_det/JPEGImages/0020.jpg
insect_det/JPEGImages/0121.jpg
insect_det/JPEGImages/0146.jpg
insect_det/JPEGImages/0142.jpg
insect_det/JPEGImages/0156.jpg
insect_det/JPEGImages/0047.jpg
insect_det/JPEGImages/0215.jpg
insect_det/JPEGImages/0050.jpg
insect_det/JPEGImages/0094.jpg
insect_det/JPEGImages/0063.jpg
insect_det/JPEGImages/0211.jpg
insect_det/JPEGImages/0032.jpg
insect_det/JPEGImages/0010.jpg
insect_det/JPEGImages/0080.jpg
insect_det/JPEGImages/0181.jpg
insect_det/JPEGImages/0016.jpg
insect_det/JPEGImages/0165.jpg
insect_det/JPEGImages/0099.jpg
insect_det/JPEGImages/0105.jpg
insect_det/JPEGImages/0072.jpg
insect_det/JPEGImages/0149.jpg
insect_det/JPEGImages/0194.jpg
insect_det/JPEGImages/0091.jpg
insect_det/JPEGImages/0204.jpg
insect_det/JPEGImages/0206.jpg
insect_det/JPEGImages/0089.jpg
insect_det/JPEGImages/0152.jpg
insect_det/JPEGImages/0057.jpg
insect_det/JPEGImages/0198.jpg
insect_det/JPEGImages/0023.jpg
insect_det/JPEGImages/0128.jpg
insect_det/JPEGImages/0086.jpg
insect_det/JPEGImages/0074.jpg
insect_det/JPEGImages/0041.jpg
insect_det/JPEGImages/0124.jpg
insect_det/JPEGImages/0068.jpg
insect_det/JPEGImages/0170.jpg
insect_det/JPEGImages/0129.jpg
insect_det/JPEGImages/0201.jpg
insect_det/JPEGImages/0012.jpg
insect_det/JPEGImages/0040.jpg
insect_det/JPEGImages/0187.jpg
insect_det/JPEGImages/0017.jpg
insect_det/JPEGImages/0145.jpg
insect_det/JPEGImages/0116.jpg
insect_det/JPEGImages/0136.jpg
insect_det/JPEGImages/0186.jpg
insect_det/JPEGImages/0114.jpg
insect_det/JPEGImages/0166.jpg
insect_det/JPEGImages/0061.jpg
insect_det/JPEGImages/0203.jpg
insect_det/JPEGImages/0172.jpg
insect_det/JPEGImages/0055.jpg
insect_det/JPEGImages/0191.jpg
insect_det/JPEGImages/0190.jpg
insect_det/JPEGImages/0192.jpg
insect_det/JPEGImages/0093.jpg
insect_det/JPEGImages/0217.jpg
insect_det/JPEGImages/0065.jpg
insect_det/JPEGImages/0069.jpg
insect_det/JPEGImages/0048.jpg
insect_det/JPEGImages/0077.jpg
insect_det/JPEGImages/0179.jpg
insect_det/JPEGImages/0151.jpg
insect_det/JPEGImages/0144.jpg
insect_det/JPEGImages/0180.jpg
insect_det/JPEGImages/0213.jpg
insect_det/JPEGImages/0004.jpg
insect_det/JPEGImages/0178.jpg
insect_det/JPEGImages/0141.jpg
insect_det/JPEGImages/0193.jpg
insect_det/JPEGImages/0002.jpg
insect_det/JPEGImages/0158.jpg
insect_det/JPEGImages/0015.jpg
insect_det/JPEGImages/0070.jpg
insect_det/JPEGImages/0076.jpg
insect_det/JPEGImages/0108.jpg
insect_det/JPEGImages/0062.jpg
insect_det/JPEGImages/0081.jpg
insect_det/JPEGImages/0131.jpg
insect_det/JPEGImages/0202.jpg
insect_det/JPEGImages/0119.jpg
insect_det/JPEGImages/0059.jpg
insect_det/JPEGImages/0008.jpg
insect_det/JPEGImages/0184.jpg
insect_det/JPEGImages/0033.jpg
insect_det/JPEGImages/0037.jpg
insect_det/JPEGImages/0007.jpg
insect_det/JPEGImages/0162.jpg
insect_det/JPEGImages/0027.jpg
insect_det/JPEGImages/0169.jpg
insect_det/JPEGImages/0205.jpg
insect_det/JPEGImages/0159.jpg
insect_det/JPEGImages/0028.jpg
insect_det/JPEGImages/0049.jpg
insect_det/JPEGImages/0123.jpg
insect_det/JPEGImages/0100.jpg
insect_det/JPEGImages/0195.jpg
insect_det/JPEGImages/0148.jpg
insect_det/JPEGImages/0029.jpg
insect_det/JPEGImages/0164.jpg
insect_det/JPEGImages/0098.jpg
insect_det/JPEGImages/0163.jpg
insect_det/JPEGImages/0134.jpg
insect_det/JPEGImages/0018.jpg
insect_det/JPEGImages/0019.jpg
insect_det/JPEGImages/0209.jpg
insect_det/JPEGImages/0083.jpg
insect_det/JPEGImages/0167.jpg
insect_det/JPEGImages/0006.jpg
insect_det/JPEGImages/0182.jpg
insect_det/JPEGImages/0025.jpg
insect_det/JPEGImages/0125.jpg
insect_det/JPEGImages/0122.jpg
insect_det/JPEGImages/0053.jpg
insect_det/JPEGImages/0147.jpg
insect_det/JPEGImages/0079.jpg
insect_det/JPEGImages/0107.jpg
insect_det/JPEGImages/0200.jpg
insect_det/JPEGImages/0113.jpg
insect_det/JPEGImages/0075.jpg
insect_det/JPEGImages/0096.jpg
insect_det/JPEGImages/0036.jpg
insect_det/JPEGImages/0034.jpg
insect_det/JPEGImages/0043.jpg
insect_det/JPEGImages/0071.jpg
insect_det/JPEGImages/0021.jpg
insect_det/JPEGImages/0011.jpg
insect_det/JPEGImages/0005.jpg
insect_det/JPEGImages/0111.jpg
insect_det/JPEGImages/0154.jpg
insect_det/JPEGImages/0030.jpg
insect_det/JPEGImages/0045.jpg
insect_det/JPEGImages/0168.jpg
insect_det/JPEGImages/0183.jpg
insect_det/JPEGImages/0103.jpg
insect_det/JPEGImages/0097.jpg
insect_det/JPEGImages/0210.jpg
insect_det/JPEGImages/0150.jpg
insect_det/JPEGImages/0052.jpg
insect_det/JPEGImages/0046.jpg
insect_det/JPEGImages/0073.jpg
insect_det/JPEGImages/0106.jpg
insect_det/JPEGImages/0001.jpg
insect_det/JPEGImages/0042.jpg
insect_det/JPEGImages/0199.jpg
insect_det/JPEGImages/0084.jpg
insect_det/JPEGImages/0066.jpg
insect_det/JPEGImages/0176.jpg
insect_det/JPEGImages/0143.jpg
insect_det/JPEGImages/0207.jpg
insect_det/JPEGImages/0054.jpg
insect_det/JPEGImages/0118.jpg
insect_det/JPEGImages/0214.jpg
insect_det/JPEGImages/0185.jpg
insect_det/JPEGImages/0160.jpg
insect_det/JPEGImages/0022.jpg
insect_det/JPEGImages/0112.jpg
insect_det/JPEGImages/0085.jpg
insect_det/JPEGImages/0060.jpg
insect_det/JPEGImages/0038.jpg
insect_det/JPEGImages/0044.jpg
insect_det/JPEGImages/0157.jpg
insect_det/JPEGImages/0189.jpg
insect_det/JPEGImages/0137.jpg
insect_det/JPEGImages/0024.jpg
insect_det/JPEGImages/0155.jpg
insect_det/JPEGImages/0171.jpg
insect_det/JPEGImages/0092.jpg
insect_det/JPEGImages/0102.jpg
insect_det/JPEGImages/0051.jpg
insect_det/JPEGImages/0139.jpg
insect_det/JPEGImages/0058.jpg
insect_det/JPEGImages/0064.jpg
insect_det/train_list.txt
insect_det/val_list.txt
insect_det/Annotations/
insect_det/Annotations/0009.xml
insect_det/Annotations/0011.xml
insect_det/Annotations/0063.xml
insect_det/Annotations/0073.xml
insect_det/Annotations/0076.xml
insect_det/Annotations/0050.xml
insect_det/Annotations/0019.xml
insect_det/Annotations/0181.xml
insect_det/Annotations/0003.xml
insect_det/Annotations/0188.xml
insect_det/Annotations/0083.xml
insect_det/Annotations/0112.xml
insect_det/Annotations/0135.xml
insect_det/Annotations/0121.xml
insect_det/Annotations/0069.xml
insect_det/Annotations/0006.xml
insect_det/Annotations/0051.xml
insect_det/Annotations/0097.xml
insect_det/Annotations/0103.xml
insect_det/Annotations/0048.xml
insect_det/Annotations/0162.xml
insect_det/Annotations/0136.xml
insect_det/Annotations/0080.xml
insect_det/Annotations/0166.xml
insect_det/Annotations/0114.xml
insect_det/Annotations/0171.xml
insect_det/Annotations/0216.xml
insect_det/Annotations/0012.xml
insect_det/Annotations/0119.xml
insect_det/Annotations/0104.xml
insect_det/Annotations/0131.xml
insect_det/Annotations/0178.xml
insect_det/Annotations/0102.xml
insect_det/Annotations/0185.xml
insect_det/Annotations/0040.xml
insect_det/Annotations/0183.xml
insect_det/Annotations/0064.xml
insect_det/Annotations/0039.xml
insect_det/Annotations/0109.xml
insect_det/Annotations/0125.xml
insect_det/Annotations/0054.xml
insect_det/Annotations/0173.xml
insect_det/Annotations/0168.xml
insect_det/Annotations/0105.xml
insect_det/Annotations/0206.xml
insect_det/Annotations/0187.xml
insect_det/Annotations/0147.xml
insect_det/Annotations/0208.xml
insect_det/Annotations/0044.xml
insect_det/Annotations/0014.xml
insect_det/Annotations/0211.xml
insect_det/Annotations/0144.xml
insect_det/Annotations/0037.xml
insect_det/Annotations/0134.xml
insect_det/Annotations/0172.xml
insect_det/Annotations/0128.xml
insect_det/Annotations/0141.xml
insect_det/Annotations/0074.xml
insect_det/Annotations/0203.xml
insect_det/Annotations/0081.xml
insect_det/Annotations/0066.xml
insect_det/Annotations/0038.xml
insect_det/Annotations/0085.xml
insect_det/Annotations/0133.xml
insect_det/Annotations/0182.xml
insect_det/Annotations/0153.xml
insect_det/Annotations/0202.xml
insect_det/Annotations/0156.xml
insect_det/Annotations/0068.xml
insect_det/Annotations/0043.xml
insect_det/Annotations/0013.xml
insect_det/Annotations/0023.xml
insect_det/Annotations/0094.xml
insect_det/Annotations/0008.xml
insect_det/Annotations/0036.xml
insect_det/Annotations/0184.xml
insect_det/Annotations/0176.xml
insect_det/Annotations/0130.xml
insect_det/Annotations/0100.xml
insect_det/Annotations/0196.xml
insect_det/Annotations/0165.xml
insect_det/Annotations/0137.xml
insect_det/Annotations/0213.xml
insect_det/Annotations/0132.xml
insect_det/Annotations/0154.xml
insect_det/Annotations/0086.xml
insect_det/Annotations/0120.xml
insect_det/Annotations/0149.xml
insect_det/Annotations/0022.xml
insect_det/Annotations/0067.xml
insect_det/Annotations/0091.xml
insect_det/Annotations/0195.xml
insect_det/Annotations/0189.xml
insect_det/Annotations/0190.xml
insect_det/Annotations/0090.xml
insect_det/Annotations/0155.xml
insect_det/Annotations/0061.xml
insect_det/Annotations/0087.xml
insect_det/Annotations/0004.xml
insect_det/Annotations/0116.xml
insect_det/Annotations/0148.xml
insect_det/Annotations/0031.xml
insect_det/Annotations/0207.xml
insect_det/Annotations/0152.xml
insect_det/Annotations/0214.xml
insect_det/Annotations/0032.xml
insect_det/Annotations/0180.xml
insect_det/Annotations/0010.xml
insect_det/Annotations/0056.xml
insect_det/Annotations/0191.xml
insect_det/Annotations/0209.xml
insect_det/Annotations/0186.xml
insect_det/Annotations/0072.xml
insect_det/Annotations/0001.xml
insect_det/Annotations/0065.xml
insect_det/Annotations/0200.xml
insect_det/Annotations/0082.xml
insect_det/Annotations/0157.xml
insect_det/Annotations/0058.xml
insect_det/Annotations/0053.xml
insect_det/Annotations/0101.xml
insect_det/Annotations/0118.xml
insect_det/Annotations/0108.xml
insect_det/Annotations/0093.xml
insect_det/Annotations/0002.xml
insect_det/Annotations/0110.xml
insect_det/Annotations/0059.xml
insect_det/Annotations/0161.xml
insect_det/Annotations/0146.xml
insect_det/Annotations/0096.xml
insect_det/Annotations/0127.xml
insect_det/Annotations/0126.xml
insect_det/Annotations/0113.xml
insect_det/Annotations/0123.xml
insect_det/Annotations/0210.xml
insect_det/Annotations/0075.xml
insect_det/Annotations/0077.xml
insect_det/Annotations/0215.xml
insect_det/Annotations/0041.xml
insect_det/Annotations/0016.xml
insect_det/Annotations/0129.xml
insect_det/Annotations/0078.xml
insect_det/Annotations/0197.xml
insect_det/Annotations/0193.xml
insect_det/Annotations/0159.xml
insect_det/Annotations/0028.xml
insect_det/Annotations/0060.xml
insect_det/Annotations/0052.xml
insect_det/Annotations/0160.xml
insect_det/Annotations/0174.xml
insect_det/Annotations/0164.xml
insect_det/Annotations/0194.xml
insect_det/Annotations/0027.xml
insect_det/Annotations/0151.xml
insect_det/Annotations/0092.xml
insect_det/Annotations/0024.xml
insect_det/Annotations/0046.xml
insect_det/Annotations/0169.xml
insect_det/Annotations/0005.xml
insect_det/Annotations/0020.xml
insect_det/Annotations/0138.xml
insect_det/Annotations/0034.xml
insect_det/Annotations/0205.xml
insect_det/Annotations/0047.xml
insect_det/Annotations/0139.xml
insect_det/Annotations/0204.xml
insect_det/Annotations/0201.xml
insect_det/Annotations/0062.xml
insect_det/Annotations/0033.xml
insect_det/Annotations/0025.xml
insect_det/Annotations/0099.xml
insect_det/Annotations/0030.xml
insect_det/Annotations/0057.xml
insect_det/Annotations/0217.xml
insect_det/Annotations/0018.xml
insect_det/Annotations/0170.xml
insect_det/Annotations/0115.xml
insect_det/Annotations/0179.xml
insect_det/Annotations/0150.xml
insect_det/Annotations/0199.xml
insect_det/Annotations/0124.xml
insect_det/Annotations/0070.xml
insect_det/Annotations/0045.xml
insect_det/Annotations/0029.xml
insect_det/Annotations/0021.xml
insect_det/Annotations/0095.xml
insect_det/Annotations/0158.xml
insect_det/Annotations/0212.xml
insect_det/Annotations/0167.xml
insect_det/Annotations/0177.xml
insect_det/Annotations/0079.xml
insect_det/Annotations/0192.xml
insect_det/Annotations/0122.xml
insect_det/Annotations/0035.xml
insect_det/Annotations/0071.xml
insect_det/Annotations/0049.xml
insect_det/Annotations/0163.xml
insect_det/Annotations/0145.xml
insect_det/Annotations/0142.xml
insect_det/Annotations/0015.xml
insect_det/Annotations/0175.xml
insect_det/Annotations/0026.xml
insect_det/Annotations/0055.xml
insect_det/Annotations/0042.xml
insect_det/Annotations/0140.xml
insect_det/Annotations/0089.xml
insect_det/Annotations/0084.xml
insect_det/Annotations/0088.xml
insect_det/Annotations/0017.xml
insect_det/Annotations/0007.xml
insect_det/Annotations/0143.xml
insect_det/Annotations/0098.xml
insect_det/Annotations/0117.xml
insect_det/Annotations/0107.xml
insect_det/Annotations/0111.xml
insect_det/Annotations/0106.xml
insect_det/Annotations/0198.xml
insect_det/test_list.txt

PaddleX---Faster RCNN目标检测_第1张图片PaddleX---Faster RCNN目标检测_第2张图片

PaddleX---Faster RCNN目标检测_第3张图片PaddleX---Faster RCNN目标检测_第4张图片

3.模型训练

3.1 配置GPU

# 设置使用0号GPU卡(如无GPU,执行此代码后仍然会使用CPU训练模型)
import matplotlib
matplotlib.use('Agg') 
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import paddlex as pdx

3.2 定义图像处理流程transforms

定义数据处理流程,其中训练和测试需要分别定义,训练包括了部分测试过程中不需要的数据增强操作,在本示例中,训练过程使用了RandomHorizontalFlip数据增强方式。

from paddlex.det import transforms
train_transforms = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.Normalize(),
    transforms.ResizeByShort(short_size=800, max_size=1333),
    transforms.Padding(coarsest_stride=32)
])

eval_transforms = transforms.Compose([
    transforms.Normalize(),
    transforms.ResizeByShort(short_size=800, max_size=1333),
    transforms.Padding(coarsest_stride=32),
])

3.3 定义数据集Dataset

目标检测可使用VOCDetection格式和COCODetection两种数据集,这里由于数据集为VOC格式,因此采用pdx.datasets.VOCDetection来加载数据集。

train_dataset = pdx.datasets.VOCDetection(
    data_dir='insect_det',
    file_list='insect_det/train_list.txt',
    label_list='insect_det/labels.txt',
    transforms=train_transforms,
    shuffle=True)
eval_dataset = pdx.datasets.VOCDetection(
    data_dir='insect_det',
    file_list='insect_det/val_list.txt',
    label_list='insect_det/labels.txt',
    transforms=eval_transforms)

PaddleX---Faster RCNN目标检测_第5张图片

PaddleX---Faster RCNN目标检测_第6张图片

PaddleX---Faster RCNN目标检测_第7张图片

PaddleX---Faster RCNN目标检测_第8张图片

PaddleX---Faster RCNN目标检测_第9张图片

3.4 模型开始训练

使用本数据集在P40上训练,如有GPU,模型的训练过程预估为9分钟,如无GPU,则预估为6小时左右。模型训练过程每间隔save_interval_epochs轮会保存一次模型在save_dir目录下,同时在保存的过程中也会在验证数据集上计算相关指标。

num_classes = len(train_dataset.labels) + 1
model = pdx.det.FasterRCNN(num_classes=num_classes)
model.train(
    num_epochs=12,
    train_dataset=train_dataset,
    train_batch_size=2,
    eval_dataset=eval_dataset,
    learning_rate=0.0025,
    lr_decay_epochs=[8, 11],
    save_interval_epochs=1,
    save_dir='output/faster_rcnn_r50_fpn')

PaddleX---Faster RCNN目标检测_第10张图片

4 模型预测

使用模型进行预测,同时使用pdx.det.visualize将结果进行可视化,可视化结果将保存到./output/faster_rcnn_r50_fpn下,其中threshold代表Box的置信度阈值,将Box置信度低于该阈值的框过滤进行可视化。

PaddleX---Faster RCNN目标检测_第11张图片

image_name = 'insect_det/JPEGImages/0216.jpg'
result = model.predict(image_name)
pdx.det.visualize(image_name, result, threshold=0.5, save_dir='./output/faster_rcnn_r50_fpn')

PaddleX---Faster RCNN目标检测_第12张图片

你可能感兴趣的:(学习记录)