【问题标题】:Keras: methods to enlarge spartial dimension of the layer output blobKeras:扩大层输出blob的空间维度的方法
【发布时间】:2017-10-26 10:20:22
【问题描述】:

有哪些方法可以放大层输出blob的空间维度?

据我从文档中可以看出:

  1. UpSampling2D 它是否仅以 2 的幂进行上采样? 还有Repeats the rows and columns of the data by size[0] and size[1] respectively.,也不是很聪明。
  2. Conv2DTranspose 它可以具有任意输出大小(不是 2 upsampple 的幂)吗?

    如何进行具有任意维度的双线性插值放大(可以是具有固定权重的 Conv2DTranspose?)

    还有哪些其他选项可用于放大层输出 blob 的空间维度?

【问题讨论】:

标签: deep-learning keras conv-neural-network convolution keras-layer


【解决方案1】:

在answer from y300 的基础上展开,这里是一个在 Keras Lambda 层中封装 TensorFlow 双线性图像大小调整的完整示例:

from keras import Sequential
from keras.layers import Lambda
import tensorflow as tf

def UpSampling2DBilinear(size):
    return Lambda(lambda x: tf.image.resize_bilinear(x, size, align_corners=True))

upsampler = Sequential([UpSampling2DBilinear((256, 256))])

upsampled = upsampler.predict(images)

请注意,align_corners=True 可以获得与其他双线性图像上采样算法相似的性能,如this post 中所述。

要使用双三次重采样,请创建一个新函数并将 resize_bilinear 替换为 resize_bicubic。

对于更类似于 UpSampling2D 的实现,试试这个:

from keras import backend as K

def UpSampling2DBilinear(stride, **kwargs):
    def layer(x):
        input_shape = K.int_shape(x)
        output_shape = (stride * input_shape[1], stride * input_shape[2])
        return tf.image.resize_bilinear(x, output_shape, align_corners=True)
    return Lambda(layer, **kwargs)

这将允许您使用name=''、input_shape='' 和Lamba 的其他参数,并允许您传递整数步幅/上采样量。

【讨论】:

    【解决方案2】:

    您可以定义自己的调整大小层:

    from keras import layers, models, utils
    from keras.backend import tf as ktf
    
    class Interp(layers.Layer):
        def __init__(self, new_size, **kwargs):
            self.new_size = new_size
            super(Interp, self).__init__(**kwargs)
    
        def build(self, input_shape):
            super(Interp, self).build(input_shape)
    
        def call(self, inputs, **kwargs):
            new_height, new_width = self.new_size
            resized = ktf.image.resize_images(inputs, [new_height, new_width],
                                              align_corners=True)
            return resized
    
        def compute_output_shape(self, input_shape):
            return tuple([None, self.new_size[0], self.new_size[1], input_shape[3]])
    
        def get_config(self):
            config = super(Interp, self).get_config()
            config['new_size'] = self.new_size
            return config
    

    【讨论】:

      猜你喜欢
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      相关资源
      最近更新 更多