簡體   English   中英

從Keras的輸出層創建一個“解開”掩碼

[英]Create an “unpooling” mask from output layers in Keras

我正在使用Tensorflow后端在Keras寫CNN。 我正在嘗試創建一個“unpooling”掩碼(或匯集索引),如下所述: https ://arxiv.org/pdf/1511.00561.pdf

我已經建立了一個沒有這個解開掩碼的CNN,它工作正常。 我按照以下方式創建掩碼(這只是更大網絡的一部分,每個conv / maxpooling塊都有相同的想法):

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM