gpt4 book ai didi

python - Keras 图像数据生成器抛出未找到文件错误?

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:43 25 4
gpt4 key购买 nike

我无法从 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

当我做一个 os. listdir 我得到这样的输出

os.listdir('C:\\Users\\aanilil\\PycharmProjects\\untitled\\images_input') 
['download (1).png', 'download.jpg', 'download.png', 'images.jpg']

因此输入文件夹中有图像,但它仍然抛出与找不到文件相关的错误

最佳答案

Keras 假定图像存储在文件夹树中,每个类有一个单独的子文件夹,如下所示:

  • 一些/路径/
    • class1/
      • image1.jpg
      • image2.jpg
    • class2/
      • image3.jpg
      • 等等
    • 等等

因此,在您的情况下,解决方案是在“C:\Users\aanilil\PycharmProjects\untitled\images_input”下创建一个子文件夹并将图像移动到那里。当然,如果这是您的目标,您将需要多个类子文件夹来训练分类器。

关于python - Keras 图像数据生成器抛出未找到文件错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43318101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com