Python 2.7 Tensorflow返回相似的图像

Python 2.7 Tensorflow返回相似的图像,python-2.7,neural-network,deep-learning,tensorflow,google-image-search,Python 2.7,Neural Network,Deep Learning,Tensorflow,Google Image Search,我想使用Google的Tensorflow将类似的图像返回到输入图像 我已经在虚拟机CPU上的Ubuntu14.04上安装了Tensorflow from(使用PIP安装-PIP和python 2.7) 我下载了经过训练的模型Inception-V3(Inception-2015-12-05.tgz),该模型使用2012年的数据在ImageNet大型视觉识别挑战赛上进行训练,但我认为它包含了神经网络和分类器(与预测类别的任务一样)。我还下载了文件classify_image.py,该文件将图像分

我想使用Google的Tensorflow将类似的图像返回到输入图像

我已经在虚拟机CPU上的Ubuntu14.04上安装了Tensorflow from(使用PIP安装-PIP和python 2.7)

我下载了经过训练的模型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完成分类)

现在,手头的任务是从60000个图像(jpg格式,保存在/home/amit/images文件夹中)的数据库中查找与输入图像(image.jpg)相似的图像。我相信这可以通过从inception-v3模型中移除最终的分类层来实现,并使用输入图像的特征集从特征集中找到所有60000个图像的余弦距离,我们可以返回距离较小的图像(cos 0=1)


请向我建议解决此问题的方法,以及如何使用Python API解决此问题。

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

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


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

您的问题听起来与此类似。

Tensorflow现在有一个很好的教程,介绍如何在最后一层之前获得激活,并使用不同的类别重新训练新的分类层:

示例代码:

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

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

(来源:)

我想到了一些解决方案,但我想进一步了解这个问题。我想你是想在60000张图片中找到所有的“风衣”。如果是这样的话,您只需在Inception中运行所有60000个图像,在“trench coat”显示为前5位的位置提取图像(如果您愿意,添加一个阈值),就完成了。这能回答吗?谢谢你的帮助。但问题是上面的图像文件image.jpg实际上是一个saree,我不想仅限于模型中的1000个类。此外,如果我找到一种添加新类的方法,它将是一个非常广泛的分类器,并且图像的标记将是一项乏味的任务。所以,我想从训练过的模型中去掉分类层,然后使用特征向量来计算最近的图像。对。因此,问题是:“给定60000张图像,如何使用改进版的Inception将所有Saree聚集在一起?”。我为所有50000张图像提取了pool3功能集,然后用输入图像的功能集和其余图像计算余弦距离。。它显示的是相关的结果,但颜色没有那么相关。嘿@AmitMadan,我想知道你是如何管理所有
50K图像的
功能集
数据的?你用过lucene吗?或者你做了其他的事情吗?我只是把所有的图像放在一个文件夹中,然后使用循环一个接一个地选择每个图像,并将其作为输入传递。在classify_image.py文件中进行了一些修改。我生成了功能集,并不断将其添加到npy文件中。你能估计出用整个50K功能集计算单个图像所需的时间吗?因此,在我的笔记本电脑上计算33k图像大约需要10到12个小时。。i5 8 gb内存。这取决于处理器上的图像大小。
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)
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))