Tensorflow和Python中的NameError

时间:2017-02-15 19:10:29

标签: python tensorflow

此行表示代码启动了处理部分, "处理图像/ Young-Bengal-tiger.jpg ......"。

我正在尝试解决以下错误:

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

1 个答案:

答案 0 :(得分:0)

感谢您的回答。我听了你的推荐,我通过更好的认同解决了我的问题。