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トレーニングと予測の過程で、ピクチャの幾何学的変化トレーニング段階:原図->(512512)->(128)予測段階:(128)->(512512)->原図
inferenceフェーズでは、モデルの出力は128 x 128のfeature shapeであり、feature mapで得られた結果は、512 x 512にマッピングするにはscale=4.0を乗算する必要があり、次に、512 x 512から原図へのシミュレーション変換を経て、モデルの最終出力値を得る必要がある.
コア・オペレーションおよび復号コア・オペレーション
# 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のある次元で3 x 3の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)

512 x 512から原図にマッピング
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