Tensorflow 有平均池化和最大池化操作,但没有最小池化操作。
是否有某种解决办法来获得最小池化?
最佳答案
我们可以通过这种方式手动重新创建 MaxPooling...
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
切换到 MinPooling
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)
关于python - 如何在 tensorflow 中进行最小池化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62397028/