【问题标题】:def extract_features(list_images) ....Gives: NameError: name 'image' is not defineddef extract_features(list_images) ....Gives: NameError: name 'image' is not defined
【发布时间】:2017-07-03 07:57:20
【问题描述】:

这段代码产生了这个错误:

NameError: 名称“图像”未定义

如果有任何帮助,我将不胜感激:

  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

在前面的代码部分中,我声明了这一点:

list_images = [images_dir+f for f in os.listdir(images_dir) if   re.search('jpg|JPG', f)]

【问题讨论】:

  • 您只能在 for 循环中定义图像。如果 list_images 为空,则实际上从未定义过

标签: python tensorflow


【解决方案1】:

看起来你有缩进错误,你需要添加另一个标签

  if not gfile.Exists(image):
        tf.logging.fatal('File does not exist %s', image)

行。

问题:仅当 list_images 具有至少一个值的可迭代对象时才定义图像。

【讨论】:

  • 感谢上面的回答。它通过重新定义 images_dir 来工作。
猜你喜欢
  • 2018-10-28
  • 2022-12-02
  • 2013-04-09
  • 1970-01-01
  • 2020-11-07
  • 2017-01-09
  • 2017-06-14
  • 2023-03-11
相关资源
最近更新 更多