Tensorflow提供了平均池化和最大池化的操作,但没有最小池化的操作。
有没有什么方法可以绕过这个问题来实现最小池化呢?
回答:
我们可以用这种方式手动重现最大池化…
x = np.random.uniform(0,1, (5,30,30,3)).astype('float32')n_channel = x.shape[-1]patches = tf.image.extract_patches(x, sizes = [1, 3, 3, 1], strides = 4*[1], rates = 4*[1], padding = 'VALID')channel_pool = [tf.reduce_max(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)]res = tf.concat(channel_pool, axis=-1)tf.reduce_all(res == MaxPool2D(pool_size=(3, 3), strides=(1,1), padding="valid")(x)) ## TRUE !!!
根据上面的例子,我们可以简单地使用tf.reduce_min
来切换到最小池化
def min_pool(x): n_channel = x.shape[-1] patches = tf.image.extract_patches(x, sizes = [1, 3, 3, 1], strides = 4*[1], rates = 4*[1], padding = 'VALID') channel_pool = [tf.reduce_min(patches[:,:,:,c::n_channel], keepdims=True, axis=-1) for c in range(n_channel)] return tf.concat(channel_pool, axis=-1)
我们可以将整个过程包装在一个Lambda层中,以便在keras模型中使用:Lambda(min_pool)(x)