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等包;
conda install pytorch torchvision -c pytorch
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工具。
以下是编译结果
chensq@chensq-Ciky:~/M2Det$ sh make.sh
running build_ext
cythoning nms/cpu_nms.pyx to nms/cpu_nms.c
/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
tree = Parsing.p_module(s, pxd, full_module_name)
building 'nms.cpu_nms' extension
creating build
creating build/temp.linux-x86_64-3.7
creating build/temp.linux-x86_64-3.7/nms
{'gcc': ['-Wno-cpp', '-Wno-unused-function']}
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
nms/cpu_nms.c: In function ‘__pyx_pf_3nms_7cpu_nms_2cpu_soft_nms’:
nms/cpu_nms.c:3459:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
__pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0);
^
nms/cpu_nms.c:3970:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
__pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0);
^
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
cythoning nms/gpu_nms.pyx to nms/gpu_nms.cpp
/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
tree = Parsing.p_module(s, pxd, full_module_name)
building 'nms.gpu_nms' extension
{'gcc': ['-Wno-unused-function'], 'nvcc': ['-arch=sm_52', '--ptxas-options=-v', '-c', '--compiler-options', "'-fPIC'"]}
/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'
ptxas info : 0 bytes gmem
ptxas info : Compiling entry function '_Z10nms_kernelifPKfPy' for 'sm_52'
ptxas info : Function properties for _Z10nms_kernelifPKfPy
0 bytes stack frame, 0 bytes spill stores, 0 bytes spill loads
ptxas info : Used 24 registers, 1280 bytes smem, 344 bytes cmem[0], 12 bytes cmem[2]
{'gcc': ['-Wno-unused-function'], 'nvcc': ['-arch=sm_52', '--ptxas-options=-v', '-c', '--compiler-options', "'-fPIC'"]}
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
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
In file included from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/ndarraytypes.h:1823:0,
from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:18,
from /home/chensq/anaconda3/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from nms/gpu_nms.cpp:627:
/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]
#warning "Using deprecated NumPy API, disable it by " \
^
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
cythoning pycocotools/_mask.pyx to pycocotools/_mask.c
/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
tree = Parsing.p_module(s, pxd, full_module_name)
building 'pycocotools._mask' extension
creating build/temp.linux-x86_64-3.7/pycocotools
{'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']}
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
{'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']}
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
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/. 将模型文件移至该目录下;
运行如下指令即可对它自带的图片做测试了。
python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show
如果弹出如下错误:
chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show
Traceback (most recent call last):
File "demo.py", line 8, in <module>
from configs.CC import Config
File "/home/chensq/M2Det/configs/CC.py", line 7, in <module>
from addict import Dict
ModuleNotFoundError: No module named 'addict'
表示你未安装addict库,用下面指令进行安装(基于我用anaconda来配置的方式)
pip install addict
再次运行,如果弹出如下错误;
chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show
Traceback (most recent call last):
File "demo.py", line 11, in <module>
from m2det import build_net
File "/home/chensq/M2Det/m2det.py", line 24, in <module>
from utils.core import print_info
File "/home/chensq/M2Det/utils/core.py", line 14, in <module>
from termcolor import cprint
ModuleNotFoundError: No module named 'termcolor'
表示你未安装termcolor,同理用如下指令安装即可;
pip install termcolor
如下,运行成功了!
chensq@chensq-Ciky:~/M2Det$ python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show
----------------------------------------------------------------------
| M2Det Demo Program |
----------------------------------------------------------------------
The Anchor info:
{'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}
===> Constructing M2Det model
Loading resume network...
===> Finished constructing and loading model
pos:(2.0,146.5,36.2,273.8), ids:person, score:0.972
pos:(41.9,124.6,118.1,344.2), ids:person, score:0.913
pos:(123.3,142.2,181.3,355.8), ids:person, score:0.667
pos:(94.8,148.4,134.6,264.5), ids:person, score:0.506
pos:(43.7,141.8,70.7,191.0), ids:person, score:0.447
pos:(94.2,145.9,111.8,166.7), ids:person, score:0.128
pos:(38.6,127.3,111.5,342.4), ids:person, score:0.124
pos:(32.5,146.8,65.7,286.9), ids:person, score:0.120
.....
pos:(878.1,82.3,891.9,146.8), ids:tie, score:0.006
pos:(578.5,96.9,595.2,142.5), ids:tie, score:0.003
测试中其中一张图片检测效果如下;终端打印出目标物的坐标,id,置信度等信息;
你也可以运行实时的demo,它会调用你的摄像头,如果有多个相机,你可以通过传入参数--cam=相机ID 来指定相机;如下指令是调用摄像头的,我指定了相机ID=0;
python demo.py -c=configs/m2det512_vgg.py -m=weights/m2det512_vgg.pth --show --cam=0
测试效果如上,大致FPS : 约为4.5吧,这是GPU上的结果~~~~我的电脑是GeForce 1050ti的GPU
pos:(179.8,146.8,665.3,473.3), ids:person, score:0.002
pos:(324.1,302.9,372.3,368.0), ids:cell_phone, score:0.372
pos:(325.1,302.0,375.9,370.7), ids:cell_phone, score:0.035
pos:(321.7,304.2,366.5,366.8), ids:cell_phone, score:0.006
pos:(148.5,94.4,628.4,475.8), ids:person, score:0.992
pos:(208.2,119.8,650.3,469.3), ids:person, score:0.089
pos:(128.1,101.0,622.1,472.6), ids:person, score:0.011
pos:(176.7,158.0,613.0,487.2), ids:person, score:0.002
pos:(121.1,94.4,623.6,471.6), ids:person, score:0.994
pos:(116.6,103.3,621.6,468.5), ids:person, score:0.041
pos:(151.7,103.7,597.4,467.2), ids:person, score:0.002
...............