1. 程式人生 > >mask rcnn實現教程

mask rcnn實現教程

一,首先去github上下載mask-rcnn原始碼,這裡提供一個百度網盤地址

連結:https://pan.baidu.com/s/1htJYyNy 密碼:0r2b

二,下載對應的mask_rcnn_coco.h5模型,這裡給出百度網盤下載地址

連結:https://pan.baidu.com/s/1drKvfg 密碼:yer9

三,執行如下程式碼,根據提示安裝相應的庫

import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt

import coco
import utils
import model as modellib
import visualize

對於pycocotools庫安裝方法如下

git clone https://github.com/pdollar/coco

cd coco/PythonAPI

將makefile中的python 改為python3

然後先執行安裝python3-dev

然後命令列輸入

make -j8

然後將pycocotools資料夾複製到mask-rcnn下

最 後再

sudo pip3 install h5py

四,當編譯器不再報錯時執行如下程式

import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt

import coco
import utils
import model as modellib
import visualize

#%matplotlib inline 

# Root directory of the project
ROOT_DIR = os.getcwd()

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

# Local path to trained weights file
COCO_MODEL_PATH =  "mask_rcnn_coco.h5"


# 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'])
print('OK')

至此mask-rcnn完成

五,用mask-rcnn訓練自己資料

這裡提供一個最新原始碼(沒積分的留言聯絡我,我發給你的郵箱)

這裡我們主要用到原始碼提供的coco.py

首先我們去如下兩個網址下載coco資料集

http://images.cocodataset.org/zips/train2014.zip

http://images.cocodataset.org/zips/val2014.zip

接著我們下載對應的json檔案

https://dl.dropboxusercontent.com/s/o43o90bna78omob/instances_minival2014.json.zip?dl=0

https://dl.dropboxusercontent.com/s/s3tw5zcg7395368/instances_valminusminival2014.json.zip?dl=0

可能上面連結失效,這裡提供instances_minival2014.json和instances_valminusminival2014.json一個csdn的下載地址json下載

instances_train2014.json和instances_val2014.json百度雲下載連結為:

連結:https://pan.baidu.com/s/1qHoeAOULbsAFiPnBr4a8JA 密碼:fk62

將上面下載的原始碼解壓,將sample中coco/coco.py複製到Mask_RCNN-master 根目錄下,新建一個資料夾coco用來存放我們上面下載的資料圖片及json檔案

進入coco資料夾中解壓train2014.zip和val2014.zip 到當前目錄下

解壓上面的包含json檔案的zip,這裡我們只需要

instances_minival2014.json  instances_train2014.json  instances_val2014.json  instances_valminusminival2014.json

這四個json ,在coco目錄下新建一個資料夾annotations用來存放上面的四個json,

最終目錄如下:

在home目錄存放預訓練模型mask_rcnn_coco.h5

此時我們可以回到Mask_RCNN-master目錄下,執行命令

python3 coco.py train --dataset=coco/ --model=coco

然後我們會看到如下介面

Logs處就是我們儲存訓練後的模型所在目錄

到此,我們成功開始訓練coco資料集

六,分析coco資料集

1,為了更好地分析coco資料集,這裡我們準備一個工具labelme,這是一個打標的工具

安裝方法如下:

pip3 install labelme

安裝完成之後開啟

labelme

第二幅圖就是我們自己給圖片打標註後,我們進行儲存會生成一個json檔案,開啟生成的json檔案我們可以看到標註的所有點的x,y座標

                                      

這個工具可以用來標註我們自己的資料集,然後進行訓練。

2,獲取coco標註檔案內容

coco標註檔案比較大,一個json有500M多,我們用普通的記事本是打不開的,這裡我們要用到coco官網提供的一個python API包,該api是抽象的,封裝了各裝函式用來獲取json的資料,我們分析後發現該json相當於一個字典檔案,鍵值對形式呈現。

# The following API functions are defined:
#  COCO       - COCO api class that loads COCO annotation file and prepare data structures.
#  decodeMask - Decode binary mask M encoded via run-length encoding.
#  encodeMask - Encode binary mask M using run-length encoding.
#  getAnnIds  - Get ann ids that satisfy given filter conditions.
#  getCatIds  - Get cat ids that satisfy given filter conditions.
#  getImgIds  - Get img ids that satisfy given filter conditions.
#  loadAnns   - Load anns with the specified ids.
#  loadCats   - Load cats with the specified ids.
#  loadImgs   - Load imgs with the specified ids.
#  annToMask  - Convert segmentation in an annotation to binary mask.
#  showAnns   - Display the specified annotations.
#  loadRes    - Load algorithm results and create API for accessing them.
#  download   - Download COCO images from mscoco.org server.
# Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
# Help on each functions can be accessed by: "help COCO>function".

# See also COCO>decodeMask,
# COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
# COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
# COCO>loadImgs, COCO>annToMask, COCO>showAnns

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from pycocotools import mask as maskUtils
coco=COCO("pycocotools/instances_train2014.json")

3,分析coco中的segemention

我們提取其中一幅圖片的segemention用如下程式碼將其按labelme要求的json格式寫入test.txt檔案中

l = [345.28, 220.68, 348.17, 269.8, 355.4, 307.36, 377.07, 318.92, 395.85, 370.93, 444.97, 565.96, 473.86, 616.52, 478.19, 628.08, 431.96, 628.08, 401.63, 581.85, 377.07, 477.83, 375.62, 529.84, 387.18, 600.63, 397.29, 628.08, 325.06, 623.75, 216.7, 622.3, 216.7, 606.41, 251.38, 529.84, 223.93, 529.84, 209.48, 528.4, 202.26, 505.28, 193.59, 485.06, 167.58, 375.26, 179.14, 334.81, 203.7, 324.7, 229.71, 313.14, 209.48, 278.47, 193.59, 248.13, 208.04, 188.89, 223.93, 175.89, 236.93, 168.67, 258.6, 162.89, 294.72, 168.67, 310.61, 174.45, 326.5, 197.56]
l0 = []
l1 = []
l3 = []
l4 = []
for i in range(len(l)):
	if i%2==0:
		l0.append(l[i])
	else:
		l1.append(l[i])
for i in range(len(l)):
	if i%2==0:
		l3.append(l[i])
	else:
		l3.append(l[i])
		l4.append(l3)
		l3 = []
print(l0)
print(l1)
print(l4)
f = open("test.txt","w")
for e in l4:
	f.write('\n        [\n          ')
	f.write(str(e[0]))
	f.write(',\n          ')
	f.write(str(e[1]))
	f.write('\n        ],')
f.close()
a = input()

然後我們將之前labelme儲存的json檔案中的位置座標進行替換,我們得到如下圖片: