【问题标题】:Spatial Pyramidal Pooling Layer空间金字塔池化层
【发布时间】:2018-03-07 16:29:16
【问题描述】:

我正在实现一个空间金字塔池(SPP)层,它提供一个固定大小的向量作为输出。此 SPP 层的代码是项目的一部分,该项目在可变大小的图像上训练模型。因此,我将模型的输入形状设为 (None , None, 3) 。 SPP 层将其输入张量作为参数。根据 SPP 的原始论文,池化层的窗口和步幅大小定义如下:

window_height= ceil(input_tensor.shape[1]/pooling_level)
window_width= ceil(input_tensor.shape[2]/pooling_level)
stride_height=floor(input_tensor.shape[1]/pooling_level)
stride_width=floor(input_tensor.shape[2]/pooling_level)

但由于我将输入形状设为 (None , None, 3) 因此我收到一条错误消息,指出“int 返回了非 int (type NoneType)”。

这是我的代码:

def Pooling2D(input_tensor, pool_levels):
    pool_list=[]
    shape=input_tensor.shape
    for level in pool_levels:
        window_height=np.ceil(int(shape[1])/level)
        window_width=np.ceil(int(shape[2])/level)
        stride_height=np.floor(int(shape[1])/level)
        stride_width=np.floor(int(shape[2])/level)
        pooling=MaxPool2D(pool_size=(window_height,window_width),strides=(stride_height,stride_width))(input_tensor)
        flattened_tensor=K.flatten(pooling)
        pool_list.append(flattened_tensor)
    output_tensor=K.concatenate(pool_list,axis=-1)
    return(output_tensor)

除了为输入图像设置特定的形状之外,还有什么办法可以解决这个错误吗?

【问题讨论】:

    标签: python-3.x deep-learning keras keras-layer


    【解决方案1】:

    重新考虑您要在这里做什么。

    window_height= ceil(input_tensor.shape[1]/pooling_level)
    

    您想在不告诉代码实际图像大小的情况下计算窗口宽度。所以你的代码实际上是

    window_height= ceil(None/pooling_level)
    

    我认为您对 SPP 层的作用有些困惑。 SPP 层将任意大小的图像作为输入并生成 具有固定长度的输出向量。这意味着即使您使用 SPP 层 您需要知道图像的宽度和高度。这不是一个缺点或什么。 SPP 层将接受任何图像大小。

    【讨论】:

    • 我明白了。但是我正在训练的模型应该采用可变尺寸的输入图像。所以我将输入 _shape 定义为 (None, None,3)。但是现在,如果我为输入指定特定大小,那么我的模型只会接受该特定形状的输入张量,并在看到不同的东西时抛出错误
    • 为什么设置为无?您可以通过编程方式确定图像大小并将其单独传递给任何图像吗?
    • 谢谢认识。我一定会尝试并让您知道结果。 :)
    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2017-12-19
    • 2019-09-14
    相关资源
    最近更新 更多