python - 从 Keras 中的输出层创建一个 "unpooling"掩码

标签 python tensorflow keras

我正在使用 Tensorflow 后端在 Keras 中编写 CNN。我正在尝试创建一个“unpooling”掩码(或合并索引),如下所述:https://arxiv.org/pdf/1511.00561.pdf

我构建了一个没有这个 unpooling mask 的 CNN,它运行良好。我通过以下方式创建掩码(这只是更大网络的一部分,每个 conv/maxpooling block 的想法相同):

img_input = Input(shape=(num_channels, img_h, img_w))
x = conv_block(img_input, kernel, 512)
orig = x #Save output x
x = MaxPooling2D()(x)

x = UpSampling2D()(x)

bool_mask = K.greater_equal(orig, x)
mask = K.cast(bool_mask, dtype='float32')

mask_input = Input(tensor=mask) # Makes the mask to a Keras tensor to use as input
x = keras.layers.multiply([mask_input, x])
x = deconv_block(x, kernel, 512, 512)

x = Reshape((n_labels, img_h * img_w))(x)
x = Permute((2, 1))(x)
main_output = Activation('softmax')(x)

model = Model(inputs=img_input, outputs=main_output)

由于我从其他层创建了“第二个输入”mask_input,所以我不想将其作为模型输入。但如果我不这样做,我就无法创建模型。如果我将最后一行更改为:

model = Model(inputs=[img_input, mask_input], outputs=main_output)

我现在可以创建模型,但是当我想使用它时,我需要第二个输入,在我创建它之前我没有。

有没有人有不同的解决方案来创建一个 unpooling-mask 或者知道如何解决多个输入的问题?

最佳答案

我会将所有操作放在层中,这是模型所期望的(我假设函数 conv_blockdeconv_block 完全由层组成,否则,它们应该去也进入 Lambda 层)。

您不需要将处理过的 x 作为输入。您可以像以前一样拆分模型,然后再次合并它,形成平行分支。

我无法使用您的数据和维度进行测试,但在我在这里运行的有关串联的简单测试中,它有效。 (我在 theano 中测试过,因为我没有 tensorflow。我希望一切都能正常工作......但也许你应该在连接和 greater_equal 上试验不同的轴)

img_input = Input(shape=(num_channels, img_h, img_w))

x = conv_block(img_input, kernel, 512)

orig = x #Save output x
x = MaxPooling2D()(x)

x = UpSampling2D()(x)

#here we're going to reshape the data for a concatenation:
#xReshaped and origReshaped are now split branches
xReshaped = Reshape((1,channels_after_conv_block, h_after, w_after))(x)
origReshaped = Reshape((1,channels_after_conv_block, h_after, w_after))(orig)

#concatenation - here, you unite both branches again
    #normally you don't need to reshape or use the axis var, 
    #but here we want to keep track of what was x and what was orig.
together = Concatenate(axis=1)([origReshaped,xReshaped])

bool_mask = Lambda(lambda t: K.greater_equal(t[:,0], t[:,1]),
    output_shape=(channels_after_conv_block, h_after, w_after))(together)
mask = Lambda(lambda t: K.cast(t, dtype='float32'))(bool_mask)

x = Multiply()([mask, x])
x = deconv_block(x, kernel, 512, 512)

x = Reshape((n_labels, img_h * img_w))(x)
x = Permute((2, 1))(x)
main_output = Activation('softmax')(x)

model = Model(inputs=img_input, outputs=main_output)

这是我用 MNIST 数据运行的简单测试:

inp1 = Input((28,28))
inp2 = Input((28,28))
in1 = Reshape((1,28,28))(inp1)
in2 = Reshape((1,28,28))(inp2)
c = Concatenate(axis =1)([in1,in2])

#here, the lambda expression sums two MNIST images
c = Lambda(lambda x: x[:,0]+x[:,1],output_shape=(28,28))(c)

m = Model([inp1,inp2],c)

res = m.predict([xTraining[:10],xTraining[10:20]])
print(res.shape)

#if you plot the res, you will see the images superposed, which was the expected result.

关于python - 从 Keras 中的输出层创建一个 "unpooling"掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657825/

相关文章:

python - 如何使函数的变量在类中全局?

performance - 使用 CPU 与 GPU 训练模型 - 速度与内存

python - Keras - 如何将图像数组传递给 ImageDataGenerator.flow

tensorflow - Keras 多个二进制输出

python - Pandas 将周日期转换为周末周五日期

python - SQLAlchemy 和通行证库

tensorflow - Syntaxnet/Parsey McParseface 中使用了哪些依赖关系表示?

machine-learning - 从 URL 加载权重

python - 如何获取控制台返回的内容(字符串)并将其放入变量中?

python - 在Tensorflow中,sampled_softmax_loss和softmax_cross_entropy_with_logits有什么区别