學習Mask RCNN網絡結構,並構建顏色填充器應用
該版本以ResNet101 + FPN爲backbone,heads包括檢測和Mask預測兩部分,其中檢測部分包括類別預測和bbox迴歸。
English Version 中文版git
Mask R-CNN是用於實例分割和目標檢測的,在目標檢測網絡Faster R-CNN基礎上增長Mask預測分支
(圖片來源:http://www.javashuo.com/article/p-oodjvowl-gg.html)github
在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.))
jupyter notebook
啓動,打開文件後Download as Python(.py)