centernet筆記 - inference階段後處理

預備知識,mxnet.ndarray的一些操作
shape_array

shape_array([[1,2,3,4], [5,6,7,8]]) = [2,4]  # 獲取ndarry的shape
# Returns a 1D int64 array containing the shape of data.

split

Splits an array along a particular axis into multiple sub-arrays.
cc = [  1  80 128 128]
N, C, H, W = cc.split(num_outputs=4, axis=0)
# N = [1], C = [80], H = [128], W = [128]

topk

topk(data=None, axis=_Null, k=_Null, ret_typ=_Null, is_ascend=_Null, dtype=_Null, out=None, name=None, **kwargs)
# data : The input array
# axis : 從哪個維度選取topk,默認-1,即最後一個維度
# ret_typ : return type,可以選擇返回值,或者index,或者both
# is_ascend : 是否逆序,默認爲0,返回 top k largest

cast

Casts all elements of the input to a new type.
# 將array中元素轉換爲指定的類型
cast([0.9, 1.3], dtype='int32') = [0, 1]

slice_like

# Slices a region of the array like the shape of another array. 
x = [[  1.,   2.,   3.,   4.],
     [  5.,   6.,   7.,   8.],
     [  9.,  10.,  11.,  12.]]
y = [[  0.,   0.,   0.],
     [  0.,   0.,   0.]]
slice_like(x, y) = [[ 1.,  2.,  3.]
                    [ 5.,  6.,  7.]]
slice_like(x, y, axes=(0, 1)) = [[ 1.,  2.,  3.]
                                 [ 5.,  6.,  7.]]
slice_like(x, y, axes=(0)) = [[ 1.,  2.,  3.,  4.]
                              [ 5.,  6.,  7.,  8.]]
slice_like(x, y, axes=(-1)) = [[  1.,   2.,   3.]
                               [  5.,   6.,   7.]
                               [  9.,  10.,  11.]]

expand_dims

# Inserts a new axis of size 1 into the array shape For example, given x with shape (2,3,4),
# then expand_dims(x, axis=1) will return a new array with shape (2,1,3,4).
mxnet.ndarray.expand_dims(data=None, axis=_Null, out=None, name=None, **kwargs)
# axis=-1, 則在末尾增加一個維度

tile

# 將array複製n次
x = [[1, 2],
     [3, 4]]
tile(x, reps=(2,3)) = [[ 1.,  2.,  1.,  2.,  1.,  2.],
                       [ 3.,  4.,  3.,  4.,  3.,  4.],
                       [ 1.,  2.,  1.,  2.,  1.,  2.],
                       [ 3.,  4.,  3.,  4.,  3.,  4.]]

gather_nd
參考:https://discuss.gluon.ai/t/topic/2413/3

# 從array中,按照輸入的索引取出其中的元素
data = [[0, 1], [2, 3]]
indices = [[1, 1, 0], [0, 1, 0]]
gather_nd(data, indices) = [2, 3, 0] # 取出(1,0),(1,1),(0,0)的元素

centernet訓練與預測過程中,圖片的幾何變化
訓練階段:原圖 -> (512,512) -> (128,128)
預測階段:(128,128) -> (512,512) -> 原圖

在inference階段,模型的輸出是在128x128的feature shape上,在feature map上得到的結果,需要乘以scale=4.0,才能映射到512x512上,接着,還必須經過512x512到原圖的一個仿射變換,才能得到模型最終輸出值

核心操作及解碼
核心操作

# self.heatmap_nms = nn.MaxPool2D(pool_size=3, strides=1, padding=1)
heatmap = outs[0]
keep = F.broadcast_equal(self.heatmap_nms(heatmap), heatmap)
results = self.decoder(keep * heatmap, outs[1], outs[2])

outs爲模型輸出,在heatmap所在維度上做3x3的max pooling,把峯值點找出來
解碼

    def hybrid_forward(self, F, x, wh, reg):
        """Forward of decoder"""
        import pdb
        pdb.set_trace()
        # 這裏假設batch_size = 4, resize w和h爲512x512
        _, _, out_h, out_w = x.shape_array().split(num_outputs=4, axis=0) # 獲取feature map的H,W
        scores, indices = x.reshape((0, -1)).topk(k=self._topk, ret_typ='both') # 獲取top_100的scores和indices,x nx80x128x128 -> nx(80x128x128), 某類的128x128元素連在一起
        indices = F.cast(indices, 'int64')
        topk_classes = F.cast(
            F.broadcast_div(
                indices,
                (out_h * out_w)),
            'float32')  #  根據indices,獲取top_100對應的類別,0-79
        topk_indices = F.broadcast_mod(indices, (out_h * out_w))  # 獲取在128x128平面拉平後的索引
        topk_ys = F.broadcast_div(topk_indices, out_w) # 1維索引恢復出2維平面上的y
        topk_xs = F.broadcast_mod(topk_indices, out_w) # 1維索引恢復出2維平面上的x
        center = reg.transpose((0, 2, 3, 1)).reshape((0, -1, 2)) # shape: (4, 16384, 2)
        wh = wh.transpose((0, 2, 3, 1)).reshape((0, -1, 2)) # shape: (4, 16384, 2)
        batch_indices = F.cast(F.arange(256).slice_like(center, axes=(
            0)).expand_dims(-1).tile(reps=(1, self._topk)), 'int64') # shape: (4, 100), 依次爲全0,全1,全2,全3
        reg_xs_indices = F.zeros_like(batch_indices, dtype='int64') # shape: (4, 100),全0
        reg_ys_indices = F.ones_like(batch_indices, dtype='int64') # shape: (4, 100),全1
        reg_xs = F.concat(
            batch_indices, topk_indices, reg_xs_indices, dim=0).reshape(
            (3, -1)) # shape: (3, 400)
        ‘’‘reg_xs = 
        [[   0    0    0 ...    3    3    3]
 		 [9664 8207 9425 ... 9639 5593 9044]
 		 [   0    0    0 ...    0    0    0]]
        ’‘’
        reg_ys = F.concat(
            batch_indices, topk_indices, reg_ys_indices, dim=0).reshape(
            (3, -1))
        ‘’‘reg_ys = 
        [[   0    0    0 ...    3    3    3]
 		 [9664 8207 9425 ... 9639 5593 9044]
 		 [   1    1    1 ...    1    1    1]]
        ’‘’
        xs = F.cast(
            F.gather_nd(
                center, reg_xs).reshape(
                (-1, self._topk)), 'float32') # shape : (4, 100)
        ys = F.cast(
            F.gather_nd(
                center, reg_ys).reshape(
                (-1, self._topk)), 'float32') # shape : (4, 100)
        topk_xs = F.cast(topk_xs, 'float32') + xs
        topk_ys = F.cast(topk_ys, 'float32') + ys # feature map上座標+偏移 = 真實座標
        w = F.cast(F.gather_nd(wh, reg_xs).reshape((-1, self._topk)), 'float32')  # noqa
        h = F.cast(F.gather_nd(wh, reg_ys).reshape((-1, self._topk)), 'float32')  # noqa
        half_w = w / 2
        half_h = h / 2
        results = [
            topk_xs - half_w,
            topk_ys - half_h,
            topk_xs + half_w,
            topk_ys + half_h]
        results = F.concat(*[tmp.expand_dims(-1) for tmp in results], dim=-1) # shape: (4, 100, 4)
        return topk_classes, scores, results * self._scale # (4, 100),(4, 100),(4, 100, 4)

由512x512映射到原圖

affine_mat = get_post_transform(orig_width, orig_height, 512, 512) # 獲取仿射變換矩陣
bbox[0:2] = affine_transform(bbox[0:2], affine_mat) # 對x1,y1做仿射變換
bbox[2:4] = affine_transform(bbox[2:4], affine_mat) # 對x2,y2做仿射變換
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章