mask rcnn属于dnn么_安装maskrcnn-benchmark记录

标签:

文章来自芒果浩明,原文链接

安装maskrcnn_benchmark记录

毕设课题需要,打算是基于maskrcnn_benchmark框架做了,把安装过程记录下来,这个框架比较新,没什么参考,都是自己一遍一遍尝试安装过来的。

框架简介

Facebook AI Research 开源了 Faster R-CNN 和 Mask R-CNN 的 PyTorch 1.0实现基准:MaskRCNN-Benchmark。相比 Detectron 和 mmdetection,MaskRCNN-Benchmark 的性能相当,并拥有更快的训练速度和更低的 GPU 内存占用 。

项目地址https://github.com/facebookresearch/maskrcnn-benchmark

在项目主页可以看到项目的主要亮点

Highlights

PyTorch 1.0: RPN, Faster R-CNN and Mask R-CNN implementations that matches or exceeds Detectron accuracies

Very fast: up to 2x faster than Detectron and 30% faster than mmdetection during training. See MODEL_ZOO.md for more details.

Memory efficient: uses roughly 500MB less GPU memory than mmdetection during training

Multi-GPU training and inference

Batched inference: can perform inference using multiple images per batch per GPU

CPU support for inference: runs on CPU in inference time. See our webcam demo for an example

Provides pre-trained models for almost all reference Mask R-CNN and Faster R-CNN configurations with 1x schedule.

PyTorch 1.0:相当或者超越 Detectron 准确率的 RPN、Faster R-CNN、Mask R-CNN 实现;

非常快:训练速度是 Detectron 的两倍,是 mmdection 的 1.3 倍。

节省内存:在训练过程中使用的 GPU 内存比 mmdetection 少大约 500MB;

使用多 GPU 训练和推理;

批量化推理:可以在每 GPU 每批量上使用多张图像进行推理;

支持 CPU 推理:可以在推理时间内于 CPU 上运行。

提供几乎所有参考 Mask R-CNN 和 Faster R-CNN 配置的预训练模型,具有 1x 的 schedule。

失败尝试

其实项目文件已经给了一个step-by-step的安装教程,非常清楚。但是期间还是出现了不少的问题,唉习惯了,总有意想不到的意外嘛。回顾了一下碰到的坑和困难。有这么一下几点

cuda版本需要看清楚,比较严格苛刻

对于安装过不少次opencv的我来说早早有预防知道这个,但我没料到坑爹的是居然要同时考虑gcc版本。我的计划是先安装好cuda和cudNN这些,从而安装pytorch1.0最后安装框架。嗯这个顺序是没错的,但是坑爹的是在安装cuda和pytorch1.0的时候,我参考了一篇博文,从里面了解到cuda9.0需要的gcc是5.3的以下的,ubuntu16自带的是5.4,嗯那就跟着降级把,于是按照教程降级到4.8。好了pytorch1.0这一步做好了,我cuda版什么的都看准了,测试了一下,也正常使用。但是到安装maskrcnn__benmark框架时,看到GCC>=4.9,what!,瞬间傻眼,好了知道之前的白做了

使用anaconda包管理

这个非常重要,千万不要直接在系统原始环境下面安装,这样安装失败了有得你哭。新建一个安装环境随便怎么折腾,失败了删掉重来就ok

opencv安装失败

截止到我安装成功,我现在也搞不明白我为何安装失败的,我花费最多时间就是在安装opencv之上,虽然opencv是可选的,但是因为测试demo需要用到以及后续工作肯定要用,于是我还是要把opencv给装上。我先后尝试多个安装方式,都是莫名的安装失败,提示各种是与框架冲突、没有相关模块等。

最后成功的经验是顺寻是

创建新环境

安装一边opencv

conda install -c conda-forge opencv

maskrcnn_benchmark官方教程安装框架

此时测试发现opencv失效,无法import cv2

重新安装opencv

pip install opencv-python

成功的顺寻就是如此,至于原因说实话我也没搞懂。

框架所需环境

这里贴一下笔者安装成功的环境

ubuntu16.04LTS

GPU GTX950M

显卡驱动nvidia-390

anaconda

cuda 9.0

cuDNN v7

PyTorch 1.0 from a nightly release

torchvision from master

cocoapi

yacs

matplotlib

GCC 4.9

opencv3.4.2

GCC降级

cuda9.0仅支持5.3以及以下版本,maskrcnn_benchmark需要的gcc版本又GCC>=4.9,那么选择4.9的版本好了

下载安装gcc4.9

sudo apt-get install gcc-4.9

sudo apt-get install g++-4.9

接下来是配置

cd /usr/bin

sudo mv gcc gcc.bak#备份

sudo ln -s gcc-4.9 gcc#重新链接

sudo mv g++ g++.bak

sudo ln -s g++-4.9 g++

降级完成,可以查看以下是否降级成功

gcc -v && g++ -v

显示版本正确,则说明降级成功。

安装cuda

在此之前先安装一下驱动,

sudo apt install nvidia-390

安装完成后重启

nvidia-smi #检查安装是否成功

安装cuda

cuda9.0下载链接cuda9.0

如图,选择正确的版本

下方已经有官方安装教程

sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb

sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub#具体版本号不同可以使用tab键补全

sudo apt-get update

sudo apt-get install cuda

安装cuDNN

再次重申需要选择正确对应版本,选择的是cuDNN v7的版本

下载地址[cuDNN]

下载完成后,cd到文件文件目录,执行安装命令

sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb#具体版本可能不同,不用复制这个命令

安装anaconda

安装

从官网下载速度比较慢,这里推荐一下清华的开源镜像

从这里下载速度比较快,下载链接https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.0-Linux-x86_64.sh

cd到文件的目录,假如默认下载目录是download

cd download

bash Anaconda3-5.3.0-Linux-x86_64.sh#可使用tab补全

简单使用

查看已安装版本信息

conda --version

更新至最新版

conda update conda

查看帮助信息

conda --help

#or

conda -h

卸载

rm -rf ~/anaconda3

创建新环境

conda create --name

切换环境

source activate

推出环境

source deativate

显示已经创建的环境

conda info --envs

#or

conda info -e

#or

conda env list

删除环境

conda remove --name --all

安装包

conda install

查看当前环境已经安装的包

conda list

安装maskrcnn_benchmark

在cuda安装完成,gcc也成功降级之后,到这一步直接按照maskrcnn_benchmark的官方教程按照就可以了,只不过要安装opencv,就稍加修改。中文部分就是添加的部分

# first, make sure that your conda is setup properly with the right environment

# for that, check that `which conda`, `which pip` and `which python` points to the

# right path. From a clean conda env, this is what you need to do

conda create --name maskrcnn_benchmark

source activate maskrcnn_benchmark

#安装opencv

conda install -c conda-forge opencv

#安装opencv

# this installs the right pip and dependencies for the fresh python

conda install ipython

# maskrnn_benchmark and coco api dependencies

pip install ninja yacs cython matplotlib

# follow PyTorch installation in https://pytorch.org/get-started/locally/

# we give the instructions for CUDA 9.0

conda install pytorch-nightly -c pytorch

# install torchvision

cd ~/github

git clone https://github.com/pytorch/vision.git

cd vision

python setup.py install

# install pycocotools

cd ~/github

git clone https://github.com/cocodataset/cocoapi.git

cd cocoapi/PythonAPI

python setup.py build_ext install

# install PyTorch Detection

cd ~/github

git clone https://github.com/facebookresearch/maskrcnn-benchmark.git

cd maskrcnn-benchmark

# the following will install the lib with

# symbolic links, so that you can modify

# the files if you want and won't need to

# re-build it

python setup.py build develop

# or if you are on macOS

# MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py build develop

#到这里发现opencv失效,但是conda list查看包还在,无奈只能重新安装,但命令变化了

pip install opencv-python

最后附上demo的测试使用

cd demo

# by default, it runs on the GPU

# for best results, use min-image-size 800

python webcam.py --min-image-size 800

# can also run it on the CPU

python webcam.py --min-image-size 300 MODEL.DEVICE cpu

# or change the model that you want to use

python webcam.py --config-file ../configs/caffe2/e2e_mask_rcnn_R_101_FPN_1x_caffe2.yaml --min-image-size 300 MODEL.DEVICE cpu

# in order to see the probability heatmaps, pass --show-mask-heatmaps

python webcam.py --min-image-size 300 --show-mask-heatmaps MODEL.DEVICE cpu

自己编写推理应用代码参考

from maskrcnn_benchmark.config import cfg

from predictor import COCODemo

config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"

# update the config options with the config file

cfg.merge_from_file(config_file)

# manual override some options

cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

coco_demo = COCODemo(

cfg,

min_image_size=800,

confidence_threshold=0.7,

)

# load image and then run prediction

image = ...

predictions = coco_demo.run_on_opencv_image(image)

使用COCO数据集训练

# symlink the coco dataset

cd ~/github/maskrcnn-benchmark

mkdir -p datasets/coco

ln -s /path_to_coco_dataset/annotations datasets/coco/annotations

ln -s /path_to_coco_dataset/train2014 datasets/coco/train2014

ln -s /path_to_coco_dataset/test2014 datasets/coco/test2014

ln -s /path_to_coco_dataset/val2014 datasets/coco/val2014

添加自己的数据集

from maskrcnn_benchmark.structures.bounding_box import BoxList

class MyDataset(object):

def __init__(self, ...):

# as you would do normally

def __getitem__(self, idx):

# load the image as a PIL Image

image = ...

# load the bounding boxes as a list of list of boxes

# in this case, for illustrative purposes, we use

# x1, y1, x2, y2 order.

boxes = [[0, 0, 10, 10], [10, 20, 50, 50]]

# and labels

labels = torch.tensor([10, 20])

# create a BoxList from the boxes

boxlist = BoxList(boxes, image.size, mode="xyxy")

# add the labels to the boxlist

boxlist.add_field("labels", labels)

if self.transforms:

image, boxlist = self.transforms(image, boxlist)

# return the image, the boxlist and the idx in your dataset

return image, boxlist, idx

def get_img_info(self, idx):

# get img_height and img_width. This is used if

# we want to split the batches according to the aspect ratio

# of the image, as it can be more efficient than loading the

# image from disk

return {"height": img_height, "width": img_width}

标签:

来源: https://blog.csdn.net/mango_haoming/article/details/84257401

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值