为什么这个循环似乎只处理一个图像?

时间:2017-02-17 03:02:12

标签: python loops

有人可以帮助理解为什么此代码不处理所有图像?或如何确保它处理所有图像?请查看打印出功能和标签时发生的情况:

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

如下所示在[11]中:"处理图像/ Young-Bengal-tiger.jpg ......"似乎LOOP只能获得一张图像。

In [11]: features,labels = extract_features(list_images)

处理图片/ Young-Bengal-tiger.jpg .. 。 W tensorflow / core / framework / op_def_util.cc:332]不建议使用批处理BatchNormWithGlobalNormalization。它将停止在GraphDef版本9中工作。使用tf.nn.batch_normalization()。

我应该看到类似的东西:

Processing images/baby_shoe_0.jpg...
Processing images/basketball_hoop_0.jpg...
Processing images/bath_spa_0.jpg...
Processing images/binocular_0.jpg...
Processing images/birdcage_0.jpg...
Processing images/birdhouse_0.jpg...
Processing images/boot_0.jpg...
Processing images/cabinet_0.jpg...
Processing images/calculator_0.jpg...
...etc...

请参阅包含以下内容的图片

  1. print(features)
  2. 打印(标签)
  3. enter image description here

1 个答案:

答案 0 :(得分:0)

您已将其设置为仅在处理的每100张图片上打印“处理.....”。

删除行if (ind%100 == 0):,它应该显示您在假设其余代码有效后的内容。

如果有数千或数百万条记录,那么if not i % number事情的效果会非常好,但是对于少数图像来说,应该没有太多需要。

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