detectron2 大部分代码都需要GPU
detectron2 主要是用于检测和分割的代码框架,像分类这种任务的代码暂时没有
官方示例有一些是基于Colab的,需要访问国外网站才能访问
sudo pip install -U torch==1.4+cu100 torchvision==0.5+cu100 -f https://download.pytorch.org/whl/torch_stable.html
sudo pip install cython pyyaml==5.1 --ingnore-installed
# 安装 cocoapi
sudo pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
其中cocoapi 需要从GitHub下载代码,如果安装太慢,可以先clone下代码,再进PythonAPI
子目录,运行setup.py
安装:
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
sudo python3 setup.py install
这里直接安装编译好的二进制文件。
sudo pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu100/index.html
如果文件下载太慢或者超时,可以手动在浏览器里面下载好文件,再用下面的命令安装(假设下载的whl
文件是xxx.whl
):
sudo pip install xxx.whl
安装完后,打开 Python 命令行,执行下面的命令,如果不报错,说明安装成功:
import detectron2
为了测试,需要下载 detectron2 的源代码,基于 demo/demo.py
进行简单的测试:
git clone https://github.com/facebookresearch/detectron2
python3 detectron2/demo/demo.py --config-file detectron2/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input ~/test.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
注意上述代码需要在 detectron2 的 git 仓库外面执行,否则会报错。 测试时输入支持单张图片、多张图片、单个图片文件夹、网络摄像头以及视频文件,每种情况参数设置如下:
# 单张图片
--input test.jpg
# 多张图片
--input test1.jpg test2.jpg test3.jpg
# 单个图片文件夹
--input imgs/*.jpg
# 网络摄像头
--webcame
# 视频文件
--video-input test.mp4
``–opts MODEL.WEIGHTS表示测试用的模型参数,可以是一个本地目录,也可以是一个
detectron2://`开头的一个模型路径,这时会先下载模型到本地再测试:
# 使用本地的模型参数
--opts MODEL.WEIGHTS ./model_final_f10217.pkl
# 使用网络模型地址
--opts MODEL.WEIGHTS ./model_final_f10217.pkl
模型的名字可以在 Model Zoo 查看。
训练代码参考 tools/train_net.py
,目前Detection看。