【发布时间】:2018-06-28 01:39:59
【问题描述】:
我正在处理一个图像分类项目,其中包含 17,255 个图像和 49 个类别。这是一个概念验证练习。实际的最终产品将涉及 100,000 到 500,000 张图像。考虑到大量图像及其大小,我决定研究 Keras 的“flow_from_directory”功能。
当我最初针对整个图像集运行以下代码时,它运行了一个多小时而没有完成。为了解决这个问题,我创建了图像和目录类别的子集。对于一百张左右的图像,脚本在大约 30 秒内完成。
当我将图片增加到大约 1,400 张时,脚本需要 30 多分钟才能完成。对于我的数据集,这将是每小时 2,800 张图像或超过 6 小时(请随意检查我的数学)。这只是数据生成部分,不包括任何实际训练
我在具有 8 个 CPU 和 50 Gig RAM 的 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/Linux
Python 规格:
>>> 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 Cloud。示例目录为:
instance-4 > ls -1 ./data/val
cat1
cat2
cat3
cat4
cat5
cat6
每个目录/类别中都有指向实际图像文件的符号链接(也在 Google Cloud 上)。
我想到链接可能是问题所在,但是当我运行大约一百个图像文件时,性能与符号链接大致相同(~ 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))
【问题讨论】:
标签: python-3.x machine-learning keras