Keras Image 数据生成器抛出未找到文件错误?
Posted
技术标签:
【中文标题】Keras Image 数据生成器抛出未找到文件错误?【英文标题】:Keras Image data generator throwing no files found error? 【发布时间】:2017-09-05 04:45:23 【问题描述】:我无法从 keras 运行简单的数据生成器代码
import os
import keras as K
from keras.preprocessing.image import ImageDataGenerator
def save_images_from_generator(maximal_nb_of_images, generator):
nb_of_images_processed = 0
for x, _ in generator:
nb_of_images += x.shape[0]
if nb_of_images <= maximal_nb_of_images:
for image_nb in range(x.shape[0]):
your_custom_save(x[image_nb]) # your custom function for saving images
else:
break
Gen=ImageDataGenerator(featurewise_center=True,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=True,
rotation_range=90,
width_shift_range=0.2,
height_shift_range=0.1,
shear_range=0.5,
zoom_range=0.2,
channel_shift_range=0.1,
fill_mode='nearest',
cval=0.,
horizontal_flip=True,
vertical_flip=True,
rescale=None,
preprocessing_function=None)
if __name__ == '__main__':
save_images_from_generator(40,Gen.flow_from_directory('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input', target_size=(150, 150),class_mode=None,save_prefix='augm',save_to_dir='C:\\Users\\aanilil\\PycharmProjects\\untitled\\im_output\\'))
输出
Using TensorFlow backend.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Found 0 images belonging to 0 classes.
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 1578, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\pydevd.py", line 1015, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/aanilil/PycharmProjects/untitled/generate_data_from_folder.py", line 35, in <module>
save_images_from_generator(40,Gen.flow_from_directory('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input', target_size=(150, 150),class_mode=None,save_prefix='augm',save_to_dir='C:\\Users\\aanilil\\PycharmProjects\\untitled\\im_output\\'))
File "C:/Users/aanilil/PycharmProjects/untitled/generate_data_from_folder.py", line 7, in save_images_from_generator
for x, _ in generator:
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 727, in __next__
return self.next(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 950, in next
index_array, current_index, current_batch_size = next(self.index_generator)
File "C:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 710, in _flow_index
current_index = (self.batch_index * batch_size) % n
ZeroDivisionError: integer division or modulo by zero
当我做一个操作系统时。 listdir 我得到这样的输出
os.listdir('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input')
['download (1).png', 'download.jpg', 'download.png', 'images.jpg']
所以输入文件夹中有图像,它仍然会抛出与找不到文件相关的错误
【问题讨论】:
【参考方案1】:Keras 假设图像存储在一个文件夹树中,每个类都有一个单独的子文件夹,如下所示:
一些/路径/ class1/ image1.jpg image2.jpg class2/ image3.jpg 等 等因此,在您的情况下,解决方案是在“C:\Users\aanilil\PycharmProjects\untitled\images_input”下创建一个子文件夹并将图像移动到那里。当然,如果这是您的目标,您将需要多个类子文件夹来训练分类器。
【讨论】:
【参考方案2】:如果您没有预定义类,另一种可能性是将所有图像放在图像文件夹中的子文件夹中,例如:
flow_from_directory(directory = "/path/images/",…)
您在图像/数据中的实际数据
【讨论】:
【参考方案3】:错误是因为路径有子目录'category',例如猫和狗。您应该创建一个包含所有图像的新目录。 示例数据集包含:
-
../输入.../train/
自闭症/
-
../输入.../train/
非自闭症/
将所有图片复制到一个目录/文件夹:
from distutils.dir_util import copy_tree
toDir = "AllTrain"
fromdir = "../input/autistic-children-data-set/train/autistic"
copy_tree(fromdir ,toDir)
fromdirNon = "../input/autistic-children-data-set/train/non_autistic"
copy_tree(fromdirNon ,toDir)
为每个类别添加标签:
filenames = []
categories = []
Train_autistic = os.listdir("../input/autistic-children-data-set/train/autistic/")
for filename in Train_autistic :
categories.append(1)
filenames.extend(Train_autistic )
Train_non_autistic = os.listdir("../input/autistic-children-data-set/train/non_autistic/")
for filename in Train_non_autistic :
categories.append(0)
filenames.extend(Train_non_autistic )
train_df = pd.DataFrame(
'filename': filenames,
'category': categories
)
train_df["category"] = train_df["category"].replace(0: 'non_autistic', 1: 'autistic')
然后使用:
train_generator = train_datagen.flow_from_dataframe(
train_df, "AllTrain/",
x_col='filename',
y_col='category',
target_size=IMAGE_SIZE,
class_mode='categorical',
batch_size=batch_size
)
不知道:
train_generator = train_datagen.flow_from_dataframe(
train_df, "../input/autistic-children-data-set/train",
target_size=IMAGE_SIZE,
class_mode='binary',
batch_size=batch_size
)
【讨论】:
【参考方案4】:它只是关于你的文件路径 看 这是我用于训练图像的文件 =
C:/Users/Admin/python/Dataset/training_set/data
这是我的测试图像文件 =
C:/Users/Admin/python/Dataset/test_set/data
并在每个路径的data
文件夹中放置我的图像。
但是现在,如果你在命令中给出这个,你需要给它:
test_set = train_datagen.flow_from_directory('C:/Users/Admin/python/Dataset/test_set',target_size=(435,116),batch_size=4,class_mode='binary')
和
test_set = train_datagen.flow_from_directory('C:/Users/Admin/python/Dataset/test_set',target_size=(435,116),batch_size=4,class_mode='binary')
不要在此路径中提及“数据”文件夹。 这将解决问题
【讨论】:
以上是关于Keras Image 数据生成器抛出未找到文件错误?的主要内容,如果未能解决你的问题,请参考以下文章
NestJS + Angular 项目抛出未找到模块:错误:无法解析“缓存管理器”