【问题标题】:Has anyone written weldon pooling for keras?有没有人为keras写过weldon pooling?
【发布时间】:2018-10-03 22:20:58
【问题描述】:

Weldon 池化 [1] 是否已在 Keras 中实现?

我可以看到它已由作者 [2] 在 pytorch 中实现,但找不到 keras 等效项。

[1] T. Durand、N. Thome 和 M. Cord。 Weldon:弱su- 深度卷积神经网络的监督学习。在 CVPR,2016 年。 [2]https://github.com/durandtibo/weldon.resnet.pytorch/tree/master/weldon

【问题讨论】:

    标签: keras max-pooling


    【解决方案1】:

    这是一个基于 lua 版本的版本(有一个 pytorch impl,但我认为取 max+min 的平均值时会出错)。我假设 lua 版本的最高最大值和最小值的平均值仍然正确。我还没有测试过整个自定义层方面,但已经足够接近了,欢迎 cmets。

    托尼

    class WeldonPooling(Layer):
        """Class to implement Weldon selective spacial pooling with negative evidence
        """
    
        #@interfaces.legacy_global_pooling_support
        def __init__(self, kmax, kmin=-1, data_format=None, **kwargs):
            super(WeldonPooling, self).__init__(**kwargs)
            self.data_format = conv_utils.normalize_data_format(data_format)
            self.input_spec = InputSpec(ndim=4)
            self.kmax=kmax
            self.kmin=kmin
    
        def compute_output_shape(self, input_shape):
            if self.data_format == 'channels_last':
                return (input_shape[0], input_shape[3])
            else:
                return (input_shape[0], input_shape[1])
    
        def get_config(self):
            config = {'data_format': self.data_format}
            base_config = super(_GlobalPooling2D, self).get_config()
            return dict(list(base_config.items()) + list(config.items()))
    
        def call(self, inputs):
            if self.data_format == "channels_last":
                inputs = tf.transpose(inputs, [0, 3, 1, 2])
            kmax=self.kmax
            kmin=self.kmin
            shape=tf.shape(inputs)
            batch_size = shape[0]
            num_channels = shape[1]
            h = shape[2]
            w = shape[3]
            n = h * w
            view = tf.reshape(inputs, [batch_size, num_channels, n])
            sorted, indices = tf.nn.top_k(view, n, sorted=True)
            #indices_max = tf.slice(indices,[0,0,0],[batch_size, num_channels, kmax])
            output = tf.div(tf.reduce_sum(tf.slice(sorted,[0,0,0],[batch_size, num_channels, kmax]),2),kmax)
    
            if kmin > 0:
                #indices_min = tf.slice(indices,[0,0, n-kmin],[batch_size, num_channels, kmin])
                output=tf.add(output,tf.div(tf.reduce_sum(tf.slice(sorted,[0,0,n-kmin],[batch_size, num_channels, kmin]),2),kmin))
    
            return tf.reshape(output,[batch_size, num_channels])
    

    【讨论】:

    • 另请注意,这仅适用于 TF 后端。 “tf”可以替换为“K.tf”。使它更像Keras-y。对于其他后端,它需要它们的特定实现,因为没有所需操作的抽象。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多