我想比较两张照片。
我采用的方法是对它们进行编码。
然后计算两个编码向量之间的角度以进行相似性度量。
下面的代码是用来编码和解码图像使用有线电视新闻网与Keras。
但是,我需要得到张量的值。
如何实现?
非常感谢你。

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')])

最佳答案

如果我正确地理解了您的问题,您想从卷积自动编码器中获得128维编码表示,用于图像比较吗?
你可以做的是在网络的编码器部分创建一个引用,训练整个自动编码器,然后用编码器引用的权重对图像进行编码。
放这个:
self.autoencoder = autoencoderself.encoder = Model(inputs=self.autoencoder.input, outputs=self.autoencoder.get_layer('encoded').output)
autoencoder.compile()之后
并创建编码:
encoded_img = self.encoder.predict(input)

关于python - 如何在Keras中获得张量值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50596380/

10-12 03:56