Guest User

keras blurpool lambda

a guest
Jul 29th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. def get_blur_kernel(n_ch):
  2.     filter_bank = np.zeros((5, 5, n_ch, 1), dtype="float32")
  3.     v = np.array([1, 4, 6, 4, 1], dtype="float32")
  4.     k = np.outer(v, v)
  5.     k /= k.sum()
  6.     for i in range(n_ch):
  7.         filter_bank[:, :, i, 0] = k
  8.     return tf.constant(filter_bank)
  9.  
  10. def blurpool(x):
  11.     x_padded = tf.pad(x, [[0, 0], [2, 2], [2, 2], [0, 0]], "REFLECT")
  12.     y = tf.nn.depthwise_conv2d_native(x_padded, get_blur_kernel(x.shape[-1]),
  13.                                       strides=[1, 2, 2, 1], padding="VALID")
  14.     return y
  15.  
  16.  
  17. def test_model():
  18.     x = tf.keras.layers.Input((32, 32, 3))
  19.     y = tf.keras.layers.Conv2D(64, 3, padding="same")(x)
  20.     y = tf.keras.layers.Lambda(blurpool)(y)
  21.     return tf.keras.models.Model(x, y)
Advertisement
Add Comment
Please, Sign In to add comment