第一章 环境搭建
说明: 部分环境搭建的细节在以往博客中已有记录,这里只列举清单,不详细展开。
1.1 参考资源链接
1.1.1 GitHub
1.1.2 Anaconda下载
1.2 软硬件环境介绍
1.2.1 硬件环境
电脑型号:联想R720-15IKBN笔记本
处理器:Intel® Core™ i7-7700HQ CPU @ 2.80GHz
内存:16.0GB
独显:NVIDIA GeForce GTX 1050 Ti
核显:Intel® HD Graphics 630
1.2.2 软件环境
操作系统:Win10、CUDA Toolkit 9.0、cuDNN 7.3
开发平台:Anaconda3-4.7.5、Python3.6、tensorflow_gpu-1.13.0
1.2.3 云端环境
- 滴滴云GPU https://app.didiyun.com/#/auth/
- 易学智能GPU云 https://www.easyaiforum.cn/
1.3 开发环境搭建
1.3.1 软件下载和安装
- .Net Framework库,版本为4.6。
- Nvidia并行计算的平台CUDA,版本为9.0。
- Nvidia用于深度学习加速计算的cuDNN库,版本为7.3。
- Google深度学习开发框架TensorFlow,GPU版本。
- 安装运行tensorflow必需的msgpack库。
1.3.2 开发环境测试
1.4 问题解决
1.4.1 找不到TensorFlow库
如果一直出现找不到tensorflow库的错误,本文读者提示可以使用conda install tensorflow_gpu
的方法先解决环境问题,然后再conda uninstall tensorflow_gpu
,最后再实现本文当中的操作。
第二章 运行示例
2.1 源码结构分析
2.1.1 源码下载
下载地址 https://github.com/matterport/Mask_RCNN
2.1.2 源码目录结构
解压缩,并修改主目录名为Mask_RCNN。目录结构如下:
- Mask_RCNN
- datasets 存放数据集文件
- my_road
- train
- *.jpg
- val
- *.png
- train
- my_road
- mrcnn 包含模型、配置和通用文件
- init_models
- mask_rcnn_balloon.h5
- mask_rcnn_coco.h5
- samples 项目文件
- my_road_v1_0 针对UAS数据集,仅分割道路和背景两类
- logs
- train
- eval
- export
- vis
- my_road_v1_1 针对UAS数据集,仅分割道路和背景两类
- my_road_v1_0 针对UAS数据集,仅分割道路和背景两类
- README.md
- datasets 存放数据集文件
2.2 运行shapes测试示例
2.2.1 项目准备文件
shapes.py文件解析
- 引入包
ROOT_DIR = os.path.abspath("../../")
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
- 定义config类
class ShapesConfig(Config):
""" 该类继承自Config,主要写入一些预定义的配置参数
"""
# Give the configuration a recognizable name
NAME = "shapes"
IMAGES_PER_GPU = 8
NUM_CLASSES = 1 + 3 # background + 3 shapes
IMAGE_MIN_DIM = 128
IMAGE_MAX_DIM = 128
RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)
TRAIN_ROIS_PER_IMAGE = 32
STEPS_PER_EPOCH = 100
VALIDATION_STEPS = 5
- 定义ShapesDataset类
class ShapesDataset(utils.Dataset):
def load_shapes(self, count, height, width):
"""Generate the requested number of synthetic images.
count: number of images to generate.
height, width: the size of the generated images.
"""
# Add classes
self.add_class("shapes", 1, "square")
self.add_class("shapes", 2, "circle")
self.add_class("shapes", 3, "triangle")
for i in range(count):
bg_color, shapes = self.random_image(height, width)
self.add_image("shapes", image_id=i, path=None,
width=width, height=height,
bg_color=bg_color, shapes=shapes)
def load_image(self, image_id):
"""根据图片在数据集中的索引,载入图片"""
return image
def image_reference(self, image_id):
return info["shapes"]
def load_mask(self, image_id):
"""载入图片的mask,这里是需要重写的函数"""
return mask, class_ids.astype(np.int32)
def draw_shape(self, image, shape, dims, color):
return image
def random_shape(self, height, width):
"""随机生成形状"""
return shape, color, (x, y, s)
def random_image(self, height, width):
"""随机生成图片 """
return bg_color, shapes
train_shapes文件解析
- 导入包
ROOT_DIR = os.path.abspath("../../")
sys.path.append(ROOT_DIR) # To find local version of the library
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
- 配置文件Configurations
class ShapesConfig(Config):
"""覆盖一些配置参数 """
NAME = "shapes"
GPU_COUNT = 1
IMAGES_PER_GPU = 8
NUM_CLASSES = 1 + 3 # background + 3 shapes
IMAGE_MIN_DIM = 128
IMAGE_MAX_DIM = 128
RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128)
TRAIN_ROIS_PER_IMAGE = 32
STEPS_PER_EPOCH = 100
VALIDATION_STEPS = 5
config = ShapesConfig()
config.display()
- 配置绘图
def get_ax(rows=1, cols=1, size=8):
_, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
return ax
- 数据集
# Training dataset
dataset_train = ShapesDataset()
dataset_train.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
dataset_train.prepare()
# Validation dataset
dataset_val = ShapesDataset()
dataset_val.load_shapes(50, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
dataset_val.prepare()
- 训练