Mask_RCNN学习记录(matterport版本)

资源链接

安装

  • 参考matterport版本的GitHub的README.md中requirements
  • 另外如果要在MS COCO数据集上训练、测试,还需pycocotools

相关博客

学习Mask RCNN网络结构,并构建颜色填充器应用
该版本以ResNet101 + FPN为backbone,heads包括检测和Mask预测两部分,其中检测部分包括类别预测和bbox回归。
English Version 中文版

网络介绍

Mask R-CNN是用于实例分割和目标检测的,在目标检测网络Faster R-CNN基础上增加Mask预测分支
uploading-image-362940.png
(图片来源:https://blog.csdn.net/linolzhang/article/details/71774168)

Mask RCNN改进

  1. Mask Scoring R-CNN: 给Mask也打个分
  2. Faster Training of Mask R-CNN by Focusing on Instance Boundaries: 利用实例边缘信息加速训练:训练过程中,对预测的Mask和GT的Mask进行边缘检测,计算两者的均方误差(Mean Square Error, MSE),将其作为损失函数的一部分(我把Paper中Edge Argument Head简单实现了)。我个人理解是:该文章更多是加速了网络训练的速度,因此精度有一定的提高(在训练过程中用边缘信息指明了一条道路,因此在梯度下降的过程中快了一些)
  3. 在mrcnn/model.py中添加edge_loss函数项
def mrcnn_edge_loss_graph(target_masks, target_class_ids, pred_masks):
  """Edge L2 loss for mask edge head
  
  target_masks: [batch, num_rois, height, width].
      A float32 tensor of Value 0 or 1(boolean?). Use zero padding to fill array
  target_class_ids: [batch, num_rois]. Integer class IDs. Zeros padded.
  pred_masks: [batch, proposal, height, width, num_classes] float32 tensor
              with value from 0 to 1(soft mask)(more information)    
  """
  # Reshape for simplicity. Merge first two dimensions into one
  # 即将batch 和 num_rois 合并为一项
  target_class_ids = K.reshape(target_class_ids, (-1,))
  mask_shape = tf.shape(target_masks)
  target_masks = K.reshape(target_masks, (-1, mask_shape[2], mask_shape[3]))
  pred_shape = tf.shape(pred_masks)
  pred_masks = K.reshape(pred_masks,
                         (-1, pred_shape[2], pred_shape[3], pred_shape[4]))
  #Permute predicted masks to [N, num_classes, height, width]
  pred_masks = tf.transpose(pred_masks, [0, 3, 1, 2])
  
  # Only positive ROIs contribute to the loss. (正的ROI是相对BG而言吗)
  # And only the class specific mask of each ROI
  # tf.where 获得索引值
  # tf.gather 根据索引值从张量中获得元素构成新张量Tensor
  # tf.cast 类型转换
  # tf.stack
  positive_ix = tf.where(target_class_ids > 0)[:, 0]
  positive_class_ids = tf.cast(
      tf.gather(target_class_ids, positive_ix), tf.int64)
  indices = tf.stack([positive_ix, positive_class_ids], axis=1)
  
  # Gather the masks (predicted and true) that contribute to loss
  y_true = tf.gather(target_masks, positive_ix)
  y_pred = tf.gather_nd(pred_masks, indices)
      
  # shape: [batch * rois, height, width, 1]
  y_true = tf.expand_dims(y_true, -1)
  y_pred = tf.expand_dims(y_pred, -1)
  
  y_true = 255 * y_true
  y_pred = 255 * y_pred
  
  # shape: [3, 3, 1, 2]
  sobel_kernel = tf.constant([[[[1, 1]], [[0, 2]], [[-1, 1]]],
                              [[[2, 0]], [[0, 0]], [[-2, 0]]],
                              [[[1,-1]], [[0,-2]], [[-1,-1]]]], dtype=tf.float32)
                              
  # Conv2D with kernel
  edge_true = tf.nn.conv2d(y_true, sobel_kernel, strides=[1, 1, 1, 1], padding="SAME")    
  edge_pred = tf.nn.conv2d(y_pred, sobel_kernel, strides=[1, 1, 1, 1], padding="SAME")
  
  # abs and clip
  edge_true = tf.clip_by_value(abs(edge_true), 0, 255)
  edge_pred = tf.clip_by_value(abs(edge_pred), 0, 255)    
  
  # Mean Square Error(MSE) Loss
  return tf.reduce_mean(tf.square(edge_true/255. - edge_pred/255.))

说明

  • Keras中fit函数中,每个Epoch训练的数目是 batch_size × steps per epoch,故每个Epoch不一定是把整个train_set全部训练一遍。原帖子
  • 使用conda install命令安装tensorflow-gpu教程
  • 如果用不习惯.ipynb文件,可以用Jupyter NoteBook将其保存为.py文件
    命令行输入jupyter notebook启动,打开文件后Download as Python(.py)
  • 另外,感觉有一个idea还是不够的,而且还要做充分的实验进行验证(比如说为什么边缘检测用的是Sobel,而是Laplace或是Canny)(又有点实践指导理论的意思。。。)。
  • 最后,欢迎批评指正!
posted @ 2019-03-25 21:05  达可奈特  阅读(1334)  评论(2编辑  收藏  举报