我有一个图像分类项目,我正在工作,有17255张图片和49个类别。这是一个概念的证明练习。实际的最终产品将涉及10万到50万张图片。考虑到图像的数量和大小,我决定研究Keras 'flow_from_directory‘功能。
当我最初针对整个映像集运行下面的代码时,它运行了一个多小时而没有完成。为了绑定问题,我创建了图像和目录类别的子集。在大约100张左右的图像中,脚本在大约30秒内完成。
当我把事情增加到大约1,400张图片时,脚本花了30分钟才完成。这将是每小时2,800张图像,或者我的数据集超过6小时(请随时检查我的数学)。这只是数据生成部分,不包括任何实际培训。
我正在运行一个带有8个CPU和50G内存的Google实例。当脚本运行时,CPU和内存的使用是最小的,所以硬件不是问题。
机器规格:
instance-4 > uname -a
Linux instance-4 4.4.0-109-generic #132~14.04.1-Ubuntu SMP Tue Jan 9 21:46:42 UTC 2018 x86_64 x86_64 x86_64 GNU/LinuxPython规范:
>>> print(keras.__version__)
2.1.2
>>> import tensorflow as tf
>>> print(tf.__version__)
1.4.1
>>>
instance-4 > python -V
Python 3.6.3 :: Anaconda, Inc.文件商店是Google。示例目录是:
instance-4 > ls -1 ./data/val
cat1
cat2
cat3
cat4
cat5
cat6在每个目录/类别中都有指向实际图像文件(也在Google上)的符号链接。
我突然意识到链接可能是问题所在,但当我运行100个左右的图像文件时,性能与符号链接(~ 30秒)大致相同。
所以我的问题是:我是做错什么了还是Keras 'flow_from_directory‘只是无法处理大量的图像(尽管有广告/文档)?
样本代码:
#!/usr/bin/env python
import warnings
#... Supress TensorFlow warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import keras
from keras.preprocessing.image import ImageDataGenerator
from datetime import datetime
import time
test_datagen = ImageDataGenerator()
validation_dir = './data/val'
start_time = time.time()
print( str(datetime.now()) )
validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(100,100),
batch_size=32,
class_mode='categorical',
follow_links=True
)
print(validation_generator)
print("--- %s seconds ---" % (time.time() - start_time))发布于 2018-01-19 02:17:30
问题解决了。我将从Google复制到实例上的磁盘,整个映像集的生成器运行时间不到2秒。
https://stackoverflow.com/questions/48330508
复制相似问题