Python 使用tensorflow上的自动编码器获取图像名称

Python 使用tensorflow上的自动编码器获取图像名称,python,tensorflow,Python,Tensorflow,我正在使用tensorflow图像搜索脚本: 非常感谢!:) 您需要修改搜索功能 具体来说,请看这一行: best_states.append(imported_images[i]) 如果要在返回的图像和文件名之间进行映射,则需要记录并返回该索引,i。考虑添加 BestStaseStEngult变量并返回两者,或者简单地替换导入的图像[I] > i>代码>,并使用它访问文件名和图像数据。 更明确地说: def search(image): hidden_states = [ses

我正在使用tensorflow图像搜索脚本:


非常感谢!:)

您需要修改搜索功能

具体来说,请看这一行:

best_states.append(imported_images[i])
如果要在返回的图像和文件名之间进行映射,则需要记录并返回该索引,
i
。考虑添加<代码> BestStaseStEngult变量并返回两者,或者简单地替换<代码>导入的图像[I] <代码> > <代码> i>代码>,并使用它访问文件名和图像数据。

更明确地说:

def search(image):
    hidden_states = [sess.run(hidden_state(X, mask, W, b),
                     feed_dict={X: im.reshape(1, pixels), mask:  
                     np.random.binomial(1, 1-corruption_level, (1, pixels))})
                     for im in image_set]

    query = sess.run(hidden_state(X, mask, W, b),
                     feed_dict={X: image.reshape(1,pixels), mask: np.random.binomial(1, 1-corruption_level, (1, pixels))})

    starting_state = int(np.random.random()*len(hidden_states)) #choose random starting state
    best_states = [imported_images[starting_state]]
    best_states_index = [starting_state]
    distance = euclidean_distance(query[0], hidden_states[starting_state][0]) #Calculate similarity between hidden states
    for i in range(len(hidden_states)):
        dist = euclidean_distance(query[0], hidden_states[i][0])
        if dist <= distance:
            distance = dist #as the method progresses, it gets better at identifying similiar images
            best_states.append(imported_images[i])
            best_states_index.append(i)
    if len(best_states)>0:
        return best_states, best_states_index
    else:
        return best_states[len(best_states)-101:], best_states_index[len(best_states)-101:]
def搜索(图像):
隐藏状态=[sess.run(隐藏状态(X,掩码,W,b),
feed_dict={X:im.重塑(1,像素),遮罩:
np.随机二项(1,1-U级,(1,像素))}
用于图像中的即时消息\u集]
query=sess.run(隐藏状态(X,掩码,W,b),
feed_dict={X:image.reformate(1,像素),mask:np.random.二项式(1,1-decorrupt_level,(1,像素))})
起始状态=int(np.random.random()*len(隐藏状态))#选择随机起始状态
最佳状态=[导入的图像[开始状态]]
最佳状态指数=[起始状态]
距离=欧几里德距离(查询[0],隐藏状态[起始状态][0])#计算隐藏状态之间的相似性
对于范围内的i(len(隐藏状态)):
dist=欧几里德距离(查询[0],隐藏状态[i][0])
如果距离为0:
返回最佳状态、最佳状态索引
其他:
返回最佳状态[len(最佳状态)-101:],最佳状态索引[len(最佳状态)-101:]

嘿!非常感谢您的回答!:)我不能理解你。。我试图添加
最佳州索引
比如:
最佳州索引。append(I)
,但我认为我做错了。你能更具体地说明你尝试了什么以及为什么它看起来是错的吗?谢谢你,约翰!我添加了您修改过的搜索函数,当我运行results函数时,我得到了以下信息:
---------------------------------------------------------------TypeError Traceback(最近一次调用)in()4 for im in results[::-1]:#向后读取结果(首先是更相似的图像)5 plt.子批次(10,10,插槽+1)--->6 plt.imshow(cv2.cvtColor(im,cv2.COLOR_BGR2RGB));plt.axis('off')7个插槽+=1类型错误:src不是numpy数组,也不是标量
best_states.append(imported_images[i])
def search(image):
    hidden_states = [sess.run(hidden_state(X, mask, W, b),
                     feed_dict={X: im.reshape(1, pixels), mask:  
                     np.random.binomial(1, 1-corruption_level, (1, pixels))})
                     for im in image_set]

    query = sess.run(hidden_state(X, mask, W, b),
                     feed_dict={X: image.reshape(1,pixels), mask: np.random.binomial(1, 1-corruption_level, (1, pixels))})

    starting_state = int(np.random.random()*len(hidden_states)) #choose random starting state
    best_states = [imported_images[starting_state]]
    best_states_index = [starting_state]
    distance = euclidean_distance(query[0], hidden_states[starting_state][0]) #Calculate similarity between hidden states
    for i in range(len(hidden_states)):
        dist = euclidean_distance(query[0], hidden_states[i][0])
        if dist <= distance:
            distance = dist #as the method progresses, it gets better at identifying similiar images
            best_states.append(imported_images[i])
            best_states_index.append(i)
    if len(best_states)>0:
        return best_states, best_states_index
    else:
        return best_states[len(best_states)-101:], best_states_index[len(best_states)-101:]