Win10下快速复现Mask_RCNN避坑指南

硬件环境:笔记本电脑win10系统、1050Ti显卡

软件环境:Anaconda、Pycharm

项目地址:https://github.com/matterport/Mask_RCNN

一.环境搭建

1. Anaconda创建一个虚拟环境

conda create -n maskrcnn python=3.6

2. 安装必要依赖包(从github上down下来的项目内有requirements,保险起见还是一个个安装,强调按我这个顺序来

首先配置最重要的keras和tensorflow,注意版本号

# 有显卡使用gpu版,没显卡就不要-gpu
# 这个版本配套的是CUDA9.0和cudnn7.6.4
conda install tensorflow-gpu==1.11.0

# 避免多余错误使用keras2.0.8
conda install keras-gpu==2.0.8

装好tensorflow和keras后numpy、scipy之类的包已经顺带装好了,接下来就是查漏补缺,根据requiremens补充

conda install pillow

conda install cython

conda install matplotlib

#这个推荐pip安装 conda装的容易出问题阉割版opencv
pip install opencv-python

conda install ipython

有一个重点是imgaug这个包,requirements里面没有说安装shapely,不装这个是无法安装imgaug的

conda install shapely

pip install imgaug 

如果imgaug有问题的话可以参见https://blog.csdn.net/hesongzefairy/article/details/104693782

最后,安装pycocotools工具,windows和linux下安装方法不同,windows下安装必须要有visual c++2015这个东西,没有的话可以参考https://github.com/philferriere/cocoapi来安装vc++2015,必须是在线安装,有这个之后就能安装pycocotool了

pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

这东西的路径在D:\Anaconda\envs\mask\Lib\site-packages\pycocotools(后面会用到!)

扫描二维码关注公众号,回复: 10878554 查看本文章

至此,环境搭建完毕!

二. 权重文件下载

官方给出的下载地址:https://github.com/matterport/Mask_RCNN/releases

样例有一个做气球和coco数据集的,看名称就可以简单分辨,下载对应的就行

下载好的mask_rcnn_coco.h5文件放在项目下的logs文件夹内

三. 运行demo

samples目录中demo.ipynb文件是运行在jupyter notebook上的,用Anaconda自带的jupyter notebook运行时注意要选择对应的虚拟环境来运行,可参考https://blog.csdn.net/hesongzefairy/article/details/104700167

这里我用Pycharm来复现,所以把demo文件直接转为py文件,小改动,大家可直接复制下面代码使用

import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import cv2
import time
# Root directory of the project
ROOT_DIR = os.path.abspath("../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
import coco


# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(MODEL_DIR ,"mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()
config.display()


# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
               'bus', 'train', 'truck', 'boat', 'traffic light',
               'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
               'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
               'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
               'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
               'kite', 'baseball bat', 'baseball glove', 'skateboard',
               'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
               'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
               'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
               'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
               'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
               'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
               'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
               'teddy bear', 'hair drier', 'toothbrush']

# Load a random image from the images folder

file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))

# Run detection
results = model.detect([image], verbose=1)

# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                        class_names, r['scores'])

注意,代码运行之前,还需要修改一个地方不然会报错:

UserWarning: Matplotlib is currently using agg,which is a non-GUI backend, so cannot show the figure

解决这个问题就需要用到之前说的pycocotool工具的路径,找到pycocotool文件夹修改其中的coco.py文件

matplotlib.use('Agg')改为matplotlib.use('TkAgg')即可

测试的图片是项目文件images中的图片,也可以换成自己的图片来测试,结果如下:

 

发布了80 篇原创文章 · 获赞 184 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/hesongzefairy/article/details/104702119
今日推荐