可變形卷積解讀和程式碼實現(TF)
阿新 • • 發佈:2018-12-10
#呼叫普通卷積獲得輸出,輸出結果為(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