Tensorflow和Python中的NameError
该行表示代码启动了处理部件, “处理图像/小孟加拉虎.jpg…” 我正在尝试解决以下错误:Tensorflow和Python中的NameError,python,tensorflow,Python,Tensorflow,该行表示代码启动了处理部件, “处理图像/小孟加拉虎.jpg…” 我正在尝试解决以下错误: In [50]: def extract_features(list_images): ...: nb_features = 2048 ...: features = np.empty((len(list_images),nb_features)) ...: labels = [] ...: ...: create_graph() ...:
In [50]: def extract_features(list_images):
...: nb_features = 2048
...: features = np.empty((len(list_images),nb_features))
...: labels = []
...:
...: create_graph()
...:
...: with tf.Session() as sess:
...:
...: next_to_last_tensor = sess.graph.get_tensor_by_name('pool_3:0')
...:
...: for ind, image in enumerate(list_images):
...: if (ind%100 == 0):
...: print('Processing %s...' % (image))
...: if not gfile.Exists(image):
...: tf.logging.fatal('File does not exist %s', image)
...:
...: image_data = gfile.FastGFile(image, 'rb').read()
...: predictions = sess.run(next_to_last_tensor,{'DecodeJpeg/contents:0': image_data})
...: features[ind,:] = np.squeeze(predictions)
...: labels.append(re.split('_\d+',image.split('/')[1])[0])
...:
...: return features, labels
Processing images/Young-Bengal-tiger.jpg...
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-50-ba615e05226c> in <module>()
18 image_data = gfile.FastGFile(image, 'rb').read()
19 predictions = sess.run(next_to_last_tensor,{'DecodeJpeg/contents:0': image_data})
---> 20 features[ind,:] = np.squeeze(predictions)
21 labels.append(re.split('_\d+',image.split('/')[1])[0])
22
NameError: name 'predictions' is not defined
[50]中的:def提取功能(列出图像):
…:nb_特征=2048
…:features=np.empty((len(列表图像),nb\u特征))
…:标签=[]
...:
…:创建_图()
...:
…:将tf.Session()作为sess:
...:
…:next_to_last_tensor=sess.graph.get_tensor_by_name('pool_3:0'))
...:
…:对于ind,枚举中的图像(列出图像):
…:如果(ind%100==0):
…:打印('正在处理%s…。%(图像))
…:如果不存在gfile.Exists(图像):
…:tf.logging.fatal('文件不存在%s',映像)
...:
…:image_data=gfile.FastGFile(image,'rb').read()
运行(下一个到最后一个张量,{'DecodeJpeg/contents:0':图像数据})
…:特征[ind,:]=np.挤压(预测)
…:labels.append(re.split(“\ud+”,image.split(“/”)[1])[0])
...:
…:返回特征、标签
正在处理图像/孟加拉虎幼崽.jpg。。。
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
18 image_data=gfile.FastGFile(image,'rb').read()
19 predictions=sess.run(下一个到最后一个张量,{'DecodeJpeg/contents:0':图像数据})
--->20个特征[ind,:]=np.挤压(预测)
21 labels.append(关于拆分(“\ud+”,image.split(“/”)[1])[0])
22
NameError:未定义名称“预测”
谢谢您的回答。我遵循了你的建议,通过更好的识别解决了我的问题。如果图像文件确实存在,什么是预测
?嗨,丹尼尔,谢谢你准备好回答。错误消息表示代码正在处理一幅图像“images/Young Bengal tiger.jpg”。代码的这一部分没有指出缺少任何图像:if not gfile.Exists(图像):…:tf.logging.fatal('File not Exists%s',image)if not gfile.Exists(image):…:tf.logging.fatal('File not Exists%s',image)当我去打印(图像数据)…时,我得到以下信息,意思是“预测”有参数。Z� $W�K�P��(L)��?����x�我�/����+�`��2.��X��v�+Q��kJ0zF��3.�廱λ��Y���9$�QEw�EPV������T�QR�C{�QT∑�]���]��O{u&7�Q���$���*�R��)�量化宽松�Daniel是对的。说得清楚一点,很可能是image\u data
和predictions
缩进错误。现在,当文件不存在时,它们位于if分支中。