当前位置: 代码迷 >> python >> Tensorflow和Python中的NameError
  详细解决方案

Tensorflow和Python中的NameError

热度:32   发布时间:2023-06-13 20:24:57.0

此行表示代码已启动处理部分“ Processing images / 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

感谢您的回答。 我遵循了您的建议,并通过更好的识别解决了我的问题。