写下前面:前面我们对mask rcnn进行了解释,其中很多参考和引用了其他朋友的经验和资源,并对开源代码进行了实例测试,这里再给出mask rcnn项目开源地址:https://github.com/matterport/Mask_RCNN.后面我会就如何训练自己的数据集进行简要的总结,并将自己对训练和测试部分的代码注释和理解分享给大家,包括训练数据。
训练自己的数据集要用到数据标注工具:这里建议基于开源项目图片标记工具:https://github.com/wkentaro/labelme ,
这里有关数据标记工具labelme的使用可以参考这篇博客:https://blog.csdn.net/shwan_ma/article/details/77823281
利用标注工具准备好数据集:
将标注好的数据分别放在四个文件夹下:
《一》:cv2_mask:
《二》:json:
《三》:labelme_json\1_json
《四》:pic:
====》ok --- 训练数据准备完成:
下面新建train_selfdata.py脚本:添加如下代码:
# -*- coding: utf-8 -*-
import os
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import tensorflow as tf
from mrcnn.config import Config
# import utils
from mrcnn import model as modellib, utils
from mrcnn import visualize
import yaml
from mrcnn.model import log
from PIL import Image
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# Root directory of the project
ROOT_DIR = os.getcwd()
# ROOT_DIR = os.path.abspath("../")
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
iter_num = 0
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
class ShapesConfig(Config):
"""Configuration for training on the toy shapes dataset.
Derives from the base Config class and overrides values specific
to the toy shapes dataset.
"""
# Give the configuration a recognizable name
NAME = "shapes"
# Train on 1 GPU and 8 images per GPU. We can put multiple images on each
# GPU because the images are sm