one-stage 目标检测——M2Det源码运行测试

转自:https://blog.csdn.net/c20081052/article/details/88803408

 

github地址:https://github.com/qijiezhao/M2Det

Preparation

the supported version is pytorch-0.4.1

  • Prepare python environment using Anaconda3.
  • Install deeplearning framework, i.e., pytorch, torchvision and other libs.

准备工作,支持pytorch版本是0.4.1

推荐使用anaconda进行配置,用以下指令安装pytorch,torchvision,opencv-python,tqdm等包;

 
  1. conda install pytorch torchvision -c pytorch

  2. pip install opencv-python,tqdm

  • Clone this repository.
  • 下载M2Det工程
git clone https://github.com/qijiezhao/M2Det.git
  • Compile the nms and coco tools:
  • 使用如下指令编译nms和coco工具。

以下是编译结果

 
  1. chensq@chensq-Ciky:~/M2Det$ sh make.sh

  2. running build_ext

  3. cythoning nms/cpu_nms.pyx to nms/cpu_nms.c

  4. /home/chensq/anaconda3/lib/python3.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/chensq/M2Det/utils/nms/cpu_nms.pyx

  5. tree = Parsing.p_module(s, pxd, full_module_name)

  6. building 'nms.cpu_nms' extension

  7. creating build

  8. creating build/temp.linux-x86_64-3.7

  9. creating build/temp.linux-x86_64-3.7/nms

  10. {'gcc': ['-Wno-cpp', '-Wno-unused-function']}

  11. gcc -pthread -B /home/chensq/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include -I/home/chensq/anaconda3/include/python3.7m -c nms/cpu_nms.c -o build/temp.linux-x86_64-3.7/nms/cpu_nms.o -Wno-cpp -Wno-unused-function

  12. nms/cpu_nms.c: In function ‘__pyx_pf_3nms_7cpu_nms_2cpu_soft_nms’:

  13. nms/cpu_nms.c:3459:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

  14. __pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0);

  15. ^

  16. nms/cpu_nms.c:3970:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

  17. __pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0);

  18. ^

  19. gcc -pthread -shared -B /home/chensq/anaconda3/compiler_compat -L/home/chensq/anaconda3/lib -Wl,-rpath=/home/chensq/anaconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.7/nms/cpu_nms.o -o /home/chensq/M2Det/utils/nms/cpu_nms.cpython-37m-x86_64-linux-gnu.so

  20. cythoning nms/gpu_nms.pyx to nms/gpu_nms.cpp

  21. /home/chensq/anaconda3/lib/python3.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/chensq/M2Det/utils/nms/gpu_nms.pyx

  22. tree = Parsing.p_module(s, pxd, full_module_name)

  23. building 'nms.gpu_nms' extension

  24. {'gcc': ['-Wno-unused-function'], 'nvcc': ['-arch=sm_52', '--ptxas-options=-v', '-c', '--compiler-options', "'-fPIC'"]}

  25. /usr/local/cuda-10.1/bin/nvcc -I/home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include -I/usr/local/cuda-10.1/include -I/home/chensq/anaconda3/include/python3.7m -c nms/nms_kernel.cu -o build/temp.linux-x86_64-3.7/nms/nms_kernel.o -arch=sm_52 --ptxas-options=-v -c --compiler-options '-fPIC'

  26. ptxas info : 0 bytes gmem

  27. ptxas info : Compiling entry function '_Z10nms_kernelifPKfPy' for 'sm_52'

  28. ptxas info : Function properties for _Z10nms_kernelifPKfPy

  29. 0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads

  30. ptxas info : Used 24 registers, 1280 bytes smem, 344 bytes cmem[0], 12 bytes cmem[2]

  31. {'gcc': ['-Wno-unused-function'], 'nvcc': ['-arch=sm_52', '--ptxas-options=-v', '-c', '--compiler-options', "'-fPIC'"]}

  32. gcc -pthread -B /home/chensq/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include -I/usr/local/cuda-10.1/include -I/home/chensq/anaconda3/include/python3.7m -c nms/gpu_nms.cpp -o build/temp.linux-x86_64-3.7/nms/gpu_nms.o -Wno-unused-function

  33. cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++

  34. In file included from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1823:0,

  35. from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,

  36. from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,

  37. from nms/gpu_nms.cpp:627:

  38. /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]

  39. #warning "Using deprecated NumPy API, disable it by " \

  40. ^

  41. g++ -pthread -shared -B /home/chensq/anaconda3/compiler_compat -L/home/chensq/anaconda3/lib -Wl,-rpath=/home/chensq/anaconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.7/nms/nms_kernel.o build/temp.linux-x86_64-3.7/nms/gpu_nms.o -L/usr/local/cuda-10.1/lib64 -Wl,-R/usr/local/cuda-10.1/lib64 -lcudart -o /home/chensq/M2Det/utils/nms/gpu_nms.cpython-37m-x86_64-linux-gnu.so

  42. cythoning pycocotools/_mask.pyx to pycocotools/_mask.c

  43. /home/chensq/anaconda3/lib/python3.7/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/chensq/M2Det/utils/pycocotools/_mask.pyx

  44. tree = Parsing.p_module(s, pxd, full_module_name)

  45. building 'pycocotools._mask' extension

  46. creating build/temp.linux-x86_64-3.7/pycocotools

  47. {'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']}

  48. gcc -pthread -B /home/chensq/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include -Ipycocotools -I/home/chensq/anaconda3/include/python3.7m -c pycocotools/maskApi.c -o build/temp.linux-x86_64-3.7/pycocotools/maskApi.o -Wno-cpp -Wno-unused-function -std=c99

  49. {'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']}

  50. gcc -pthread -B /home/chensq/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include -Ipycocotools -I/home/chensq/anaconda3/include/python3.7m -c pycocotools/_mask.c -o build/temp.linux-x86_64-3.7/pycocotools/_mask.o -Wno-cpp -Wno-unused-function -std=c99

  51. gcc -pthread -shared -B /home/chensq/anaconda3/compiler_compat -L/home/chensq/anaconda3/lib -Wl,-rpath=/home/chensq/anaconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.7/pycocotools/maskApi.o build/temp.linux-x86_64-3.7/pycocotools/_mask.o -o /home/chensq/M2Det/utils/pycocotools/_mask.cpython-37m-x86_64-linux-gnu.so

 Prepare dataset (e.g., VOC, COCO), refer to sss.pytorch for detailed instructions.

如果需要训练,则需要参考对应链接,准备数据集


Demo展示

作者目前(20190325)提供了 M2Det512_vgg 预训练模型作为验证测试(支持结果可视化):

首先,下载预训练模型 m2det512_vgg.pth (baidu cloud) 文件. 然后新建一个文件夹 weights/. 将模型文件移至该目录下;

one-stage 目标检测——M2Det源码运行测试_第1张图片

运行如下指令即可对它自带的图片做测试了。

python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show

如果弹出如下错误: 

 
  1. chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show

  2. Traceback (most recent call last):

  3. File "demo.py", line 8, in

  4. from configs.CC import Config

  5. File "/home/chensq/M2Det/configs/CC.py", line 7, in

  6. from addict import Dict

  7. ModuleNotFoundError: No module named 'addict'

表示你未安装addict库,用下面指令进行安装(基于我用anaconda来配置的方式)

pip install addict

再次运行,如果弹出如下错误;

 
  1. chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show

  2. Traceback (most recent call last):

  3. File "demo.py", line 11, in

  4. from m2det import build_net

  5. File "/home/chensq/M2Det/m2det.py", line 24, in

  6. from utils.core import print_info

  7. File "/home/chensq/M2Det/utils/core.py", line 14, in

  8. from termcolor import cprint

  9. ModuleNotFoundError: No module named 'termcolor'

表示你未安装termcolor,同理用如下指令安装即可;

pip install termcolor

如下,运行成功了!

 
  1. chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show

  2. ----------------------------------------------------------------------

  3. | M2Det Demo Program |

  4. ----------------------------------------------------------------------

  5. The Anchor info:

  6. {'feature_maps': [64, 32, 16, 8, 4, 2], 'min_dim': 512, 'steps': [8, 16, 32, 64, 128, 256], 'min_sizes': [30.72, 76.8, 168.96, 261.12, 353.28, 445.44], 'max_sizes': [76.8, 168.96, 261.12, 353.28, 445.44, 537.6], 'aspect_ratios': [[2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3]], 'variance': [0.1, 0.2], 'clip': True}

  7. ===> Constructing M2Det model

  8. Loading resume network...

  9. ===> Finished constructing and loading model

  10. pos:(2.0,146.5,36.2,273.8), ids:person, score:0.972

  11. pos:(41.9,124.6,118.1,344.2), ids:person, score:0.913

  12. pos:(123.3,142.2,181.3,355.8), ids:person, score:0.667

  13. pos:(94.8,148.4,134.6,264.5), ids:person, score:0.506

  14. pos:(43.7,141.8,70.7,191.0), ids:person, score:0.447

  15. pos:(94.2,145.9,111.8,166.7), ids:person, score:0.128

  16. pos:(38.6,127.3,111.5,342.4), ids:person, score:0.124

  17. pos:(32.5,146.8,65.7,286.9), ids:person, score:0.120

  18. .....

  19. pos:(878.1,82.3,891.9,146.8), ids:tie, score:0.006

  20. pos:(578.5,96.9,595.2,142.5), ids:tie, score:0.003

测试中其中一张图片检测效果如下;终端打印出目标物的坐标,id,置信度等信息; 

one-stage 目标检测——M2Det源码运行测试_第2张图片

你也可以运行实时的demo,它会调用你的摄像头,如果有多个相机,你可以通过传入参数--cam=相机ID 来指定相机;如下指令是调用摄像头的,我指定了相机ID=0;

python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show --cam=0

one-stage 目标检测——M2Det源码运行测试_第3张图片

测试效果如上,大致FPS : 约为4.5吧,这是GPU上的结果~~~~我的电脑是GeForce 1050ti的GPU

 
  1. pos:(179.8,146.8,665.3,473.3), ids:person, score:0.002

  2. pos:(324.1,302.9,372.3,368.0), ids:cell_phone, score:0.372

  3. pos:(325.1,302.0,375.9,370.7), ids:cell_phone, score:0.035

  4. pos:(321.7,304.2,366.5,366.8), ids:cell_phone, score:0.006

  5. pos:(148.5,94.4,628.4,475.8), ids:person, score:0.992

  6. pos:(208.2,119.8,650.3,469.3), ids:person, score:0.089

  7. pos:(128.1,101.0,622.1,472.6), ids:person, score:0.011

  8. pos:(176.7,158.0,613.0,487.2), ids:person, score:0.002

  9. pos:(121.1,94.4,623.6,471.6), ids:person, score:0.994

  10. pos:(116.6,103.3,621.6,468.5), ids:person, score:0.041

  11. pos:(151.7,103.7,597.4,467.2), ids:person, score:0.002

  12. ...............

 

你可能感兴趣的:(神经网络)