旋转不变卷积层的此Tensorflow代码的正确Keras实现是什么?

时间:2018-12-30 20:37:29

标签: tensorflow keras deep-learning computer-vision keras-layer

这样的代码的正确实现是什么:     将tensorflow导入为tf

# The clockwise shift-1 rotation permutation.
permutation = [[1, 0], [0, 0], [0, 1], [2, 0], [1, 1], [0, 2], [2, 1], [2, 
2], [1, 2]]


def shift_rotate(w, shift=1):
  shape = w.get_shape()
  for i in range(shift):
     w = tf.reshape(tf.gather_nd(w, permutation), shape)
  return w


def conv2d(x, W, **kwargs):
   # Determine all 7 rotations of w.
   w = W
   w_rot = [w]
   for i in range(7):
      w = shift_rotate(w)
      w_rot.append(w)

   # Convolve with all 8 rotations and stack.
   outputs = tf.stack([tf.nn.conv2d(x, w_i, **kwargs) for w_i in w_rot])

   # Max filter activation across rotations.
   output = tf.reduce_max(outputs, 0)
   return output 

代码从这里获得: https://raghakot.github.io/2017/01/09/Baking-rotational-invariance-into-a-neuron.html

我一直在使用tensorflow实现,但是找不到关于如何编写keras层的很好的教程。我不确定x和W参数将在keras中表示什么。

非常感谢。

0 个答案:

没有答案