为什么两个不同图像的特征非常相同(高余弦距离)?

时间:2019-07-03 14:27:13

标签: python tensorflow tf-slim

我在TF-Slim中使用了经过预训练的ResNet50。 但是我发现两个非常不同的图像的特征是非常接近的(余弦距离)。规范余弦距离为0.996531。

第一个预测ID是928,第二个是458。

您能解释一下吗?

谢谢!

图片1:https://drive.google.com/file/d/1si_tsh8zPBZmu6p_Nq0kiWQA-JPo0SHD/view?usp=sharing

图片2:https://drive.google.com/open?id=16Kj0cMG5FItD3WcAUV__zQ3imXDI1cM1

import tensorflow.contrib.slim.nets as slim_nets
from tensorflow.contrib import slim
import tensorflow as tf
import numpy as np
import cv2
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "3"

input_ph = tf.placeholder(tf.float32, [None, 224, 224, 3])
_R_MEAN = 123.68
_G_MEAN = 116.78
_B_MEAN = 103.94
means = [_R_MEAN, _G_MEAN, _B_MEAN]
channels = tf.split(axis=3, num_or_size_splits=3, value=input_ph)
for i in range(3):
    channels[i] -= means[i]
input_tensor = tf.concat(axis=3, values=channels)
with slim.arg_scope(slim.nets.resnet_v2.resnet_arg_scope()):
    _, end_points = slim_nets.resnet_v2.resnet_v2_50(input_ph,
                                                     is_training=False,
                                                     num_classes=1001)
output_feature_map = end_points['resnet_v2_50/block4']
prob_tensor = end_points['predictions']
norm_feature_tensor = tf.nn.l2_normalize(tf.reduce_mean(output_feature_map, axis=[1, 2]), axis=1)
restore_path = '/mnt/cephfs_hl/vc/liangdong.tony/pretrained_model/ResNet/resnet_v2_50_2017_04_14/resnet_v2_50.ckpt'
image_path1 = '/mnt/cephfs_hl/vc/liangdong.tony/PycharmProjects/RetrievalCCWebVideo/tmp/v0202d030000bjuimasd1drf3jr17470/6.jpg'
image_path2 = '/mnt/cephfs_hl/vc/liangdong.tony/PycharmProjects/RetrievalCCWebVideo/tmp/v0202d3d0000bjub5qgghl0nka6tqjbg/0.jpg'
saver = tf.train.Saver()
sess = tf.Session()
saver.restore(sess, restore_path)
img1 = cv2.resize(cv2.imread(image_path1)[:, :, ::-1], (224, 224))
img2 = cv2.resize(cv2.imread(image_path2)[:, :, ::-1], (224, 224))
probs, norm1 = sess.run([prob_tensor, norm_feature_tensor], feed_dict={input_ph: np.asarray(np.expand_dims(img1, axis=0), np.float32)})
print probs, set(np.squeeze(probs)), np.argmax(np.squeeze(probs))
probs, norm2 = sess.run([prob_tensor, norm_feature_tensor], feed_dict={input_ph: np.asarray(np.expand_dims(img2, axis=0), np.float32)})
print probs, set(np.squeeze(probs)), np.argmax(np.squeeze(probs))
print norm1, np.min(norm1), np.max(norm1)
print norm2, np.min(norm2), np.max(norm2)
print np.sum(norm1 * norm2)

0 个答案:

没有答案