我是张量流新用户 . 我想使用以下代码测试我训练的模型 . 运行程序后,它显示错误消息“ TypeError: Cannot interpret feed_dict key as Tensor: The name 'DecodeJpeg/contents:0' refers to a Tensor which does not exist. The operation, 'DecodeJpeg/contents', does not exist in the graph."
有谁知道如何解决这一问题?非常感谢 .
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()