MASKRCNN(之三)demo.

一 使用TF

import os
import sys
import random
import math
import numpy as np
import scipy.misc
import matplotlib
import matplotlib.pyplot as plt

import coco                      #coco.py,里面是mask rcnn中MSCOCO的配和数据加载
import utils                    #util.py,里面是mask rcnn的通用函数和类实现
import model as modellib     #model.py,里面是mask rcnn模型的实现
import visualize               #visualize.py,里面封装了matplotlib IPython.display ,用来显示图像画图

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

# Path to trained weights file
# Download this file and place in the root of your 
# project (See README file for details)
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

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


MODEL_DIR:  保存log,和训练的模型

IMAGE_DIR: 图片存放路径 images/

COCO_MODEL_PATH:mask_rcnn_coco.h5  download下来的coco model

二 配置

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.print()

配置信息读取:从coco.py里的class CocoConfig(Config)读取,Config又是从config.py里读取的,然后更新了GPU_COUNT IMAGES_PER_GPU的配置

GPU的数量*每GPU处理的图片数

输出结果是最终的配置信息

三 创建Model 加载weights

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) #Model.py的MaskRCNN类实现

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

四 加载coco name

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 = scipy.misc.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'])
结果如下图:


Model.py和coco.py 下一节继续解析

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
要配置maskrcnn的tensorflow2环境,可以按照以下步骤进行操作: 1. 首先,确保您已经安装了Python3,并且已经安装了tensorflow2的GPU版本,可以使用以下命令进行安装: ``` pip install tensorflow-gpu==2.0 ``` 2. 接下来,您需要下载并安装maskrcnn_tf2的源代码,可以在GitHub上找到该项目。 3. 一旦您下载了源代码,您需要将mrcnn文件夹以及tensorflow1.x版本中的maskrcnn文件夹替换为maskrcnn_tf2的相应文件。请注意,在替换过程中可能会出现一些错误,您可以查看相关文章了解更多细节。 4. 配置完成后,您可以开始使用maskrcnn进行图像分割。如果您希望使用labelme进行数据标注,可以打开一张图片并使用labelme进行标注。 请记住,以上步骤是基于tensorflow2版本的maskrcnn环境配置。如果您使用的是tensorflow1.x版本或没有GPU,您可以根据引用和引用中提到的步骤进行配置。 希望对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [深度学习Mask_Rcnn——tensorflow环境配置](https://blog.csdn.net/weixin_67859995/article/details/127121339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Mask RCNN tensorflow 训练自己的数据运行demo实例【从标注数据到最终训练和测试】超全教程](https://blog.csdn.net/alicema1111/article/details/107403957)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值