1. 程式人生 > >deformable convolution(可變形卷積)演算法解析及程式碼分析

deformable convolution(可變形卷積)演算法解析及程式碼分析

可變形卷積是指卷積核在每一個元素上額外增加了一個引數方向引數,這樣卷積核就能在訓練過程中擴充套件到很大的範圍。

可變形卷積的論文為:Deformable Convolutional Networks【1】

而之前google一篇論文對這篇論文有指導意義:Spatial Transformer Networks【2】

論文【1】的github程式碼地址為https://github.com/felixlaumon/deform-conv

——————————————————————————————————————————

可變形卷積很好理解,但如何實現呢?實現方面需要關注兩個限制:

1、如何將它變成單獨的一個層,而不影響別的層;

2、在前向傳播實現可變性卷積中,如何能有效地進行反向傳播。

這兩個問題的答案分別是:

1、在實際操作時,並不是真正地把卷積核進行擴充套件,而是對卷積前圖片的畫素重新整合,

變相地實現卷積核的擴張;

2、在圖片畫素整合時,需要對畫素進行偏移操作,偏移量的生成會產生浮點數型別,

而偏移量又必須轉換為整形,直接對偏移量取整的話無法進行反向傳播,這時採用雙線性差值的方式來得到對應的畫素。

——————————————————————————————————————————

可變性卷積的流程為:

1、原始圖片batch(大小為b*h*w*c),記為U,經過一個普通卷積,卷積填充為same,即輸出輸入大小不變,

對應的輸出結果為(b*h*w*2c),記為V,輸出的結果是指原圖片batch中每個畫素的偏移量(x偏移與y偏移,因此為2c)。

2、將U中圖片的畫素索引值與V相加,得到偏移後的position(即在原始圖片U中的座標值),需要將position值限定為圖片大小以內。

position的大小為(b*h*w*2c),但position只是一個座標值,而且還是float型別的,我們需要這些float型別的座標值獲取畫素。

3、例,取一個座標值(a,b),將其轉換為四個整數,floor(a), ceil(a), floor(b), ceil(b),將這四個整數進行整合,

得到四對座標(floor(a),floor(b)),  ((floor(a),ceil(b)),  ((ceil(a),floor(b)),  ((ceil(a),ceil(b))。這四對座標每個座標都對應U

中的一個畫素值,而我們需要得到(a,b)的畫素值,這裡採用雙線性差值的方式計算

(一方面得到的畫素準確,另一方面可以進行反向傳播)。

4、在得到position的所有畫素後,即得到了一個新圖片M,將這個新圖片M作為輸入資料輸入到別的層中,如普通卷積。

———————————————————————————————————————————

以上是可變性卷積的實現流程,但實際程式碼實現起來涉及到比較多的tensor操作,比較繁瑣。

程式碼實現主要的檔案有

cnn.py:採用keras定義了所有訓練需要的層,可變形卷積層為ConvOffset2D,

layer.py:定義了ConvOffset2D可變形卷積類,主要包括keras中需要的call函式與init函式,

call函式首先呼叫普通卷積,然後呼叫deform_conv.py中函式實際計算。

deform_conv.py:真正實現可變形卷積計算的檔案。

———————————————————————————————————————————

layer.py主要程式碼:

def __init__(self, filters, init_normal_stddev=0.01, **kwargs):
self.filters = filters
    super(ConvOffset2D, self).__init__(
        self.filters * 2, (3, 3), padding='same', use_bias=False,
        kernel_initializer=RandomNormal(0, init_normal_stddev),
        **kwargs
    )
def call(self, x):
    """Return the deformed featured map"""
    #獲取x大小,x大小為(b,h,w,c),分別為batch_size,圖片高度,圖片寬度,特徵圖大小
x_shape = x.get_shape()
    #呼叫普通卷積獲得輸出,輸出結果為(b,h,w,2c)表示圖片中每個畫素需要偏移的量(x,y)
    offsets = super(ConvOffset2D, self).call(x)
     #reshape一下輸出,方便後續操作,(b*c,h,w,2)表示共有b*c個圖片,每個圖片為h*w大小,每個畫素對應2個方向
    # offsets: (b*c, h, w, 2)    
    offsets = self._to_bc_h_w_2(offsets, x_shape)
    #將原始輸入也重新reshape一下方便後續操作
    # x: (b*c, h, w)
    x = self._to_bc_h_w(x, x_shape)
    #呼叫deform_conv.py中的函式根據原始圖片與偏移量生成新圖片資料。
    # X_offset: (b*c, h, w)
    x_offset = tf_batch_map_offsets(x, offsets)
    # x_offset: (b, h, w, c)
    x_offset = self._to_b_h_w_c(x_offset, x_shape)
    return x_offset
def compute_output_shape(self, input_shape):
    """Output shape is the same as input shape    Because this layer does only the deformation part
    """
return input_shape
@staticmethod
def _to_bc_h_w_2(x, x_shape):
    """(b, h, w, 2c) -> (b*c, h, w, 2)"""
x = tf.transpose(x, [0, 3, 1, 2])
    x = tf.reshape(x, (-1, int(x_shape[1]), int(x_shape[2]), 2))
    return x
@staticmethod
def _to_bc_h_w(x, x_shape):
    """(b, h, w, c) -> (b*c, h, w)"""
x = tf.transpose(x, [0, 3, 1, 2])
    x = tf.reshape(x, (-1, int(x_shape[1]), int(x_shape[2])))
    return x
@staticmethod
def _to_b_h_w_c(x, x_shape):
    """(b*c, h, w) -> (b, h, w, c)"""
x = tf.reshape(
        x, (-1, int(x_shape[3]), int(x_shape[1]), int(x_shape[2]))
    )
    x = tf.transpose(x, [0, 2, 3, 1])
    return x

deform_conv.py主要程式碼:

def tf_flatten(a):
    """Flatten tensor"""
return tf.reshape(a, [-1])
def tf_repeat(a, repeats, axis=0):
    """TensorFlow version of np.repeat for 1D"""
# https://github.com/tensorflow/tensorflow/issues/8521
    assert len(a.get_shape()) == 1
    a = tf.expand_dims(a, -1)
    a = tf.tile(a, [1, repeats])
    a = tf_flatten(a)
    return a
def tf_repeat_2d(a, repeats):
    """Tensorflow version of np.repeat for 2D"""assert len(a.get_shape()) == 2
    a = tf.expand_dims(a, 0)
    a = tf.tile(a, [repeats, 1, 1])
    return a
def tf_map_coordinates(input, coords, order=1):
    """Tensorflow verion of scipy.ndimage.map_coordinates    Note that coords is transposed and only 2D is supported    Parameters
    ----------
    input : tf.Tensor. shape = (s, s)
    coords : tf.Tensor. shape = (n_points, 2)
    """assert order == 1
    coords_lt = tf.cast(tf.floor(coords), 'int32')
    coords_rb = tf.cast(tf.ceil(coords), 'int32')
    coords_lb = tf.stack([coords_lt[:, 0], coords_rb[:, 1]], axis=1)
    coords_rt = tf.stack([coords_rb[:, 0], coords_lt[:, 1]], axis=1)

    vals_lt = tf.gather_nd(input, coords_lt)
    vals_rb = tf.gather_nd(input, coords_rb)
    vals_lb = tf.gather_nd(input, coords_lb)
    vals_rt = tf.gather_nd(input, coords_rt)
    coords_offset_lt = coords - tf.cast(coords_lt, 'float32')
    vals_t = vals_lt + (vals_rt - vals_lt) * coords_offset_lt[:, 0]
    vals_b = vals_lb + (vals_rb - vals_lb) * coords_offset_lt[:, 0]
    mapped_vals = vals_t + (vals_b - vals_t) * coords_offset_lt[:, 1]
    return mapped_vals
def sp_batch_map_coordinates(inputs, coords):
    """Reference implementation for batch_map_coordinates"""
coords = coords.clip(0, inputs.shape[1] - 1)
    mapped_vals = np.array([
        sp_map_coordinates(input, coord.T, mode='nearest', order=1)
        for input, coord in zip(inputs, coords)
    ])
    return mapped_vals
def tf_batch_map_coordinates(input, coords, order=1):
    """Batch version of tf_map_coordinates    Only supports 2D feature maps    Parameters
    ----------
    input : tf.Tensor. shape = (b, s, s)
    coords : tf.Tensor. shape = (b, n_points, 2)    Returns
    -------
    tf.Tensor. shape = (b, s, s)
    """input_shape = tf.shape(input)
    batch_size = input_shape[0]
    input_size = input_shape[1]
    n_coords = tf.shape(coords)[1]
    coords = tf.clip_by_value(coords, 0, tf.cast(input_size, 'float32') - 1)
    #得到目標座標左上角(left top)的整數座標
    coords_lt = tf.cast(tf.floor(coords), 'int32')
    #得到又下角的整數座標
    coords_rb = tf.cast(tf.ceil(coords), 'int32')
    #得到左下角的整數座標
    coords_lb = tf.stack([coords_lt[..., 0], coords_rb[..., 1]], axis=-1)
    #得到右上角的整數座標
    coords_rt = tf.stack([coords_rb[..., 0], coords_lt[..., 1]], axis=-1)
    #idx為索引展開,idx大小為(b*c*h*w),形如(0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
    #b*c為5,h*w為4,總數為所有圖片所有座標總數
    idx = tf_repeat(tf.range(batch_size), n_coords)
    def _get_vals_by_coords(input, coords):
        #stack完後,每一個點表示一個座標
        #形如
	    #(0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3)
	    # (3,2,1,2,3,1,2,3,0,0,0,3,2,1,1,2,3,2,0,0,2)
            # (3,2,1,0,0,2,0,3,1,2,3,0,0,2,3,0,1,2,0,2,3)
        indices = tf.stack([
            idx, tf_flatten(coords[..., 0]), tf_flatten(coords[..., 1])
        ], axis=-1)
        vals = tf.gather_nd(input, indices)
        vals = tf.reshape(vals, (batch_size, n_coords))
        return vals
     #以下為分別得到左上,左下,右上,右下四個點的畫素值。
    vals_lt = _get_vals_by_coords(input, coords_lt)
    vals_rb = _get_vals_by_coords(input, coords_rb)
    vals_lb = _get_vals_by_coords(input, coords_lb)
    vals_rt = _get_vals_by_coords(input, coords_rt)
     #用雙線性插值得到畫素值。
    coords_offset_lt = coords - tf.cast(coords_lt, 'float32')
    vals_t = vals_lt + (vals_rt - vals_lt) * coords_offset_lt[..., 0]
    vals_b = vals_lb + (vals_rb - vals_lb) * coords_offset_lt[..., 0]
    mapped_vals = vals_t + (vals_b - vals_t) * coords_offset_lt[..., 1]
    return mapped_vals
def sp_batch_map_offsets(input, offsets):
    """Reference implementation for tf_batch_map_offsets"""batch_size = input.shape[0]
    input_size = input.shape[1]
    #生成grid,grid表示將一個圖片的所有座標變成兩列,每一行兩個元素表示x,y
    (grid的最後大小為(b*c,h*w,2)
    offsets = offsets.reshape(batch_size, -1, 2)
    grid = np.stack(np.mgrid[:input_size, :input_size], -1).reshape(-1, 2)
    grid = np.repeat([grid], batch_size, axis=0)
    #將原始座標與座標偏移量相加,得到目標座標,coords的大小為(b*c,h*w,2)
    coords = offsets + grid
    #目標座標需要在圖片最大座標範圍內,將目標座標進行切割限制
    coords = coords.clip(0, input_size - 1)
    #根據原始輸入與目標座標得到畫素。
    mapped_vals = sp_batch_map_coordinates(input, coords)
    return mapped_vals
def tf_batch_map_offsets(input, offsets, order=1):
    """Batch map offsets into input    Parameters
    ---------
    input : tf.Tensor. shape = (b, s, s)
    offsets: tf.Tensor. shape = (b, s, s, 2)    Returns
    -------
    tf.Tensor. shape = (b, s, s)
    """input_shape = tf.shape(input)
    batch_size = input_shape[0]
    input_size = input_shape[1]
    offsets = tf.reshape(offsets, (batch_size, -1, 2))
    grid = tf.meshgrid(
        tf.range(input_size), tf.range(input_size), indexing='ij'
    )
    grid = tf.stack(grid, axis=-1)
    grid = tf.cast(grid, 'float32')
    grid = tf.reshape(grid, (-1, 2))
    grid = tf_repeat_2d(grid, batch_size)
    coords = offsets + grid
    mapped_vals = tf_batch_map_coordinates(input, coords)
    return mapped_vals