def extract_features(list_images)....给:NameError:name' image'没有定义

时间:2017-02-14 21:00:49

标签: python tensorflow

此代码产生此错误:

  

NameError:name' image'未定义

我将不胜感激任何帮助:

  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)]

1 个答案:

答案 0 :(得分:0)

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

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

行。

问题:如果list_images是一个至少有一个值的迭代,则定义图像。