简体   繁体   English

如何在Keras中获得张量值?

[英]How to get a tensor value in Keras?

I want to compare 2 images. 我想比较2张图片。

The approach I adopt is to encode them. 我采用的方法是对它们进行编码。

The angle between the two encoded vectors is then calculated for similarity measure. 然后计算两个编码矢量之间的角度以用于相似性度量。

The code below is used to encode and then decode images using CNN with Keras. 以下代码用于使用带有Keras的CNN编码和解码图像。

However, I need to get the value of the tensor encoded . 但是,我需要获取Tensor encoded的值。

How to achieve it? 如何实现呢?

Thank you very much. 非常感谢你。

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(28, 28, 1))  
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
#----------------------------------------------------------------#
# How to get the values of the tensor "encoded"?                   #
#----------------------------------------------------------------#
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

.....

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test),
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

In order to get an intermediate output, you need to create a separate model that contains the computation graph up to that point. 为了获得中间输出,您需要创建一个单独的模型,其中包含到那时的计算图。 In your case, you can: 就您而言,您可以:

encoder = Model(input_img, encoded)

After training with autoencoder is complete, you can encoder.predict which will return you the intermediate encoded result. autoencoder encoder.predict训练完成后,您可以encoder.predict autoencoder返回中间编码结果。 You can also save the models separately as you would any other model and not have to train every time. 您还可以像保存其他模型一样分别保存模型,而不必每次都进行训练。 In short, a Model is container for layers that construct a computation graph. 简而言之, Model是用于构建计算图的图层的容器。

If I understand your question correctly you would like to get the 128 dimensional encoded representation, from the convolutional autoencoder, for image comparison? 如果我正确理解了您的问题,您想从卷积自动编码器中获取128维编码表示形式,以进行图像比较吗?

What you could do is create a reference on the encoder part of the network, train the whole autoencoder and then encode the images with the weights of the encoder reference. 您可以做的是在网络的编码器部分上创建参考,训练整个自动编码器,然后使用编码器参考的权重对图像进行编码。

Put this: 把这个:

self.autoencoder = autoencoder self.encoder = Model(inputs=self.autoencoder.input, outputs=self.autoencoder.get_layer('encoded').output)

after autoencoder.compile() autoencoder.compile()

and create encodings with: 并使用以下代码创建编码:

encoded_img = self.encoder.predict(input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM