项目网址:maskrcnn benchmark
一、环境搭建
1.准备工作
我的基础环境:
系统:windows 10 ,Ubutun 18.01
Python环境:Anaconda3
conda: 4.5.4
pip : 20.2.2
Python :3.7.0
GPU :
NVIDIA GeForce RTX 2060 6G (windows)
Telsa v100 16G *8 (Ubutun)
pytorch :1.2
cuda:10.0
首先进入github网址,点击INSTALL.md
windows环境下和linux有所不同 ,严格安装他给的文档安装 不然后续会报错,这里以windows为例
#首先创建一个conda的虚拟环境并进入
open a cmd and change to desired installation directory
from now on will be refered as INSTALL_DIR
conda create --name maskrcnn_benchmark
conda activate maskrcnn_benchmark
#按照他给出的顺序安装
# this installs the right pip and dependencies for the fresh python
conda install ipython
# maskrcnn_benchmark and coco api dependencies
pip install ninja yacs cython matplotlib tqdm opencv-python
# follow PyTorch installation in https://pytorch.org/get-started/locally/
# we give the instructions for CUDA 9.0
## Important : check the cuda version installed on your computer by running the command in the cmd :
nvcc -- version
#这里他安装的cuda 9.0 实测20系列的显卡可能会与cuda9.0不匹配,我在安装9.0之后报错了,无法执行cudnn, 然后卸载重新装了10.0就解决了问题
conda install -c pytorch pytorch-nightly torchvision cudatoolkit=9.0
git clone https://github.com/cocodataset/cocoapi.git
#To prevent installation error do the following after commiting cocooapi :
#using file explorer naviagate to cocoapi\PythonAPI\setup.py and change line 14 from:
#extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'],
#to
#extra_compile_args={'gcc': ['/Qstd=c99']},
#Based on https://github.com/cocodataset/cocoapi/issues/51
cd cocoapi/PythonAPI
python setup.py build_ext install
# navigate back to INSTALL_DIR
cd ..
cd ..
# install apex
git clone https://github.com/NVIDIA/apex.git
cd apex
python setup.py install --cuda_ext --cpp_ext
# navigate back to INSTALL_DIR
cd ..
# install PyTorch Detection
git clone https://github.com/Idolized22/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
出现的问题
在 linux 下 执行 git clone https://github.com/NVIDIA/apex.git python setup.py install --cuda_ext --cpp_ext 出现 nvidia apex build error 以及 error: command 'gcc' failed with exit status 1解决方案
git checkout f3a960f80244cf9e80558ab30f7f7e8cbf03c0a0
二、数据集准备
1.labelme2coco
maskrcnn benchmark 需要的数据集格式为coco数据集格式。我是通过labelme来标注数据,然后在转换为coco格式。网上有很多转换的代码,很多都有问题。在linux下测试时出现如下问题:
AssertionError: tensor([398.9765, 403.2801, 415.9314, 402.9380])
解决方法:
import argparse
import json
import matplotlib.pyplot as plt
import skimage.io as io
import cv2
from labelme import utils
import numpy as np
import glob
import PIL.Image
REQUIRE_MASK = False
class labelme2coco(object):
def __init__(self,labelme_json=[],save_json_path='./new.json'):
'''
:param labelme_json: the list of all labelme json file paths
:param save_json_path: the path to save new json
'''
self.labelme_json=labelme_json
self.save_json_path=save_json_path
self.images=[]
self.categories=[]
self.annotations=[]
# self.data_coco = {}
self.label=[]
self.annID=1
self.height=0
self.width=0
self.require_mask = REQUIRE_MASK
self.save_json()
def data_transfer(self):
for num,json_file in enumerate(self.labelme_json):
if not json_file == self.save_json_path:
with open(json_file,'r') as fp:
data = json.load(fp)
self.images.append(self.image(data,num))
for shapes in data['shapes']:
print("label is ")
print(shapes['label'])
label=shapes['label']
# if label[1] not in self.label:
if label not in self.label:
print("find new category: ")
self.categories.append(self.categorie(label))
print(self.categories)
# self.label.append(label[1])
self.label.append(label)
points=shapes['points']
self.annotations.append(self.annotation(points,label,num))
self.annID+=1
def image(self,data,num):
image={
}
img = utils.img_b64_to_arr(data['imageData'])
# img=io.imread(data['imagePath'])
# img = cv2.imread(data['imagePath'], 0)
height, width = img.shape[:2]
img = None
image['height']=height
image['width'] = width
image['id']=num+1
image['file_name'] = data['imagePath'].split('/')[-1]
self.height=height
self.width=width
return image
def categorie(self,label):
categorie={
}
categorie['supercategory'] = label
# categorie['supercategory'] = label
categorie['id']=len(self.label)+1
categorie['name'] = label
# categorie['name'] = label[1]
return categorie
def annotation(self,points,label,num):
annotation={
}
print(points)
x1 = points[0][0]
y1 = points[0][1]
x2 = points[1][0]
y2 = points[1][1]
contour = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]]) #points = [[x1, y1], [x2, y2]] for rectangle
contour = contour.astype(int)
area = cv2.contourArea(contour)
print("contour is ", contour, " area = ", area)
annotation['segmentation']= [list(np.asarray([[x1, y1], [x2, y1], [x2, y2],