为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

侃侃无极 2022-06-02 18:03:53
我正在使用这个 tensorflow 图像搜索脚本: https ://www.kaggle.com/jonmarty/using-autoencoder-to-search-imagesdef 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]]    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])    if len(best_states)>0:        return best_states    else:        return best_states[len(best_states)-101:]我想知道是否有可能知道图像名称(例如:homer.jpg)。我迷路了,我不知道我应该在代码中添加什么来知道这一点。这是我打印结果的脚本部分: print(len(results))slots = 0plt.figure(figsize = (125,125))for im in results[::-1]: #reads through results backwards (more similiar images first)    plt.subplot(10, 10, slots+1)     plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)); plt.axis('off')    slots += 1十分感谢!:)
查看完整描述

1 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

您需要修改搜索功能。


具体来说,看这一行:


best_states.append(imported_images[i])

如果要在返回的图像和文件名之间进行映射,则需要记录并返回该索引,i. 考虑添加一个best_states_index变量并返回两者,或者简单地替换imported_images[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:]



查看完整回答
反对 回复 2022-06-02
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信