实际上,任务是尽可能准确地复制theano 上 SpatialMaxPooling 和 SpatialMaxUnpooling 层的行为。
在这种情况下,SpatialMaxUnpooling 只填充对应于相应 SpatialMaxPooling 中最大值索引的那些“单元格”。
例如 - 这是输入图像
SpatialMaxPooling 将存储每个 2x2 区域中具有最大值的像素及其索引。
而 SpatialMaxUnpooling - 只会将值设置为与索引对应的那些像素。也就是说,输出将是
我发布了以下实现:
def pooling2d_2x2(self, x):
reshaped = x.reshape([
x.shape[0], x.shape[1], x.shape[2] // 2, 2, x.shape[3] // 2, 2
])
max_values, max_indices = T.max_and_argmax(reshaped, (3,5,))
return max_values, max_indices
def unpooling2d_2x2(self, pooled, indices):
tmp_shape = [pooled.shape[0], pooled.shape[1], pooled.shape[2], 2, pooled.shape[3], 2]
# Resize image
resized = pooled.repeat(2, 2).repeat(2, 3)
pooled_reshaped = resized.reshape(tmp_shape)
# Resize indices
indices_repeaten = indices.repeat(2, 2).repeat(2, 3).reshape(tmp_shape)
# Calculate output
result = pooled_reshaped * 0.0
result = T.set_subtensor(result[:, :, :, 0, :, 0],
pooled_reshaped[:, :, :, 0, :, 0] * T.eq(indices_repeaten[:, :, :, 0, :, 0], 0))
result = T.set_subtensor(result[:, :, :, 0, :, 1],
pooled_reshaped[:, :, :, 0, :, 1] * T.eq(indices_repeaten[:, :, :, 0, :, 1], 1))
result = T.set_subtensor(result[:, :, :, 1, :, 0],
pooled_reshaped[:, :, :, 1, :, 0] * T.eq(indices_repeaten[:, :, :, 1, :, 0], 2))
result = T.set_subtensor(result[:, :, :, 1, :, 1],
pooled_reshaped[:, :, :, 1, :, 1] * T.eq(indices_repeaten[:, :, :, 1, :, 1], 3))
result_shape = [pooled.shape[0], pooled.shape[1], pooled.shape[2] * 2, pooled.shape[3] * 2]
return result.reshape(result_shape)
但她在速度上并不出色(顺便说一句,我不会拒绝建议 - 如何配置文件)。因此问题 - 这里可以改进什么?


下一个替换(据我了解 theano(这可能是一个非常平庸的理解 :-))——这里我们不再为新张量分配内存,仅指“增加的”输入张量和索引)稍微增加了速度。但是,也许 - 还有其他可能的改进吗?