当前位置:   article > 正文

训练自己的Mask-RCNN实例分割模型_训练分割模型

训练分割模型

首先,要进行本文试验,需具备GPU,CPU上只能看看效果,没法进行实测

图像深度学习技术的四大方向

图像深度学习算法主要而已分为4大类:1)图像识别,实现图像中单一目标的类别识别;2)目标检测:实现图像中多个目标,且目标间可能存在粘连和堆叠的情况,对图片中的所有目标范围进行定位,同时对获得的目标进行识别;3)语义分割:pixel-wise的图像识别模型,输入图像和输出标签图逐像素对应,获得原图中所有像素的类别,可以根据识别结果获得目标的轮廓,形状和类别;4)实例分割,将目标检测和语义分割进行组合,简单来说,就是先进行目标检测获得各类别的区域,在对对应区域进行语义分割。相对于语义分割的优势在于:可以对同类目标的堆叠进行分割和识别。作者将详细记录如何进行该四类模型在自己所用数据集上的训练和测试。识别,目标检测和语义分割网上已经有一些可行的教程,后续会整理一下与大家分享,本文主要记录如何训练自己的实例分割模型。

1.Mask-RCNN模型测试

https://github.com/matterport/Mask_RCNN

PS:原文中有教程,但是实测中发现,对于自己的数据还是比较不友好的,需要一定的修改,英语水平还行,又比较有耐心的可以自行尝试从该文章中进行自己的数据训练测试。

下载后的模型结构大概是这样的,有能力的尽量阅读一下README.md,里面讲了模型的一些工作原理,也介绍了该模型的用法

1)安装requiremets.txt中的要求进行需求包的安装,直接运行setup.py,可能会有一些毛病。不行的话就自己一个个装把,不费事。需求包如下,需要注意的是imgaug安装可能会失败,没找到好的解决办法,反正就一直装,运气好两三遍就可以装好了(装过两台电脑,一台2次就装好了,一台装了大概半个小时,忘了几次了),可以参考https://github.com/aleju/imgaug (建议pip和conda都试试).PS:除了文件中要求的需求包外,还需要安装pycocotools

2)先跑跑已有的模型,看看效果

在release的Mask-rcnn2.0版本中,下载预训练的coco模型权重(mask_rcnn_coco.h5)https://github.com/matterport/Mask_RCNN/releases

将mask_rcnn_coco.h5放到工程的根目录下,运行samples文件夹红的demo.ipynb,获得测试结果图如下,是不是很期待在自己的数据上跑是什么效果了

2.准备自己的数据

使用VIA进行标注,不需要下载软件,直接在网页上运行http://www.robots.ox.ac.uk/~vgg/software/via/via.html

1.在Region Attributes 中新建一个类别信息,根据自己的需要进行设置

2.加载图片,进行标注,本文仅演示一下流程,就标注了3张图片

全部图片标注完成后,输出成.json格式

3.在根目录中简历一个datasets文件夹,结构如下图所示,将数据和标签json文件放到同一个文间架中(懒得标注,val中的数据是train中复制黏贴过去的)

3.训练自己的数据

1)在samples文件夹下新建如下几个文件(README.md可以不要,3个文件代码贴在下面,建议详细看看注释,根据自己的数据需要修改的部分也都注释出来了)

cdw.py(有几个参数需要根据数据进行修改,在代码中用中文注释出来了,详细看下英文注释有利于代码的理解)

  1. """
  2. Mask R-CNN
  3. Train on your dataset (CDW is my research Construction and Demolition Waste).
  4. Revised according to the work:
  5. Copyright (c) 2018 Matterport, Inc.
  6. Licensed under the MIT License (see LICENSE for details)
  7. Written by Waleed Abdulla
  8. ------------------------------------------------------------
  9. Usage: import the module (see Jupyter notebooks for examples), or run from
  10. the command line as such:
  11. # Train a new model starting from pre-trained COCO weights
  12. python3 cdw.py train --dataset=/path/to/cdw/dataset --weights=coco
  13. # Resume training a model that you had trained earlier
  14. python3 cdw.py train --dataset=/path/to/cdw/dataset --weights=last
  15. # Train a new model starting from ImageNet weights
  16. python3 cdw.py train --dataset=/path/to/cdw/dataset --weights=imagenet
  17. """
  18. import os
  19. import sys
  20. import json
  21. import datetime
  22. import numpy as np
  23. import skimage.draw
  24. # Root directory of the project
  25. ROOT_DIR = os.path.abspath("../../")
  26. # Import Mask RCNN
  27. sys.path.append(ROOT_DIR) # To find local version of the library
  28. from mrcnn.config import Config
  29. from mrcnn import model as modellib, utils
  30. # Path to trained weights file
  31. COCO_WEIGHTS_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
  32. # Directory to save logs and model checkpoints, if not provided
  33. # through the command line argument --logs
  34. DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")
  35. ############################################################
  36. # Configurations
  37. ############################################################
  38. class CDWConfig(Config):
  39. """Configuration for training on the toy dataset.
  40. Derives from the base Config class and overrides some values.
  41. """
  42. # Give the configuration a recognizable name
  43. NAME = "cdw" #建议将名字改成自己数据集的简称,注意上下信息要对应
  44. # We use a GPU with 12GB memory, which can fit two images.
  45. # Adjust down if you use a smaller GPU.
  46. IMAGES_PER_GPU = 2
  47. # Number of classes (including background)
  48. NUM_CLASSES = 1 + 4 # 模型识别的类别数=Background + wood, brick, concrete and rubber
  49. # Number of training steps per epoch
  50. STEPS_PER_EPOCH = 100
  51. # Skip detections with < 50% confidence
  52. DETECTION_MIN_CONFIDENCE = 0.5
  53. ############################################################
  54. # Dataset
  55. ############################################################
  56. class CDWDataset(utils.Dataset):
  57. def load_cdw(self, dataset_dir, subset):
  58. """Load a subset of the CDW dataset.
  59. dataset_dir: Root directory of the dataset.
  60. subset: Subset to load: train or val
  61. """
  62. # Add classes. We have only one class to add.
  63. #添加类别名称,这里的“cdw”要和上面设的名字一样
  64. self.add_class("cdw", 1, "wood")
  65. self.add_class("cdw", 2, "brick")
  66. self.add_class("cdw", 3, "concrete")
  67. self.add_class("cdw", 4, "rubber")
  68. # Train or validation dataset?
  69. assert subset in ["train", "val"]
  70. dataset_dir = os.path.join(dataset_dir, subset)
  71. # Load annotations
  72. # VGG Image Annotator (up to version 1.6) saves each image in the form:
  73. # { 'filename': '28503151_5b5b7ec140_b.jpg',
  74. # 'regions': {
  75. # '0': {
  76. # 'region_attributes': {'class':class_name},
  77. # 'shape_attributes': {
  78. # 'all_points_x': [...],
  79. # 'all_points_y': [...],
  80. # 'name': 'polygon'}},
  81. # ... more regions ...
  82. # },
  83. # 'size': 100202
  84. # }
  85. # We mostly care about the x and y coordinates of each region
  86. # Note: In VIA 2.0, regions was changed from a dict to a list.
  87. annotations = json.load(open(os.path.join(dataset_dir, "via_export_json.json")))
  88. annotations = list(annotations.values()) # don't need the dict keys
  89. # The VIA tool saves images in the JSON even if they don't have any
  90. # annotations. Skip unannotated images.
  91. annotations = [a for a in annotations if a['regions']]
  92. # Add images
  93. for a in annotations:
  94. # Get the x, y coordinaets of points of the polygons that make up
  95. # the outline of each object instance. These are stores in the
  96. # shape_attributes (see json format above)
  97. # The if condition is needed to support VIA versions 1.x and 2.x.
  98. if type(a['regions']) is dict:
  99. polygons = [r['shape_attributes'] for r in a['regions'].values()]
  100. else:
  101. polygons = [r['shape_attributes'] for r in a['regions']]
  102. # load_mask() needs the image size to convert polygons to masks.
  103. # Unfortunately, VIA doesn't include it in JSON, so we must read
  104. # the image. This is only managable since the dataset is tiny.
  105. image_path = os.path.join(dataset_dir, a['filename'])
  106. image = skimage.io.imread(image_path)
  107. height, width = image.shape[:2]
  108. region_attribute=[r['region_attributes'] for r in a['regions']]
  109. #print(region_attribute)
  110. class_name = [a['class'] for a in region_attribute if a is not None]
  111. #print(class_name)
  112. self.add_image(
  113. "cdw",
  114. image_id=a['filename'], # use file name as a unique image id
  115. path=image_path,
  116. width=width, height=height,
  117. polygons=polygons,
  118. cdw=class_name)
  119. def load_mask(self, image_id):
  120. """Generate instance masks for an image.
  121. Returns:
  122. masks: A bool array of shape [height, width, instance count] with
  123. one mask per instance.
  124. class_ids: a 1D array of class IDs of the instance masks.
  125. """
  126. # If not a cdw dataset image, delegate to parent class.
  127. image_info = self.image_info[image_id]
  128. if image_info["source"] != "cdw":
  129. return super(self.__class__, self).load_mask(image_id)
  130. # Convert polygons to a bitmap mask of shape
  131. # [height, width, instance_count]
  132. info = self.image_info[image_id]
  133. #print('image info',info)
  134. cdws = info['cdw']
  135. #print('cdws',cdws)
  136. mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
  137. dtype=np.uint8)
  138. for i, p in enumerate(info["polygons"]):
  139. # Get indexes of pixels inside the polygon and set them to 1
  140. rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
  141. mask[rr, cc, i] = 1
  142. # Return mask, and array of class IDs of each instance.
  143. class_ids = np.array([self.class_names.index(s) for s in cdws])
  144. return mask,class_ids.astype(np.int32)
  145. def image_reference(self, image_id):
  146. """Return the path of the image."""
  147. info = self.image_info[image_id]
  148. if info["source"] == "cdw":
  149. return info["path"]
  150. else:
  151. super(self.__class__, self).image_reference(image_id)
  152. def train(model):
  153. """Train the model."""
  154. # Training dataset.
  155. dataset_train = CDWDataset()
  156. dataset_train.load_cdw(args.dataset, "train")
  157. dataset_train.prepare()
  158. # Validation dataset
  159. dataset_val = CDWDataset()
  160. dataset_val.load_cdw(args.dataset, "val")
  161. dataset_val.prepare()
  162. # *** This training schedule is an example. Update to your needs ***
  163. # Since we're using a very small dataset, and starting from
  164. # COCO trained weights, we don't need to train too long. Also,
  165. # no need to train all layers, just the heads should do it.
  166. print("Training network heads")
  167. model.train(dataset_train, dataset_val,
  168. learning_rate=config.LEARNING_RATE,
  169. epochs=30,
  170. layers='heads')
  171. ############################################################
  172. # Training
  173. ############################################################
  174. if __name__ == '__main__':
  175. import argparse
  176. # Parse command line arguments
  177. parser = argparse.ArgumentParser(
  178. description='Train Mask R-CNN to detect balloons.')
  179. parser.add_argument("command",
  180. metavar="<command>",
  181. help="train")
  182. parser.add_argument('--dataset', required=False,
  183. metavar="/path/to/cdw/dataset/",
  184. help='Directory of the CDW dataset')
  185. parser.add_argument('--weights', required=True,
  186. metavar="/path/to/weights.h5",
  187. help="Path to weights .h5 file or 'coco'")
  188. parser.add_argument('--logs', required=False,
  189. default=DEFAULT_LOGS_DIR,
  190. metavar="/path/to/logs/",
  191. help='Logs and checkpoints directory (default=logs/)')
  192. parser.add_argument('--image', required=False,
  193. metavar="path or URL to image",
  194. help='Image to apply the color splash effect on')
  195. parser.add_argument('--video', required=False,
  196. metavar="path or URL to video",
  197. help='Video to apply the color splash effect on')
  198. args = parser.parse_args()
  199. # Validate arguments
  200. if args.command == "train":
  201. assert args.dataset, "Argument --dataset is required for training"
  202. elif args.command == "splash":
  203. assert args.image or args.video,\
  204. "Provide --image or --video to apply color splash"
  205. print("Weights: ", args.weights)
  206. print("Dataset: ", args.dataset)
  207. print("Logs: ", args.logs)
  208. # Configurations
  209. if args.command == "train":
  210. config = CDWConfig()
  211. else:
  212. class InferenceConfig(CDWConfig):
  213. # Set batch size to 1 since we'll be running inference on
  214. # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
  215. GPU_COUNT = 1
  216. IMAGES_PER_GPU = 1
  217. config = InferenceConfig()
  218. config.display()
  219. # Create model
  220. if args.command == "train":
  221. model = modellib.MaskRCNN(mode="training", config=config,
  222. model_dir=args.logs)
  223. else:
  224. model = modellib.MaskRCNN(mode="inference", config=config,
  225. model_dir=args.logs)
  226. # Select weights file to load
  227. if args.weights.lower() == "coco":
  228. weights_path = COCO_WEIGHTS_PATH
  229. # Download weights file
  230. if not os.path.exists(weights_path):
  231. utils.download_trained_weights(weights_path)
  232. elif args.weights.lower() == "last":
  233. # Find last trained weights
  234. weights_path = model.find_last()
  235. elif args.weights.lower() == "imagenet":
  236. # Start from ImageNet trained weights
  237. weights_path = model.get_imagenet_weights()
  238. else:
  239. weights_path = args.weights
  240. # Load weights
  241. print("Loading weights ", weights_path)
  242. if args.weights.lower() == "coco":
  243. # Exclude the last layers because they require a matching
  244. # number of classes
  245. model.load_weights(weights_path, by_name=True, exclude=[
  246. "mrcnn_class_logits", "mrcnn_bbox_fc",
  247. "mrcnn_bbox", "mrcnn_mask"])
  248. else:
  249. model.load_weights(weights_path, by_name=True)
  250. # Train or evaluate
  251. if args.command == "train":
  252. train(model)
  253. else:
  254. print("'{}' is not recognized. "
  255. "Use 'train' or 'splash'".format(args.command))

inspect_cdw_data.ipynb(此模块可以看到数据加载及预处理的过程,训练之前可以先看看数据处理是否正确)

insepect_cdw_model.ipynb(查看训练好的模型效果)

这两个文件代码太长啦,主要是验证py文件中的代码正确性,不影响最终测试,就不贴了,一定要看看自己的数据处理是否正确的话可以留个邮箱我发给你

inspect_cdw_data.ipynb部分处理结果如下:

1.加载Mask图像,同一类别中的不同目标需要加以区分

2.加载到原图中,检测目标和类别是否对应,同时根据Mask轮廓尝试BoundingBox

3.生成的一系列Anchors(在训练中需要一些如右图的反例来进行惩罚)

insepect_cdw_model.ipynb部分很多处理效果和上面数据处理是相似的,只是把它加载到模型中而已,代码中也有详细的注释啦

除了上述数据加载之外,后续网络处理的效果如下所示:

1.区域提取结果

2.对提取的所有区域进行识别,忽略结果为背景和置信度低于阈值的区域

3.特征提取网络输出的部分特征图

4.测试训练结果

demo_cdw.ipynb(放在samples目录下即可,放其他地方的话也可以,但是有些地方自己要改一下哦,主要几个路径改成自己实际用的就行啦)

  1. #%% md
  2. # Mask R-CNN Demo
  3. A quick intro to using the pre-trained model to detect and segment objects.
  4. #%%
  5. import os
  6. import sys
  7. import random
  8. import math
  9. import numpy as np
  10. import skimage.io
  11. import matplotlib
  12. import matplotlib.pyplot as plt
  13. # Root directory of the project
  14. ROOT_DIR = os.path.abspath("../")
  15. # Import Mask RCNN
  16. sys.path.append(ROOT_DIR) # To find local version of the library
  17. from mrcnn import utils
  18. import mrcnn.model as modellib
  19. from mrcnn import visualize
  20. sys.path.append(os.path.join(ROOT_DIR, "samples/cdw/")) # To find local version
  21. from samples.cdw import cdw
  22. %matplotlib inline
  23. # Directory to save logs and trained model
  24. MODEL_DIR = os.path.join(ROOT_DIR, "logs")
  25. # Local path to trained weights file
  26. CDW_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_cdw.h5")
  27. # Directory of images to run detection on
  28. IMAGE_DIR = os.path.join(ROOT_DIR, "datasets/cdw/test")
  29. #%% md
  30. ## Configurations
  31. We'll be using a model trained on the MS-COCO dataset. The configurations of this model are in the ```CocoConfig``` class in ```coco.py```.
  32. For inferencing, modify the configurations a bit to fit the task. To do so, sub-class the ```CocoConfig``` class and override the attributes you need to change.
  33. #%%
  34. class InferenceConfig(cdw.CDWConfig):
  35. # Set batch size to 1 since we'll be running inference on
  36. # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
  37. GPU_COUNT = 1
  38. IMAGES_PER_GPU = 1
  39. config = InferenceConfig()
  40. config.display()
  41. #%% md
  42. ## Create Model and Load Trained Weights
  43. #%%
  44. # Create model object in inference mode.
  45. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
  46. # Load weights trained on MS-COCO
  47. model.load_weights(CDW_MODEL_PATH, by_name=True)
  48. #%%
  49. class_names = ['BG','wood','brick','concrete','rubber']
  50. #%% md
  51. ## Run Object Detection
  52. #%%
  53. # Load a random image from the images folder
  54. file_names = next(os.walk(IMAGE_DIR))[2]
  55. image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
  56. # Run detection
  57. import time
  58. t1=time.clock()
  59. results = model.detect([image], verbose=1)
  60. print("time:",time.clock()-t1)
  61. # Visualize results
  62. r = results[0]
  63. visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
  64. class_names, r['scores'])
  65. #%
  66. #%%

在未知图片中的测试,可以看出来效果一般哈,比较只用了3张图片做训练啊,功能还是实现了的,测试一张图片360ms,在GPU上哦,笔记本CPU上要30多秒,所以没有GPU的可以看看效果,想要训练的话,请慎重。

主要参考

https://github.com/matterport/Mask_RCNN

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/122360
推荐阅读
相关标签
  

闽ICP备14008679号