MASKRCNN(之三)demo.

来源:互联网 发布:华润网络校园招聘 编辑:程序博客网 时间:2024/06/06 00:42

一 使用TF

import osimport sysimport randomimport mathimport numpy as npimport scipy.miscimport matplotlibimport matplotlib.pyplot as pltimport 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 projectROOT_DIR = os.getcwd()# Directory to save logs and trained modelMODEL_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 onIMAGE_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 = 1config = 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-COCOmodel.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 folderfile_names = next(os.walk(IMAGE_DIR))[2]image = scipy.misc.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))# Run detectionresults = model.detect([image], verbose=1)# Visualize resultsr = results[0]visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],                             class_names, r['scores'])
结果如下图:


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

原创粉丝点击