python - 如何在 tensorflow 中进行最小池化?

标签 python tensorflow machine-learning keras deep-learning

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/

相关文章:

python - 生成器与序列对象

python - tkinter:更改特定菜单项的前景色

tensorflow - tf 如何从同一个变量中恢复两个变量

machine-learning - 交叉验证如何进行测试?

python - 为连续 Action 空间实现强化(Humanoid-v2)?

python - 无法在我的 Django 应用程序的 models.py 中使用 imageField 显示照片?

Python(pandas)循环遍历列中的值,对每个值进行计算

Tensorflow:LSTM 中变量范围的值错误

tensorflow - 边缘 TPU 编译器 : ERROR: quantized_dimension must be in range [0, 1)。是 3

android-studio - 如何在 Android Studio 中使用 onnxruntime 和 .ort 模型