Tensorflow return 相似图片

Tensorflow return similar images

我想使用 Google 的 Tensorflow 来 return 与输入图像相似的图像。

我已经在虚拟机 CPU.

的 Ubuntu14.04 上从 http://www.tensorflow.org 安装了 Tensorflow(使用 PIP 安装 - pip 和 python 2.7)

我已经从 http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz 下载了经过训练的模型 Inception-V3 (inception-2015-12-05.tgz),该模型是使用 2012 年的数据在 ImageNet 大型视觉识别挑战赛上训练的,但我认为它里面既有神经网络又有分类器(因为那里的任务是预测类别)。我还下载了文件 classify_image.py,该文件将模型中的 1000 个 类 中的一个图像分类。

所以我有一个随机图像 image.jpg,我 运行 测试模型。当我 运行 命令时:

python /home/amit/classify_image.py --image_file=/home/amit/image.jpg

我得到以下输出:(分类是使用 softmax 完成的)

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 3
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 3
trench coat (score = 0.62218)
overskirt (score = 0.18911)
cloak (score = 0.07508)
velvet (score = 0.02383)
hoopskirt, crinoline (score = 0.01286)

现在,手头的任务是从包含 60,000 张图像(jpg 格式,并保存在 [=29= 的文件夹中)的数据库中找到与输入图像 (image.jpg) 相似的图像]).我相信这可以通过从 inception-v3 模型中移除最终分类层,并使用输入图像的特征集来计算所有 60,000 张图像的特征集的余弦距离来完成,我们可以 return距离较小的图像 (cos 0 = 1)

请建议我解决这个问题的方法以及如何使用 Python API.

我想我找到了问题的答案:

在使用预训练模型(NN + 分类器)对图像进行分类的文件 classify_image.py 中,我进行了以下提到的更改(旁边写有 #ADDED 的语句):

def run_inference_on_image(image):
  """Runs inference on an image.
  Args:
    image: Image file name.
  Returns:
    Nothing
  """
  if not gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = gfile.FastGFile(image, 'rb').read()

  # Creates graph from saved GraphDef.
  create_graph()

with tf.Session() as sess:
 # Some useful tensors:
 # 'softmax:0': A tensor containing the normalized prediction across
 #   1000 labels.
 # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
 #   float description of the image.
 # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
 #   encoding of the image.
 # Runs the softmax tensor by feeding the image_data as input to the graph.
 softmax_tensor = sess.graph.get_tensor_by_name('softmax:0')
 feature_tensor = sess.graph.get_tensor_by_name('pool_3:0') #ADDED
 predictions = sess.run(softmax_tensor,
                        {'DecodeJpeg/contents:0': image_data})
 predictions = np.squeeze(predictions)
 feature_set = sess.run(feature_tensor,
                        {'DecodeJpeg/contents:0': image_data}) #ADDED
 feature_set = np.squeeze(feature_set) #ADDED
 print(feature_set) #ADDED
 # Creates node ID --> English string lookup.
 node_lookup = NodeLookup()

 top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
 for node_id in top_k:
   human_string = node_lookup.id_to_string(node_id)
   score = predictions[node_id]
   print('%s (score = %.5f)' % (human_string, score))

我 运行 通过将 image_data 馈入 pool_3:0 张量。如果我做错了,请告诉我。如果这是正确的,我相信我们可以使用这个张量进行进一步的计算。

您的问题听起来与此类似visual search project

Tensorflow 现在有一个很好的教程,介绍如何在最后一层之前获得激活并重新训练具有不同类别的新分类层: https://www.tensorflow.org/versions/master/how_tos/image_retraining/

示例代码: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py

在你的情况下,是的,你可以从 pool_3 softmax 层下面的层(或所谓的瓶颈)获得激活并将它们作为输入发送到其他操作:

最后,关于寻找相似图像,我认为 imagenet 的瓶颈激活不是图像搜索的非常相关的表示。您可以考虑使用具有直接图像输入的自动编码器网络。


(来源:deeplearning4j.org