TypeError:无法将feed_dict key解释为Tensor:名称“DecodeJpeg”/内容:0'是指不存在的张量

2024-03-29 00:38:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是tensorflow的新用户。我想用下面的代码测试我训练过的模型。运行程序后,它向我显示错误消息“TypeError:Cannot interpretate-feed-tu-dict key as Tensor:the name'DecodeJpeg/内容:0'是指不存在的张量。图形中不存在“DecodeJpeg/contents”操作。“

有人知道怎么解决这个问题吗?非常感谢。在

import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt

lines = tf.gfile.GFile('retrain/output_labels.txt').readline()
uid_to_human={}
#read data line by line
for uid,line in enumerate(lines):
    #remove new line char
    line=line.strip('\n')
    uid_to_human[uid]=line

def id_to_string(node_id):
    if node_id not in uid_to_human:
        return ''
    return uid_to_human[node_id]

#create a graph to place the trained model
with tf.gfile.FastGFile('retrain/output_graph.pb','rb') as f:
    graph_def=tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def,name='')

with tf.Session() as sess:
    softmax_tensor=sess.graph.get_tensor_by_name('final_result:0')
    #iterate directory
    for root,dirs,files in os.walk('retrain/images/'):
        for file in files:
            #load image
            image_data=tf.gfile.FastGFile(os.path.join(root,file),'rb').read()
            # Decode the image as a JPEG file, this will turn it into a Tensor which we can
            # then use in training.


            predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})#image format is jpg
            #predictions=sess.run(softmax_tensor,{'DecodeJpeg/contents:0':image_data})#image format is jpg
            predictions=np.squeeze(predictions)#convert result to 1dimension data

            #print image path and name
            image_path=os.path.join(root,file)
            print (image_path)
            #show image
            img=Image.open(image_path)
            plt.imshow(img)
            plt.axis('off')
            plt.show()

            #sort
            top_k=predictions.argsort()[::-1]
            print (top_k)
            for node_id in top_k:
                #get classify name
                human_string=id_to_string(node_id)
                #get the score of the classify
                score=predictions[node_id]
                print('%s (score=%.5f)'% (human_string,score))
            print()

Tags: thetopathinimageimportidnode