資源鏈接
- Mask R-CNN論文
- matterport版本的GitHub 基於Keras和Tensorflow,關於程序的安裝與使用,readme文件中寫得很清楚
- GitHub上還有Facebook的官方實現版本:Detectron maskrcnn-benchmark
安裝
- 參考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預測分支
(圖片來源:https://blog.csdn.net/linolzhang/article/details/71774168)
Mask RCNN改進
- Mask Scoring R-CNN: 給Mask也打個分
- Faster Training of Mask R-CNN by Focusing on Instance Boundaries: 利用實例邊緣信息加速訓練:訓練過程中,對預測的Mask和GT的Mask進行邊緣檢測,計算兩者的均方誤差(Mean Square Error, MSE),將其作為損失函數的一部分(我把Paper中Edge Argument Head簡單實現了)。我個人理解是:該文章更多是加速了網絡訓練的速度,因此精度有一定的提高(在訓練過程中用邊緣信息指明了一條道路,因此在梯度下降的過程中快了一些)
Code is Here: 點擊查看詳細內容
在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.))</code> </pre>
說明
- 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)(又有點實踐指導理論的意思。。。)。
- 最后,歡迎批評指正!