mask rcnn实现教程「建议收藏」

mask rcnn实现教程「建议收藏」一,首先去github上下载mask-rcnn源码,这里提供一个百度网盘地址链接:https://pan.baidu.com/s/1htJYyNy密码:0r2b含可运行DEMO更新链接:https://pan.baidu.com/s/1HBPtIPz0xpZsCprl7rc6hw密码:bvm5二,下载对应的mask_rcnn_coco.h5模型,这里给出百度网盘下载地址链接:h……

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

一,首先去github上下载mask-rcnn源码,这里提供一个百度网盘地址

链接:https://pan.baidu.com/s/1htJYyNy 密码:0r2b

含可运行DEMO更新链接:百度网盘 请输入提取码 密码:bvm5

二,下载对应的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,

最终目录如下:

mask rcnn实现教程「建议收藏」

在home目录存放预训练模型mask_rcnn_coco.h5

此时我们可以回到Mask_RCNN-master目录下,运行命令

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

然后我们会看到如下界面

mask rcnn实现教程「建议收藏」

mask rcnn实现教程「建议收藏」

Logs处就是我们保存训练后的模型所在目录

到此,我们成功开始训练coco数据集

六,分析coco数据集

1,为了更好地分析coco数据集,这里我们准备一个工具labelme,这是一个打标的工具

安装方法如下:

pip3 install labelme

安装完成之后打开

labelme

mask rcnn实现教程「建议收藏」

mask rcnn实现教程「建议收藏」

第二幅图就是我们自己给图片打标注后,我们进行保存会生成一个json文件,打开生成的json文件我们可以看到标注的所有点的x,y坐标

mask rcnn实现教程「建议收藏」                                      mask rcnn实现教程「建议收藏」

这个工具可以用来标注我们自己的数据集,然后进行训练。

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")

mask rcnn实现教程「建议收藏」

mask rcnn实现教程「建议收藏」

mask rcnn实现教程「建议收藏」

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文件中的位置坐标进行替换,我们得到如下图片:

mask rcnn实现教程「建议收藏」

最新源码和相关json文件的下载地址见下:

GitHub – wjd1994/mask-rcnn

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185883.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 视频要不要开hdr_hdr在什么情况下使用

    视频要不要开hdr_hdr在什么情况下使用最近两年HDR这个概念可谓是铺天盖地而来,手机也好PC也好电视也好,都拼命往自己头上扣HDR的帽子。而在某些发烧友眼中,如果看片子不带HDR,堪比步兵变骑兵,一下子变得索然无味。然而,新事物往往也伴随着众多新坑,特别是在软硬件环境复杂的PC平台,稍有不慎就会摔得脸青鼻肿,播HDR的效果甚至不如播普通的片子。PC播HDR的大坑有几何?PC并不是专门为视频播放设计的机器,和专业的蓝光机等播放器相比…

    2025年11月14日
    3
  • 本地数据库同步到云主机上

    本地数据库同步到云主机上同步前的准备:首先你本地跟云主机上都要有数据库、可视化的辅助工具(我用的NavicatPremium,其他的也都一个道理),这里靠的就是这个NavicatPremium工具1.首先在云主机上创建一个链接,建一个数据库,最好是与本地数据库同名2.在本地新建一个连接,可以点击下边的链接测试,测试一下看看是否能连接成功3.找到工具栏里的:工具->数据传输4.经过上述三步你最起码有了两个连接

    2022年5月25日
    41
  • vue2 renrne 引入tinymce[通俗易懂]

    vue2 renrne 引入tinymce[通俗易懂]tinymce1.引入和原始使用下载地址:https://www.tiny.cloud/get-tiny/self-hosted/语言包地址:https://www.tiny.clou

    2022年7月2日
    80
  • JDK卸载删除

    JDK卸载删除Java卸载1.进入环境变量,点击Java_Home2.进入路径,删除JDK清理环境变量删除path下关于Java的环境变量查看是否清除cdm运行输入java-version

    2022年6月20日
    35
  • WEBZIP为什么打不开网页

    WEBZIP为什么打不开网页

    2021年9月21日
    47
  • hexdump -C_linux生成dump

    hexdump -C_linux生成dump描述:hexdump命令一般用来查看”二进制”文件的十六进制编码,从手册上查看,其查看的内容还要很多,诸如:ascii,decimal,hexadecimal,octal参数:hexdump[-bcCdovx][-eformat_string][-fformat_file][-nlength][-sskip]file示例:新增一个文本文件,在test文本中添加如下内容:…

    2026年2月6日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号